[BACK]Return to readconf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/readconf.c, Revision 1.378

1.378   ! djm         1: /* $OpenBSD: readconf.c,v 1.377 2023/06/21 05:10:26 djm Exp $ */
1.1       deraadt     2: /*
1.18      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for reading the configuration files.
1.26      markus      7:  *
1.46      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.18      deraadt    13:  */
1.1       deraadt    14:
1.147     stevesk    15: #include <sys/types.h>
                     16: #include <sys/stat.h>
1.152     stevesk    17: #include <sys/socket.h>
1.206     djm        18: #include <sys/wait.h>
1.220     millert    19: #include <sys/un.h>
1.152     stevesk    20:
1.378   ! djm        21: #include <net/if.h>
1.152     stevesk    22: #include <netinet/in.h>
1.190     djm        23: #include <netinet/ip.h>
1.148     stevesk    24:
                     25: #include <ctype.h>
1.154     stevesk    26: #include <errno.h>
1.206     djm        27: #include <fcntl.h>
1.252     djm        28: #include <glob.h>
1.378   ! djm        29: #include <ifaddrs.h>
1.155     stevesk    30: #include <netdb.h>
1.206     djm        31: #include <paths.h>
                     32: #include <pwd.h>
1.159     deraadt    33: #include <signal.h>
1.158     stevesk    34: #include <stdio.h>
1.157     stevesk    35: #include <string.h>
1.312     deraadt    36: #include <stdarg.h>
1.156     stevesk    37: #include <unistd.h>
1.228     deraadt    38: #include <limits.h>
1.200     dtucker    39: #include <util.h>
1.221     djm        40: #include <vis.h>
1.1       deraadt    41:
1.159     deraadt    42: #include "xmalloc.h"
1.1       deraadt    43: #include "ssh.h"
1.297     djm        44: #include "ssherr.h"
1.58      markus     45: #include "cipher.h"
1.55      markus     46: #include "pathnames.h"
1.58      markus     47: #include "log.h"
1.227     djm        48: #include "sshkey.h"
1.220     millert    49: #include "misc.h"
1.58      markus     50: #include "readconf.h"
                     51: #include "match.h"
1.62      markus     52: #include "kex.h"
                     53: #include "mac.h"
1.206     djm        54: #include "uidswap.h"
1.221     djm        55: #include "myproposal.h"
1.224     djm        56: #include "digest.h"
1.1       deraadt    57:
                     58: /* Format of the configuration file:
                     59:
                     60:    # Configuration data is parsed as follows:
                     61:    #  1. command line options
                     62:    #  2. user-specific file
                     63:    #  3. system-wide file
                     64:    # Any configuration value is only changed the first time it is set.
                     65:    # Thus, host-specific definitions should be at the beginning of the
                     66:    # configuration file, and defaults at the end.
                     67:
                     68:    # Host-specific declarations.  These may override anything above.  A single
                     69:    # host may match multiple declarations; these are processed in the order
                     70:    # that they are given in.
                     71:
                     72:    Host *.ngs.fi ngs.fi
1.96      markus     73:      User foo
1.1       deraadt    74:
                     75:    Host fake.com
1.306     jmc        76:      Hostname another.host.name.real.org
1.1       deraadt    77:      User blaah
                     78:      Port 34289
                     79:      ForwardX11 no
                     80:      ForwardAgent no
                     81:
                     82:    Host books.com
                     83:      RemoteForward 9999 shadows.cs.hut.fi:9999
1.266     djm        84:      Ciphers 3des-cbc
1.1       deraadt    85:
                     86:    Host fascist.blob.com
                     87:      Port 23123
                     88:      User tylonen
                     89:      PasswordAuthentication no
                     90:
                     91:    Host puukko.hut.fi
                     92:      User t35124p
                     93:      ProxyCommand ssh-proxy %h %p
                     94:
                     95:    Host *.fr
1.96      markus     96:      PublicKeyAuthentication no
1.1       deraadt    97:
                     98:    Host *.su
1.266     djm        99:      Ciphers aes128-ctr
1.1       deraadt   100:      PasswordAuthentication no
                    101:
1.144     reyk      102:    Host vpn.fake.com
                    103:      Tunnel yes
                    104:      TunnelDevice 3
                    105:
1.1       deraadt   106:    # Defaults for various options
                    107:    Host *
                    108:      ForwardAgent no
1.50      markus    109:      ForwardX11 no
1.1       deraadt   110:      PasswordAuthentication yes
                    111:      StrictHostKeyChecking yes
1.126     markus    112:      TcpKeepAlive no
1.1       deraadt   113:      IdentityFile ~/.ssh/identity
                    114:      Port 22
                    115:      EscapeChar ~
                    116:
                    117: */
                    118:
1.252     djm       119: static int read_config_file_depth(const char *filename, struct passwd *pw,
                    120:     const char *host, const char *original_host, Options *options,
1.302     djm       121:     int flags, int *activep, int *want_final_pass, int depth);
1.252     djm       122: static int process_config_line_depth(Options *options, struct passwd *pw,
                    123:     const char *host, const char *original_host, char *line,
1.302     djm       124:     const char *filename, int linenum, int *activep, int flags,
                    125:     int *want_final_pass, int depth);
1.252     djm       126:
1.1       deraadt   127: /* Keyword tokens. */
                    128:
1.17      markus    129: typedef enum {
                    130:        oBadOption,
1.252     djm       131:        oHost, oMatch, oInclude,
1.186     djm       132:        oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
                    133:        oGatewayPorts, oExitOnForwardFailure,
1.317     dtucker   134:        oPasswordAuthentication,
1.358     dtucker   135:        oXAuthLocation,
1.317     dtucker   136:        oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
1.351     markus    137:        oPermitRemoteOpen,
1.253     markus    138:        oCertificateFile, oAddKeysToAgent, oIdentityAgent,
1.317     dtucker   139:        oUser, oEscapeChar, oProxyCommand,
1.17      markus    140:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    141:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.317     dtucker   142:        oTCPKeepAlive, oNumberOfPasswordPrompts,
1.339     djm       143:        oLogFacility, oLogLevel, oLogVerbose, oCiphers, oMacs,
1.221     djm       144:        oPubkeyAuthentication,
1.67      markus    145:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    146:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.282     djm       147:        oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
1.96      markus    148:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       149:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    150:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    151:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.290     djm       152:        oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
1.187     djm       153:        oHashKnownHosts,
1.277     bluhm     154:        oTunnel, oTunnelDevice,
                    155:        oLocalCommand, oPermitLocalCommand, oRemoteCommand,
1.248     markus    156:        oVisualHostKey,
1.360     djm       157:        oKexAlgorithms, oIPQoS, oRequestTTY, oSessionType, oStdinNull,
1.361     djm       158:        oForkAfterAuthentication, oIgnoreUnknown, oProxyUseFdpass,
1.209     djm       159:        oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
                    160:        oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
1.223     djm       161:        oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
1.350     dtucker   162:        oFingerprintHash, oUpdateHostkeys, oHostbasedAcceptedAlgorithms,
1.349     dtucker   163:        oPubkeyAcceptedAlgorithms, oCASignatureAlgorithms, oProxyJump,
1.369     djm       164:        oSecurityKeyProvider, oKnownHostsCommand, oRequiredRSASize,
1.370     djm       165:        oEnableEscapeCommandline,
1.273     djm       166:        oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
1.1       deraadt   167: } OpCodes;
                    168:
                    169: /* Textual representations of the tokens. */
                    170:
