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

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