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

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