1.17      markus    171: static struct {
                    172:        const char *name;
                    173:        OpCodes opcode;
                    174: } keywords[] = {
1.266     djm       175:        /* Deprecated options */
1.273     djm       176:        { "protocol", oIgnore }, /* NB. silently ignored */
1.274     djm       177:        { "cipher", oDeprecated },
1.266     djm       178:        { "fallbacktorsh", oDeprecated },
                    179:        { "globalknownhostsfile2", oDeprecated },
                    180:        { "rhostsauthentication", oDeprecated },
                    181:        { "userknownhostsfile2", oDeprecated },
                    182:        { "useroaming", oDeprecated },
                    183:        { "usersh", oDeprecated },
1.294     dtucker   184:        { "useprivilegedport", oDeprecated },
1.266     djm       185:
                    186:        /* Unsupported options */
                    187:        { "afstokenpassing", oUnsupported },
                    188:        { "kerberosauthentication", oUnsupported },
                    189:        { "kerberostgtpassing", oUnsupported },
1.318     dtucker   190:        { "rsaauthentication", oUnsupported },
                    191:        { "rhostsrsaauthentication", oUnsupported },
                    192:        { "compressionlevel", oUnsupported },
1.266     djm       193:
                    194:        /* Sometimes-unsupported options */
                    195: #if defined(GSSAPI)
                    196:        { "gssapiauthentication", oGssAuthentication },
                    197:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    198: # else
                    199:        { "gssapiauthentication", oUnsupported },
                    200:        { "gssapidelegatecredentials", oUnsupported },
                    201: #endif
                    202: #ifdef ENABLE_PKCS11
1.304     djm       203:        { "pkcs11provider", oPKCS11Provider },
1.266     djm       204:        { "smartcarddevice", oPKCS11Provider },
                    205: # else
                    206:        { "smartcarddevice", oUnsupported },
                    207:        { "pkcs11provider", oUnsupported },
                    208: #endif
                    209:
1.17      markus    210:        { "forwardagent", oForwardAgent },
                    211:        { "forwardx11", oForwardX11 },
1.123     markus    212:        { "forwardx11trusted", oForwardX11Trusted },
1.186     djm       213:        { "forwardx11timeout", oForwardX11Timeout },
1.153     markus    214:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    215:        { "xauthlocation", oXAuthLocation },
1.17      markus    216:        { "gatewayports", oGatewayPorts },
                    217:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    218:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    219:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.358     dtucker   220:        { "challengeresponseauthentication", oKbdInteractiveAuthentication }, /* alias */
                    221:        { "skeyauthentication", oKbdInteractiveAuthentication }, /* alias */
                    222:        { "tisauthentication", oKbdInteractiveAuthentication },  /* alias */
1.50      markus    223:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    224:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.73      markus    225:        { "hostbasedauthentication", oHostbasedAuthentication },
1.17      markus    226:        { "identityfile", oIdentityFile },
1.174     stevesk   227:        { "identityfile2", oIdentityFile },                     /* obsolete */
1.128     markus    228:        { "identitiesonly", oIdentitiesOnly },
1.241     djm       229:        { "certificatefile", oCertificateFile },
1.246     jcs       230:        { "addkeystoagent", oAddKeysToAgent },
1.253     markus    231:        { "identityagent", oIdentityAgent },
1.306     jmc       232:        { "hostname", oHostname },
1.52      markus    233:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    234:        { "proxycommand", oProxyCommand },
                    235:        { "port", oPort },
1.25      markus    236:        { "ciphers", oCiphers },
1.62      markus    237:        { "macs", oMacs },
1.17      markus    238:        { "remoteforward", oRemoteForward },
                    239:        { "localforward", oLocalForward },
1.351     markus    240:        { "permitremoteopen", oPermitRemoteOpen },
1.17      markus    241:        { "user", oUser },
                    242:        { "host", oHost },
1.206     djm       243:        { "match", oMatch },
1.17      markus    244:        { "escapechar", oEscapeChar },
                    245:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.174     stevesk   246:        { "userknownhostsfile", oUserKnownHostsFile },
1.17      markus    247:        { "connectionattempts", oConnectionAttempts },
                    248:        { "batchmode", oBatchMode },
                    249:        { "checkhostip", oCheckHostIP },
                    250:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    251:        { "compression", oCompression },
1.126     markus    252:        { "tcpkeepalive", oTCPKeepAlive },
                    253:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    254:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
1.271     dtucker   255:        { "syslogfacility", oLogFacility },
1.17      markus    256:        { "loglevel", oLogLevel },
1.339     djm       257:        { "logverbose", oLogVerbose },
1.71      markus    258:        { "dynamicforward", oDynamicForward },
1.67      markus    259:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    260:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.298     djm       261:        { "casignaturealgorithms", oCASignatureAlgorithms },
1.77      markus    262:        { "bindaddress", oBindAddress },
1.282     djm       263:        { "bindinterface", oBindInterface },
1.93      deraadt   264:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    265:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     266:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   267:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    268:        { "rekeylimit", oRekeyLimit },
1.111     djm       269:        { "connecttimeout", oConnectTimeout },
1.112     djm       270:        { "addressfamily", oAddressFamily },
1.127     markus    271:        { "serveraliveinterval", oServerAliveInterval },
                    272:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       273:        { "sendenv", oSendEnv },
1.290     djm       274:        { "setenv", oSetEnv },
1.132     djm       275:        { "controlpath", oControlPath },
                    276:        { "controlmaster", oControlMaster },
1.187     djm       277:        { "controlpersist", oControlPersist },
1.136     djm       278:        { "hashknownhosts", oHashKnownHosts },
1.252     djm       279:        { "include", oInclude },
1.144     reyk      280:        { "tunnel", oTunnel },
                    281:        { "tunneldevice", oTunnelDevice },
                    282:        { "localcommand", oLocalCommand },
                    283:        { "permitlocalcommand", oPermitLocalCommand },
1.277     bluhm     284:        { "remotecommand", oRemoteCommand },
1.167     grunk     285:        { "visualhostkey", oVisualHostKey },
1.189     djm       286:        { "kexalgorithms", oKexAlgorithms },
1.190     djm       287:        { "ipqos", oIPQoS },
1.192     djm       288:        { "requesttty", oRequestTTY },
1.359     djm       289:        { "sessiontype", oSessionType },
1.360     djm       290:        { "stdinnull", oStdinNull },
1.361     djm       291:        { "forkafterauthentication", oForkAfterAuthentication },
1.205     djm       292:        { "proxyusefdpass", oProxyUseFdpass },
1.208     djm       293:        { "canonicaldomains", oCanonicalDomains },
1.209     djm       294:        { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
                    295:        { "canonicalizehostname", oCanonicalizeHostname },
                    296:        { "canonicalizemaxdots", oCanonicalizeMaxDots },
                    297:        { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
1.220     millert   298:        { "streamlocalbindmask", oStreamLocalBindMask },
                    299:        { "streamlocalbindunlink", oStreamLocalBindUnlink },
1.223     djm       300:        { "revokedhostkeys", oRevokedHostKeys },
1.224     djm       301:        { "fingerprinthash", oFingerprintHash },
1.229     djm       302:        { "updatehostkeys", oUpdateHostkeys },
1.354     naddy     303:        { "hostbasedacceptedalgorithms", oHostbasedAcceptedAlgorithms },
1.350     dtucker   304:        { "hostbasedkeytypes", oHostbasedAcceptedAlgorithms }, /* obsolete */
1.352     dtucker   305:        { "pubkeyacceptedalgorithms", oPubkeyAcceptedAlgorithms },
1.349     dtucker   306:        { "pubkeyacceptedkeytypes", oPubkeyAcceptedAlgorithms }, /* obsolete */
1.199     djm       307:        { "ignoreunknown", oIgnoreUnknown },
1.257     djm       308:        { "proxyjump", oProxyJump },
1.318     dtucker   309:        { "securitykeyprovider", oSecurityKeyProvider },
1.346     djm       310:        { "knownhostscommand", oKnownHostsCommand },
1.369     djm       311:        { "requiredrsasize", oRequiredRSASize },
1.370     djm       312:        { "enableescapecommandline", oEnableEscapeCommandline },
1.171     djm       313:
1.92      stevesk   314:        { NULL, oBadOption }
1.13      markus    315: };
                    316:
1.351     markus    317: static const char *lookup_opcode_name(OpCodes code);
1.320     dtucker   318:
                    319: const char *
                    320: kex_default_pk_alg(void)
                    321: {
1.344     djm       322:        static char *pkalgs;
                    323:
                    324:        if (pkalgs == NULL) {
                    325:                char *all_key;
                    326:
                    327:                all_key = sshkey_alg_list(0, 0, 1, ',');
                    328:                pkalgs = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
                    329:                free(all_key);
                    330:        }
                    331:        return pkalgs;
1.320     dtucker   332: }
                    333:
1.327     dtucker   334: char *
                    335: ssh_connection_hash(const char *thishost, const char *host, const char *portstr,
                    336:     const char *user)
                    337: {
                    338:        struct ssh_digest_ctx *md;
                    339:        u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
                    340:
                    341:        if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
                    342:            ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
                    343:            ssh_digest_update(md, host, strlen(host)) < 0 ||
                    344:            ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
                    345:            ssh_digest_update(md, user, strlen(user)) < 0 ||
                    346:            ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
1.340     djm       347:                fatal_f("mux digest failed");
1.327     dtucker   348:        ssh_digest_free(md);
                    349:        return tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
                    350: }
                    351:
1.19      markus    352: /*
                    353:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    354:  * error.
                    355:  */
1.1       deraadt   356:
1.26      markus    357: void
1.220     millert   358: add_local_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   359: {
1.220     millert   360:        struct Forward *fwd;
1.251     djm       361:        int i;
1.185     djm       362:
1.251     djm       363:        /* Don't add duplicates */
                    364:        for (i = 0; i < options->num_local_forwards; i++) {
                    365:                if (forward_equals(newfwd, options->local_forwards + i))
                    366:                        return;
                    367:        }
1.234     deraadt   368:        options->local_forwards = xreallocarray(options->local_forwards,
1.185     djm       369:            options->num_local_forwards + 1,
                    370:            sizeof(*options->local_forwards));
1.17      markus    371:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       372:
1.172     stevesk   373:        fwd->listen_host = newfwd->listen_host;
1.135     djm       374:        fwd->listen_port = newfwd->listen_port;
1.220     millert   375:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   376:        fwd->connect_host = newfwd->connect_host;
1.135     djm       377:        fwd->connect_port = newfwd->connect_port;
1.220     millert   378:        fwd->connect_path = newfwd->connect_path;
1.1       deraadt   379: }
                    380:
1.19      markus    381: /*
                    382:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    383:  * an error.
                    384:  */
1.1       deraadt   385:
1.26      markus    386: void
1.220     millert   387: add_remote_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   388: {
1.220     millert   389:        struct Forward *fwd;
1.251     djm       390:        int i;
1.185     djm       391:
1.251     djm       392:        /* Don't add duplicates */
                    393:        for (i = 0; i < options->num_remote_forwards; i++) {
                    394:                if (forward_equals(newfwd, options->remote_forwards + i))
                    395:                        return;
                    396:        }
1.234     deraadt   397:        options->remote_forwards = xreallocarray(options->remote_forwards,
1.185     djm       398:            options->num_remote_forwards + 1,
                    399:            sizeof(*options->remote_forwards));
1.17      markus    400:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       401:
1.172     stevesk   402:        fwd->listen_host = newfwd->listen_host;
1.135     djm       403:        fwd->listen_port = newfwd->listen_port;
1.220     millert   404:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   405:        fwd->connect_host = newfwd->connect_host;
1.135     djm       406:        fwd->connect_port = newfwd->connect_port;
1.220     millert   407:        fwd->connect_path = newfwd->connect_path;
1.194     markus    408:        fwd->handle = newfwd->handle;
1.184     markus    409:        fwd->allocated_port = 0;
1.1       deraadt   410: }
                    411:
1.90      stevesk   412: static void
                    413: clear_forwardings(Options *options)
                    414: {
                    415:        int i;
                    416:
1.135     djm       417:        for (i = 0; i < options->num_local_forwards; i++) {
1.202     djm       418:                free(options->local_forwards[i].listen_host);
1.220     millert   419:                free(options->local_forwards[i].listen_path);
1.202     djm       420:                free(options->local_forwards[i].connect_host);
1.220     millert   421:                free(options->local_forwards[i].connect_path);
1.135     djm       422:        }
1.185     djm       423:        if (options->num_local_forwards > 0) {
1.202     djm       424:                free(options->local_forwards);
1.185     djm       425:                options->local_forwards = NULL;
                    426:        }
1.90      stevesk   427:        options->num_local_forwards = 0;
1.135     djm       428:        for (i = 0; i < options->num_remote_forwards; i++) {
1.202     djm       429:                free(options->remote_forwards[i].listen_host);
1.220     millert   430:                free(options->remote_forwards[i].listen_path);
1.202     djm       431:                free(options->remote_forwards[i].connect_host);
1.220     millert   432:                free(options->remote_forwards[i].connect_path);
1.135     djm       433:        }
1.185     djm       434:        if (options->num_remote_forwards > 0) {
1.202     djm       435:                free(options->remote_forwards);
1.185     djm       436:                options->remote_forwards = NULL;
                    437:        }
1.90      stevesk   438:        options->num_remote_forwards = 0;
1.145     reyk      439:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   440: }
                    441:
1.195     dtucker   442: void
1.241     djm       443: add_certificate_file(Options *options, const char *path, int userprovided)
                    444: {
                    445:        int i;
                    446:
                    447:        if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
                    448:                fatal("Too many certificate files specified (max %d)",
                    449:                    SSH_MAX_CERTIFICATE_FILES);
                    450:
                    451:        /* Avoid registering duplicates */
                    452:        for (i = 0; i < options->num_certificate_files; i++) {
                    453:                if (options->certificate_file_userprovided[i] == userprovided &&
                    454:                    strcmp(options->certificate_files[i], path) == 0) {
1.340     djm       455:                        debug2_f("ignoring duplicate key %s", path);
1.241     djm       456:                        return;
                    457:                }
                    458:        }
                    459:
                    460:        options->certificate_file_userprovided[options->num_certificate_files] =
                    461:            userprovided;
                    462:        options->certificate_files[options->num_certificate_files++] =
                    463:            xstrdup(path);
                    464: }
                    465:
                    466: void
1.195     dtucker   467: add_identity_file(Options *options, const char *dir, const char *filename,
                    468:     int userprovided)
                    469: {
                    470:        char *path;
1.219     djm       471:        int i;
1.195     dtucker   472:
                    473:        if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
                    474:                fatal("Too many identity files specified (max %d)",
                    475:                    SSH_MAX_IDENTITY_FILES);
                    476:
                    477:        if (dir == NULL) /* no dir, filename is absolute */
                    478:                path = xstrdup(filename);
1.276     djm       479:        else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
                    480:                fatal("Identity file path %s too long", path);
1.219     djm       481:
                    482:        /* Avoid registering duplicates */
                    483:        for (i = 0; i < options->num_identity_files; i++) {
                    484:                if (options->identity_file_userprovided[i] == userprovided &&
                    485:                    strcmp(options->identity_files[i], path) == 0) {
1.340     djm       486:                        debug2_f("ignoring duplicate key %s", path);
1.219     djm       487:                        free(path);
                    488:                        return;
                    489:                }
                    490:        }
1.195     dtucker   491:
                    492:        options->identity_file_userprovided[options->num_identity_files] =
                    493:            userprovided;
                    494:        options->identity_files[options->num_identity_files++] = path;
                    495: }
                    496:
1.206     djm       497: int
                    498: default_ssh_port(void)
                    499: {
                    500:        static int port;
                    501:        struct servent *sp;
                    502:
                    503:        if (port == 0) {
                    504:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    505:                port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
                    506:        }
                    507:        return port;
                    508: }
                    509:
                    510: /*
                    511:  * Execute a command in a shell.
                    512:  * Return its exit status or -1 on abnormal exit.
                    513:  */
                    514: static int
                    515: execute_in_shell(const char *cmd)
                    516: {
1.243     dtucker   517:        char *shell;
1.206     djm       518:        pid_t pid;
1.337     djm       519:        int status;
1.206     djm       520:
                    521:        if ((shell = getenv("SHELL")) == NULL)
                    522:                shell = _PATH_BSHELL;
1.308     djm       523:
                    524:        if (access(shell, X_OK) == -1) {
                    525:                fatal("Shell \"%s\" is not executable: %s",
                    526:                    shell, strerror(errno));
                    527:        }
1.206     djm       528:
                    529:        debug("Executing command: '%.500s'", cmd);
                    530:
                    531:        /* Fork and execute the command. */
                    532:        if ((pid = fork()) == 0) {
1.245     djm       533:                char *argv[4];
1.206     djm       534:
1.337     djm       535:                if (stdfd_devnull(1, 1, 0) == -1)
1.340     djm       536:                        fatal_f("stdfd_devnull failed");
1.206     djm       537:                closefrom(STDERR_FILENO + 1);
1.245     djm       538:
                    539:                argv[0] = shell;
                    540:                argv[1] = "-c";
                    541:                argv[2] = xstrdup(cmd);
                    542:                argv[3] = NULL;
1.206     djm       543:
                    544:                execv(argv[0], argv);
                    545:                error("Unable to execute '%.100s': %s", cmd, strerror(errno));
                    546:                /* Die with signal to make this error apparent to parent. */
1.321     dtucker   547:                ssh_signal(SIGTERM, SIG_DFL);
1.206     djm       548:                kill(getpid(), SIGTERM);
                    549:                _exit(1);
                    550:        }
                    551:        /* Parent. */
1.307     deraadt   552:        if (pid == -1)
1.340     djm       553:                fatal_f("fork: %.100s", strerror(errno));
1.206     djm       554:
                    555:        while (waitpid(pid, &status, 0) == -1) {
                    556:                if (errno != EINTR && errno != EAGAIN)
1.340     djm       557:                        fatal_f("waitpid: %s", strerror(errno));
1.206     djm       558:        }
                    559:        if (!WIFEXITED(status)) {
                    560:                error("command '%.100s' exited abnormally", cmd);
                    561:                return -1;
1.221     djm       562:        }
1.206     djm       563:        debug3("command returned status %d", WEXITSTATUS(status));
                    564:        return WEXITSTATUS(status);
                    565: }
                    566:
                    567: /*
1.378   ! djm       568:  * Check whether a local network interface address appears in CIDR pattern-
        !           569:  * list 'addrlist'. Returns 1 if matched or 0 otherwise.
        !           570:  */
        !           571: static int
        !           572: check_match_ifaddrs(const char *addrlist)
        !           573: {
        !           574:        struct ifaddrs *ifa, *ifaddrs = NULL;
        !           575:        int r, found = 0;
        !           576:        char addr[NI_MAXHOST];
        !           577:        socklen_t salen;
        !           578:
        !           579:        if (getifaddrs(&ifaddrs) != 0) {
        !           580:                error("match localnetwork: getifaddrs failed: %s",
        !           581:                    strerror(errno));
        !           582:                return 0;
        !           583:        }
        !           584:        for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
        !           585:                if (ifa->ifa_addr == NULL || ifa->ifa_name == NULL ||
        !           586:                    (ifa->ifa_flags & IFF_UP) == 0)
        !           587:                        continue;
        !           588:                switch (ifa->ifa_addr->sa_family) {
        !           589:                case AF_INET:
        !           590:                        salen = sizeof(struct sockaddr_in);
        !           591:                        break;
        !           592:                case AF_INET6:
        !           593:                        salen = sizeof(struct sockaddr_in6);
        !           594:                        break;
        !           595:                case AF_LINK:
        !           596:                        /* ignore */
        !           597:                        continue;
        !           598:                default:
        !           599:                        debug2_f("interface %s: unsupported address family %d",
        !           600:                            ifa->ifa_name, ifa->ifa_addr->sa_family);
        !           601:                        continue;
        !           602:                }
        !           603:                if ((r = getnameinfo(ifa->ifa_addr, salen, addr, sizeof(addr),
        !           604:                    NULL, 0, NI_NUMERICHOST)) != 0) {
        !           605:                        debug2_f("interface %s getnameinfo failed: %s",
        !           606:                            ifa->ifa_name, gai_strerror(r));
        !           607:                        continue;
        !           608:                }
        !           609:                debug3_f("interface %s addr %s", ifa->ifa_name, addr);
        !           610:                if (addr_match_cidr_list(addr, addrlist) == 1) {
        !           611:                        debug3_f("matched interface %s: address %s in %s",
        !           612:                            ifa->ifa_name, addr, addrlist);
        !           613:                        found = 1;
        !           614:                        break;
        !           615:                }
        !           616:        }
        !           617:        freeifaddrs(ifaddrs);
        !           618:        return found;
        !           619: }
        !           620:
        !           621: /*
1.206     djm       622:  * Parse and execute a Match directive.
                    623:  */
                    624: static int
                    625: match_cfg_line(Options *options, char **condition, struct passwd *pw,
1.302     djm       626:     const char *host_arg, const char *original_host, int final_pass,
                    627:     int *want_final_pass, const char *filename, int linenum)
1.206     djm       628: {
1.221     djm       629:        char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
1.211     djm       630:        const char *ruser;
1.221     djm       631:        int r, port, this_result, result = 1, attributes = 0, negate;
1.206     djm       632:        char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
1.288     djm       633:        char uidstr[32];
1.206     djm       634:
                    635:        /*
                    636:         * Configuration is likely to be incomplete at this point so we
                    637:         * must be prepared to use default values.
                    638:         */
                    639:        port = options->port <= 0 ? default_ssh_port() : options->port;
                    640:        ruser = options->user == NULL ? pw->pw_name : options->user;
1.302     djm       641:        if (final_pass) {
1.250     djm       642:                host = xstrdup(options->hostname);
                    643:        } else if (options->hostname != NULL) {
1.212     djm       644:                /* NB. Please keep in sync with ssh.c:main() */
1.211     djm       645:                host = percent_expand(options->hostname,
                    646:                    "h", host_arg, (char *)NULL);
1.250     djm       647:        } else {
1.211     djm       648:                host = xstrdup(host_arg);
1.250     djm       649:        }
1.206     djm       650:
1.221     djm       651:        debug2("checking match for '%s' host %s originally %s",
                    652:            cp, host, original_host);
                    653:        while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
1.356     djm       654:                /* Terminate on comment */
                    655:                if (*attrib == '#') {
                    656:                        cp = NULL; /* mark all arguments consumed */
                    657:                        break;
                    658:                }
                    659:                arg = criteria = NULL;
1.221     djm       660:                this_result = 1;
1.374     jcs       661:                if ((negate = (attrib[0] == '!')))
1.221     djm       662:                        attrib++;
1.356     djm       663:                /* Criterion "all" has no argument and must appear alone */
1.213     dtucker   664:                if (strcasecmp(attrib, "all") == 0) {
1.356     djm       665:                        if (attributes > 1 || ((arg = strdelim(&cp)) != NULL &&
                    666:                            *arg != '\0' && *arg != '#')) {
1.221     djm       667:                                error("%.200s line %d: '%s' cannot be combined "
                    668:                                    "with other Match attributes",
                    669:                                    filename, linenum, oattrib);
1.213     dtucker   670:                                result = -1;
                    671:                                goto out;
                    672:                        }
1.356     djm       673:                        if (arg != NULL && *arg == '#')
                    674:                                cp = NULL; /* mark all arguments consumed */
1.221     djm       675:                        if (result)
                    676:                                result = negate ? 0 : 1;
1.213     dtucker   677:                        goto out;
                    678:                }
1.221     djm       679:                attributes++;
1.356     djm       680:                /* criteria "final" and "canonical" have no argument */
1.302     djm       681:                if (strcasecmp(attrib, "canonical") == 0 ||
                    682:                    strcasecmp(attrib, "final") == 0) {
                    683:                        /*
                    684:                         * If the config requests "Match final" then remember
                    685:                         * this so we can perform a second pass later.
                    686:                         */
                    687:                        if (strcasecmp(attrib, "final") == 0 &&
                    688:                            want_final_pass != NULL)
                    689:                                *want_final_pass = 1;
                    690:                        r = !!final_pass;  /* force bitmask member to boolean */
1.221     djm       691:                        if (r == (negate ? 1 : 0))
                    692:                                this_result = result = 0;
                    693:                        debug3("%.200s line %d: %smatched '%s'",
                    694:                            filename, linenum,
                    695:                            this_result ? "" : "not ", oattrib);
                    696:                        continue;
                    697:                }
                    698:                /* All other criteria require an argument */
1.356     djm       699:                if ((arg = strdelim(&cp)) == NULL ||
                    700:                    *arg == '\0' || *arg == '#') {
1.206     djm       701:                        error("Missing Match criteria for %s", attrib);
1.211     djm       702:                        result = -1;
                    703:                        goto out;
1.206     djm       704:                }
                    705:                if (strcasecmp(attrib, "host") == 0) {
1.221     djm       706:                        criteria = xstrdup(host);
1.235     djm       707:                        r = match_hostname(host, arg) == 1;
1.221     djm       708:                        if (r == (negate ? 1 : 0))
                    709:                                this_result = result = 0;
1.206     djm       710:                } else if (strcasecmp(attrib, "originalhost") == 0) {
1.221     djm       711:                        criteria = xstrdup(original_host);
1.235     djm       712:                        r = match_hostname(original_host, arg) == 1;
1.221     djm       713:                        if (r == (negate ? 1 : 0))
                    714:                                this_result = result = 0;
1.206     djm       715:                } else if (strcasecmp(attrib, "user") == 0) {
1.221     djm       716:                        criteria = xstrdup(ruser);
1.235     djm       717:                        r = match_pattern_list(ruser, arg, 0) == 1;
1.221     djm       718:                        if (r == (negate ? 1 : 0))
                    719:                                this_result = result = 0;
1.206     djm       720:                } else if (strcasecmp(attrib, "localuser") == 0) {
1.221     djm       721:                        criteria = xstrdup(pw->pw_name);
1.235     djm       722:                        r = match_pattern_list(pw->pw_name, arg, 0) == 1;
1.221     djm       723:                        if (r == (negate ? 1 : 0))
                    724:                                this_result = result = 0;
1.378   ! djm       725:                } else if (strcasecmp(attrib, "localnetwork") == 0) {
        !           726:                        if (addr_match_cidr_list(NULL, arg) == -1) {
        !           727:                                /* Error already printed */
        !           728:                                result = -1;
        !           729:                                goto out;
        !           730:                        }
        !           731:                        r = check_match_ifaddrs(arg) == 1;
        !           732:                        if (r == (negate ? 1 : 0))
        !           733:                                this_result = result = 0;
1.210     djm       734:                } else if (strcasecmp(attrib, "exec") == 0) {
1.333     dtucker   735:                        char *conn_hash_hex, *keyalias;
1.327     dtucker   736:
1.206     djm       737:                        if (gethostname(thishost, sizeof(thishost)) == -1)
                    738:                                fatal("gethostname: %s", strerror(errno));
                    739:                        strlcpy(shorthost, thishost, sizeof(shorthost));
                    740:                        shorthost[strcspn(thishost, ".")] = '\0';
                    741:                        snprintf(portstr, sizeof(portstr), "%d", port);
1.288     djm       742:                        snprintf(uidstr, sizeof(uidstr), "%llu",
                    743:                            (unsigned long long)pw->pw_uid);
1.327     dtucker   744:                        conn_hash_hex = ssh_connection_hash(thishost, host,
1.353     djm       745:                            portstr, ruser);
1.333     dtucker   746:                        keyalias = options->host_key_alias ?
                    747:                            options->host_key_alias : host;
1.206     djm       748:
                    749:                        cmd = percent_expand(arg,
1.327     dtucker   750:                            "C", conn_hash_hex,
1.206     djm       751:                            "L", shorthost,
                    752:                            "d", pw->pw_dir,
                    753:                            "h", host,
1.333     dtucker   754:                            "k", keyalias,
1.206     djm       755:                            "l", thishost,
1.221     djm       756:                            "n", original_host,
1.206     djm       757:                            "p", portstr,
                    758:                            "r", ruser,
                    759:                            "u", pw->pw_name,
1.288     djm       760:                            "i", uidstr,
1.206     djm       761:                            (char *)NULL);
1.327     dtucker   762:                        free(conn_hash_hex);
1.217     djm       763:                        if (result != 1) {
                    764:                                /* skip execution if prior predicate failed */
1.221     djm       765:                                debug3("%.200s line %d: skipped exec "
                    766:                                    "\"%.100s\"", filename, linenum, cmd);
                    767:                                free(cmd);
                    768:                                continue;
                    769:                        }
                    770:                        r = execute_in_shell(cmd);
                    771:                        if (r == -1) {
                    772:                                fatal("%.200s line %d: match exec "
                    773:                                    "'%.100s' error", filename,
                    774:                                    linenum, cmd);
1.217     djm       775:                        }
1.221     djm       776:                        criteria = xstrdup(cmd);
1.206     djm       777:                        free(cmd);
1.221     djm       778:                        /* Force exit status to boolean */
                    779:                        r = r == 0;
                    780:                        if (r == (negate ? 1 : 0))
                    781:                                this_result = result = 0;
1.206     djm       782:                } else {
                    783:                        error("Unsupported Match attribute %s", attrib);
1.211     djm       784:                        result = -1;
                    785:                        goto out;
1.206     djm       786:                }
1.378   ! djm       787:                debug3("%.200s line %d: %smatched '%s%s%.100s%s' ",
        !           788:                    filename, linenum, this_result ? "": "not ", oattrib,
        !           789:                    criteria == NULL ? "" : " \"",
        !           790:                    criteria == NULL ? "" : criteria,
        !           791:                    criteria == NULL ? "" : "\"");
1.221     djm       792:                free(criteria);
1.213     dtucker   793:        }
                    794:        if (attributes == 0) {
                    795:                error("One or more attributes required for Match");
                    796:                result = -1;
                    797:                goto out;
1.206     djm       798:        }
1.221     djm       799:  out:
                    800:        if (result != -1)
                    801:                debug2("match %sfound", result ? "" : "not ");
1.206     djm       802:        *condition = cp;
1.211     djm       803:        free(host);
1.206     djm       804:        return result;
                    805: }
                    806:
1.286     djm       807: /* Remove environment variable by pattern */
                    808: static void
                    809: rm_env(Options *options, const char *arg, const char *filename, int linenum)
                    810: {
1.368     djm       811:        u_int i, j, onum_send_env = options->num_send_env;
1.286     djm       812:
                    813:        /* Remove an environment variable */
                    814:        for (i = 0; i < options->num_send_env; ) {
1.367     millert   815:                if (!match_pattern(options->send_env[i], arg + 1)) {
1.286     djm       816:                        i++;
                    817:                        continue;
                    818:                }
                    819:                debug3("%s line %d: removing environment %s",
1.367     millert   820:                    filename, linenum, options->send_env[i]);
1.286     djm       821:                free(options->send_env[i]);
                    822:                options->send_env[i] = NULL;
                    823:                for (j = i; j < options->num_send_env - 1; j++) {
                    824:                        options->send_env[j] = options->send_env[j + 1];
                    825:                        options->send_env[j + 1] = NULL;
                    826:                }
                    827:                options->num_send_env--;
                    828:                /* NB. don't increment i */
1.330     djm       829:        }
                    830:        if (onum_send_env != options->num_send_env) {
                    831:                options->send_env = xrecallocarray(options->send_env,
                    832:                    onum_send_env, options->num_send_env,
                    833:                    sizeof(*options->send_env));
1.286     djm       834:        }
                    835: }
                    836:
1.19      markus    837: /*
1.70      stevesk   838:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    839:  */
1.26      markus    840: static OpCodes
1.199     djm       841: parse_token(const char *cp, const char *filename, int linenum,
                    842:     const char *ignored_unknown)
1.1       deraadt   843: {
1.199     djm       844:        int i;
1.1       deraadt   845:
1.17      markus    846:        for (i = 0; keywords[i].name; i++)
1.199     djm       847:                if (strcmp(cp, keywords[i].name) == 0)
1.17      markus    848:                        return keywords[i].opcode;
1.235     djm       849:        if (ignored_unknown != NULL &&
                    850:            match_pattern_list(cp, ignored_unknown, 1) == 1)
1.199     djm       851:                return oIgnoredUnknownOption;
1.75      stevesk   852:        error("%s: line %d: Bad configuration option: %s",
                    853:            filename, linenum, cp);
1.17      markus    854:        return oBadOption;
1.1       deraadt   855: }
                    856:
1.207     djm       857: /* Multistate option parsing */
                    858: struct multistate {
                    859:        char *key;
                    860:        int value;
                    861: };
                    862: static const struct multistate multistate_flag[] = {
                    863:        { "true",                       1 },
                    864:        { "false",                      0 },
                    865:        { "yes",                        1 },
                    866:        { "no",                         0 },
                    867:        { NULL, -1 }
                    868: };
                    869: static const struct multistate multistate_yesnoask[] = {
                    870:        { "true",                       1 },
                    871:        { "false",                      0 },
                    872:        { "yes",                        1 },
                    873:        { "no",                         0 },
                    874:        { "ask",                        2 },
                    875:        { NULL, -1 }
                    876: };
1.278     djm       877: static const struct multistate multistate_strict_hostkey[] = {
                    878:        { "true",                       SSH_STRICT_HOSTKEY_YES },
                    879:        { "false",                      SSH_STRICT_HOSTKEY_OFF },
                    880:        { "yes",                        SSH_STRICT_HOSTKEY_YES },
                    881:        { "no",                         SSH_STRICT_HOSTKEY_OFF },
                    882:        { "ask",                        SSH_STRICT_HOSTKEY_ASK },
                    883:        { "off",                        SSH_STRICT_HOSTKEY_OFF },
                    884:        { "accept-new",                 SSH_STRICT_HOSTKEY_NEW },
                    885:        { NULL, -1 }
                    886: };
1.246     jcs       887: static const struct multistate multistate_yesnoaskconfirm[] = {
                    888:        { "true",                       1 },
                    889:        { "false",                      0 },
                    890:        { "yes",                        1 },
                    891:        { "no",                         0 },
                    892:        { "ask",                        2 },
                    893:        { "confirm",                    3 },
                    894:        { NULL, -1 }
                    895: };
