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

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