[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.379

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