1.207     djm       896: static const struct multistate multistate_addressfamily[] = {
                    897:        { "inet",                       AF_INET },
                    898:        { "inet6",                      AF_INET6 },
                    899:        { "any",                        AF_UNSPEC },
                    900:        { NULL, -1 }
                    901: };
                    902: static const struct multistate multistate_controlmaster[] = {
                    903:        { "true",                       SSHCTL_MASTER_YES },
                    904:        { "yes",                        SSHCTL_MASTER_YES },
                    905:        { "false",                      SSHCTL_MASTER_NO },
                    906:        { "no",                         SSHCTL_MASTER_NO },
                    907:        { "auto",                       SSHCTL_MASTER_AUTO },
                    908:        { "ask",                        SSHCTL_MASTER_ASK },
                    909:        { "autoask",                    SSHCTL_MASTER_AUTO_ASK },
                    910:        { NULL, -1 }
                    911: };
                    912: static const struct multistate multistate_tunnel[] = {
                    913:        { "ethernet",                   SSH_TUNMODE_ETHERNET },
                    914:        { "point-to-point",             SSH_TUNMODE_POINTOPOINT },
                    915:        { "true",                       SSH_TUNMODE_DEFAULT },
                    916:        { "yes",                        SSH_TUNMODE_DEFAULT },
                    917:        { "false",                      SSH_TUNMODE_NO },
                    918:        { "no",                         SSH_TUNMODE_NO },
                    919:        { NULL, -1 }
                    920: };
                    921: static const struct multistate multistate_requesttty[] = {
                    922:        { "true",                       REQUEST_TTY_YES },
                    923:        { "yes",                        REQUEST_TTY_YES },
                    924:        { "false",                      REQUEST_TTY_NO },
                    925:        { "no",                         REQUEST_TTY_NO },
                    926:        { "force",                      REQUEST_TTY_FORCE },
                    927:        { "auto",                       REQUEST_TTY_AUTO },
                    928:        { NULL, -1 }
                    929: };
1.359     djm       930: static const struct multistate multistate_sessiontype[] = {
                    931:        { "none",                       SESSION_TYPE_NONE },
                    932:        { "subsystem",                  SESSION_TYPE_SUBSYSTEM },
                    933:        { "default",                    SESSION_TYPE_DEFAULT },
                    934:        { NULL, -1 }
                    935: };
1.209     djm       936: static const struct multistate multistate_canonicalizehostname[] = {
1.208     djm       937:        { "true",                       SSH_CANONICALISE_YES },
                    938:        { "false",                      SSH_CANONICALISE_NO },
                    939:        { "yes",                        SSH_CANONICALISE_YES },
                    940:        { "no",                         SSH_CANONICALISE_NO },
                    941:        { "always",                     SSH_CANONICALISE_ALWAYS },
                    942:        { NULL, -1 }
                    943: };
1.364     djm       944: static const struct multistate multistate_pubkey_auth[] = {
                    945:        { "true",                       SSH_PUBKEY_AUTH_ALL },
                    946:        { "false",                      SSH_PUBKEY_AUTH_NO },
                    947:        { "yes",                        SSH_PUBKEY_AUTH_ALL },
                    948:        { "no",                         SSH_PUBKEY_AUTH_NO },
                    949:        { "unbound",                    SSH_PUBKEY_AUTH_UNBOUND },
                    950:        { "host-bound",                 SSH_PUBKEY_AUTH_HBOUND },
                    951:        { NULL, -1 }
                    952: };
1.322     dtucker   953: static const struct multistate multistate_compression[] = {
                    954: #ifdef WITH_ZLIB
                    955:        { "yes",                        COMP_ZLIB },
                    956: #endif
                    957:        { "no",                         COMP_NONE },
                    958:        { NULL, -1 }
                    959: };
1.207     djm       960:
1.334     djm       961: static int
                    962: parse_multistate_value(const char *arg, const char *filename, int linenum,
                    963:     const struct multistate *multistate_ptr)
                    964: {
                    965:        int i;
                    966:
1.344     djm       967:        if (!arg || *arg == '\0') {
                    968:                error("%s line %d: missing argument.", filename, linenum);
                    969:                return -1;
                    970:        }
1.334     djm       971:        for (i = 0; multistate_ptr[i].key != NULL; i++) {
                    972:                if (strcasecmp(arg, multistate_ptr[i].key) == 0)
                    973:                        return multistate_ptr[i].value;
                    974:        }
                    975:        return -1;
                    976: }
                    977:
1.19      markus    978: /*
                    979:  * Processes a single option line as used in the configuration files. This
                    980:  * only sets those values that have not already been set.
                    981:  */
1.14      markus    982: int
1.206     djm       983: process_config_line(Options *options, struct passwd *pw, const char *host,
1.221     djm       984:     const char *original_host, char *line, const char *filename,
                    985:     int linenum, int *activep, int flags)
1.1       deraadt   986: {
1.252     djm       987:        return process_config_line_depth(options, pw, host, original_host,
1.302     djm       988:            line, filename, linenum, activep, flags, NULL, 0);
1.252     djm       989: }
                    990:
                    991: #define WHITESPACE " \t\r\n"
                    992: static int
                    993: process_config_line_depth(Options *options, struct passwd *pw, const char *host,
                    994:     const char *original_host, char *line, const char *filename,
1.302     djm       995:     int linenum, int *activep, int flags, int *want_final_pass, int depth)
1.252     djm       996: {
1.366     dtucker   997:        char *str, **charptr, *endofnumber, *keyword, *arg, *arg2, *p;
1.339     djm       998:        char **cpptr, ***cppptr, fwdarg[256];
1.351     markus    999:        u_int i, *uintptr, uvalue, max_entries = 0;
1.252     djm      1000:        int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
1.377     djm      1001:        int remotefwd, dynamicfwd, ca_only = 0;
1.164     dtucker  1002:        LogLevel *log_level_ptr;
1.271     dtucker  1003:        SyslogFacility *log_facility_ptr;
1.201     dtucker  1004:        long long val64;
1.102     markus   1005:        size_t len;
1.220     millert  1006:        struct Forward fwd;
1.207     djm      1007:        const struct multistate *multistate_ptr;
1.208     djm      1008:        struct allowed_cname *cname;
1.252     djm      1009:        glob_t gl;
1.281     dtucker  1010:        const char *errstr;
1.356     djm      1011:        char **oav = NULL, **av;
                   1012:        int oac = 0, ac;
                   1013:        int ret = -1;
1.106     djm      1014:
1.206     djm      1015:        if (activep == NULL) { /* We are processing a command line directive */
                   1016:                cmdline = 1;
                   1017:                activep = &cmdline;
                   1018:        }
                   1019:
1.267     djm      1020:        /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1.233     djm      1021:        if ((len = strlen(line)) == 0)
                   1022:                return 0;
                   1023:        for (len--; len > 0; len--) {
1.267     djm      1024:                if (strchr(WHITESPACE "\f", line[len]) == NULL)
1.106     djm      1025:                        break;
                   1026:                line[len] = '\0';
                   1027:        }
1.1       deraadt  1028:
1.356     djm      1029:        str = line;
1.42      provos   1030:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.356     djm      1031:        if ((keyword = strdelim(&str)) == NULL)
1.149     djm      1032:                return 0;
1.42      provos   1033:        /* Ignore leading whitespace. */
                   1034:        if (*keyword == '\0')
1.356     djm      1035:                keyword = strdelim(&str);
1.56      deraadt  1036:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus   1037:                return 0;
1.199     djm      1038:        /* Match lowercase keyword */
1.207     djm      1039:        lowercase(keyword);
1.17      markus   1040:
1.356     djm      1041:        /* Prepare to parse remainder of line */
                   1042:        if (str != NULL)
                   1043:                str += strspn(str, WHITESPACE);
                   1044:        if (str == NULL || *str == '\0') {
                   1045:                error("%s line %d: no argument after keyword \"%s\"",
                   1046:                    filename, linenum, keyword);
                   1047:                return -1;
                   1048:        }
1.199     djm      1049:        opcode = parse_token(keyword, filename, linenum,
                   1050:            options->ignored_unknown);
1.356     djm      1051:        if (argv_split(str, &oac, &oav, 1) != 0) {
                   1052:                error("%s line %d: invalid quotes", filename, linenum);
                   1053:                return -1;
                   1054:        }
                   1055:        ac = oac;
                   1056:        av = oav;
1.17      markus   1057:
                   1058:        switch (opcode) {
                   1059:        case oBadOption:
1.19      markus   1060:                /* don't panic, but count bad options */
1.356     djm      1061:                goto out;
1.273     djm      1062:        case oIgnore:
1.356     djm      1063:                argv_consume(&ac);
                   1064:                break;
1.199     djm      1065:        case oIgnoredUnknownOption:
                   1066:                debug("%s line %d: Ignored unknown option \"%s\"",
                   1067:                    filename, linenum, keyword);
1.356     djm      1068:                argv_consume(&ac);
                   1069:                break;
1.111     djm      1070:        case oConnectTimeout:
                   1071:                intptr = &options->connection_timeout;
1.127     markus   1072: parse_time:
1.356     djm      1073:                arg = argv_next(&ac, &av);
1.344     djm      1074:                if (!arg || *arg == '\0') {
                   1075:                        error("%s line %d: missing time value.",
1.111     djm      1076:                            filename, linenum);
1.356     djm      1077:                        goto out;
1.344     djm      1078:                }
1.221     djm      1079:                if (strcmp(arg, "none") == 0)
                   1080:                        value = -1;
1.344     djm      1081:                else if ((value = convtime(arg)) == -1) {
                   1082:                        error("%s line %d: invalid time value.",
1.111     djm      1083:                            filename, linenum);
1.356     djm      1084:                        goto out;
1.344     djm      1085:                }
1.160     dtucker  1086:                if (*activep && *intptr == -1)
1.111     djm      1087:                        *intptr = value;
                   1088:                break;
                   1089:
1.17      markus   1090:        case oForwardAgent:
                   1091:                intptr = &options->forward_agent;
1.319     djm      1092:
1.356     djm      1093:                arg = argv_next(&ac, &av);
1.344     djm      1094:                if (!arg || *arg == '\0') {
                   1095:                        error("%s line %d: missing argument.",
1.319     djm      1096:                            filename, linenum);
1.356     djm      1097:                        goto out;
1.344     djm      1098:                }
1.319     djm      1099:
                   1100:                value = -1;
                   1101:                multistate_ptr = multistate_flag;
                   1102:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                   1103:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                   1104:                                value = multistate_ptr[i].value;
                   1105:                                break;
                   1106:                        }
                   1107:                }
                   1108:                if (value != -1) {
                   1109:                        if (*activep && *intptr == -1)
                   1110:                                *intptr = value;
                   1111:                        break;
                   1112:                }
                   1113:                /* ForwardAgent wasn't 'yes' or 'no', assume a path */
                   1114:                if (*activep && *intptr == -1)
                   1115:                        *intptr = 1;
                   1116:
                   1117:                charptr = &options->forward_agent_sock_path;
                   1118:                goto parse_agent_path;
                   1119:
                   1120:        case oForwardX11:
                   1121:                intptr = &options->forward_x11;
1.207     djm      1122:  parse_flag:
                   1123:                multistate_ptr = multistate_flag;
                   1124:  parse_multistate:
1.356     djm      1125:                arg = argv_next(&ac, &av);
1.334     djm      1126:                if ((value = parse_multistate_value(arg, filename, linenum,
1.353     djm      1127:                    multistate_ptr)) == -1) {
1.344     djm      1128:                        error("%s line %d: unsupported option \"%s\".",
1.207     djm      1129:                            filename, linenum, arg);
1.356     djm      1130:                        goto out;
1.334     djm      1131:                }
1.17      markus   1132:                if (*activep && *intptr == -1)
                   1133:                        *intptr = value;
                   1134:                break;
                   1135:
1.123     markus   1136:        case oForwardX11Trusted:
                   1137:                intptr = &options->forward_x11_trusted;
                   1138:                goto parse_flag;
1.221     djm      1139:
1.186     djm      1140:        case oForwardX11Timeout:
                   1141:                intptr = &options->forward_x11_timeout;
                   1142:                goto parse_time;
1.123     markus   1143:
1.17      markus   1144:        case oGatewayPorts:
1.220     millert  1145:                intptr = &options->fwd_opts.gateway_ports;
1.17      markus   1146:                goto parse_flag;
                   1147:
1.153     markus   1148:        case oExitOnForwardFailure:
                   1149:                intptr = &options->exit_on_forward_failure;
                   1150:                goto parse_flag;
                   1151:
1.17      markus   1152:        case oPasswordAuthentication:
                   1153:                intptr = &options->password_authentication;
                   1154:                goto parse_flag;
                   1155:
1.48      markus   1156:        case oKbdInteractiveAuthentication:
                   1157:                intptr = &options->kbd_interactive_authentication;
                   1158:                goto parse_flag;
                   1159:
                   1160:        case oKbdInteractiveDevices:
                   1161:                charptr = &options->kbd_interactive_devices;
                   1162:                goto parse_string;
                   1163:
1.50      markus   1164:        case oPubkeyAuthentication:
1.364     djm      1165:                multistate_ptr = multistate_pubkey_auth;
1.50      markus   1166:                intptr = &options->pubkey_authentication;
1.364     djm      1167:                goto parse_multistate;
1.30      markus   1168:
1.72      markus   1169:        case oHostbasedAuthentication:
                   1170:                intptr = &options->hostbased_authentication;
                   1171:                goto parse_flag;
                   1172:
1.118     markus   1173:        case oGssAuthentication:
                   1174:                intptr = &options->gss_authentication;
                   1175:                goto parse_flag;
                   1176:
                   1177:        case oGssDelegateCreds:
                   1178:                intptr = &options->gss_deleg_creds;
                   1179:                goto parse_flag;
                   1180:
1.17      markus   1181:        case oBatchMode:
                   1182:                intptr = &options->batch_mode;
                   1183:                goto parse_flag;
                   1184:
                   1185:        case oCheckHostIP:
                   1186:                intptr = &options->check_host_ip;
1.167     grunk    1187:                goto parse_flag;
1.17      markus   1188:
1.107     jakob    1189:        case oVerifyHostKeyDNS:
                   1190:                intptr = &options->verify_host_key_dns;
1.207     djm      1191:                multistate_ptr = multistate_yesnoask;
                   1192:                goto parse_multistate;
1.107     jakob    1193:
1.17      markus   1194:        case oStrictHostKeyChecking:
                   1195:                intptr = &options->strict_host_key_checking;
1.278     djm      1196:                multistate_ptr = multistate_strict_hostkey;
1.207     djm      1197:                goto parse_multistate;
1.17      markus   1198:
                   1199:        case oCompression:
                   1200:                intptr = &options->compression;
1.322     dtucker  1201:                multistate_ptr = multistate_compression;
                   1202:                goto parse_multistate;
1.17      markus   1203:
1.126     markus   1204:        case oTCPKeepAlive:
                   1205:                intptr = &options->tcp_keep_alive;
1.17      markus   1206:                goto parse_flag;
                   1207:
1.91      markus   1208:        case oNoHostAuthenticationForLocalhost:
                   1209:                intptr = &options->no_host_authentication_for_localhost;
                   1210:                goto parse_flag;
                   1211:
1.17      markus   1212:        case oNumberOfPasswordPrompts:
                   1213:                intptr = &options->number_of_password_prompts;
                   1214:                goto parse_int;
                   1215:
1.105     markus   1216:        case oRekeyLimit:
1.356     djm      1217:                arg = argv_next(&ac, &av);
1.344     djm      1218:                if (!arg || *arg == '\0') {
                   1219:                        error("%.200s line %d: Missing argument.", filename,
1.198     dtucker  1220:                            linenum);
1.356     djm      1221:                        goto out;
1.344     djm      1222:                }
1.198     dtucker  1223:                if (strcmp(arg, "default") == 0) {
                   1224:                        val64 = 0;
                   1225:                } else {
1.344     djm      1226:                        if (scan_scaled(arg, &val64) == -1) {
                   1227:                                error("%.200s line %d: Bad number '%s': %s",
1.200     dtucker  1228:                                    filename, linenum, arg, strerror(errno));
1.356     djm      1229:                                goto out;
1.344     djm      1230:                        }
                   1231:                        if (val64 != 0 && val64 < 16) {
                   1232:                                error("%.200s line %d: RekeyLimit too small",
1.198     dtucker  1233:                                    filename, linenum);
1.356     djm      1234:                                goto out;
1.344     djm      1235:                        }
1.105     markus   1236:                }
1.165     djm      1237:                if (*activep && options->rekey_limit == -1)
1.249     dtucker  1238:                        options->rekey_limit = val64;
1.356     djm      1239:                if (ac != 0) { /* optional rekey interval present */
                   1240:                        if (strcmp(av[0], "none") == 0) {
                   1241:                                (void)argv_next(&ac, &av);      /* discard */
1.198     dtucker  1242:                                break;
                   1243:                        }
                   1244:                        intptr = &options->rekey_interval;
                   1245:                        goto parse_time;
                   1246:                }
1.105     markus   1247:                break;
                   1248:
1.17      markus   1249:        case oIdentityFile:
1.356     djm      1250:                arg = argv_next(&ac, &av);
1.344     djm      1251:                if (!arg || *arg == '\0') {
                   1252:                        error("%.200s line %d: Missing argument.",
                   1253:                            filename, linenum);
1.356     djm      1254:                        goto out;
1.344     djm      1255:                }
1.17      markus   1256:                if (*activep) {
1.50      markus   1257:                        intptr = &options->num_identity_files;
1.344     djm      1258:                        if (*intptr >= SSH_MAX_IDENTITY_FILES) {
                   1259:                                error("%.200s line %d: Too many identity files "
                   1260:                                    "specified (max %d).", filename, linenum,
                   1261:                                    SSH_MAX_IDENTITY_FILES);
1.356     djm      1262:                                goto out;
1.344     djm      1263:                        }
1.221     djm      1264:                        add_identity_file(options, NULL,
                   1265:                            arg, flags & SSHCONF_USERCONF);
1.17      markus   1266:                }
                   1267:                break;
                   1268:
1.241     djm      1269:        case oCertificateFile:
1.356     djm      1270:                arg = argv_next(&ac, &av);
1.344     djm      1271:                if (!arg || *arg == '\0') {
                   1272:                        error("%.200s line %d: Missing argument.",
1.241     djm      1273:                            filename, linenum);
1.356     djm      1274:                        goto out;
1.344     djm      1275:                }
1.241     djm      1276:                if (*activep) {
                   1277:                        intptr = &options->num_certificate_files;
                   1278:                        if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1.344     djm      1279:                                error("%.200s line %d: Too many certificate "
1.241     djm      1280:                                    "files specified (max %d).",
                   1281:                                    filename, linenum,
                   1282:                                    SSH_MAX_CERTIFICATE_FILES);
1.356     djm      1283:                                goto out;
1.241     djm      1284:                        }
                   1285:                        add_certificate_file(options, arg,
                   1286:                            flags & SSHCONF_USERCONF);
                   1287:                }
                   1288:                break;
                   1289:
1.34      markus   1290:        case oXAuthLocation:
                   1291:                charptr=&options->xauth_location;
                   1292:                goto parse_string;
                   1293:
1.17      markus   1294:        case oUser:
                   1295:                charptr = &options->user;
                   1296: parse_string:
1.356     djm      1297:                arg = argv_next(&ac, &av);
1.344     djm      1298:                if (!arg || *arg == '\0') {
                   1299:                        error("%.200s line %d: Missing argument.",
1.193     djm      1300:                            filename, linenum);
1.356     djm      1301:                        goto out;
1.344     djm      1302:                }
1.17      markus   1303:                if (*activep && *charptr == NULL)
1.38      provos   1304:                        *charptr = xstrdup(arg);
1.17      markus   1305:                break;
                   1306:
                   1307:        case oGlobalKnownHostsFile:
1.193     djm      1308:                cpptr = (char **)&options->system_hostfiles;
                   1309:                uintptr = &options->num_system_hostfiles;
                   1310:                max_entries = SSH_MAX_HOSTS_FILES;
                   1311: parse_char_array:
1.356     djm      1312:                i = 0;
1.357     djm      1313:                value = *uintptr == 0; /* was array empty when we started? */
1.356     djm      1314:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   1315:                        if (*arg == '\0') {
                   1316:                                error("%s line %d: keyword %s empty argument",
                   1317:                                    filename, linenum, keyword);
                   1318:                                goto out;
                   1319:                        }
                   1320:                        /* Allow "none" only in first position */
                   1321:                        if (strcasecmp(arg, "none") == 0) {
                   1322:                                if (i > 0 || ac > 0) {
                   1323:                                        error("%s line %d: keyword %s \"none\" "
                   1324:                                            "argument must appear alone.",
                   1325:                                            filename, linenum, keyword);
                   1326:                                        goto out;
                   1327:                                }
                   1328:                        }
                   1329:                        i++;
1.357     djm      1330:                        if (*activep && value) {
1.344     djm      1331:                                if ((*uintptr) >= max_entries) {
1.356     djm      1332:                                        error("%s line %d: too many %s "
                   1333:                                            "entries.", filename, linenum,
                   1334:                                            keyword);
                   1335:                                        goto out;
1.344     djm      1336:                                }
1.193     djm      1337:                                cpptr[(*uintptr)++] = xstrdup(arg);
                   1338:                        }
                   1339:                }
1.356     djm      1340:                break;
1.17      markus   1341:
                   1342:        case oUserKnownHostsFile:
1.193     djm      1343:                cpptr = (char **)&options->user_hostfiles;
                   1344:                uintptr = &options->num_user_hostfiles;
                   1345:                max_entries = SSH_MAX_HOSTS_FILES;
                   1346:                goto parse_char_array;
1.27      markus   1347:
1.306     jmc      1348:        case oHostname:
1.17      markus   1349:                charptr = &options->hostname;
                   1350:                goto parse_string;
                   1351:
1.52      markus   1352:        case oHostKeyAlias:
                   1353:                charptr = &options->host_key_alias;
                   1354:                goto parse_string;
                   1355:
1.67      markus   1356:        case oPreferredAuthentications:
                   1357:                charptr = &options->preferred_authentications;
                   1358:                goto parse_string;
                   1359:
1.77      markus   1360:        case oBindAddress:
                   1361:                charptr = &options->bind_address;
                   1362:                goto parse_string;
                   1363:
1.282     djm      1364:        case oBindInterface:
                   1365:                charptr = &options->bind_interface;
                   1366:                goto parse_string;
                   1367:
1.183     markus   1368:        case oPKCS11Provider:
                   1369:                charptr = &options->pkcs11_provider;
1.86      markus   1370:                goto parse_string;
1.85      jakob    1371:
1.310     djm      1372:        case oSecurityKeyProvider:
                   1373:                charptr = &options->sk_provider;
                   1374:                goto parse_string;
                   1375:
1.346     djm      1376:        case oKnownHostsCommand:
                   1377:                charptr = &options->known_hosts_command;
                   1378:                goto parse_command;
                   1379:
1.17      markus   1380:        case oProxyCommand:
1.144     reyk     1381:                charptr = &options->proxy_command;
1.257     djm      1382:                /* Ignore ProxyCommand if ProxyJump already specified */
                   1383:                if (options->jump_host != NULL)
                   1384:                        charptr = &options->jump_host; /* Skip below */
1.144     reyk     1385: parse_command:
1.356     djm      1386:                if (str == NULL) {
1.344     djm      1387:                        error("%.200s line %d: Missing argument.",
                   1388:                            filename, linenum);
1.356     djm      1389:                        goto out;
1.344     djm      1390:                }
1.356     djm      1391:                len = strspn(str, WHITESPACE "=");
1.17      markus   1392:                if (*activep && *charptr == NULL)
1.356     djm      1393:                        *charptr = xstrdup(str + len);
                   1394:                argv_consume(&ac);
                   1395:                break;
1.17      markus   1396:
1.257     djm      1397:        case oProxyJump:
1.356     djm      1398:                if (str == NULL) {
1.344     djm      1399:                        error("%.200s line %d: Missing argument.",
1.257     djm      1400:                            filename, linenum);
1.356     djm      1401:                        goto out;
1.257     djm      1402:                }
1.356     djm      1403:                len = strspn(str, WHITESPACE "=");
                   1404:                /* XXX use argv? */
                   1405:                if (parse_jump(str + len, options, *activep) == -1) {
1.344     djm      1406:                        error("%.200s line %d: Invalid ProxyJump \"%s\"",
1.356     djm      1407:                            filename, linenum, str + len);
                   1408:                        goto out;
1.257     djm      1409:                }
1.356     djm      1410:                argv_consume(&ac);
                   1411:                break;
1.257     djm      1412:
1.17      markus   1413:        case oPort:
1.356     djm      1414:                arg = argv_next(&ac, &av);
1.344     djm      1415:                if (!arg || *arg == '\0') {
                   1416:                        error("%.200s line %d: Missing argument.",
1.300     naddy    1417:                            filename, linenum);
1.356     djm      1418:                        goto out;
1.344     djm      1419:                }
1.300     naddy    1420:                value = a2port(arg);
1.344     djm      1421:                if (value <= 0) {
                   1422:                        error("%.200s line %d: Bad port '%s'.",
1.300     naddy    1423:                            filename, linenum, arg);
1.356     djm      1424:                        goto out;
1.344     djm      1425:                }
1.300     naddy    1426:                if (*activep && options->port == -1)
                   1427:                        options->port = value;
                   1428:                break;
                   1429:
                   1430:        case oConnectionAttempts:
                   1431:                intptr = &options->connection_attempts;
1.17      markus   1432: parse_int:
1.356     djm      1433:                arg = argv_next(&ac, &av);
1.344     djm      1434:                if ((errstr = atoi_err(arg, &value)) != NULL) {
                   1435:                        error("%s line %d: integer value %s.",
1.281     dtucker  1436:                            filename, linenum, errstr);
1.356     djm      1437:                        goto out;
1.344     djm      1438:                }
1.17      markus   1439:                if (*activep && *intptr == -1)
                   1440:                        *intptr = value;
                   1441:                break;
                   1442:
1.25      markus   1443:        case oCiphers:
1.356     djm      1444:                arg = argv_next(&ac, &av);
1.344     djm      1445:                if (!arg || *arg == '\0') {
                   1446:                        error("%.200s line %d: Missing argument.",
                   1447:                            filename, linenum);
1.356     djm      1448:                        goto out;
1.344     djm      1449:                }
1.309     naddy    1450:                if (*arg != '-' &&
1.344     djm      1451:                    !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)){
                   1452:                        error("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt  1453:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1454:                        goto out;
1.344     djm      1455:                }
1.25      markus   1456:                if (*activep && options->ciphers == NULL)
1.38      provos   1457:                        options->ciphers = xstrdup(arg);
1.25      markus   1458:                break;
                   1459:
1.62      markus   1460:        case oMacs:
1.356     djm      1461:                arg = argv_next(&ac, &av);
1.344     djm      1462:                if (!arg || *arg == '\0') {
                   1463:                        error("%.200s line %d: Missing argument.",
                   1464:                            filename, linenum);
1.356     djm      1465:                        goto out;
1.344     djm      1466:                }
1.309     naddy    1467:                if (*arg != '-' &&
1.344     djm      1468:                    !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)) {
                   1469:                        error("%.200s line %d: Bad SSH2 MAC spec '%s'.",
1.93      deraadt  1470:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1471:                        goto out;
1.344     djm      1472:                }
1.62      markus   1473:                if (*activep && options->macs == NULL)
                   1474:                        options->macs = xstrdup(arg);
                   1475:                break;
                   1476:
1.189     djm      1477:        case oKexAlgorithms:
1.356     djm      1478:                arg = argv_next(&ac, &av);
1.344     djm      1479:                if (!arg || *arg == '\0') {
                   1480:                        error("%.200s line %d: Missing argument.",
1.189     djm      1481:                            filename, linenum);
1.356     djm      1482:                        goto out;
1.344     djm      1483:                }
1.268     djm      1484:                if (*arg != '-' &&
1.309     naddy    1485:                    !kex_names_valid(*arg == '+' || *arg == '^' ?
1.344     djm      1486:                    arg + 1 : arg)) {
                   1487:                        error("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1.189     djm      1488:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1489:                        goto out;
1.344     djm      1490:                }
1.189     djm      1491:                if (*activep && options->kex_algorithms == NULL)
                   1492:                        options->kex_algorithms = xstrdup(arg);
                   1493:                break;
                   1494:
1.76      markus   1495:        case oHostKeyAlgorithms:
1.238     markus   1496:                charptr = &options->hostkeyalgorithms;
1.377     djm      1497:                ca_only = 0;
1.349     dtucker  1498: parse_pubkey_algos:
1.356     djm      1499:                arg = argv_next(&ac, &av);
1.344     djm      1500:                if (!arg || *arg == '\0') {
                   1501:                        error("%.200s line %d: Missing argument.",
1.238     markus   1502:                            filename, linenum);
1.356     djm      1503:                        goto out;
1.344     djm      1504:                }
1.268     djm      1505:                if (*arg != '-' &&
1.309     naddy    1506:                    !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1.377     djm      1507:                    arg + 1 : arg, 1, ca_only)) {
1.344     djm      1508:                        error("%s line %d: Bad key types '%s'.",
                   1509:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1510:                        goto out;
1.344     djm      1511:                }
1.238     markus   1512:                if (*activep && *charptr == NULL)
                   1513:                        *charptr = xstrdup(arg);
1.76      markus   1514:                break;
                   1515:
1.298     djm      1516:        case oCASignatureAlgorithms:
                   1517:                charptr = &options->ca_sign_algorithms;
1.377     djm      1518:                ca_only = 1;
1.349     dtucker  1519:                goto parse_pubkey_algos;
1.298     djm      1520:
1.17      markus   1521:        case oLogLevel:
1.164     dtucker  1522:                log_level_ptr = &options->log_level;
1.356     djm      1523:                arg = argv_next(&ac, &av);
1.38      provos   1524:                value = log_level_number(arg);
1.344     djm      1525:                if (value == SYSLOG_LEVEL_NOT_SET) {
                   1526:                        error("%.200s line %d: unsupported log level '%s'",
1.93      deraadt  1527:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1528:                        goto out;
1.344     djm      1529:                }
1.164     dtucker  1530:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                   1531:                        *log_level_ptr = (LogLevel) value;
1.17      markus   1532:                break;
                   1533:
1.271     dtucker  1534:        case oLogFacility:
                   1535:                log_facility_ptr = &options->log_facility;
1.356     djm      1536:                arg = argv_next(&ac, &av);
1.271     dtucker  1537:                value = log_facility_number(arg);
1.344     djm      1538:                if (value == SYSLOG_FACILITY_NOT_SET) {
                   1539:                        error("%.200s line %d: unsupported log facility '%s'",
1.271     dtucker  1540:                            filename, linenum, arg ? arg : "<NONE>");
1.356     djm      1541:                        goto out;
1.344     djm      1542:                }
1.271     dtucker  1543:                if (*log_facility_ptr == -1)
                   1544:                        *log_facility_ptr = (SyslogFacility) value;
                   1545:                break;
                   1546:
1.339     djm      1547:        case oLogVerbose:
                   1548:                cppptr = &options->log_verbose;
                   1549:                uintptr = &options->num_log_verbose;
1.356     djm      1550:                i = 0;
                   1551:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   1552:                        if (*arg == '\0') {
                   1553:                                error("%s line %d: keyword %s empty argument",
                   1554:                                    filename, linenum, keyword);
                   1555:                                goto out;
                   1556:                        }
                   1557:                        /* Allow "none" only in first position */
                   1558:                        if (strcasecmp(arg, "none") == 0) {
                   1559:                                if (i > 0 || ac > 0) {
                   1560:                                        error("%s line %d: keyword %s \"none\" "
                   1561:                                            "argument must appear alone.",
                   1562:                                            filename, linenum, keyword);
                   1563:                                        goto out;
                   1564:                                }
                   1565:                        }
                   1566:                        i++;
                   1567:                        if (*activep && *uintptr == 0) {
1.339     djm      1568:                                *cppptr = xrecallocarray(*cppptr, *uintptr,
                   1569:                                    *uintptr + 1, sizeof(**cppptr));
                   1570:                                (*cppptr)[(*uintptr)++] = xstrdup(arg);
                   1571:                        }
                   1572:                }
1.356     djm      1573:                break;
1.339     djm      1574:
1.88      stevesk  1575:        case oLocalForward:
1.17      markus   1576:        case oRemoteForward:
1.168     stevesk  1577:        case oDynamicForward:
1.356     djm      1578:                arg = argv_next(&ac, &av);
1.344     djm      1579:                if (!arg || *arg == '\0') {
                   1580:                        error("%.200s line %d: Missing argument.",
1.88      stevesk  1581:                            filename, linenum);
1.356     djm      1582:                        goto out;
1.344     djm      1583:                }
1.135     djm      1584:
1.279     markus   1585:                remotefwd = (opcode == oRemoteForward);
                   1586:                dynamicfwd = (opcode == oDynamicForward);
                   1587:
                   1588:                if (!dynamicfwd) {
1.356     djm      1589:                        arg2 = argv_next(&ac, &av);
1.279     markus   1590:                        if (arg2 == NULL || *arg2 == '\0') {
                   1591:                                if (remotefwd)
                   1592:                                        dynamicfwd = 1;
1.344     djm      1593:                                else {
                   1594:                                        error("%.200s line %d: Missing target "
1.279     markus   1595:                                            "argument.", filename, linenum);
1.356     djm      1596:                                        goto out;
1.344     djm      1597:                                }
1.279     markus   1598:                        } else {
                   1599:                                /* construct a string for parse_forward */
                   1600:                                snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
                   1601:                                    arg2);
                   1602:                        }
                   1603:                }
                   1604:                if (dynamicfwd)
1.168     stevesk  1605:                        strlcpy(fwdarg, arg, sizeof(fwdarg));
                   1606:
1.344     djm      1607:                if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0) {
                   1608:                        error("%.200s line %d: Bad forwarding specification.",
1.88      stevesk  1609:                            filename, linenum);
1.356     djm      1610:                        goto out;
1.344     djm      1611:                }
1.135     djm      1612:
1.88      stevesk  1613:                if (*activep) {
1.279     markus   1614:                        if (remotefwd) {
                   1615:                                add_remote_forward(options, &fwd);
                   1616:                        } else {
1.135     djm      1617:                                add_local_forward(options, &fwd);
1.279     markus   1618:                        }
1.88      stevesk  1619:                }
1.17      markus   1620:                break;
1.71      markus   1621:
1.351     markus   1622:        case oPermitRemoteOpen:
                   1623:                uintptr = &options->num_permitted_remote_opens;
                   1624:                cppptr = &options->permitted_remote_opens;
                   1625:                uvalue = *uintptr;      /* modified later */
1.371     djm      1626:                i = 0;
1.356     djm      1627:                while ((arg = argv_next(&ac, &av)) != NULL) {
1.351     markus   1628:                        arg2 = xstrdup(arg);
1.371     djm      1629:                        /* Allow any/none only in first position */
                   1630:                        if (strcasecmp(arg, "none") == 0 ||
                   1631:                            strcasecmp(arg, "any") == 0) {
                   1632:                                if (i > 0 || ac > 0) {
                   1633:                                        error("%s line %d: keyword %s \"%s\" "
                   1634:                                            "argument must appear alone.",
                   1635:                                            filename, linenum, keyword, arg);
1.376     djm      1636:                                        free(arg2);
1.371     djm      1637:                                        goto out;
                   1638:                                }
                   1639:                        } else {
                   1640:                                p = hpdelim(&arg);
                   1641:                                if (p == NULL) {
                   1642:                                        fatal("%s line %d: missing host in %s",
                   1643:                                            filename, linenum,
                   1644:                                            lookup_opcode_name(opcode));
                   1645:                                }
                   1646:                                p = cleanhostname(p);
                   1647:                                /*
                   1648:                                 * don't want to use permitopen_port to avoid
                   1649:                                 * dependency on channels.[ch] here.
                   1650:                                 */
                   1651:                                if (arg == NULL || (strcmp(arg, "*") != 0 &&
                   1652:                                    a2port(arg) <= 0)) {
                   1653:                                        fatal("%s line %d: bad port number "
                   1654:                                            "in %s", filename, linenum,
                   1655:                                            lookup_opcode_name(opcode));
                   1656:                                }
1.351     markus   1657:                        }
                   1658:                        if (*activep && uvalue == 0) {
                   1659:                                opt_array_append(filename, linenum,
                   1660:                                    lookup_opcode_name(opcode),
                   1661:                                    cppptr, uintptr, arg2);
                   1662:                        }
                   1663:                        free(arg2);
1.371     djm      1664:                        i++;
1.351     markus   1665:                }
1.371     djm      1666:                if (i == 0)
                   1667:                        fatal("%s line %d: missing %s specification",
                   1668:                            filename, linenum, lookup_opcode_name(opcode));
1.351     markus   1669:                break;
                   1670:
1.90      stevesk  1671:        case oClearAllForwardings:
                   1672:                intptr = &options->clear_forwardings;
                   1673:                goto parse_flag;
                   1674:
1.17      markus   1675:        case oHost:
1.344     djm      1676:                if (cmdline) {
                   1677:                        error("Host directive not supported as a command-line "
1.206     djm      1678:                            "option");
1.356     djm      1679:                        goto out;
1.344     djm      1680:                }
1.17      markus   1681:                *activep = 0;
1.191     djm      1682:                arg2 = NULL;
1.356     djm      1683:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   1684:                        if (*arg == '\0') {
                   1685:                                error("%s line %d: keyword %s empty argument",
                   1686:                                    filename, linenum, keyword);
                   1687:                                goto out;
                   1688:                        }
                   1689:                        if ((flags & SSHCONF_NEVERMATCH) != 0) {
                   1690:                                argv_consume(&ac);
1.252     djm      1691:                                break;
1.356     djm      1692:                        }
1.191     djm      1693:                        negated = *arg == '!';
                   1694:                        if (negated)
                   1695:                                arg++;
1.38      provos   1696:                        if (match_pattern(host, arg)) {
1.191     djm      1697:                                if (negated) {
                   1698:                                        debug("%.200s line %d: Skipping Host "
                   1699:                                            "block because of negated match "
                   1700:                                            "for %.100s", filename, linenum,
                   1701:                                            arg);
                   1702:                                        *activep = 0;
1.356     djm      1703:                                        argv_consume(&ac);
1.191     djm      1704:                                        break;
                   1705:                                }
                   1706:                                if (!*activep)
                   1707:                                        arg2 = arg; /* logged below */
1.17      markus   1708:                                *activep = 1;
                   1709:                        }
1.191     djm      1710:                }
                   1711:                if (*activep)
                   1712:                        debug("%.200s line %d: Applying options for %.100s",
                   1713:                            filename, linenum, arg2);
1.356     djm      1714:                break;
1.17      markus   1715:
1.206     djm      1716:        case oMatch:
1.344     djm      1717:                if (cmdline) {
                   1718:                        error("Host directive not supported as a command-line "
1.206     djm      1719:                            "option");
1.356     djm      1720:                        goto out;
1.344     djm      1721:                }
1.356     djm      1722:                value = match_cfg_line(options, &str, pw, host, original_host,
1.302     djm      1723:                    flags & SSHCONF_FINAL, want_final_pass,
                   1724:                    filename, linenum);
1.344     djm      1725:                if (value < 0) {
                   1726:                        error("%.200s line %d: Bad Match condition", filename,
1.206     djm      1727:                            linenum);
1.356     djm      1728:                        goto out;
1.344     djm      1729:                }
1.252     djm      1730:                *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
1.356     djm      1731:                /*
                   1732:                 * If match_cfg_line() didn't consume all its arguments then
                   1733:                 * arrange for the extra arguments check below to fail.
                   1734:                 */
                   1735:
                   1736:                if (str == NULL || *str == '\0')
                   1737:                        argv_consume(&ac);
1.206     djm      1738:                break;
                   1739:
1.17      markus   1740:        case oEscapeChar:
                   1741:                intptr = &options->escape_char;
1.356     djm      1742:                arg = argv_next(&ac, &av);
1.344     djm      1743:                if (!arg || *arg == '\0') {
                   1744:                        error("%.200s line %d: Missing argument.",
                   1745:                            filename, linenum);
1.356     djm      1746:                        goto out;
1.344     djm      1747:                }
1.236     djm      1748:                if (strcmp(arg, "none") == 0)
                   1749:                        value = SSH_ESCAPECHAR_NONE;
                   1750:                else if (arg[1] == '\0')
                   1751:                        value = (u_char) arg[0];
                   1752:                else if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus   1753:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                   1754:                        value = (u_char) arg[1] & 31;
1.17      markus   1755:                else {
1.344     djm      1756:                        error("%.200s line %d: Bad escape character.",
1.93      deraadt  1757:                            filename, linenum);
1.356     djm      1758:                        goto out;
1.17      markus   1759:                }
                   1760:                if (*activep && *intptr == -1)
                   1761:                        *intptr = value;
1.112     djm      1762:                break;
                   1763:
                   1764:        case oAddressFamily:
1.114     djm      1765:                intptr = &options->address_family;
1.207     djm      1766:                multistate_ptr = multistate_addressfamily;
                   1767:                goto parse_multistate;
1.17      markus   1768:
1.101     markus   1769:        case oEnableSSHKeysign:
                   1770:                intptr = &options->enable_ssh_keysign;
                   1771:                goto parse_flag;
                   1772:
1.128     markus   1773:        case oIdentitiesOnly:
                   1774:                intptr = &options->identities_only;
                   1775:                goto parse_flag;
                   1776:
1.127     markus   1777:        case oServerAliveInterval:
                   1778:                intptr = &options->server_alive_interval;
                   1779:                goto parse_time;
                   1780:
                   1781:        case oServerAliveCountMax:
                   1782:                intptr = &options->server_alive_count_max;
                   1783:                goto parse_int;
                   1784:
1.130     djm      1785:        case oSendEnv:
1.356     djm      1786:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   1787:                        if (*arg == '\0' || strchr(arg, '=') != NULL) {
1.344     djm      1788:                                error("%s line %d: Invalid environment name.",
1.130     djm      1789:                                    filename, linenum);
1.356     djm      1790:                                goto out;
1.344     djm      1791:                        }
1.137     djm      1792:                        if (!*activep)
                   1793:                                continue;
1.286     djm      1794:                        if (*arg == '-') {
                   1795:                                /* Removing an env var */
                   1796:                                rm_env(options, arg, filename, linenum);
                   1797:                                continue;
                   1798:                        }
1.368     djm      1799:                        opt_array_append(filename, linenum,
                   1800:                            lookup_opcode_name(opcode),
                   1801:                            &options->send_env, &options->num_send_env, arg);
1.130     djm      1802:                }
                   1803:                break;
                   1804:
1.290     djm      1805:        case oSetEnv:
                   1806:                value = options->num_setenv;
1.356     djm      1807:                while ((arg = argv_next(&ac, &av)) != NULL) {
1.344     djm      1808:                        if (strchr(arg, '=') == NULL) {
                   1809:                                error("%s line %d: Invalid SetEnv.",
1.290     djm      1810:                                    filename, linenum);
1.356     djm      1811:                                goto out;
1.344     djm      1812:                        }
1.290     djm      1813:                        if (!*activep || value != 0)
                   1814:                                continue;
1.368     djm      1815:                        if (lookup_setenv_in_list(arg, options->setenv,
                   1816:                            options->num_setenv) != NULL) {
                   1817:                                debug2("%s line %d: ignoring duplicate env "
                   1818:                                    "name \"%.64s\"", filename, linenum, arg);
                   1819:                                continue;
1.344     djm      1820:                        }
1.368     djm      1821:                        opt_array_append(filename, linenum,
                   1822:                            lookup_opcode_name(opcode),
                   1823:                            &options->setenv, &options->num_setenv, arg);
1.290     djm      1824:                }
                   1825:                break;
                   1826:
1.132     djm      1827:        case oControlPath:
                   1828:                charptr = &options->control_path;
                   1829:                goto parse_string;
                   1830:
                   1831:        case oControlMaster:
                   1832:                intptr = &options->control_master;
1.207     djm      1833:                multistate_ptr = multistate_controlmaster;
                   1834:                goto parse_multistate;
1.132     djm      1835:
1.187     djm      1836:        case oControlPersist:
                   1837:                /* no/false/yes/true, or a time spec */
                   1838:                intptr = &options->control_persist;
1.356     djm      1839:                arg = argv_next(&ac, &av);
1.344     djm      1840:                if (!arg || *arg == '\0') {
                   1841:                        error("%.200s line %d: Missing ControlPersist"
1.187     djm      1842:                            " argument.", filename, linenum);
1.356     djm      1843:                        goto out;
1.344     djm      1844:                }
1.187     djm      1845:                value = 0;
                   1846:                value2 = 0;     /* timeout */
                   1847:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                   1848:                        value = 0;
                   1849:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                   1850:                        value = 1;
                   1851:                else if ((value2 = convtime(arg)) >= 0)
                   1852:                        value = 1;
1.344     djm      1853:                else {
                   1854:                        error("%.200s line %d: Bad ControlPersist argument.",
1.187     djm      1855:                            filename, linenum);
1.356     djm      1856:                        goto out;
1.344     djm      1857:                }
1.187     djm      1858:                if (*activep && *intptr == -1) {
                   1859:                        *intptr = value;
                   1860:                        options->control_persist_timeout = value2;
                   1861:                }
                   1862:                break;
                   1863:
1.136     djm      1864:        case oHashKnownHosts:
                   1865:                intptr = &options->hash_known_hosts;
                   1866:                goto parse_flag;
                   1867:
1.144     reyk     1868:        case oTunnel:
                   1869:                intptr = &options->tun_open;
1.207     djm      1870:                multistate_ptr = multistate_tunnel;
                   1871:                goto parse_multistate;
1.144     reyk     1872:
                   1873:        case oTunnelDevice:
1.356     djm      1874:                arg = argv_next(&ac, &av);
1.344     djm      1875:                if (!arg || *arg == '\0') {
                   1876:                        error("%.200s line %d: Missing argument.",
                   1877:                            filename, linenum);
1.356     djm      1878:                        goto out;
1.344     djm      1879:                }
1.144     reyk     1880:                value = a2tun(arg, &value2);
1.344     djm      1881:                if (value == SSH_TUNID_ERR) {
                   1882:                        error("%.200s line %d: Bad tun device.",
                   1883:                            filename, linenum);
1.356     djm      1884:                        goto out;
1.344     djm      1885:                }
1.355     dtucker  1886:                if (*activep && options->tun_local == -1) {
1.144     reyk     1887:                        options->tun_local = value;
                   1888:                        options->tun_remote = value2;
                   1889:                }
                   1890:                break;
                   1891:
                   1892:        case oLocalCommand:
                   1893:                charptr = &options->local_command;
                   1894:                goto parse_command;
                   1895:
                   1896:        case oPermitLocalCommand:
                   1897:                intptr = &options->permit_local_command;
                   1898:                goto parse_flag;
                   1899:
1.277     bluhm    1900:        case oRemoteCommand:
                   1901:                charptr = &options->remote_command;
                   1902:                goto parse_command;
                   1903:
1.167     grunk    1904:        case oVisualHostKey:
                   1905:                intptr = &options->visual_host_key;
                   1906:                goto parse_flag;
                   1907:
1.252     djm      1908:        case oInclude:
1.344     djm      1909:                if (cmdline) {
                   1910:                        error("Include directive not supported as a "
1.252     djm      1911:                            "command-line option");
1.356     djm      1912:                        goto out;
1.344     djm      1913:                }
1.252     djm      1914:                value = 0;
1.356     djm      1915:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   1916:                        if (*arg == '\0') {
                   1917:                                error("%s line %d: keyword %s empty argument",
                   1918:                                    filename, linenum, keyword);
                   1919:                                goto out;
                   1920:                        }
1.252     djm      1921:                        /*
                   1922:                         * Ensure all paths are anchored. User configuration
                   1923:                         * files may begin with '~/' but system configurations
                   1924:                         * must not. If the path is relative, then treat it
                   1925:                         * as living in ~/.ssh for user configurations or
                   1926:                         * /etc/ssh for system ones.
                   1927:                         */
1.344     djm      1928:                        if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0) {
                   1929:                                error("%.200s line %d: bad include path %s.",
1.252     djm      1930:                                    filename, linenum, arg);
1.356     djm      1931:                                goto out;
1.344     djm      1932:                        }
1.301     djm      1933:                        if (!path_absolute(arg) && *arg != '~') {
1.252     djm      1934:                                xasprintf(&arg2, "%s/%s",
                   1935:                                    (flags & SSHCONF_USERCONF) ?
                   1936:                                    "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
                   1937:                        } else
                   1938:                                arg2 = xstrdup(arg);
                   1939:                        memset(&gl, 0, sizeof(gl));
                   1940:                        r = glob(arg2, GLOB_TILDE, NULL, &gl);
                   1941:                        if (r == GLOB_NOMATCH) {
                   1942:                                debug("%.200s line %d: include %s matched no "
                   1943:                                    "files",filename, linenum, arg2);
1.269     dtucker  1944:                                free(arg2);
1.252     djm      1945:                                continue;
1.344     djm      1946:                        } else if (r != 0) {
                   1947:                                error("%.200s line %d: glob failed for %s.",
1.252     djm      1948:                                    filename, linenum, arg2);
1.356     djm      1949:                                goto out;
1.344     djm      1950:                        }
1.252     djm      1951:                        free(arg2);
                   1952:                        oactive = *activep;
1.313     deraadt  1953:                        for (i = 0; i < gl.gl_pathc; i++) {
1.252     djm      1954:                                debug3("%.200s line %d: Including file %s "
                   1955:                                    "depth %d%s", filename, linenum,
                   1956:                                    gl.gl_pathv[i], depth,
                   1957:                                    oactive ? "" : " (parse only)");
                   1958:                                r = read_config_file_depth(gl.gl_pathv[i],
                   1959:                                    pw, host, original_host, options,
                   1960:                                    flags | SSHCONF_CHECKPERM |
                   1961:                                    (oactive ? 0 : SSHCONF_NEVERMATCH),
1.302     djm      1962:                                    activep, want_final_pass, depth + 1);
1.264     djm      1963:                                if (r != 1 && errno != ENOENT) {
1.344     djm      1964:                                        error("Can't open user config file "
1.263     djm      1965:                                            "%.100s: %.100s", gl.gl_pathv[i],
                   1966:                                            strerror(errno));
1.344     djm      1967:                                        globfree(&gl);
1.356     djm      1968:                                        goto out;
1.263     djm      1969:                                }
1.252     djm      1970:                                /*
                   1971:                                 * don't let Match in includes clobber the
                   1972:                                 * containing file's Match state.
                   1973:                                 */
                   1974:                                *activep = oactive;
                   1975:                                if (r != 1)
                   1976:                                        value = -1;
                   1977:                        }
                   1978:                        globfree(&gl);
                   1979:                }
                   1980:                if (value != 0)
1.356     djm      1981:                        ret = value;
1.252     djm      1982:                break;
                   1983:
1.190     djm      1984:        case oIPQoS:
1.356     djm      1985:                arg = argv_next(&ac, &av);
1.344     djm      1986:                if ((value = parse_ipqos(arg)) == -1) {
                   1987:                        error("%s line %d: Bad IPQoS value: %s",
1.190     djm      1988:                            filename, linenum, arg);
1.356     djm      1989:                        goto out;
1.344     djm      1990:                }
1.356     djm      1991:                arg = argv_next(&ac, &av);
1.190     djm      1992:                if (arg == NULL)
                   1993:                        value2 = value;
1.344     djm      1994:                else if ((value2 = parse_ipqos(arg)) == -1) {
                   1995:                        error("%s line %d: Bad IPQoS value: %s",
1.190     djm      1996:                            filename, linenum, arg);
1.356     djm      1997:                        goto out;
1.344     djm      1998:                }
1.355     dtucker  1999:                if (*activep && options->ip_qos_interactive == -1) {
1.190     djm      2000:                        options->ip_qos_interactive = value;
                   2001:                        options->ip_qos_bulk = value2;
                   2002:                }
                   2003:                break;
                   2004:
1.192     djm      2005:        case oRequestTTY:
                   2006:                intptr = &options->request_tty;
1.207     djm      2007:                multistate_ptr = multistate_requesttty;
                   2008:                goto parse_multistate;
1.192     djm      2009:
1.359     djm      2010:        case oSessionType:
                   2011:                intptr = &options->session_type;
                   2012:                multistate_ptr = multistate_sessiontype;
                   2013:                goto parse_multistate;
                   2014:
1.360     djm      2015:        case oStdinNull:
                   2016:                intptr = &options->stdin_null;
                   2017:                goto parse_flag;
                   2018:
1.361     djm      2019:        case oForkAfterAuthentication:
                   2020:                intptr = &options->fork_after_authentication;
                   2021:                goto parse_flag;
                   2022:
1.199     djm      2023:        case oIgnoreUnknown:
                   2024:                charptr = &options->ignored_unknown;
                   2025:                goto parse_string;
                   2026:
1.205     djm      2027:        case oProxyUseFdpass:
                   2028:                intptr = &options->proxy_use_fdpass;
                   2029:                goto parse_flag;
                   2030:
1.208     djm      2031:        case oCanonicalDomains:
                   2032:                value = options->num_canonical_domains != 0;
1.356     djm      2033:                i = 0;
                   2034:                while ((arg = argv_next(&ac, &av)) != NULL) {
                   2035:                        if (*arg == '\0') {
                   2036:                                error("%s line %d: keyword %s empty argument",
                   2037:                                    filename, linenum, keyword);
                   2038:                                goto out;
                   2039:                        }
                   2040:                        /* Allow "none" only in first position */
                   2041:                        if (strcasecmp(arg, "none") == 0) {
                   2042:                                if (i > 0 || ac > 0) {
                   2043:                                        error("%s line %d: keyword %s \"none\" "
                   2044:                                            "argument must appear alone.",
                   2045:                                            filename, linenum, keyword);
                   2046:                                        goto out;
                   2047:                                }
                   2048:                        }
                   2049:                        i++;
1.280     millert  2050:                        if (!valid_domain(arg, 1, &errstr)) {
1.344     djm      2051:                                error("%s line %d: %s", filename, linenum,
1.280     millert  2052:                                    errstr);
1.356     djm      2053:                                goto out;
1.280     millert  2054:                        }
1.208     djm      2055:                        if (!*activep || value)
                   2056:                                continue;
1.344     djm      2057:                        if (options->num_canonical_domains >=
                   2058:                            MAX_CANON_DOMAINS) {
                   2059:                                error("%s line %d: too many hostname suffixes.",
1.208     djm      2060:                                    filename, linenum);
1.356     djm      2061:                                goto out;
1.344     djm      2062:                        }
1.208     djm      2063:                        options->canonical_domains[
                   2064:                            options->num_canonical_domains++] = xstrdup(arg);
                   2065:                }
                   2066:                break;
                   2067:
1.209     djm      2068:        case oCanonicalizePermittedCNAMEs:
1.208     djm      2069:                value = options->num_permitted_cnames != 0;
1.362     djm      2070:                i = 0;
1.356     djm      2071:                while ((arg = argv_next(&ac, &av)) != NULL) {
1.362     djm      2072:                        /*
                   2073:                         * Either 'none' (only in first position), '*' for
                   2074:                         * everything or 'list:list'
                   2075:                         */
                   2076:                        if (strcasecmp(arg, "none") == 0) {
                   2077:                                if (i > 0 || ac > 0) {
                   2078:                                        error("%s line %d: keyword %s \"none\" "
                   2079:                                            "argument must appear alone.",
                   2080:                                            filename, linenum, keyword);
                   2081:                                        goto out;
                   2082:                                }
                   2083:                                arg2 = "";
                   2084:                        } else if (strcmp(arg, "*") == 0) {
1.208     djm      2085:                                arg2 = arg;
1.362     djm      2086:                        } else {
1.208     djm      2087:                                lowercase(arg);
                   2088:                                if ((arg2 = strchr(arg, ':')) == NULL ||
                   2089:                                    arg2[1] == '\0') {
1.344     djm      2090:                                        error("%s line %d: "
1.208     djm      2091:                                            "Invalid permitted CNAME \"%s\"",
                   2092:                                            filename, linenum, arg);
1.356     djm      2093:                                        goto out;
1.208     djm      2094:                                }
                   2095:                                *arg2 = '\0';
                   2096:                                arg2++;
                   2097:                        }
1.362     djm      2098:                        i++;
1.208     djm      2099:                        if (!*activep || value)
                   2100:                                continue;
1.344     djm      2101:                        if (options->num_permitted_cnames >=
                   2102:                            MAX_CANON_DOMAINS) {
                   2103:                                error("%s line %d: too many permitted CNAMEs.",
1.208     djm      2104:                                    filename, linenum);
1.356     djm      2105:                                goto out;
1.344     djm      2106:                        }
1.208     djm      2107:                        cname = options->permitted_cnames +
                   2108:                            options->num_permitted_cnames++;
                   2109:                        cname->source_list = xstrdup(arg);
                   2110:                        cname->target_list = xstrdup(arg2);
                   2111:                }
                   2112:                break;
                   2113:
1.209     djm      2114:        case oCanonicalizeHostname:
                   2115:                intptr = &options->canonicalize_hostname;
                   2116:                multistate_ptr = multistate_canonicalizehostname;
1.208     djm      2117:                goto parse_multistate;
                   2118:
1.209     djm      2119:        case oCanonicalizeMaxDots:
                   2120:                intptr = &options->canonicalize_max_dots;
1.208     djm      2121:                goto parse_int;
                   2122:
1.209     djm      2123:        case oCanonicalizeFallbackLocal:
                   2124:                intptr = &options->canonicalize_fallback_local;
1.208     djm      2125:                goto parse_flag;
                   2126:
1.220     millert  2127:        case oStreamLocalBindMask:
1.356     djm      2128:                arg = argv_next(&ac, &av);
1.344     djm      2129:                if (!arg || *arg == '\0') {
                   2130:                        error("%.200s line %d: Missing StreamLocalBindMask "
                   2131:                            "argument.", filename, linenum);
1.356     djm      2132:                        goto out;
1.344     djm      2133:                }
1.220     millert  2134:                /* Parse mode in octal format */
                   2135:                value = strtol(arg, &endofnumber, 8);
1.344     djm      2136:                if (arg == endofnumber || value < 0 || value > 0777) {
                   2137:                        error("%.200s line %d: Bad mask.", filename, linenum);
1.356     djm      2138:                        goto out;
1.344     djm      2139:                }
1.220     millert  2140:                options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
                   2141:                break;
                   2142:
                   2143:        case oStreamLocalBindUnlink:
                   2144:                intptr = &options->fwd_opts.streamlocal_bind_unlink;
                   2145:                goto parse_flag;
                   2146:
1.223     djm      2147:        case oRevokedHostKeys:
                   2148:                charptr = &options->revoked_host_keys;
                   2149:                goto parse_string;
                   2150:
1.224     djm      2151:        case oFingerprintHash:
1.225     djm      2152:                intptr = &options->fingerprint_hash;
1.356     djm      2153:                arg = argv_next(&ac, &av);
1.344     djm      2154:                if (!arg || *arg == '\0') {
                   2155:                        error("%.200s line %d: Missing argument.",
1.224     djm      2156:                            filename, linenum);
1.356     djm      2157:                        goto out;
1.344     djm      2158:                }
                   2159:                if ((value = ssh_digest_alg_by_name(arg)) == -1) {
                   2160:                        error("%.200s line %d: Invalid hash algorithm \"%s\".",
1.224     djm      2161:                            filename, linenum, arg);
1.356     djm      2162:                        goto out;
1.344     djm      2163:                }
1.225     djm      2164:                if (*activep && *intptr == -1)
                   2165:                        *intptr = value;
1.224     djm      2166:                break;
                   2167:
1.229     djm      2168:        case oUpdateHostkeys:
                   2169:                intptr = &options->update_hostkeys;
1.232     djm      2170:                multistate_ptr = multistate_yesnoask;
                   2171:                goto parse_multistate;
1.229     djm      2172:
1.350     dtucker  2173:        case oHostbasedAcceptedAlgorithms:
                   2174:                charptr = &options->hostbased_accepted_algos;
1.377     djm      2175:                ca_only = 0;
1.349     dtucker  2176:                goto parse_pubkey_algos;
1.238     markus   2177:
1.349     dtucker  2178:        case oPubkeyAcceptedAlgorithms:
                   2179:                charptr = &options->pubkey_accepted_algos;
1.377     djm      2180:                ca_only = 0;
1.349     dtucker  2181:                goto parse_pubkey_algos;
1.230     djm      2182:
1.246     jcs      2183:        case oAddKeysToAgent:
1.356     djm      2184:                arg = argv_next(&ac, &av);
                   2185:                arg2 = argv_next(&ac, &av);
1.334     djm      2186:                value = parse_multistate_value(arg, filename, linenum,
1.353     djm      2187:                    multistate_yesnoaskconfirm);
1.334     djm      2188:                value2 = 0; /* unlimited lifespan by default */
                   2189:                if (value == 3 && arg2 != NULL) {
                   2190:                        /* allow "AddKeysToAgent confirm 5m" */
1.375     dtucker  2191:                        if ((value2 = convtime(arg2)) == -1) {
1.344     djm      2192:                                error("%s line %d: invalid time value.",
1.334     djm      2193:                                    filename, linenum);
1.356     djm      2194:                                goto out;
1.344     djm      2195:                        }
1.334     djm      2196:                } else if (value == -1 && arg2 == NULL) {
1.375     dtucker  2197:                        if ((value2 = convtime(arg)) == -1) {
1.344     djm      2198:                                error("%s line %d: unsupported option",
1.334     djm      2199:                                    filename, linenum);
1.356     djm      2200:                                goto out;
1.344     djm      2201:                        }
1.334     djm      2202:                        value = 1; /* yes */
                   2203:                } else if (value == -1 || arg2 != NULL) {
1.344     djm      2204:                        error("%s line %d: unsupported option",
1.334     djm      2205:                            filename, linenum);
1.356     djm      2206:                        goto out;
1.334     djm      2207:                }
                   2208:                if (*activep && options->add_keys_to_agent == -1) {
                   2209:                        options->add_keys_to_agent = value;
                   2210:                        options->add_keys_to_agent_lifespan = value2;
                   2211:                }
                   2212:                break;
1.246     jcs      2213:
1.253     markus   2214:        case oIdentityAgent:
                   2215:                charptr = &options->identity_agent;
1.356     djm      2216:                arg = argv_next(&ac, &av);
1.344     djm      2217:                if (!arg || *arg == '\0') {
                   2218:                        error("%.200s line %d: Missing argument.",
1.299     djm      2219:                            filename, linenum);
1.356     djm      2220:                        goto out;
1.344     djm      2221:                }
1.319     djm      2222:   parse_agent_path:
1.299     djm      2223:                /* Extra validation if the string represents an env var. */
1.344     djm      2224:                if ((arg2 = dollar_expand(&r, arg)) == NULL || r) {
                   2225:                        error("%.200s line %d: Invalid environment expansion "
1.331     dtucker  2226:                            "%s.", filename, linenum, arg);
1.356     djm      2227:                        goto out;
1.344     djm      2228:                }
1.331     dtucker  2229:                free(arg2);
                   2230:                /* check for legacy environment format */
1.344     djm      2231:                if (arg[0] == '$' && arg[1] != '{' &&
                   2232:                    !valid_env_name(arg + 1)) {
                   2233:                        error("%.200s line %d: Invalid environment name %s.",
1.299     djm      2234:                            filename, linenum, arg);
1.356     djm      2235:                        goto out;
1.299     djm      2236:                }
                   2237:                if (*activep && *charptr == NULL)
                   2238:                        *charptr = xstrdup(arg);
                   2239:                break;
1.253     markus   2240:
1.370     djm      2241:        case oEnableEscapeCommandline:
                   2242:                intptr = &options->enable_escape_commandline;
                   2243:                goto parse_flag;
                   2244:
1.369     djm      2245:        case oRequiredRSASize:
                   2246:                intptr = &options->required_rsa_size;
                   2247:                goto parse_int;
                   2248:
1.96      markus   2249:        case oDeprecated:
1.98      markus   2250:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   2251:                    filename, linenum, keyword);
1.356     djm      2252:                argv_consume(&ac);
                   2253:                break;
1.96      markus   2254:
1.110     jakob    2255:        case oUnsupported:
                   2256:                error("%s line %d: Unsupported option \"%s\"",
                   2257:                    filename, linenum, keyword);
1.356     djm      2258:                argv_consume(&ac);
                   2259:                break;
1.110     jakob    2260:
1.17      markus   2261:        default:
1.344     djm      2262:                error("%s line %d: Unimplemented opcode %d",
                   2263:                    filename, linenum, opcode);
1.356     djm      2264:                goto out;
1.17      markus   2265:        }
                   2266:
                   2267:        /* Check that there is no garbage at end of line. */
1.356     djm      2268:        if (ac > 0) {
                   2269:                error("%.200s line %d: keyword %s extra arguments "
                   2270:                    "at end of line", filename, linenum, keyword);
                   2271:                goto out;
1.39      ho       2272:        }
1.356     djm      2273:
                   2274:        /* success */
                   2275:        ret = 0;
                   2276:  out:
                   2277:        argv_free(oav, oac);
                   2278:        return ret;
1.1       deraadt  2279: }
                   2280:
1.19      markus   2281: /*
                   2282:  * Reads the config file and modifies the options accordingly.  Options
                   2283:  * should already be initialized before this call.  This never returns if
1.89      stevesk  2284:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   2285:  */
1.89      stevesk  2286: int
1.206     djm      2287: read_config_file(const char *filename, struct passwd *pw, const char *host,
1.302     djm      2288:     const char *original_host, Options *options, int flags,
                   2289:     int *want_final_pass)
1.1       deraadt  2290: {
1.252     djm      2291:        int active = 1;
                   2292:
                   2293:        return read_config_file_depth(filename, pw, host, original_host,
1.302     djm      2294:            options, flags, &active, want_final_pass, 0);
1.252     djm      2295: }
                   2296:
                   2297: #define READCONF_MAX_DEPTH     16
                   2298: static int
                   2299: read_config_file_depth(const char *filename, struct passwd *pw,
                   2300:     const char *host, const char *original_host, Options *options,
1.302     djm      2301:     int flags, int *activep, int *want_final_pass, int depth)
1.252     djm      2302: {
1.17      markus   2303:        FILE *f;
1.356     djm      2304:        char *line = NULL;
1.289     markus   2305:        size_t linesize = 0;
1.252     djm      2306:        int linenum;
1.17      markus   2307:        int bad_options = 0;
                   2308:
1.252     djm      2309:        if (depth < 0 || depth > READCONF_MAX_DEPTH)
                   2310:                fatal("Too many recursive configuration includes");
                   2311:
1.129     djm      2312:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  2313:                return 0;
1.129     djm      2314:
1.196     dtucker  2315:        if (flags & SSHCONF_CHECKPERM) {
1.129     djm      2316:                struct stat sb;
1.134     deraadt  2317:
1.131     dtucker  2318:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      2319:                        fatal("fstat %s: %s", filename, strerror(errno));
                   2320:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  2321:                    (sb.st_mode & 022) != 0))
1.129     djm      2322:                        fatal("Bad owner or permissions on %s", filename);
                   2323:        }
1.17      markus   2324:
                   2325:        debug("Reading configuration data %.200s", filename);
                   2326:
1.19      markus   2327:        /*
                   2328:         * Mark that we are now processing the options.  This flag is turned
                   2329:         * on/off by Host specifications.
                   2330:         */
1.17      markus   2331:        linenum = 0;
1.289     markus   2332:        while (getline(&line, &linesize, f) != -1) {
1.17      markus   2333:                /* Update line number counter. */
                   2334:                linenum++;
1.343     dtucker  2335:                /*
                   2336:                 * Trim out comments and strip whitespace.
                   2337:                 * NB - preserve newlines, they are needed to reproduce
                   2338:                 * line numbers later for error messages.
                   2339:                 */
1.252     djm      2340:                if (process_config_line_depth(options, pw, host, original_host,
1.302     djm      2341:                    line, filename, linenum, activep, flags, want_final_pass,
                   2342:                    depth) != 0)
1.17      markus   2343:                        bad_options++;
                   2344:        }
1.289     markus   2345:        free(line);
1.17      markus   2346:        fclose(f);
                   2347:        if (bad_options > 0)
1.64      millert  2348:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  2349:                    filename, bad_options);
1.89      stevesk  2350:        return 1;
1.1       deraadt  2351: }
                   2352:
1.218     djm      2353: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                   2354: int
                   2355: option_clear_or_none(const char *o)
                   2356: {
                   2357:        return o == NULL || strcasecmp(o, "none") == 0;
                   2358: }
                   2359:
1.19      markus   2360: /*
1.362     djm      2361:  * Returns 1 if CanonicalizePermittedCNAMEs have been specified, 0 otherwise.
                   2362:  * Allowed to be called on non-final configuration.
                   2363:  */
                   2364: int
                   2365: config_has_permitted_cnames(Options *options)
                   2366: {
                   2367:        if (options->num_permitted_cnames == 1 &&
                   2368:            strcasecmp(options->permitted_cnames[0].source_list, "none") == 0 &&
                   2369:            strcmp(options->permitted_cnames[0].target_list, "") == 0)
                   2370:                return 0;
                   2371:        return options->num_permitted_cnames > 0;
                   2372: }
                   2373:
                   2374: /*
1.19      markus   2375:  * Initializes options to special values that indicate that they have not yet
                   2376:  * been set.  Read_config_file will only set options with this value. Options
                   2377:  * are processed in the following order: command line, user config file,
                   2378:  * system config file.  Last, fill_default_options is called.
                   2379:  */
1.1       deraadt  2380:
1.26      markus   2381: void
1.17      markus   2382: initialize_options(Options * options)
1.1       deraadt  2383: {
1.17      markus   2384:        memset(options, 'X', sizeof(*options));
1.372     dtucker  2385:        options->host_arg = NULL;
1.17      markus   2386:        options->forward_agent = -1;
1.319     djm      2387:        options->forward_agent_sock_path = NULL;
1.17      markus   2388:        options->forward_x11 = -1;
1.123     markus   2389:        options->forward_x11_trusted = -1;
1.186     djm      2390:        options->forward_x11_timeout = -1;
1.255     dtucker  2391:        options->stdio_forward_host = NULL;
                   2392:        options->stdio_forward_port = 0;
1.256     dtucker  2393:        options->clear_forwardings = -1;
1.153     markus   2394:        options->exit_on_forward_failure = -1;
1.34      markus   2395:        options->xauth_location = NULL;
1.220     millert  2396:        options->fwd_opts.gateway_ports = -1;
                   2397:        options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
                   2398:        options->fwd_opts.streamlocal_bind_unlink = -1;
1.50      markus   2399:        options->pubkey_authentication = -1;
1.118     markus   2400:        options->gss_authentication = -1;
                   2401:        options->gss_deleg_creds = -1;
1.17      markus   2402:        options->password_authentication = -1;
1.48      markus   2403:        options->kbd_interactive_authentication = -1;
                   2404:        options->kbd_interactive_devices = NULL;
1.72      markus   2405:        options->hostbased_authentication = -1;
1.17      markus   2406:        options->batch_mode = -1;
                   2407:        options->check_host_ip = -1;
                   2408:        options->strict_host_key_checking = -1;
                   2409:        options->compression = -1;
1.126     markus   2410:        options->tcp_keep_alive = -1;
1.17      markus   2411:        options->port = -1;
1.114     djm      2412:        options->address_family = -1;
1.17      markus   2413:        options->connection_attempts = -1;
1.111     djm      2414:        options->connection_timeout = -1;
1.17      markus   2415:        options->number_of_password_prompts = -1;
1.25      markus   2416:        options->ciphers = NULL;
1.62      markus   2417:        options->macs = NULL;
1.189     djm      2418:        options->kex_algorithms = NULL;
1.76      markus   2419:        options->hostkeyalgorithms = NULL;
1.298     djm      2420:        options->ca_sign_algorithms = NULL;
1.17      markus   2421:        options->num_identity_files = 0;
1.344     djm      2422:        memset(options->identity_keys, 0, sizeof(options->identity_keys));
1.241     djm      2423:        options->num_certificate_files = 0;
1.344     djm      2424:        memset(options->certificates, 0, sizeof(options->certificates));
1.17      markus   2425:        options->hostname = NULL;
1.52      markus   2426:        options->host_key_alias = NULL;
1.17      markus   2427:        options->proxy_command = NULL;
1.257     djm      2428:        options->jump_user = NULL;
                   2429:        options->jump_host = NULL;
                   2430:        options->jump_port = -1;
                   2431:        options->jump_extra = NULL;
1.17      markus   2432:        options->user = NULL;
                   2433:        options->escape_char = -1;
1.193     djm      2434:        options->num_system_hostfiles = 0;
                   2435:        options->num_user_hostfiles = 0;
1.185     djm      2436:        options->local_forwards = NULL;
1.17      markus   2437:        options->num_local_forwards = 0;
1.185     djm      2438:        options->remote_forwards = NULL;
1.17      markus   2439:        options->num_remote_forwards = 0;
1.351     markus   2440:        options->permitted_remote_opens = NULL;
                   2441:        options->num_permitted_remote_opens = 0;
1.271     dtucker  2442:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
1.95      markus   2443:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.339     djm      2444:        options->num_log_verbose = 0;
                   2445:        options->log_verbose = NULL;
1.67      markus   2446:        options->preferred_authentications = NULL;
1.77      markus   2447:        options->bind_address = NULL;
1.282     djm      2448:        options->bind_interface = NULL;
1.183     markus   2449:        options->pkcs11_provider = NULL;
1.310     djm      2450:        options->sk_provider = NULL;
1.101     markus   2451:        options->enable_ssh_keysign = - 1;
1.91      markus   2452:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   2453:        options->identities_only = - 1;
1.105     markus   2454:        options->rekey_limit = - 1;
1.198     dtucker  2455:        options->rekey_interval = -1;
1.107     jakob    2456:        options->verify_host_key_dns = -1;
1.127     markus   2457:        options->server_alive_interval = -1;
                   2458:        options->server_alive_count_max = -1;
1.290     djm      2459:        options->send_env = NULL;
1.130     djm      2460:        options->num_send_env = 0;
1.290     djm      2461:        options->setenv = NULL;
                   2462:        options->num_setenv = 0;
1.132     djm      2463:        options->control_path = NULL;
                   2464:        options->control_master = -1;
1.187     djm      2465:        options->control_persist = -1;
                   2466:        options->control_persist_timeout = 0;
1.136     djm      2467:        options->hash_known_hosts = -1;
1.144     reyk     2468:        options->tun_open = -1;
                   2469:        options->tun_local = -1;
                   2470:        options->tun_remote = -1;
                   2471:        options->local_command = NULL;
                   2472:        options->permit_local_command = -1;
1.277     bluhm    2473:        options->remote_command = NULL;
1.246     jcs      2474:        options->add_keys_to_agent = -1;
1.334     djm      2475:        options->add_keys_to_agent_lifespan = -1;
1.253     markus   2476:        options->identity_agent = NULL;
1.167     grunk    2477:        options->visual_host_key = -1;
1.190     djm      2478:        options->ip_qos_interactive = -1;
                   2479:        options->ip_qos_bulk = -1;
1.192     djm      2480:        options->request_tty = -1;
1.359     djm      2481:        options->session_type = -1;
1.360     djm      2482:        options->stdin_null = -1;
1.361     djm      2483:        options->fork_after_authentication = -1;
1.205     djm      2484:        options->proxy_use_fdpass = -1;
1.199     djm      2485:        options->ignored_unknown = NULL;
1.208     djm      2486:        options->num_canonical_domains = 0;
                   2487:        options->num_permitted_cnames = 0;
1.209     djm      2488:        options->canonicalize_max_dots = -1;
                   2489:        options->canonicalize_fallback_local = -1;
                   2490:        options->canonicalize_hostname = -1;
1.223     djm      2491:        options->revoked_host_keys = NULL;
1.224     djm      2492:        options->fingerprint_hash = -1;
1.229     djm      2493:        options->update_hostkeys = -1;
1.350     dtucker  2494:        options->hostbased_accepted_algos = NULL;
1.349     dtucker  2495:        options->pubkey_accepted_algos = NULL;
1.346     djm      2496:        options->known_hosts_command = NULL;
1.369     djm      2497:        options->required_rsa_size = -1;
1.370     djm      2498:        options->enable_escape_commandline = -1;
1.1       deraadt  2499: }
                   2500:
1.19      markus   2501: /*
1.218     djm      2502:  * A petite version of fill_default_options() that just fills the options
                   2503:  * needed for hostname canonicalization to proceed.
                   2504:  */
                   2505: void
                   2506: fill_default_options_for_canonicalization(Options *options)
                   2507: {
                   2508:        if (options->canonicalize_max_dots == -1)
                   2509:                options->canonicalize_max_dots = 1;
                   2510:        if (options->canonicalize_fallback_local == -1)
                   2511:                options->canonicalize_fallback_local = 1;
                   2512:        if (options->canonicalize_hostname == -1)
                   2513:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
                   2514: }
                   2515:
                   2516: /*
1.19      markus   2517:  * Called after processing other sources of option data, this fills those
                   2518:  * options for which no value has been specified with their default values.
                   2519:  */
1.344     djm      2520: int
1.17      markus   2521: fill_default_options(Options * options)
1.1       deraadt  2522: {
1.298     djm      2523:        char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
1.320     dtucker  2524:        char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
1.344     djm      2525:        int ret = 0, r;
1.292     djm      2526:
1.17      markus   2527:        if (options->forward_agent == -1)
1.33      markus   2528:                options->forward_agent = 0;
1.17      markus   2529:        if (options->forward_x11 == -1)
1.23      markus   2530:                options->forward_x11 = 0;
1.123     markus   2531:        if (options->forward_x11_trusted == -1)
                   2532:                options->forward_x11_trusted = 0;
1.186     djm      2533:        if (options->forward_x11_timeout == -1)
                   2534:                options->forward_x11_timeout = 1200;
1.256     dtucker  2535:        /*
                   2536:         * stdio forwarding (-W) changes the default for these but we defer
                   2537:         * setting the values so they can be overridden.
                   2538:         */
1.153     markus   2539:        if (options->exit_on_forward_failure == -1)
1.256     dtucker  2540:                options->exit_on_forward_failure =
                   2541:                    options->stdio_forward_host != NULL ? 1 : 0;
                   2542:        if (options->clear_forwardings == -1)
                   2543:                options->clear_forwardings =
                   2544:                    options->stdio_forward_host != NULL ? 1 : 0;
                   2545:        if (options->clear_forwardings == 1)
                   2546:                clear_forwardings(options);
                   2547:
1.34      markus   2548:        if (options->xauth_location == NULL)
1.344     djm      2549:                options->xauth_location = xstrdup(_PATH_XAUTH);
1.220     millert  2550:        if (options->fwd_opts.gateway_ports == -1)
                   2551:                options->fwd_opts.gateway_ports = 0;
                   2552:        if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
                   2553:                options->fwd_opts.streamlocal_bind_mask = 0177;
                   2554:        if (options->fwd_opts.streamlocal_bind_unlink == -1)
                   2555:                options->fwd_opts.streamlocal_bind_unlink = 0;
1.50      markus   2556:        if (options->pubkey_authentication == -1)
1.364     djm      2557:                options->pubkey_authentication = SSH_PUBKEY_AUTH_ALL;
1.118     markus   2558:        if (options->gss_authentication == -1)
1.122     markus   2559:                options->gss_authentication = 0;
1.118     markus   2560:        if (options->gss_deleg_creds == -1)
                   2561:                options->gss_deleg_creds = 0;
1.17      markus   2562:        if (options->password_authentication == -1)
                   2563:                options->password_authentication = 1;
1.48      markus   2564:        if (options->kbd_interactive_authentication == -1)
1.59      markus   2565:                options->kbd_interactive_authentication = 1;
1.72      markus   2566:        if (options->hostbased_authentication == -1)
                   2567:                options->hostbased_authentication = 0;
1.17      markus   2568:        if (options->batch_mode == -1)
                   2569:                options->batch_mode = 0;
                   2570:        if (options->check_host_ip == -1)
1.348     djm      2571:                options->check_host_ip = 0;
1.17      markus   2572:        if (options->strict_host_key_checking == -1)
1.278     djm      2573:                options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
1.17      markus   2574:        if (options->compression == -1)
                   2575:                options->compression = 0;
1.126     markus   2576:        if (options->tcp_keep_alive == -1)
                   2577:                options->tcp_keep_alive = 1;
1.17      markus   2578:        if (options->port == -1)
                   2579:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      2580:        if (options->address_family == -1)
                   2581:                options->address_family = AF_UNSPEC;
1.17      markus   2582:        if (options->connection_attempts == -1)
1.84      markus   2583:                options->connection_attempts = 1;
1.17      markus   2584:        if (options->number_of_password_prompts == -1)
                   2585:                options->number_of_password_prompts = 3;
1.76      markus   2586:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.334     djm      2587:        if (options->add_keys_to_agent == -1) {
1.246     jcs      2588:                options->add_keys_to_agent = 0;
1.334     djm      2589:                options->add_keys_to_agent_lifespan = 0;
                   2590:        }
1.17      markus   2591:        if (options->num_identity_files == 0) {
1.273     djm      2592:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
                   2593:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
                   2594:                add_identity_file(options, "~/",
1.310     djm      2595:                    _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
                   2596:                add_identity_file(options, "~/",
1.273     djm      2597:                    _PATH_SSH_CLIENT_ID_ED25519, 0);
1.311     markus   2598:                add_identity_file(options, "~/",
                   2599:                    _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
1.283     markus   2600:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
1.365     dtucker  2601:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
1.27      markus   2602:        }
1.17      markus   2603:        if (options->escape_char == -1)
                   2604:                options->escape_char = '~';
1.193     djm      2605:        if (options->num_system_hostfiles == 0) {
                   2606:                options->system_hostfiles[options->num_system_hostfiles++] =
                   2607:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
                   2608:                options->system_hostfiles[options->num_system_hostfiles++] =
                   2609:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
                   2610:        }
1.336     djm      2611:        if (options->update_hostkeys == -1) {
1.338     djm      2612:                if (options->verify_host_key_dns <= 0 &&
                   2613:                    (options->num_user_hostfiles == 0 ||
1.336     djm      2614:                    (options->num_user_hostfiles == 1 && strcmp(options->
1.338     djm      2615:                    user_hostfiles[0], _PATH_SSH_USER_HOSTFILE) == 0)))
1.336     djm      2616:                        options->update_hostkeys = SSH_UPDATE_HOSTKEYS_YES;
                   2617:                else
1.325     djm      2618:                        options->update_hostkeys = SSH_UPDATE_HOSTKEYS_NO;
1.336     djm      2619:        }
1.193     djm      2620:        if (options->num_user_hostfiles == 0) {
                   2621:                options->user_hostfiles[options->num_user_hostfiles++] =
                   2622:                    xstrdup(_PATH_SSH_USER_HOSTFILE);
                   2623:                options->user_hostfiles[options->num_user_hostfiles++] =
                   2624:                    xstrdup(_PATH_SSH_USER_HOSTFILE2);
                   2625:        }
1.95      markus   2626:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   2627:                options->log_level = SYSLOG_LEVEL_INFO;
1.271     dtucker  2628:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
                   2629:                options->log_facility = SYSLOG_FACILITY_USER;
1.91      markus   2630:        if (options->no_host_authentication_for_localhost == - 1)
                   2631:                options->no_host_authentication_for_localhost = 0;
1.128     markus   2632:        if (options->identities_only == -1)
                   2633:                options->identities_only = 0;
1.101     markus   2634:        if (options->enable_ssh_keysign == -1)
                   2635:                options->enable_ssh_keysign = 0;
1.105     markus   2636:        if (options->rekey_limit == -1)
                   2637:                options->rekey_limit = 0;
1.198     dtucker  2638:        if (options->rekey_interval == -1)
                   2639:                options->rekey_interval = 0;
1.107     jakob    2640:        if (options->verify_host_key_dns == -1)
                   2641:                options->verify_host_key_dns = 0;
1.127     markus   2642:        if (options->server_alive_interval == -1)
                   2643:                options->server_alive_interval = 0;
                   2644:        if (options->server_alive_count_max == -1)
                   2645:                options->server_alive_count_max = 3;
1.132     djm      2646:        if (options->control_master == -1)
                   2647:                options->control_master = 0;
1.187     djm      2648:        if (options->control_persist == -1) {
                   2649:                options->control_persist = 0;
                   2650:                options->control_persist_timeout = 0;
                   2651:        }
1.136     djm      2652:        if (options->hash_known_hosts == -1)
                   2653:                options->hash_known_hosts = 0;
1.144     reyk     2654:        if (options->tun_open == -1)
1.145     reyk     2655:                options->tun_open = SSH_TUNMODE_NO;
                   2656:        if (options->tun_local == -1)
                   2657:                options->tun_local = SSH_TUNID_ANY;
                   2658:        if (options->tun_remote == -1)
                   2659:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     2660:        if (options->permit_local_command == -1)
                   2661:                options->permit_local_command = 0;
1.167     grunk    2662:        if (options->visual_host_key == -1)
                   2663:                options->visual_host_key = 0;
1.190     djm      2664:        if (options->ip_qos_interactive == -1)
1.284     job      2665:                options->ip_qos_interactive = IPTOS_DSCP_AF21;
1.190     djm      2666:        if (options->ip_qos_bulk == -1)
1.284     job      2667:                options->ip_qos_bulk = IPTOS_DSCP_CS1;
1.192     djm      2668:        if (options->request_tty == -1)
                   2669:                options->request_tty = REQUEST_TTY_AUTO;
1.359     djm      2670:        if (options->session_type == -1)
                   2671:                options->session_type = SESSION_TYPE_DEFAULT;
1.360     djm      2672:        if (options->stdin_null == -1)
                   2673:                options->stdin_null = 0;
1.361     djm      2674:        if (options->fork_after_authentication == -1)
                   2675:                options->fork_after_authentication = 0;
1.205     djm      2676:        if (options->proxy_use_fdpass == -1)
                   2677:                options->proxy_use_fdpass = 0;
1.209     djm      2678:        if (options->canonicalize_max_dots == -1)
                   2679:                options->canonicalize_max_dots = 1;
                   2680:        if (options->canonicalize_fallback_local == -1)
                   2681:                options->canonicalize_fallback_local = 1;
                   2682:        if (options->canonicalize_hostname == -1)
                   2683:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.224     djm      2684:        if (options->fingerprint_hash == -1)
                   2685:                options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.310     djm      2686:        if (options->sk_provider == NULL)
1.314     djm      2687:                options->sk_provider = xstrdup("internal");
1.369     djm      2688:        if (options->required_rsa_size == -1)
                   2689:                options->required_rsa_size = SSH_RSA_MINIMUM_MODULUS_SIZE;
1.370     djm      2690:        if (options->enable_escape_commandline == -1)
                   2691:                options->enable_escape_commandline = 0;
1.292     djm      2692:
                   2693:        /* Expand KEX name lists */
                   2694:        all_cipher = cipher_alg_list(',', 0);
                   2695:        all_mac = mac_alg_list(',');
                   2696:        all_kex = kex_alg_list(',');
                   2697:        all_key = sshkey_alg_list(0, 0, 1, ',');
1.298     djm      2698:        all_sig = sshkey_alg_list(0, 1, 1, ',');
1.320     dtucker  2699:        /* remove unsupported algos from default lists */
1.332     djm      2700:        def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
                   2701:        def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
                   2702:        def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
                   2703:        def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
                   2704:        def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
1.297     djm      2705: #define ASSEMBLE(what, defaults, all) \
                   2706:        do { \
                   2707:                if ((r = kex_assemble_names(&options->what, \
1.344     djm      2708:                    defaults, all)) != 0) { \
                   2709:                        error_fr(r, "%s", #what); \
                   2710:                        goto fail; \
                   2711:                } \
1.297     djm      2712:        } while (0)
1.320     dtucker  2713:        ASSEMBLE(ciphers, def_cipher, all_cipher);
                   2714:        ASSEMBLE(macs, def_mac, all_mac);
                   2715:        ASSEMBLE(kex_algorithms, def_kex, all_kex);
1.350     dtucker  2716:        ASSEMBLE(hostbased_accepted_algos, def_key, all_key);
1.349     dtucker  2717:        ASSEMBLE(pubkey_accepted_algos, def_key, all_key);
1.320     dtucker  2718:        ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
1.297     djm      2719: #undef ASSEMBLE
1.224     djm      2720:
1.207     djm      2721: #define CLEAR_ON_NONE(v) \
                   2722:        do { \
1.218     djm      2723:                if (option_clear_or_none(v)) { \
1.207     djm      2724:                        free(v); \
                   2725:                        v = NULL; \
                   2726:                } \
                   2727:        } while(0)
                   2728:        CLEAR_ON_NONE(options->local_command);
1.277     bluhm    2729:        CLEAR_ON_NONE(options->remote_command);
1.207     djm      2730:        CLEAR_ON_NONE(options->proxy_command);
                   2731:        CLEAR_ON_NONE(options->control_path);
1.223     djm      2732:        CLEAR_ON_NONE(options->revoked_host_keys);
1.304     djm      2733:        CLEAR_ON_NONE(options->pkcs11_provider);
1.310     djm      2734:        CLEAR_ON_NONE(options->sk_provider);
1.346     djm      2735:        CLEAR_ON_NONE(options->known_hosts_command);
1.287     djm      2736:        if (options->jump_host != NULL &&
                   2737:            strcmp(options->jump_host, "none") == 0 &&
                   2738:            options->jump_port == 0 && options->jump_user == NULL) {
                   2739:                free(options->jump_host);
                   2740:                options->jump_host = NULL;
                   2741:        }
1.362     djm      2742:        if (options->num_permitted_cnames == 1 &&
                   2743:            !config_has_permitted_cnames(options)) {
                   2744:                /* clean up CanonicalizePermittedCNAMEs=none */
                   2745:                free(options->permitted_cnames[0].source_list);
                   2746:                free(options->permitted_cnames[0].target_list);
                   2747:                memset(options->permitted_cnames, '\0',
                   2748:                    sizeof(*options->permitted_cnames));
                   2749:                options->num_permitted_cnames = 0;
                   2750:        }
1.254     markus   2751:        /* options->identity_agent distinguishes NULL from 'none' */
1.17      markus   2752:        /* options->user will be set in the main program if appropriate */
                   2753:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   2754:        /* options->host_key_alias should not be set by default */
1.67      markus   2755:        /* options->preferred_authentications will be set in ssh */
1.344     djm      2756:
                   2757:        /* success */
                   2758:        ret = 0;
                   2759:  fail:
                   2760:        free(all_cipher);
                   2761:        free(all_mac);
                   2762:        free(all_kex);
                   2763:        free(all_key);
                   2764:        free(all_sig);
                   2765:        free(def_cipher);
                   2766:        free(def_mac);
                   2767:        free(def_kex);
                   2768:        free(def_key);
                   2769:        free(def_sig);
                   2770:        return ret;
                   2771: }
                   2772:
                   2773: void
                   2774: free_options(Options *o)
                   2775: {
                   2776:        int i;
                   2777:
                   2778:        if (o == NULL)
                   2779:                return;
                   2780:
                   2781: #define FREE_ARRAY(type, n, a) \
                   2782:        do { \
                   2783:                type _i; \
                   2784:                for (_i = 0; _i < (n); _i++) \
                   2785:                        free((a)[_i]); \
                   2786:        } while (0)
                   2787:
                   2788:        free(o->forward_agent_sock_path);
                   2789:        free(o->xauth_location);
                   2790:        FREE_ARRAY(u_int, o->num_log_verbose, o->log_verbose);
                   2791:        free(o->log_verbose);
                   2792:        free(o->ciphers);
                   2793:        free(o->macs);
                   2794:        free(o->hostkeyalgorithms);
                   2795:        free(o->kex_algorithms);
                   2796:        free(o->ca_sign_algorithms);
                   2797:        free(o->hostname);
                   2798:        free(o->host_key_alias);
                   2799:        free(o->proxy_command);
                   2800:        free(o->user);
                   2801:        FREE_ARRAY(u_int, o->num_system_hostfiles, o->system_hostfiles);
                   2802:        FREE_ARRAY(u_int, o->num_user_hostfiles, o->user_hostfiles);
                   2803:        free(o->preferred_authentications);
                   2804:        free(o->bind_address);
                   2805:        free(o->bind_interface);
                   2806:        free(o->pkcs11_provider);
                   2807:        free(o->sk_provider);
                   2808:        for (i = 0; i < o->num_identity_files; i++) {
                   2809:                free(o->identity_files[i]);
                   2810:                sshkey_free(o->identity_keys[i]);
                   2811:        }
                   2812:        for (i = 0; i < o->num_certificate_files; i++) {
                   2813:                free(o->certificate_files[i]);
                   2814:                sshkey_free(o->certificates[i]);
                   2815:        }
                   2816:        free(o->identity_agent);
                   2817:        for (i = 0; i < o->num_local_forwards; i++) {
                   2818:                free(o->local_forwards[i].listen_host);
                   2819:                free(o->local_forwards[i].listen_path);
                   2820:                free(o->local_forwards[i].connect_host);
                   2821:                free(o->local_forwards[i].connect_path);
                   2822:        }
                   2823:        free(o->local_forwards);
                   2824:        for (i = 0; i < o->num_remote_forwards; i++) {
                   2825:                free(o->remote_forwards[i].listen_host);
                   2826:                free(o->remote_forwards[i].listen_path);
                   2827:                free(o->remote_forwards[i].connect_host);
                   2828:                free(o->remote_forwards[i].connect_path);
                   2829:        }
                   2830:        free(o->remote_forwards);
                   2831:        free(o->stdio_forward_host);
1.368     djm      2832:        FREE_ARRAY(u_int, o->num_send_env, o->send_env);
1.344     djm      2833:        free(o->send_env);
1.368     djm      2834:        FREE_ARRAY(u_int, o->num_setenv, o->setenv);
1.344     djm      2835:        free(o->setenv);
                   2836:        free(o->control_path);
                   2837:        free(o->local_command);
                   2838:        free(o->remote_command);
                   2839:        FREE_ARRAY(int, o->num_canonical_domains, o->canonical_domains);
                   2840:        for (i = 0; i < o->num_permitted_cnames; i++) {
                   2841:                free(o->permitted_cnames[i].source_list);
                   2842:                free(o->permitted_cnames[i].target_list);
                   2843:        }
                   2844:        free(o->revoked_host_keys);
1.350     dtucker  2845:        free(o->hostbased_accepted_algos);
1.349     dtucker  2846:        free(o->pubkey_accepted_algos);
1.344     djm      2847:        free(o->jump_user);
                   2848:        free(o->jump_host);
                   2849:        free(o->jump_extra);
                   2850:        free(o->ignored_unknown);
                   2851:        explicit_bzero(o, sizeof(*o));
                   2852: #undef FREE_ARRAY
1.135     djm      2853: }
                   2854:
1.220     millert  2855: struct fwdarg {
                   2856:        char *arg;
                   2857:        int ispath;
                   2858: };
                   2859:
                   2860: /*
                   2861:  * parse_fwd_field
                   2862:  * parses the next field in a port forwarding specification.
                   2863:  * sets fwd to the parsed field and advances p past the colon
                   2864:  * or sets it to NULL at end of string.
                   2865:  * returns 0 on success, else non-zero.
                   2866:  */
                   2867: static int
                   2868: parse_fwd_field(char **p, struct fwdarg *fwd)
                   2869: {
                   2870:        char *ep, *cp = *p;
                   2871:        int ispath = 0;
                   2872:
                   2873:        if (*cp == '\0') {
                   2874:                *p = NULL;
                   2875:                return -1;      /* end of string */
                   2876:        }
                   2877:
                   2878:        /*
                   2879:         * A field escaped with square brackets is used literally.
                   2880:         * XXX - allow ']' to be escaped via backslash?
                   2881:         */
                   2882:        if (*cp == '[') {
                   2883:                /* find matching ']' */
                   2884:                for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
                   2885:                        if (*ep == '/')
                   2886:                                ispath = 1;
                   2887:                }
                   2888:                /* no matching ']' or not at end of field. */
                   2889:                if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
                   2890:                        return -1;
                   2891:                /* NUL terminate the field and advance p past the colon */
                   2892:                *ep++ = '\0';
                   2893:                if (*ep != '\0')
                   2894:                        *ep++ = '\0';
                   2895:                fwd->arg = cp + 1;
                   2896:                fwd->ispath = ispath;
                   2897:                *p = ep;
                   2898:                return 0;
                   2899:        }
                   2900:
                   2901:        for (cp = *p; *cp != '\0'; cp++) {
                   2902:                switch (*cp) {
                   2903:                case '\\':
                   2904:                        memmove(cp, cp + 1, strlen(cp + 1) + 1);
1.237     djm      2905:                        if (*cp == '\0')
                   2906:                                return -1;
1.220     millert  2907:                        break;
                   2908:                case '/':
                   2909:                        ispath = 1;
                   2910:                        break;
                   2911:                case ':':
                   2912:                        *cp++ = '\0';
                   2913:                        goto done;
                   2914:                }
                   2915:        }
                   2916: done:
                   2917:        fwd->arg = *p;
                   2918:        fwd->ispath = ispath;
                   2919:        *p = cp;
                   2920:        return 0;
                   2921: }
                   2922:
1.135     djm      2923: /*
                   2924:  * parse_forward
                   2925:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  2926:  *   dynamicfwd == 0
1.220     millert  2927:  *     [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
                   2928:  *     listenpath:connectpath
1.168     stevesk  2929:  *   dynamicfwd == 1
                   2930:  *     [listenhost:]listenport
1.135     djm      2931:  * returns number of arguments parsed or zero on error
                   2932:  */
                   2933: int
1.220     millert  2934: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      2935: {
1.220     millert  2936:        struct fwdarg fwdargs[4];
                   2937:        char *p, *cp;
1.331     dtucker  2938:        int i, err;
1.135     djm      2939:
1.220     millert  2940:        memset(fwd, 0, sizeof(*fwd));
                   2941:        memset(fwdargs, 0, sizeof(fwdargs));
1.135     djm      2942:
1.331     dtucker  2943:        /*
                   2944:         * We expand environment variables before checking if we think they're
                   2945:         * paths so that if ${VAR} expands to a fully qualified path it is
                   2946:         * treated as a path.
                   2947:         */
                   2948:        cp = p = dollar_expand(&err, fwdspec);
                   2949:        if (p == NULL || err)
                   2950:                return 0;
1.135     djm      2951:
                   2952:        /* skip leading spaces */
1.214     deraadt  2953:        while (isspace((u_char)*cp))
1.135     djm      2954:                cp++;
                   2955:
1.220     millert  2956:        for (i = 0; i < 4; ++i) {
                   2957:                if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135     djm      2958:                        break;
1.220     millert  2959:        }
1.135     djm      2960:
1.170     stevesk  2961:        /* Check for trailing garbage */
1.220     millert  2962:        if (cp != NULL && *cp != '\0') {
1.135     djm      2963:                i = 0;  /* failure */
1.220     millert  2964:        }
1.135     djm      2965:
                   2966:        switch (i) {
1.168     stevesk  2967:        case 1:
1.220     millert  2968:                if (fwdargs[0].ispath) {
                   2969:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2970:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2971:                } else {
                   2972:                        fwd->listen_host = NULL;
                   2973:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2974:                }
1.168     stevesk  2975:                fwd->connect_host = xstrdup("socks");
                   2976:                break;
                   2977:
                   2978:        case 2:
1.220     millert  2979:                if (fwdargs[0].ispath && fwdargs[1].ispath) {
                   2980:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2981:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2982:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2983:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2984:                } else if (fwdargs[1].ispath) {
                   2985:                        fwd->listen_host = NULL;
                   2986:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2987:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2988:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2989:                } else {
                   2990:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2991:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   2992:                        fwd->connect_host = xstrdup("socks");
                   2993:                }
1.168     stevesk  2994:                break;
                   2995:
1.135     djm      2996:        case 3:
1.220     millert  2997:                if (fwdargs[0].ispath) {
                   2998:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2999:                        fwd->listen_port = PORT_STREAMLOCAL;
                   3000:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   3001:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   3002:                } else if (fwdargs[2].ispath) {
                   3003:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   3004:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   3005:                        fwd->connect_path = xstrdup(fwdargs[2].arg);
                   3006:                        fwd->connect_port = PORT_STREAMLOCAL;
                   3007:                } else {
                   3008:                        fwd->listen_host = NULL;
                   3009:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   3010:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   3011:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   3012:                }
1.135     djm      3013:                break;
                   3014:
                   3015:        case 4:
1.220     millert  3016:                fwd->listen_host = xstrdup(fwdargs[0].arg);
                   3017:                fwd->listen_port = a2port(fwdargs[1].arg);
                   3018:                fwd->connect_host = xstrdup(fwdargs[2].arg);
                   3019:                fwd->connect_port = a2port(fwdargs[3].arg);
1.135     djm      3020:                break;
                   3021:        default:
                   3022:                i = 0; /* failure */
                   3023:        }
                   3024:
1.202     djm      3025:        free(p);
1.135     djm      3026:
1.168     stevesk  3027:        if (dynamicfwd) {
                   3028:                if (!(i == 1 || i == 2))
                   3029:                        goto fail_free;
                   3030:        } else {
1.220     millert  3031:                if (!(i == 3 || i == 4)) {
                   3032:                        if (fwd->connect_path == NULL &&
                   3033:                            fwd->listen_path == NULL)
                   3034:                                goto fail_free;
                   3035:                }
                   3036:                if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168     stevesk  3037:                        goto fail_free;
                   3038:        }
                   3039:
1.220     millert  3040:        if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
                   3041:            (!remotefwd && fwd->listen_port == 0))
1.135     djm      3042:                goto fail_free;
                   3043:        if (fwd->connect_host != NULL &&
                   3044:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   3045:                goto fail_free;
1.356     djm      3046:        /*
                   3047:         * XXX - if connecting to a remote socket, max sun len may not
                   3048:         * match this host
                   3049:         */
1.220     millert  3050:        if (fwd->connect_path != NULL &&
                   3051:            strlen(fwd->connect_path) >= PATH_MAX_SUN)
                   3052:                goto fail_free;
1.176     djm      3053:        if (fwd->listen_host != NULL &&
                   3054:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   3055:                goto fail_free;
1.220     millert  3056:        if (fwd->listen_path != NULL &&
                   3057:            strlen(fwd->listen_path) >= PATH_MAX_SUN)
                   3058:                goto fail_free;
1.135     djm      3059:
                   3060:        return (i);
                   3061:
                   3062:  fail_free:
1.202     djm      3063:        free(fwd->connect_host);
                   3064:        fwd->connect_host = NULL;
1.220     millert  3065:        free(fwd->connect_path);
                   3066:        fwd->connect_path = NULL;
1.202     djm      3067:        free(fwd->listen_host);
                   3068:        fwd->listen_host = NULL;
1.220     millert  3069:        free(fwd->listen_path);
                   3070:        fwd->listen_path = NULL;
1.135     djm      3071:        return (0);
1.221     djm      3072: }
                   3073:
1.257     djm      3074: int
                   3075: parse_jump(const char *s, Options *o, int active)
                   3076: {
                   3077:        char *orig, *sdup, *cp;
                   3078:        char *host = NULL, *user = NULL;
1.345     djm      3079:        int r, ret = -1, port = -1, first;
1.257     djm      3080:
                   3081:        active &= o->proxy_command == NULL && o->jump_host == NULL;
                   3082:
                   3083:        orig = sdup = xstrdup(s);
1.356     djm      3084:
                   3085:        /* Remove comment and trailing whitespace */
                   3086:        if ((cp = strchr(orig, '#')) != NULL)
                   3087:                *cp = '\0';
                   3088:        rtrim(orig);
                   3089:
1.258     naddy    3090:        first = active;
1.259     djm      3091:        do {
1.287     djm      3092:                if (strcasecmp(s, "none") == 0)
                   3093:                        break;
1.259     djm      3094:                if ((cp = strrchr(sdup, ',')) == NULL)
                   3095:                        cp = sdup; /* last */
                   3096:                else
                   3097:                        *cp++ = '\0';
                   3098:
1.258     naddy    3099:                if (first) {
1.257     djm      3100:                        /* First argument and configuration is active */
1.345     djm      3101:                        r = parse_ssh_uri(cp, &user, &host, &port);
                   3102:                        if (r == -1 || (r == 1 &&
                   3103:                            parse_user_host_port(cp, &user, &host, &port) != 0))
1.257     djm      3104:                                goto out;
                   3105:                } else {
                   3106:                        /* Subsequent argument or inactive configuration */
1.345     djm      3107:                        r = parse_ssh_uri(cp, NULL, NULL, NULL);
                   3108:                        if (r == -1 || (r == 1 &&
                   3109:                            parse_user_host_port(cp, NULL, NULL, NULL) != 0))
1.257     djm      3110:                                goto out;
                   3111:                }
1.258     naddy    3112:                first = 0; /* only check syntax for subsequent hosts */
1.259     djm      3113:        } while (cp != sdup);
1.257     djm      3114:        /* success */
1.258     naddy    3115:        if (active) {
1.287     djm      3116:                if (strcasecmp(s, "none") == 0) {
                   3117:                        o->jump_host = xstrdup("none");
                   3118:                        o->jump_port = 0;
                   3119:                } else {
                   3120:                        o->jump_user = user;
                   3121:                        o->jump_host = host;
                   3122:                        o->jump_port = port;
                   3123:                        o->proxy_command = xstrdup("none");
                   3124:                        user = host = NULL;
                   3125:                        if ((cp = strrchr(s, ',')) != NULL && cp != s) {
                   3126:                                o->jump_extra = xstrdup(s);
                   3127:                                o->jump_extra[cp - s] = '\0';
                   3128:                        }
1.259     djm      3129:                }
1.258     naddy    3130:        }
1.257     djm      3131:        ret = 0;
                   3132:  out:
1.258     naddy    3133:        free(orig);
1.257     djm      3134:        free(user);
                   3135:        free(host);
                   3136:        return ret;
1.280     millert  3137: }
                   3138:
                   3139: int
                   3140: parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
                   3141: {
1.344     djm      3142:        char *user = NULL, *host = NULL, *path = NULL;
                   3143:        int r, port;
1.280     millert  3144:
1.344     djm      3145:        r = parse_uri("ssh", uri, &user, &host, &port, &path);
1.280     millert  3146:        if (r == 0 && path != NULL)
                   3147:                r = -1;         /* path not allowed */
1.344     djm      3148:        if (r == 0) {
                   3149:                if (userp != NULL) {
                   3150:                        *userp = user;
                   3151:                        user = NULL;
                   3152:                }
                   3153:                if (hostp != NULL) {
                   3154:                        *hostp = host;
                   3155:                        host = NULL;
                   3156:                }
                   3157:                if (portp != NULL)
                   3158:                        *portp = port;
                   3159:        }
                   3160:        free(user);
                   3161:        free(host);
                   3162:        free(path);
1.280     millert  3163:        return r;
1.257     djm      3164: }
                   3165:
1.221     djm      3166: /* XXX the following is a near-vebatim copy from servconf.c; refactor */
                   3167: static const char *
                   3168: fmt_multistate_int(int val, const struct multistate *m)
                   3169: {
                   3170:        u_int i;
                   3171:
                   3172:        for (i = 0; m[i].key != NULL; i++) {
                   3173:                if (m[i].value == val)
                   3174:                        return m[i].key;
                   3175:        }
                   3176:        return "UNKNOWN";
                   3177: }
                   3178:
                   3179: static const char *
                   3180: fmt_intarg(OpCodes code, int val)
                   3181: {
                   3182:        if (val == -1)
                   3183:                return "unset";
                   3184:        switch (code) {
                   3185:        case oAddressFamily:
                   3186:                return fmt_multistate_int(val, multistate_addressfamily);
                   3187:        case oVerifyHostKeyDNS:
1.232     djm      3188:        case oUpdateHostkeys:
1.221     djm      3189:                return fmt_multistate_int(val, multistate_yesnoask);
1.278     djm      3190:        case oStrictHostKeyChecking:
                   3191:                return fmt_multistate_int(val, multistate_strict_hostkey);
1.221     djm      3192:        case oControlMaster:
                   3193:                return fmt_multistate_int(val, multistate_controlmaster);
                   3194:        case oTunnel:
                   3195:                return fmt_multistate_int(val, multistate_tunnel);
                   3196:        case oRequestTTY:
                   3197:                return fmt_multistate_int(val, multistate_requesttty);
1.359     djm      3198:        case oSessionType:
                   3199:                return fmt_multistate_int(val, multistate_sessiontype);
1.221     djm      3200:        case oCanonicalizeHostname:
                   3201:                return fmt_multistate_int(val, multistate_canonicalizehostname);
1.285     djm      3202:        case oAddKeysToAgent:
                   3203:                return fmt_multistate_int(val, multistate_yesnoaskconfirm);
1.364     djm      3204:        case oPubkeyAuthentication:
                   3205:                return fmt_multistate_int(val, multistate_pubkey_auth);
1.224     djm      3206:        case oFingerprintHash:
                   3207:                return ssh_digest_alg_name(val);
1.221     djm      3208:        default:
                   3209:                switch (val) {
                   3210:                case 0:
                   3211:                        return "no";
                   3212:                case 1:
                   3213:                        return "yes";
                   3214:                default:
                   3215:                        return "UNKNOWN";
                   3216:                }
                   3217:        }
                   3218: }
                   3219:
                   3220: static const char *
                   3221: lookup_opcode_name(OpCodes code)
                   3222: {
                   3223:        u_int i;
                   3224:
                   3225:        for (i = 0; keywords[i].name != NULL; i++)
                   3226:                if (keywords[i].opcode == code)
                   3227:                        return(keywords[i].name);
                   3228:        return "UNKNOWN";
                   3229: }
                   3230:
                   3231: static void
                   3232: dump_cfg_int(OpCodes code, int val)
                   3233: {
                   3234:        printf("%s %d\n", lookup_opcode_name(code), val);
                   3235: }
                   3236:
                   3237: static void
                   3238: dump_cfg_fmtint(OpCodes code, int val)
                   3239: {
                   3240:        printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
                   3241: }
                   3242:
                   3243: static void
                   3244: dump_cfg_string(OpCodes code, const char *val)
                   3245: {
                   3246:        if (val == NULL)
                   3247:                return;
                   3248:        printf("%s %s\n", lookup_opcode_name(code), val);
                   3249: }
                   3250:
                   3251: static void
                   3252: dump_cfg_strarray(OpCodes code, u_int count, char **vals)
                   3253: {
                   3254:        u_int i;
                   3255:
                   3256:        for (i = 0; i < count; i++)
                   3257:                printf("%s %s\n", lookup_opcode_name(code), vals[i]);
                   3258: }
                   3259:
                   3260: static void
                   3261: dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
                   3262: {
                   3263:        u_int i;
                   3264:
                   3265:        printf("%s", lookup_opcode_name(code));
1.356     djm      3266:        if (count == 0)
                   3267:                printf(" none");
1.221     djm      3268:        for (i = 0; i < count; i++)
                   3269:                printf(" %s",  vals[i]);
                   3270:        printf("\n");
                   3271: }
                   3272:
                   3273: static void
                   3274: dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
                   3275: {
                   3276:        const struct Forward *fwd;
                   3277:        u_int i;
                   3278:
                   3279:        /* oDynamicForward */
                   3280:        for (i = 0; i < count; i++) {
                   3281:                fwd = &fwds[i];
1.265     djm      3282:                if (code == oDynamicForward && fwd->connect_host != NULL &&
1.221     djm      3283:                    strcmp(fwd->connect_host, "socks") != 0)
                   3284:                        continue;
1.265     djm      3285:                if (code == oLocalForward && fwd->connect_host != NULL &&
1.221     djm      3286:                    strcmp(fwd->connect_host, "socks") == 0)
                   3287:                        continue;
                   3288:                printf("%s", lookup_opcode_name(code));
                   3289:                if (fwd->listen_port == PORT_STREAMLOCAL)
                   3290:                        printf(" %s", fwd->listen_path);
                   3291:                else if (fwd->listen_host == NULL)
                   3292:                        printf(" %d", fwd->listen_port);
                   3293:                else {
                   3294:                        printf(" [%s]:%d",
                   3295:                            fwd->listen_host, fwd->listen_port);
                   3296:                }
                   3297:                if (code != oDynamicForward) {
                   3298:                        if (fwd->connect_port == PORT_STREAMLOCAL)
                   3299:                                printf(" %s", fwd->connect_path);
                   3300:                        else if (fwd->connect_host == NULL)
                   3301:                                printf(" %d", fwd->connect_port);
                   3302:                        else {
                   3303:                                printf(" [%s]:%d",
                   3304:                                    fwd->connect_host, fwd->connect_port);
                   3305:                        }
                   3306:                }
                   3307:                printf("\n");
                   3308:        }
                   3309: }
                   3310:
                   3311: void
                   3312: dump_client_config(Options *o, const char *host)
                   3313: {
1.326     djm      3314:        int i, r;
                   3315:        char buf[8], *all_key;
                   3316:
                   3317:        /*
                   3318:         * Expand HostKeyAlgorithms name lists. This isn't handled in
                   3319:         * fill_default_options() like the other algorithm lists because
                   3320:         * the host key algorithms are by default dynamically chosen based
                   3321:         * on the host's keys found in known_hosts.
                   3322:         */
                   3323:        all_key = sshkey_alg_list(0, 0, 1, ',');
                   3324:        if ((r = kex_assemble_names(&o->hostkeyalgorithms, kex_default_pk_alg(),
                   3325:            all_key)) != 0)
1.340     djm      3326:                fatal_fr(r, "expand HostKeyAlgorithms");
1.326     djm      3327:        free(all_key);
1.240     djm      3328:
1.221     djm      3329:        /* Most interesting options first: user, host, port */
1.372     dtucker  3330:        dump_cfg_string(oHost, o->host_arg);
1.221     djm      3331:        dump_cfg_string(oUser, o->user);
1.306     jmc      3332:        dump_cfg_string(oHostname, host);
1.221     djm      3333:        dump_cfg_int(oPort, o->port);
                   3334:
                   3335:        /* Flag options */
                   3336:        dump_cfg_fmtint(oAddressFamily, o->address_family);
                   3337:        dump_cfg_fmtint(oBatchMode, o->batch_mode);
                   3338:        dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
                   3339:        dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
                   3340:        dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
                   3341:        dump_cfg_fmtint(oCompression, o->compression);
                   3342:        dump_cfg_fmtint(oControlMaster, o->control_master);
                   3343:        dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
1.256     dtucker  3344:        dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
1.221     djm      3345:        dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
1.224     djm      3346:        dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
1.221     djm      3347:        dump_cfg_fmtint(oForwardX11, o->forward_x11);
                   3348:        dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
                   3349:        dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
                   3350: #ifdef GSSAPI
                   3351:        dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
                   3352:        dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
                   3353: #endif /* GSSAPI */
                   3354:        dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
                   3355:        dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
                   3356:        dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
                   3357:        dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
                   3358:        dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
                   3359:        dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
                   3360:        dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
                   3361:        dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
                   3362:        dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
                   3363:        dump_cfg_fmtint(oRequestTTY, o->request_tty);
1.359     djm      3364:        dump_cfg_fmtint(oSessionType, o->session_type);
1.360     djm      3365:        dump_cfg_fmtint(oStdinNull, o->stdin_null);
1.361     djm      3366:        dump_cfg_fmtint(oForkAfterAuthentication, o->fork_after_authentication);
1.221     djm      3367:        dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
                   3368:        dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
                   3369:        dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
                   3370:        dump_cfg_fmtint(oTunnel, o->tun_open);
                   3371:        dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
                   3372:        dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
1.229     djm      3373:        dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
1.370     djm      3374:        dump_cfg_fmtint(oEnableEscapeCommandline, o->enable_escape_commandline);
1.221     djm      3375:
                   3376:        /* Integer options */
                   3377:        dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
                   3378:        dump_cfg_int(oConnectionAttempts, o->connection_attempts);
                   3379:        dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
                   3380:        dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
                   3381:        dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
                   3382:        dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
1.369     djm      3383:        dump_cfg_int(oRequiredRSASize, o->required_rsa_size);
1.221     djm      3384:
                   3385:        /* String options */
                   3386:        dump_cfg_string(oBindAddress, o->bind_address);
1.282     djm      3387:        dump_cfg_string(oBindInterface, o->bind_interface);
1.320     dtucker  3388:        dump_cfg_string(oCiphers, o->ciphers);
1.221     djm      3389:        dump_cfg_string(oControlPath, o->control_path);
1.240     djm      3390:        dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
1.221     djm      3391:        dump_cfg_string(oHostKeyAlias, o->host_key_alias);
1.350     dtucker  3392:        dump_cfg_string(oHostbasedAcceptedAlgorithms, o->hostbased_accepted_algos);
1.253     markus   3393:        dump_cfg_string(oIdentityAgent, o->identity_agent);
1.285     djm      3394:        dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
1.221     djm      3395:        dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
1.320     dtucker  3396:        dump_cfg_string(oKexAlgorithms, o->kex_algorithms);
                   3397:        dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms);
1.221     djm      3398:        dump_cfg_string(oLocalCommand, o->local_command);
1.277     bluhm    3399:        dump_cfg_string(oRemoteCommand, o->remote_command);
1.221     djm      3400:        dump_cfg_string(oLogLevel, log_level_name(o->log_level));
1.320     dtucker  3401:        dump_cfg_string(oMacs, o->macs);
1.266     djm      3402: #ifdef ENABLE_PKCS11
1.221     djm      3403:        dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
1.266     djm      3404: #endif
1.310     djm      3405:        dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
1.221     djm      3406:        dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
1.349     dtucker  3407:        dump_cfg_string(oPubkeyAcceptedAlgorithms, o->pubkey_accepted_algos);
1.230     djm      3408:        dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
1.221     djm      3409:        dump_cfg_string(oXAuthLocation, o->xauth_location);
1.346     djm      3410:        dump_cfg_string(oKnownHostsCommand, o->known_hosts_command);
1.221     djm      3411:
1.230     djm      3412:        /* Forwards */
1.221     djm      3413:        dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
                   3414:        dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
                   3415:        dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
                   3416:
                   3417:        /* String array options */
                   3418:        dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
                   3419:        dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
1.285     djm      3420:        dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
1.221     djm      3421:        dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
                   3422:        dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
                   3423:        dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
1.290     djm      3424:        dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
1.339     djm      3425:        dump_cfg_strarray_oneline(oLogVerbose,
                   3426:            o->num_log_verbose, o->log_verbose);
1.221     djm      3427:
                   3428:        /* Special cases */
1.351     markus   3429:
                   3430:        /* PermitRemoteOpen */
                   3431:        if (o->num_permitted_remote_opens == 0)
                   3432:                printf("%s any\n", lookup_opcode_name(oPermitRemoteOpen));
                   3433:        else
                   3434:                dump_cfg_strarray_oneline(oPermitRemoteOpen,
                   3435:                    o->num_permitted_remote_opens, o->permitted_remote_opens);
1.334     djm      3436:
                   3437:        /* AddKeysToAgent */
                   3438:        if (o->add_keys_to_agent_lifespan <= 0)
                   3439:                dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
                   3440:        else {
                   3441:                printf("addkeystoagent%s %d\n",
                   3442:                    o->add_keys_to_agent == 3 ? " confirm" : "",
                   3443:                    o->add_keys_to_agent_lifespan);
                   3444:        }
1.319     djm      3445:
                   3446:        /* oForwardAgent */
                   3447:        if (o->forward_agent_sock_path == NULL)
                   3448:                dump_cfg_fmtint(oForwardAgent, o->forward_agent);
                   3449:        else
                   3450:                dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
1.221     djm      3451:
                   3452:        /* oConnectTimeout */
                   3453:        if (o->connection_timeout == -1)
                   3454:                printf("connecttimeout none\n");
                   3455:        else
                   3456:                dump_cfg_int(oConnectTimeout, o->connection_timeout);
                   3457:
                   3458:        /* oTunnelDevice */
                   3459:        printf("tunneldevice");
                   3460:        if (o->tun_local == SSH_TUNID_ANY)
                   3461:                printf(" any");
                   3462:        else
                   3463:                printf(" %d", o->tun_local);
                   3464:        if (o->tun_remote == SSH_TUNID_ANY)
                   3465:                printf(":any");
                   3466:        else
                   3467:                printf(":%d", o->tun_remote);
                   3468:        printf("\n");
                   3469:
                   3470:        /* oCanonicalizePermittedCNAMEs */
1.362     djm      3471:        printf("canonicalizePermittedcnames");
                   3472:        if (o->num_permitted_cnames == 0)
1.363     djm      3473:                printf(" none");
1.362     djm      3474:        for (i = 0; i < o->num_permitted_cnames; i++) {
                   3475:                printf(" %s:%s", o->permitted_cnames[i].source_list,
                   3476:                    o->permitted_cnames[i].target_list);
1.221     djm      3477:        }
1.362     djm      3478:        printf("\n");
1.221     djm      3479:
                   3480:        /* oControlPersist */
                   3481:        if (o->control_persist == 0 || o->control_persist_timeout == 0)
                   3482:                dump_cfg_fmtint(oControlPersist, o->control_persist);
                   3483:        else
                   3484:                dump_cfg_int(oControlPersist, o->control_persist_timeout);
                   3485:
                   3486:        /* oEscapeChar */
                   3487:        if (o->escape_char == SSH_ESCAPECHAR_NONE)
                   3488:                printf("escapechar none\n");
                   3489:        else {
1.257     djm      3490:                vis(buf, o->escape_char, VIS_WHITE, 0);
                   3491:                printf("escapechar %s\n", buf);
1.221     djm      3492:        }
                   3493:
                   3494:        /* oIPQoS */
                   3495:        printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
                   3496:        printf("%s\n", iptos2str(o->ip_qos_bulk));
                   3497:
                   3498:        /* oRekeyLimit */
1.249     dtucker  3499:        printf("rekeylimit %llu %d\n",
                   3500:            (unsigned long long)o->rekey_limit, o->rekey_interval);
1.221     djm      3501:
                   3502:        /* oStreamLocalBindMask */
                   3503:        printf("streamlocalbindmask 0%o\n",
                   3504:            o->fwd_opts.streamlocal_bind_mask);
1.285     djm      3505:
                   3506:        /* oLogFacility */
                   3507:        printf("syslogfacility %s\n", log_facility_name(o->log_facility));
1.257     djm      3508:
                   3509:        /* oProxyCommand / oProxyJump */
                   3510:        if (o->jump_host == NULL)
                   3511:                dump_cfg_string(oProxyCommand, o->proxy_command);
                   3512:        else {
                   3513:                /* Check for numeric addresses */
                   3514:                i = strchr(o->jump_host, ':') != NULL ||
                   3515:                    strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
                   3516:                snprintf(buf, sizeof(buf), "%d", o->jump_port);
                   3517:                printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
1.259     djm      3518:                    /* optional additional jump spec */
                   3519:                    o->jump_extra == NULL ? "" : o->jump_extra,
                   3520:                    o->jump_extra == NULL ? "" : ",",
1.257     djm      3521:                    /* optional user */
                   3522:                    o->jump_user == NULL ? "" : o->jump_user,
                   3523:                    o->jump_user == NULL ? "" : "@",
                   3524:                    /* opening [ if hostname is numeric */
                   3525:                    i ? "[" : "",
                   3526:                    /* mandatory hostname */
                   3527:                    o->jump_host,
                   3528:                    /* closing ] if hostname is numeric */
                   3529:                    i ? "]" : "",
                   3530:                    /* optional port number */
                   3531:                    o->jump_port <= 0 ? "" : ":",
1.259     djm      3532:                    o->jump_port <= 0 ? "" : buf);
1.257     djm      3533:        }
1.1       deraadt  3534: }