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

1.319   ! djm         1: /* $OpenBSD: readconf.c,v 1.318 2019/12/20 02:42:42 dtucker Exp $ */
1.1       deraadt     2: /*
1.18      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for reading the configuration files.
1.26      markus      7:  *
1.46      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.18      deraadt    13:  */
1.1       deraadt    14:
1.147     stevesk    15: #include <sys/types.h>
                     16: #include <sys/stat.h>
1.152     stevesk    17: #include <sys/socket.h>
1.206     djm        18: #include <sys/wait.h>
1.220     millert    19: #include <sys/un.h>
1.152     stevesk    20:
                     21: #include <netinet/in.h>
1.190     djm        22: #include <netinet/ip.h>
1.148     stevesk    23:
                     24: #include <ctype.h>
1.154     stevesk    25: #include <errno.h>
1.206     djm        26: #include <fcntl.h>
1.252     djm        27: #include <glob.h>
1.155     stevesk    28: #include <netdb.h>
1.206     djm        29: #include <paths.h>
                     30: #include <pwd.h>
1.159     deraadt    31: #include <signal.h>
1.158     stevesk    32: #include <stdio.h>
1.157     stevesk    33: #include <string.h>
1.312     deraadt    34: #include <stdarg.h>
1.156     stevesk    35: #include <unistd.h>
1.228     deraadt    36: #include <limits.h>
1.200     dtucker    37: #include <util.h>
1.221     djm        38: #include <vis.h>
1.1       deraadt    39:
1.159     deraadt    40: #include "xmalloc.h"
1.1       deraadt    41: #include "ssh.h"
1.297     djm        42: #include "ssherr.h"
1.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.59      markus    134:        oChallengeResponseAuthentication, oXAuthLocation,
1.317     dtucker   135:        oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
1.253     markus    136:        oCertificateFile, oAddKeysToAgent, oIdentityAgent,
1.317     dtucker   137:        oUser, oEscapeChar, oProxyCommand,
1.17      markus    138:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    139:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.317     dtucker   140:        oTCPKeepAlive, oNumberOfPasswordPrompts,
                    141:        oLogFacility, oLogLevel, oCiphers, oMacs,
1.221     djm       142:        oPubkeyAuthentication,
1.67      markus    143:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    144:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.282     djm       145:        oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
1.96      markus    146:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       147:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    148:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    149:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.290     djm       150:        oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
1.187     djm       151:        oHashKnownHosts,
1.277     bluhm     152:        oTunnel, oTunnelDevice,
                    153:        oLocalCommand, oPermitLocalCommand, oRemoteCommand,
1.248     markus    154:        oVisualHostKey,
1.205     djm       155:        oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
1.209     djm       156:        oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
                    157:        oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
1.223     djm       158:        oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
1.230     djm       159:        oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
1.298     djm       160:        oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
1.310     djm       161:        oSecurityKeyProvider,
1.273     djm       162:        oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
1.1       deraadt   163: } OpCodes;
                    164:
                    165: /* Textual representations of the tokens. */
                    166:
1.17      markus    167: static struct {
                    168:        const char *name;
                    169:        OpCodes opcode;
                    170: } keywords[] = {
1.266     djm       171:        /* Deprecated options */
1.273     djm       172:        { "protocol", oIgnore }, /* NB. silently ignored */
1.274     djm       173:        { "cipher", oDeprecated },
1.266     djm       174:        { "fallbacktorsh", oDeprecated },
                    175:        { "globalknownhostsfile2", oDeprecated },
                    176:        { "rhostsauthentication", oDeprecated },
                    177:        { "userknownhostsfile2", oDeprecated },
                    178:        { "useroaming", oDeprecated },
                    179:        { "usersh", oDeprecated },
1.294     dtucker   180:        { "useprivilegedport", oDeprecated },
1.266     djm       181:
                    182:        /* Unsupported options */
                    183:        { "afstokenpassing", oUnsupported },
                    184:        { "kerberosauthentication", oUnsupported },
                    185:        { "kerberostgtpassing", oUnsupported },
1.318     dtucker   186:        { "rsaauthentication", oUnsupported },
                    187:        { "rhostsrsaauthentication", oUnsupported },
                    188:        { "compressionlevel", oUnsupported },
1.266     djm       189:
                    190:        /* Sometimes-unsupported options */
                    191: #if defined(GSSAPI)
                    192:        { "gssapiauthentication", oGssAuthentication },
                    193:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    194: # else
                    195:        { "gssapiauthentication", oUnsupported },
                    196:        { "gssapidelegatecredentials", oUnsupported },
                    197: #endif
                    198: #ifdef ENABLE_PKCS11
1.304     djm       199:        { "pkcs11provider", oPKCS11Provider },
1.266     djm       200:        { "smartcarddevice", oPKCS11Provider },
                    201: # else
                    202:        { "smartcarddevice", oUnsupported },
                    203:        { "pkcs11provider", oUnsupported },
                    204: #endif
                    205:
1.17      markus    206:        { "forwardagent", oForwardAgent },
                    207:        { "forwardx11", oForwardX11 },
1.123     markus    208:        { "forwardx11trusted", oForwardX11Trusted },
1.186     djm       209:        { "forwardx11timeout", oForwardX11Timeout },
1.153     markus    210:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    211:        { "xauthlocation", oXAuthLocation },
1.17      markus    212:        { "gatewayports", oGatewayPorts },
                    213:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    214:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    215:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.50      markus    216:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    217:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.73      markus    218:        { "hostbasedauthentication", oHostbasedAuthentication },
1.59      markus    219:        { "challengeresponseauthentication", oChallengeResponseAuthentication },
                    220:        { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
                    221:        { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
1.17      markus    222:        { "identityfile", oIdentityFile },
1.174     stevesk   223:        { "identityfile2", oIdentityFile },                     /* obsolete */
1.128     markus    224:        { "identitiesonly", oIdentitiesOnly },
1.241     djm       225:        { "certificatefile", oCertificateFile },
1.246     jcs       226:        { "addkeystoagent", oAddKeysToAgent },
1.253     markus    227:        { "identityagent", oIdentityAgent },
1.306     jmc       228:        { "hostname", oHostname },
1.52      markus    229:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    230:        { "proxycommand", oProxyCommand },
                    231:        { "port", oPort },
1.25      markus    232:        { "ciphers", oCiphers },
1.62      markus    233:        { "macs", oMacs },
1.17      markus    234:        { "remoteforward", oRemoteForward },
                    235:        { "localforward", oLocalForward },
                    236:        { "user", oUser },
                    237:        { "host", oHost },
1.206     djm       238:        { "match", oMatch },
1.17      markus    239:        { "escapechar", oEscapeChar },
                    240:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.174     stevesk   241:        { "userknownhostsfile", oUserKnownHostsFile },
1.17      markus    242:        { "connectionattempts", oConnectionAttempts },
                    243:        { "batchmode", oBatchMode },
                    244:        { "checkhostip", oCheckHostIP },
                    245:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    246:        { "compression", oCompression },
1.126     markus    247:        { "tcpkeepalive", oTCPKeepAlive },
                    248:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    249:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
1.271     dtucker   250:        { "syslogfacility", oLogFacility },
1.17      markus    251:        { "loglevel", oLogLevel },
1.71      markus    252:        { "dynamicforward", oDynamicForward },
1.67      markus    253:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    254:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.298     djm       255:        { "casignaturealgorithms", oCASignatureAlgorithms },
1.77      markus    256:        { "bindaddress", oBindAddress },
1.282     djm       257:        { "bindinterface", oBindInterface },
1.93      deraadt   258:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    259:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     260:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   261:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    262:        { "rekeylimit", oRekeyLimit },
1.111     djm       263:        { "connecttimeout", oConnectTimeout },
1.112     djm       264:        { "addressfamily", oAddressFamily },
1.127     markus    265:        { "serveraliveinterval", oServerAliveInterval },
                    266:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       267:        { "sendenv", oSendEnv },
1.290     djm       268:        { "setenv", oSetEnv },
1.132     djm       269:        { "controlpath", oControlPath },
                    270:        { "controlmaster", oControlMaster },
1.187     djm       271:        { "controlpersist", oControlPersist },
1.136     djm       272:        { "hashknownhosts", oHashKnownHosts },
1.252     djm       273:        { "include", oInclude },
1.144     reyk      274:        { "tunnel", oTunnel },
                    275:        { "tunneldevice", oTunnelDevice },
                    276:        { "localcommand", oLocalCommand },
                    277:        { "permitlocalcommand", oPermitLocalCommand },
1.277     bluhm     278:        { "remotecommand", oRemoteCommand },
1.167     grunk     279:        { "visualhostkey", oVisualHostKey },
1.189     djm       280:        { "kexalgorithms", oKexAlgorithms },
1.190     djm       281:        { "ipqos", oIPQoS },
1.192     djm       282:        { "requesttty", oRequestTTY },
1.205     djm       283:        { "proxyusefdpass", oProxyUseFdpass },
1.208     djm       284:        { "canonicaldomains", oCanonicalDomains },
1.209     djm       285:        { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
                    286:        { "canonicalizehostname", oCanonicalizeHostname },
                    287:        { "canonicalizemaxdots", oCanonicalizeMaxDots },
                    288:        { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
1.220     millert   289:        { "streamlocalbindmask", oStreamLocalBindMask },
                    290:        { "streamlocalbindunlink", oStreamLocalBindUnlink },
1.223     djm       291:        { "revokedhostkeys", oRevokedHostKeys },
1.224     djm       292:        { "fingerprinthash", oFingerprintHash },
1.229     djm       293:        { "updatehostkeys", oUpdateHostkeys },
1.230     djm       294:        { "hostbasedkeytypes", oHostbasedKeyTypes },
1.238     markus    295:        { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
1.199     djm       296:        { "ignoreunknown", oIgnoreUnknown },
1.257     djm       297:        { "proxyjump", oProxyJump },
1.318     dtucker   298:        { "securitykeyprovider", oSecurityKeyProvider },
1.171     djm       299:
1.92      stevesk   300:        { NULL, oBadOption }
1.13      markus    301: };
                    302:
1.19      markus    303: /*
                    304:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    305:  * error.
                    306:  */
1.1       deraadt   307:
1.26      markus    308: void
1.220     millert   309: add_local_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   310: {
1.220     millert   311:        struct Forward *fwd;
1.251     djm       312:        int i;
1.185     djm       313:
1.251     djm       314:        /* Don't add duplicates */
                    315:        for (i = 0; i < options->num_local_forwards; i++) {
                    316:                if (forward_equals(newfwd, options->local_forwards + i))
                    317:                        return;
                    318:        }
1.234     deraadt   319:        options->local_forwards = xreallocarray(options->local_forwards,
1.185     djm       320:            options->num_local_forwards + 1,
                    321:            sizeof(*options->local_forwards));
1.17      markus    322:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       323:
1.172     stevesk   324:        fwd->listen_host = newfwd->listen_host;
1.135     djm       325:        fwd->listen_port = newfwd->listen_port;
1.220     millert   326:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   327:        fwd->connect_host = newfwd->connect_host;
1.135     djm       328:        fwd->connect_port = newfwd->connect_port;
1.220     millert   329:        fwd->connect_path = newfwd->connect_path;
1.1       deraadt   330: }
                    331:
1.19      markus    332: /*
                    333:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    334:  * an error.
                    335:  */
1.1       deraadt   336:
1.26      markus    337: void
1.220     millert   338: add_remote_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   339: {
1.220     millert   340:        struct Forward *fwd;
1.251     djm       341:        int i;
1.185     djm       342:
1.251     djm       343:        /* Don't add duplicates */
                    344:        for (i = 0; i < options->num_remote_forwards; i++) {
                    345:                if (forward_equals(newfwd, options->remote_forwards + i))
                    346:                        return;
                    347:        }
1.234     deraadt   348:        options->remote_forwards = xreallocarray(options->remote_forwards,
1.185     djm       349:            options->num_remote_forwards + 1,
                    350:            sizeof(*options->remote_forwards));
1.17      markus    351:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       352:
1.172     stevesk   353:        fwd->listen_host = newfwd->listen_host;
1.135     djm       354:        fwd->listen_port = newfwd->listen_port;
1.220     millert   355:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   356:        fwd->connect_host = newfwd->connect_host;
1.135     djm       357:        fwd->connect_port = newfwd->connect_port;
1.220     millert   358:        fwd->connect_path = newfwd->connect_path;
1.194     markus    359:        fwd->handle = newfwd->handle;
1.184     markus    360:        fwd->allocated_port = 0;
1.1       deraadt   361: }
                    362:
1.90      stevesk   363: static void
                    364: clear_forwardings(Options *options)
                    365: {
                    366:        int i;
                    367:
1.135     djm       368:        for (i = 0; i < options->num_local_forwards; i++) {
1.202     djm       369:                free(options->local_forwards[i].listen_host);
1.220     millert   370:                free(options->local_forwards[i].listen_path);
1.202     djm       371:                free(options->local_forwards[i].connect_host);
1.220     millert   372:                free(options->local_forwards[i].connect_path);
1.135     djm       373:        }
1.185     djm       374:        if (options->num_local_forwards > 0) {
1.202     djm       375:                free(options->local_forwards);
1.185     djm       376:                options->local_forwards = NULL;
                    377:        }
1.90      stevesk   378:        options->num_local_forwards = 0;
1.135     djm       379:        for (i = 0; i < options->num_remote_forwards; i++) {
1.202     djm       380:                free(options->remote_forwards[i].listen_host);
1.220     millert   381:                free(options->remote_forwards[i].listen_path);
1.202     djm       382:                free(options->remote_forwards[i].connect_host);
1.220     millert   383:                free(options->remote_forwards[i].connect_path);
1.135     djm       384:        }
1.185     djm       385:        if (options->num_remote_forwards > 0) {
1.202     djm       386:                free(options->remote_forwards);
1.185     djm       387:                options->remote_forwards = NULL;
                    388:        }
1.90      stevesk   389:        options->num_remote_forwards = 0;
1.145     reyk      390:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   391: }
                    392:
1.195     dtucker   393: void
1.241     djm       394: add_certificate_file(Options *options, const char *path, int userprovided)
                    395: {
                    396:        int i;
                    397:
                    398:        if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
                    399:                fatal("Too many certificate files specified (max %d)",
                    400:                    SSH_MAX_CERTIFICATE_FILES);
                    401:
                    402:        /* Avoid registering duplicates */
                    403:        for (i = 0; i < options->num_certificate_files; i++) {
                    404:                if (options->certificate_file_userprovided[i] == userprovided &&
                    405:                    strcmp(options->certificate_files[i], path) == 0) {
                    406:                        debug2("%s: ignoring duplicate key %s", __func__, path);
                    407:                        return;
                    408:                }
                    409:        }
                    410:
                    411:        options->certificate_file_userprovided[options->num_certificate_files] =
                    412:            userprovided;
                    413:        options->certificate_files[options->num_certificate_files++] =
                    414:            xstrdup(path);
                    415: }
                    416:
                    417: void
1.195     dtucker   418: add_identity_file(Options *options, const char *dir, const char *filename,
                    419:     int userprovided)
                    420: {
                    421:        char *path;
1.219     djm       422:        int i;
1.195     dtucker   423:
                    424:        if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
                    425:                fatal("Too many identity files specified (max %d)",
                    426:                    SSH_MAX_IDENTITY_FILES);
                    427:
                    428:        if (dir == NULL) /* no dir, filename is absolute */
                    429:                path = xstrdup(filename);
1.276     djm       430:        else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
                    431:                fatal("Identity file path %s too long", path);
1.219     djm       432:
                    433:        /* Avoid registering duplicates */
                    434:        for (i = 0; i < options->num_identity_files; i++) {
                    435:                if (options->identity_file_userprovided[i] == userprovided &&
                    436:                    strcmp(options->identity_files[i], path) == 0) {
                    437:                        debug2("%s: ignoring duplicate key %s", __func__, path);
                    438:                        free(path);
                    439:                        return;
                    440:                }
                    441:        }
1.195     dtucker   442:
                    443:        options->identity_file_userprovided[options->num_identity_files] =
                    444:            userprovided;
                    445:        options->identity_files[options->num_identity_files++] = path;
                    446: }
                    447:
1.206     djm       448: int
                    449: default_ssh_port(void)
                    450: {
                    451:        static int port;
                    452:        struct servent *sp;
                    453:
                    454:        if (port == 0) {
                    455:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    456:                port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
                    457:        }
                    458:        return port;
                    459: }
                    460:
                    461: /*
                    462:  * Execute a command in a shell.
                    463:  * Return its exit status or -1 on abnormal exit.
                    464:  */
                    465: static int
                    466: execute_in_shell(const char *cmd)
                    467: {
1.243     dtucker   468:        char *shell;
1.206     djm       469:        pid_t pid;
                    470:        int devnull, status;
                    471:
                    472:        if ((shell = getenv("SHELL")) == NULL)
                    473:                shell = _PATH_BSHELL;
1.308     djm       474:
                    475:        if (access(shell, X_OK) == -1) {
                    476:                fatal("Shell \"%s\" is not executable: %s",
                    477:                    shell, strerror(errno));
                    478:        }
1.206     djm       479:
                    480:        /* Need this to redirect subprocess stdin/out */
                    481:        if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
                    482:                fatal("open(/dev/null): %s", strerror(errno));
                    483:
                    484:        debug("Executing command: '%.500s'", cmd);
                    485:
                    486:        /* Fork and execute the command. */
                    487:        if ((pid = fork()) == 0) {
1.245     djm       488:                char *argv[4];
1.206     djm       489:
                    490:                /* Redirect child stdin and stdout. Leave stderr */
                    491:                if (dup2(devnull, STDIN_FILENO) == -1)
                    492:                        fatal("dup2: %s", strerror(errno));
                    493:                if (dup2(devnull, STDOUT_FILENO) == -1)
                    494:                        fatal("dup2: %s", strerror(errno));
                    495:                if (devnull > STDERR_FILENO)
                    496:                        close(devnull);
                    497:                closefrom(STDERR_FILENO + 1);
1.245     djm       498:
                    499:                argv[0] = shell;
                    500:                argv[1] = "-c";
                    501:                argv[2] = xstrdup(cmd);
                    502:                argv[3] = NULL;
1.206     djm       503:
                    504:                execv(argv[0], argv);
                    505:                error("Unable to execute '%.100s': %s", cmd, strerror(errno));
                    506:                /* Die with signal to make this error apparent to parent. */
                    507:                signal(SIGTERM, SIG_DFL);
                    508:                kill(getpid(), SIGTERM);
                    509:                _exit(1);
                    510:        }
                    511:        /* Parent. */
1.307     deraadt   512:        if (pid == -1)
1.206     djm       513:                fatal("%s: fork: %.100s", __func__, strerror(errno));
                    514:
                    515:        close(devnull);
                    516:
                    517:        while (waitpid(pid, &status, 0) == -1) {
                    518:                if (errno != EINTR && errno != EAGAIN)
                    519:                        fatal("%s: waitpid: %s", __func__, strerror(errno));
                    520:        }
                    521:        if (!WIFEXITED(status)) {
                    522:                error("command '%.100s' exited abnormally", cmd);
                    523:                return -1;
1.221     djm       524:        }
1.206     djm       525:        debug3("command returned status %d", WEXITSTATUS(status));
                    526:        return WEXITSTATUS(status);
                    527: }
                    528:
                    529: /*
                    530:  * Parse and execute a Match directive.
                    531:  */
                    532: static int
                    533: match_cfg_line(Options *options, char **condition, struct passwd *pw,
1.302     djm       534:     const char *host_arg, const char *original_host, int final_pass,
                    535:     int *want_final_pass, const char *filename, int linenum)
1.206     djm       536: {
1.221     djm       537:        char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
1.211     djm       538:        const char *ruser;
1.221     djm       539:        int r, port, this_result, result = 1, attributes = 0, negate;
1.206     djm       540:        char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
1.288     djm       541:        char uidstr[32];
1.206     djm       542:
                    543:        /*
                    544:         * Configuration is likely to be incomplete at this point so we
                    545:         * must be prepared to use default values.
                    546:         */
                    547:        port = options->port <= 0 ? default_ssh_port() : options->port;
                    548:        ruser = options->user == NULL ? pw->pw_name : options->user;
1.302     djm       549:        if (final_pass) {
1.250     djm       550:                host = xstrdup(options->hostname);
                    551:        } else if (options->hostname != NULL) {
1.212     djm       552:                /* NB. Please keep in sync with ssh.c:main() */
1.211     djm       553:                host = percent_expand(options->hostname,
                    554:                    "h", host_arg, (char *)NULL);
1.250     djm       555:        } else {
1.211     djm       556:                host = xstrdup(host_arg);
1.250     djm       557:        }
1.206     djm       558:
1.221     djm       559:        debug2("checking match for '%s' host %s originally %s",
                    560:            cp, host, original_host);
                    561:        while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
                    562:                criteria = NULL;
                    563:                this_result = 1;
                    564:                if ((negate = attrib[0] == '!'))
                    565:                        attrib++;
                    566:                /* criteria "all" and "canonical" have no argument */
1.213     dtucker   567:                if (strcasecmp(attrib, "all") == 0) {
1.221     djm       568:                        if (attributes > 1 ||
1.213     dtucker   569:                            ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
1.221     djm       570:                                error("%.200s line %d: '%s' cannot be combined "
                    571:                                    "with other Match attributes",
                    572:                                    filename, linenum, oattrib);
1.213     dtucker   573:                                result = -1;
                    574:                                goto out;
                    575:                        }
1.221     djm       576:                        if (result)
                    577:                                result = negate ? 0 : 1;
1.213     dtucker   578:                        goto out;
                    579:                }
1.221     djm       580:                attributes++;
1.302     djm       581:                if (strcasecmp(attrib, "canonical") == 0 ||
                    582:                    strcasecmp(attrib, "final") == 0) {
                    583:                        /*
                    584:                         * If the config requests "Match final" then remember
                    585:                         * this so we can perform a second pass later.
                    586:                         */
                    587:                        if (strcasecmp(attrib, "final") == 0 &&
                    588:                            want_final_pass != NULL)
                    589:                                *want_final_pass = 1;
                    590:                        r = !!final_pass;  /* force bitmask member to boolean */
1.221     djm       591:                        if (r == (negate ? 1 : 0))
                    592:                                this_result = result = 0;
                    593:                        debug3("%.200s line %d: %smatched '%s'",
                    594:                            filename, linenum,
                    595:                            this_result ? "" : "not ", oattrib);
                    596:                        continue;
                    597:                }
                    598:                /* All other criteria require an argument */
1.206     djm       599:                if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
                    600:                        error("Missing Match criteria for %s", attrib);
1.211     djm       601:                        result = -1;
                    602:                        goto out;
1.206     djm       603:                }
                    604:                if (strcasecmp(attrib, "host") == 0) {
1.221     djm       605:                        criteria = xstrdup(host);
1.235     djm       606:                        r = match_hostname(host, arg) == 1;
1.221     djm       607:                        if (r == (negate ? 1 : 0))
                    608:                                this_result = result = 0;
1.206     djm       609:                } else if (strcasecmp(attrib, "originalhost") == 0) {
1.221     djm       610:                        criteria = xstrdup(original_host);
1.235     djm       611:                        r = match_hostname(original_host, arg) == 1;
1.221     djm       612:                        if (r == (negate ? 1 : 0))
                    613:                                this_result = result = 0;
1.206     djm       614:                } else if (strcasecmp(attrib, "user") == 0) {
1.221     djm       615:                        criteria = xstrdup(ruser);
1.235     djm       616:                        r = match_pattern_list(ruser, arg, 0) == 1;
1.221     djm       617:                        if (r == (negate ? 1 : 0))
                    618:                                this_result = result = 0;
1.206     djm       619:                } else if (strcasecmp(attrib, "localuser") == 0) {
1.221     djm       620:                        criteria = xstrdup(pw->pw_name);
1.235     djm       621:                        r = match_pattern_list(pw->pw_name, arg, 0) == 1;
1.221     djm       622:                        if (r == (negate ? 1 : 0))
                    623:                                this_result = result = 0;
1.210     djm       624:                } else if (strcasecmp(attrib, "exec") == 0) {
1.206     djm       625:                        if (gethostname(thishost, sizeof(thishost)) == -1)
                    626:                                fatal("gethostname: %s", strerror(errno));
                    627:                        strlcpy(shorthost, thishost, sizeof(shorthost));
                    628:                        shorthost[strcspn(thishost, ".")] = '\0';
                    629:                        snprintf(portstr, sizeof(portstr), "%d", port);
1.288     djm       630:                        snprintf(uidstr, sizeof(uidstr), "%llu",
                    631:                            (unsigned long long)pw->pw_uid);
1.206     djm       632:
                    633:                        cmd = percent_expand(arg,
                    634:                            "L", shorthost,
                    635:                            "d", pw->pw_dir,
                    636:                            "h", host,
                    637:                            "l", thishost,
1.221     djm       638:                            "n", original_host,
1.206     djm       639:                            "p", portstr,
                    640:                            "r", ruser,
                    641:                            "u", pw->pw_name,
1.288     djm       642:                            "i", uidstr,
1.206     djm       643:                            (char *)NULL);
1.217     djm       644:                        if (result != 1) {
                    645:                                /* skip execution if prior predicate failed */
1.221     djm       646:                                debug3("%.200s line %d: skipped exec "
                    647:                                    "\"%.100s\"", filename, linenum, cmd);
                    648:                                free(cmd);
                    649:                                continue;
                    650:                        }
                    651:                        r = execute_in_shell(cmd);
                    652:                        if (r == -1) {
                    653:                                fatal("%.200s line %d: match exec "
                    654:                                    "'%.100s' error", filename,
                    655:                                    linenum, cmd);
1.217     djm       656:                        }
1.221     djm       657:                        criteria = xstrdup(cmd);
1.206     djm       658:                        free(cmd);
1.221     djm       659:                        /* Force exit status to boolean */
                    660:                        r = r == 0;
                    661:                        if (r == (negate ? 1 : 0))
                    662:                                this_result = result = 0;
1.206     djm       663:                } else {
                    664:                        error("Unsupported Match attribute %s", attrib);
1.211     djm       665:                        result = -1;
                    666:                        goto out;
1.206     djm       667:                }
1.221     djm       668:                debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
                    669:                    filename, linenum, this_result ? "": "not ",
                    670:                    oattrib, criteria);
                    671:                free(criteria);
1.213     dtucker   672:        }
                    673:        if (attributes == 0) {
                    674:                error("One or more attributes required for Match");
                    675:                result = -1;
                    676:                goto out;
1.206     djm       677:        }
1.221     djm       678:  out:
                    679:        if (result != -1)
                    680:                debug2("match %sfound", result ? "" : "not ");
1.206     djm       681:        *condition = cp;
1.211     djm       682:        free(host);
1.206     djm       683:        return result;
                    684: }
                    685:
1.286     djm       686: /* Remove environment variable by pattern */
                    687: static void
                    688: rm_env(Options *options, const char *arg, const char *filename, int linenum)
                    689: {
                    690:        int i, j;
                    691:        char *cp;
                    692:
                    693:        /* Remove an environment variable */
                    694:        for (i = 0; i < options->num_send_env; ) {
                    695:                cp = xstrdup(options->send_env[i]);
                    696:                if (!match_pattern(cp, arg + 1)) {
                    697:                        free(cp);
                    698:                        i++;
                    699:                        continue;
                    700:                }
                    701:                debug3("%s line %d: removing environment %s",
                    702:                    filename, linenum, cp);
                    703:                free(cp);
                    704:                free(options->send_env[i]);
                    705:                options->send_env[i] = NULL;
                    706:                for (j = i; j < options->num_send_env - 1; j++) {
                    707:                        options->send_env[j] = options->send_env[j + 1];
                    708:                        options->send_env[j + 1] = NULL;
                    709:                }
                    710:                options->num_send_env--;
                    711:                /* NB. don't increment i */
                    712:        }
                    713: }
                    714:
1.19      markus    715: /*
1.70      stevesk   716:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    717:  */
1.26      markus    718: static OpCodes
1.199     djm       719: parse_token(const char *cp, const char *filename, int linenum,
                    720:     const char *ignored_unknown)
1.1       deraadt   721: {
1.199     djm       722:        int i;
1.1       deraadt   723:
1.17      markus    724:        for (i = 0; keywords[i].name; i++)
1.199     djm       725:                if (strcmp(cp, keywords[i].name) == 0)
1.17      markus    726:                        return keywords[i].opcode;
1.235     djm       727:        if (ignored_unknown != NULL &&
                    728:            match_pattern_list(cp, ignored_unknown, 1) == 1)
1.199     djm       729:                return oIgnoredUnknownOption;
1.75      stevesk   730:        error("%s: line %d: Bad configuration option: %s",
                    731:            filename, linenum, cp);
1.17      markus    732:        return oBadOption;
1.1       deraadt   733: }
                    734:
1.207     djm       735: /* Multistate option parsing */
                    736: struct multistate {
                    737:        char *key;
                    738:        int value;
                    739: };
                    740: static const struct multistate multistate_flag[] = {
                    741:        { "true",                       1 },
                    742:        { "false",                      0 },
                    743:        { "yes",                        1 },
                    744:        { "no",                         0 },
                    745:        { NULL, -1 }
                    746: };
                    747: static const struct multistate multistate_yesnoask[] = {
                    748:        { "true",                       1 },
                    749:        { "false",                      0 },
                    750:        { "yes",                        1 },
                    751:        { "no",                         0 },
                    752:        { "ask",                        2 },
                    753:        { NULL, -1 }
                    754: };
1.278     djm       755: static const struct multistate multistate_strict_hostkey[] = {
                    756:        { "true",                       SSH_STRICT_HOSTKEY_YES },
                    757:        { "false",                      SSH_STRICT_HOSTKEY_OFF },
                    758:        { "yes",                        SSH_STRICT_HOSTKEY_YES },
                    759:        { "no",                         SSH_STRICT_HOSTKEY_OFF },
                    760:        { "ask",                        SSH_STRICT_HOSTKEY_ASK },
                    761:        { "off",                        SSH_STRICT_HOSTKEY_OFF },
                    762:        { "accept-new",                 SSH_STRICT_HOSTKEY_NEW },
                    763:        { NULL, -1 }
                    764: };
1.246     jcs       765: static const struct multistate multistate_yesnoaskconfirm[] = {
                    766:        { "true",                       1 },
                    767:        { "false",                      0 },
                    768:        { "yes",                        1 },
                    769:        { "no",                         0 },
                    770:        { "ask",                        2 },
                    771:        { "confirm",                    3 },
                    772:        { NULL, -1 }
                    773: };
1.207     djm       774: static const struct multistate multistate_addressfamily[] = {
                    775:        { "inet",                       AF_INET },
                    776:        { "inet6",                      AF_INET6 },
                    777:        { "any",                        AF_UNSPEC },
                    778:        { NULL, -1 }
                    779: };
                    780: static const struct multistate multistate_controlmaster[] = {
                    781:        { "true",                       SSHCTL_MASTER_YES },
                    782:        { "yes",                        SSHCTL_MASTER_YES },
                    783:        { "false",                      SSHCTL_MASTER_NO },
                    784:        { "no",                         SSHCTL_MASTER_NO },
                    785:        { "auto",                       SSHCTL_MASTER_AUTO },
                    786:        { "ask",                        SSHCTL_MASTER_ASK },
                    787:        { "autoask",                    SSHCTL_MASTER_AUTO_ASK },
                    788:        { NULL, -1 }
                    789: };
                    790: static const struct multistate multistate_tunnel[] = {
                    791:        { "ethernet",                   SSH_TUNMODE_ETHERNET },
                    792:        { "point-to-point",             SSH_TUNMODE_POINTOPOINT },
                    793:        { "true",                       SSH_TUNMODE_DEFAULT },
                    794:        { "yes",                        SSH_TUNMODE_DEFAULT },
                    795:        { "false",                      SSH_TUNMODE_NO },
                    796:        { "no",                         SSH_TUNMODE_NO },
                    797:        { NULL, -1 }
                    798: };
                    799: static const struct multistate multistate_requesttty[] = {
                    800:        { "true",                       REQUEST_TTY_YES },
                    801:        { "yes",                        REQUEST_TTY_YES },
                    802:        { "false",                      REQUEST_TTY_NO },
                    803:        { "no",                         REQUEST_TTY_NO },
                    804:        { "force",                      REQUEST_TTY_FORCE },
                    805:        { "auto",                       REQUEST_TTY_AUTO },
                    806:        { NULL, -1 }
                    807: };
1.209     djm       808: static const struct multistate multistate_canonicalizehostname[] = {
1.208     djm       809:        { "true",                       SSH_CANONICALISE_YES },
                    810:        { "false",                      SSH_CANONICALISE_NO },
                    811:        { "yes",                        SSH_CANONICALISE_YES },
                    812:        { "no",                         SSH_CANONICALISE_NO },
                    813:        { "always",                     SSH_CANONICALISE_ALWAYS },
                    814:        { NULL, -1 }
                    815: };
1.207     djm       816:
1.19      markus    817: /*
                    818:  * Processes a single option line as used in the configuration files. This
                    819:  * only sets those values that have not already been set.
                    820:  */
1.14      markus    821: int
1.206     djm       822: process_config_line(Options *options, struct passwd *pw, const char *host,
1.221     djm       823:     const char *original_host, char *line, const char *filename,
                    824:     int linenum, int *activep, int flags)
1.1       deraadt   825: {
1.252     djm       826:        return process_config_line_depth(options, pw, host, original_host,
1.302     djm       827:            line, filename, linenum, activep, flags, NULL, 0);
1.252     djm       828: }
                    829:
                    830: #define WHITESPACE " \t\r\n"
                    831: static int
                    832: process_config_line_depth(Options *options, struct passwd *pw, const char *host,
                    833:     const char *original_host, char *line, const char *filename,
1.302     djm       834:     int linenum, int *activep, int flags, int *want_final_pass, int depth)
1.252     djm       835: {
1.193     djm       836:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
                    837:        char **cpptr, fwdarg[256];
1.199     djm       838:        u_int i, *uintptr, max_entries = 0;
1.252     djm       839:        int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
1.279     markus    840:        int remotefwd, dynamicfwd;
1.164     dtucker   841:        LogLevel *log_level_ptr;
1.271     dtucker   842:        SyslogFacility *log_facility_ptr;
1.201     dtucker   843:        long long val64;
1.102     markus    844:        size_t len;
1.220     millert   845:        struct Forward fwd;
1.207     djm       846:        const struct multistate *multistate_ptr;
1.208     djm       847:        struct allowed_cname *cname;
1.252     djm       848:        glob_t gl;
1.281     dtucker   849:        const char *errstr;
1.106     djm       850:
1.206     djm       851:        if (activep == NULL) { /* We are processing a command line directive */
                    852:                cmdline = 1;
                    853:                activep = &cmdline;
                    854:        }
                    855:
1.267     djm       856:        /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1.233     djm       857:        if ((len = strlen(line)) == 0)
                    858:                return 0;
                    859:        for (len--; len > 0; len--) {
1.267     djm       860:                if (strchr(WHITESPACE "\f", line[len]) == NULL)
1.106     djm       861:                        break;
                    862:                line[len] = '\0';
                    863:        }
1.1       deraadt   864:
1.42      provos    865:        s = line;
                    866:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       867:        if ((keyword = strdelim(&s)) == NULL)
                    868:                return 0;
1.42      provos    869:        /* Ignore leading whitespace. */
                    870:        if (*keyword == '\0')
1.43      markus    871:                keyword = strdelim(&s);
1.56      deraadt   872:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    873:                return 0;
1.199     djm       874:        /* Match lowercase keyword */
1.207     djm       875:        lowercase(keyword);
1.17      markus    876:
1.199     djm       877:        opcode = parse_token(keyword, filename, linenum,
                    878:            options->ignored_unknown);
1.17      markus    879:
                    880:        switch (opcode) {
                    881:        case oBadOption:
1.19      markus    882:                /* don't panic, but count bad options */
                    883:                return -1;
1.273     djm       884:        case oIgnore:
                    885:                return 0;
1.199     djm       886:        case oIgnoredUnknownOption:
                    887:                debug("%s line %d: Ignored unknown option \"%s\"",
                    888:                    filename, linenum, keyword);
                    889:                return 0;
1.111     djm       890:        case oConnectTimeout:
                    891:                intptr = &options->connection_timeout;
1.127     markus    892: parse_time:
1.111     djm       893:                arg = strdelim(&s);
                    894:                if (!arg || *arg == '\0')
                    895:                        fatal("%s line %d: missing time value.",
                    896:                            filename, linenum);
1.221     djm       897:                if (strcmp(arg, "none") == 0)
                    898:                        value = -1;
                    899:                else if ((value = convtime(arg)) == -1)
1.111     djm       900:                        fatal("%s line %d: invalid time value.",
                    901:                            filename, linenum);
1.160     dtucker   902:                if (*activep && *intptr == -1)
1.111     djm       903:                        *intptr = value;
                    904:                break;
                    905:
1.17      markus    906:        case oForwardAgent:
                    907:                intptr = &options->forward_agent;
1.319   ! djm       908:
        !           909:                arg = strdelim(&s);
        !           910:                if (!arg || *arg == '\0')
        !           911:                        fatal("%s line %d: missing argument.",
        !           912:                            filename, linenum);
        !           913:
        !           914:                value = -1;
        !           915:                multistate_ptr = multistate_flag;
        !           916:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
        !           917:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
        !           918:                                value = multistate_ptr[i].value;
        !           919:                                break;
        !           920:                        }
        !           921:                }
        !           922:                if (value != -1) {
        !           923:                        if (*activep && *intptr == -1)
        !           924:                                *intptr = value;
        !           925:                        break;
        !           926:                }
        !           927:                /* ForwardAgent wasn't 'yes' or 'no', assume a path */
        !           928:                if (*activep && *intptr == -1)
        !           929:                        *intptr = 1;
        !           930:
        !           931:                charptr = &options->forward_agent_sock_path;
        !           932:                goto parse_agent_path;
        !           933:
        !           934:        case oForwardX11:
        !           935:                intptr = &options->forward_x11;
1.207     djm       936:  parse_flag:
                    937:                multistate_ptr = multistate_flag;
                    938:  parse_multistate:
1.42      provos    939:                arg = strdelim(&s);
1.40      ho        940:                if (!arg || *arg == '\0')
1.207     djm       941:                        fatal("%s line %d: missing argument.",
                    942:                            filename, linenum);
                    943:                value = -1;
                    944:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                    945:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                    946:                                value = multistate_ptr[i].value;
                    947:                                break;
                    948:                        }
                    949:                }
                    950:                if (value == -1)
                    951:                        fatal("%s line %d: unsupported option \"%s\".",
                    952:                            filename, linenum, arg);
1.17      markus    953:                if (*activep && *intptr == -1)
                    954:                        *intptr = value;
                    955:                break;
                    956:
1.123     markus    957:        case oForwardX11Trusted:
                    958:                intptr = &options->forward_x11_trusted;
                    959:                goto parse_flag;
1.221     djm       960:
1.186     djm       961:        case oForwardX11Timeout:
                    962:                intptr = &options->forward_x11_timeout;
                    963:                goto parse_time;
1.123     markus    964:
1.17      markus    965:        case oGatewayPorts:
1.220     millert   966:                intptr = &options->fwd_opts.gateway_ports;
1.17      markus    967:                goto parse_flag;
                    968:
1.153     markus    969:        case oExitOnForwardFailure:
                    970:                intptr = &options->exit_on_forward_failure;
                    971:                goto parse_flag;
                    972:
1.17      markus    973:        case oPasswordAuthentication:
                    974:                intptr = &options->password_authentication;
                    975:                goto parse_flag;
                    976:
1.48      markus    977:        case oKbdInteractiveAuthentication:
                    978:                intptr = &options->kbd_interactive_authentication;
                    979:                goto parse_flag;
                    980:
                    981:        case oKbdInteractiveDevices:
                    982:                charptr = &options->kbd_interactive_devices;
                    983:                goto parse_string;
                    984:
1.50      markus    985:        case oPubkeyAuthentication:
                    986:                intptr = &options->pubkey_authentication;
1.30      markus    987:                goto parse_flag;
                    988:
1.72      markus    989:        case oHostbasedAuthentication:
                    990:                intptr = &options->hostbased_authentication;
                    991:                goto parse_flag;
                    992:
1.59      markus    993:        case oChallengeResponseAuthentication:
1.78      markus    994:                intptr = &options->challenge_response_authentication;
1.17      markus    995:                goto parse_flag;
1.108     jakob     996:
1.118     markus    997:        case oGssAuthentication:
                    998:                intptr = &options->gss_authentication;
                    999:                goto parse_flag;
                   1000:
                   1001:        case oGssDelegateCreds:
                   1002:                intptr = &options->gss_deleg_creds;
                   1003:                goto parse_flag;
                   1004:
1.17      markus   1005:        case oBatchMode:
                   1006:                intptr = &options->batch_mode;
                   1007:                goto parse_flag;
                   1008:
                   1009:        case oCheckHostIP:
                   1010:                intptr = &options->check_host_ip;
1.167     grunk    1011:                goto parse_flag;
1.17      markus   1012:
1.107     jakob    1013:        case oVerifyHostKeyDNS:
                   1014:                intptr = &options->verify_host_key_dns;
1.207     djm      1015:                multistate_ptr = multistate_yesnoask;
                   1016:                goto parse_multistate;
1.107     jakob    1017:
1.17      markus   1018:        case oStrictHostKeyChecking:
                   1019:                intptr = &options->strict_host_key_checking;
1.278     djm      1020:                multistate_ptr = multistate_strict_hostkey;
1.207     djm      1021:                goto parse_multistate;
1.17      markus   1022:
                   1023:        case oCompression:
                   1024:                intptr = &options->compression;
                   1025:                goto parse_flag;
                   1026:
1.126     markus   1027:        case oTCPKeepAlive:
                   1028:                intptr = &options->tcp_keep_alive;
1.17      markus   1029:                goto parse_flag;
                   1030:
1.91      markus   1031:        case oNoHostAuthenticationForLocalhost:
                   1032:                intptr = &options->no_host_authentication_for_localhost;
                   1033:                goto parse_flag;
                   1034:
1.17      markus   1035:        case oNumberOfPasswordPrompts:
                   1036:                intptr = &options->number_of_password_prompts;
                   1037:                goto parse_int;
                   1038:
1.105     markus   1039:        case oRekeyLimit:
                   1040:                arg = strdelim(&s);
                   1041:                if (!arg || *arg == '\0')
1.198     dtucker  1042:                        fatal("%.200s line %d: Missing argument.", filename,
                   1043:                            linenum);
                   1044:                if (strcmp(arg, "default") == 0) {
                   1045:                        val64 = 0;
                   1046:                } else {
1.200     dtucker  1047:                        if (scan_scaled(arg, &val64) == -1)
                   1048:                                fatal("%.200s line %d: Bad number '%s': %s",
                   1049:                                    filename, linenum, arg, strerror(errno));
1.198     dtucker  1050:                        if (val64 != 0 && val64 < 16)
                   1051:                                fatal("%.200s line %d: RekeyLimit too small",
                   1052:                                    filename, linenum);
1.105     markus   1053:                }
1.165     djm      1054:                if (*activep && options->rekey_limit == -1)
1.249     dtucker  1055:                        options->rekey_limit = val64;
1.198     dtucker  1056:                if (s != NULL) { /* optional rekey interval present */
                   1057:                        if (strcmp(s, "none") == 0) {
                   1058:                                (void)strdelim(&s);     /* discard */
                   1059:                                break;
                   1060:                        }
                   1061:                        intptr = &options->rekey_interval;
                   1062:                        goto parse_time;
                   1063:                }
1.105     markus   1064:                break;
                   1065:
1.17      markus   1066:        case oIdentityFile:
1.42      provos   1067:                arg = strdelim(&s);
1.40      ho       1068:                if (!arg || *arg == '\0')
1.17      markus   1069:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1070:                if (*activep) {
1.50      markus   1071:                        intptr = &options->num_identity_files;
1.27      markus   1072:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus   1073:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt  1074:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.221     djm      1075:                        add_identity_file(options, NULL,
                   1076:                            arg, flags & SSHCONF_USERCONF);
1.17      markus   1077:                }
                   1078:                break;
                   1079:
1.241     djm      1080:        case oCertificateFile:
                   1081:                arg = strdelim(&s);
                   1082:                if (!arg || *arg == '\0')
                   1083:                        fatal("%.200s line %d: Missing argument.",
                   1084:                            filename, linenum);
                   1085:                if (*activep) {
                   1086:                        intptr = &options->num_certificate_files;
                   1087:                        if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
                   1088:                                fatal("%.200s line %d: Too many certificate "
                   1089:                                    "files specified (max %d).",
                   1090:                                    filename, linenum,
                   1091:                                    SSH_MAX_CERTIFICATE_FILES);
                   1092:                        }
                   1093:                        add_certificate_file(options, arg,
                   1094:                            flags & SSHCONF_USERCONF);
                   1095:                }
                   1096:                break;
                   1097:
1.34      markus   1098:        case oXAuthLocation:
                   1099:                charptr=&options->xauth_location;
                   1100:                goto parse_string;
                   1101:
1.17      markus   1102:        case oUser:
                   1103:                charptr = &options->user;
                   1104: parse_string:
1.42      provos   1105:                arg = strdelim(&s);
1.40      ho       1106:                if (!arg || *arg == '\0')
1.193     djm      1107:                        fatal("%.200s line %d: Missing argument.",
                   1108:                            filename, linenum);
1.17      markus   1109:                if (*activep && *charptr == NULL)
1.38      provos   1110:                        *charptr = xstrdup(arg);
1.17      markus   1111:                break;
                   1112:
                   1113:        case oGlobalKnownHostsFile:
1.193     djm      1114:                cpptr = (char **)&options->system_hostfiles;
                   1115:                uintptr = &options->num_system_hostfiles;
                   1116:                max_entries = SSH_MAX_HOSTS_FILES;
                   1117: parse_char_array:
                   1118:                if (*activep && *uintptr == 0) {
                   1119:                        while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1120:                                if ((*uintptr) >= max_entries)
                   1121:                                        fatal("%s line %d: "
                   1122:                                            "too many authorized keys files.",
                   1123:                                            filename, linenum);
                   1124:                                cpptr[(*uintptr)++] = xstrdup(arg);
                   1125:                        }
                   1126:                }
                   1127:                return 0;
1.17      markus   1128:
                   1129:        case oUserKnownHostsFile:
1.193     djm      1130:                cpptr = (char **)&options->user_hostfiles;
                   1131:                uintptr = &options->num_user_hostfiles;
                   1132:                max_entries = SSH_MAX_HOSTS_FILES;
                   1133:                goto parse_char_array;
1.27      markus   1134:
1.306     jmc      1135:        case oHostname:
1.17      markus   1136:                charptr = &options->hostname;
                   1137:                goto parse_string;
                   1138:
1.52      markus   1139:        case oHostKeyAlias:
                   1140:                charptr = &options->host_key_alias;
                   1141:                goto parse_string;
                   1142:
1.67      markus   1143:        case oPreferredAuthentications:
                   1144:                charptr = &options->preferred_authentications;
                   1145:                goto parse_string;
                   1146:
1.77      markus   1147:        case oBindAddress:
                   1148:                charptr = &options->bind_address;
                   1149:                goto parse_string;
                   1150:
1.282     djm      1151:        case oBindInterface:
                   1152:                charptr = &options->bind_interface;
                   1153:                goto parse_string;
                   1154:
1.183     markus   1155:        case oPKCS11Provider:
                   1156:                charptr = &options->pkcs11_provider;
1.86      markus   1157:                goto parse_string;
1.85      jakob    1158:
1.310     djm      1159:        case oSecurityKeyProvider:
                   1160:                charptr = &options->sk_provider;
                   1161:                goto parse_string;
                   1162:
1.17      markus   1163:        case oProxyCommand:
1.144     reyk     1164:                charptr = &options->proxy_command;
1.257     djm      1165:                /* Ignore ProxyCommand if ProxyJump already specified */
                   1166:                if (options->jump_host != NULL)
                   1167:                        charptr = &options->jump_host; /* Skip below */
1.144     reyk     1168: parse_command:
1.113     markus   1169:                if (s == NULL)
                   1170:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus   1171:                len = strspn(s, WHITESPACE "=");
1.17      markus   1172:                if (*activep && *charptr == NULL)
1.102     markus   1173:                        *charptr = xstrdup(s + len);
1.17      markus   1174:                return 0;
                   1175:
1.257     djm      1176:        case oProxyJump:
                   1177:                if (s == NULL) {
                   1178:                        fatal("%.200s line %d: Missing argument.",
                   1179:                            filename, linenum);
                   1180:                }
                   1181:                len = strspn(s, WHITESPACE "=");
                   1182:                if (parse_jump(s + len, options, *activep) == -1) {
                   1183:                        fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
                   1184:                            filename, linenum, s + len);
                   1185:                }
                   1186:                return 0;
                   1187:
1.17      markus   1188:        case oPort:
1.300     naddy    1189:                arg = strdelim(&s);
                   1190:                if (!arg || *arg == '\0')
                   1191:                        fatal("%.200s line %d: Missing argument.",
                   1192:                            filename, linenum);
                   1193:                value = a2port(arg);
                   1194:                if (value <= 0)
                   1195:                        fatal("%.200s line %d: Bad port '%s'.",
                   1196:                            filename, linenum, arg);
                   1197:                if (*activep && options->port == -1)
                   1198:                        options->port = value;
                   1199:                break;
                   1200:
                   1201:        case oConnectionAttempts:
                   1202:                intptr = &options->connection_attempts;
1.17      markus   1203: parse_int:
1.42      provos   1204:                arg = strdelim(&s);
1.281     dtucker  1205:                if ((errstr = atoi_err(arg, &value)) != NULL)
                   1206:                        fatal("%s line %d: integer value %s.",
                   1207:                            filename, linenum, errstr);
1.17      markus   1208:                if (*activep && *intptr == -1)
                   1209:                        *intptr = value;
                   1210:                break;
                   1211:
1.25      markus   1212:        case oCiphers:
1.42      provos   1213:                arg = strdelim(&s);
1.40      ho       1214:                if (!arg || *arg == '\0')
1.32      markus   1215:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.309     naddy    1216:                if (*arg != '-' &&
                   1217:                    !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
1.31      markus   1218:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt  1219:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1220:                if (*activep && options->ciphers == NULL)
1.38      provos   1221:                        options->ciphers = xstrdup(arg);
1.25      markus   1222:                break;
                   1223:
1.62      markus   1224:        case oMacs:
                   1225:                arg = strdelim(&s);
                   1226:                if (!arg || *arg == '\0')
                   1227:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.309     naddy    1228:                if (*arg != '-' &&
                   1229:                    !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
1.305     dtucker  1230:                        fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
1.93      deraadt  1231:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus   1232:                if (*activep && options->macs == NULL)
                   1233:                        options->macs = xstrdup(arg);
                   1234:                break;
                   1235:
1.189     djm      1236:        case oKexAlgorithms:
                   1237:                arg = strdelim(&s);
                   1238:                if (!arg || *arg == '\0')
                   1239:                        fatal("%.200s line %d: Missing argument.",
                   1240:                            filename, linenum);
1.268     djm      1241:                if (*arg != '-' &&
1.309     naddy    1242:                    !kex_names_valid(*arg == '+' || *arg == '^' ?
                   1243:                    arg + 1 : arg))
1.189     djm      1244:                        fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
                   1245:                            filename, linenum, arg ? arg : "<NONE>");
                   1246:                if (*activep && options->kex_algorithms == NULL)
                   1247:                        options->kex_algorithms = xstrdup(arg);
                   1248:                break;
                   1249:
1.76      markus   1250:        case oHostKeyAlgorithms:
1.238     markus   1251:                charptr = &options->hostkeyalgorithms;
                   1252: parse_keytypes:
1.76      markus   1253:                arg = strdelim(&s);
                   1254:                if (!arg || *arg == '\0')
1.238     markus   1255:                        fatal("%.200s line %d: Missing argument.",
                   1256:                            filename, linenum);
1.268     djm      1257:                if (*arg != '-' &&
1.309     naddy    1258:                    !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
                   1259:                    arg + 1 : arg, 1))
1.238     markus   1260:                        fatal("%s line %d: Bad key types '%s'.",
                   1261:                                filename, linenum, arg ? arg : "<NONE>");
                   1262:                if (*activep && *charptr == NULL)
                   1263:                        *charptr = xstrdup(arg);
1.76      markus   1264:                break;
                   1265:
1.298     djm      1266:        case oCASignatureAlgorithms:
                   1267:                charptr = &options->ca_sign_algorithms;
                   1268:                goto parse_keytypes;
                   1269:
1.17      markus   1270:        case oLogLevel:
1.164     dtucker  1271:                log_level_ptr = &options->log_level;
1.42      provos   1272:                arg = strdelim(&s);
1.38      provos   1273:                value = log_level_number(arg);
1.95      markus   1274:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert  1275:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt  1276:                            filename, linenum, arg ? arg : "<NONE>");
1.164     dtucker  1277:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                   1278:                        *log_level_ptr = (LogLevel) value;
1.17      markus   1279:                break;
                   1280:
1.271     dtucker  1281:        case oLogFacility:
                   1282:                log_facility_ptr = &options->log_facility;
                   1283:                arg = strdelim(&s);
                   1284:                value = log_facility_number(arg);
                   1285:                if (value == SYSLOG_FACILITY_NOT_SET)
                   1286:                        fatal("%.200s line %d: unsupported log facility '%s'",
                   1287:                            filename, linenum, arg ? arg : "<NONE>");
                   1288:                if (*log_facility_ptr == -1)
                   1289:                        *log_facility_ptr = (SyslogFacility) value;
                   1290:                break;
                   1291:
1.88      stevesk  1292:        case oLocalForward:
1.17      markus   1293:        case oRemoteForward:
1.168     stevesk  1294:        case oDynamicForward:
1.42      provos   1295:                arg = strdelim(&s);
1.135     djm      1296:                if (arg == NULL || *arg == '\0')
1.88      stevesk  1297:                        fatal("%.200s line %d: Missing port argument.",
                   1298:                            filename, linenum);
1.135     djm      1299:
1.279     markus   1300:                remotefwd = (opcode == oRemoteForward);
                   1301:                dynamicfwd = (opcode == oDynamicForward);
                   1302:
                   1303:                if (!dynamicfwd) {
1.168     stevesk  1304:                        arg2 = strdelim(&s);
1.279     markus   1305:                        if (arg2 == NULL || *arg2 == '\0') {
                   1306:                                if (remotefwd)
                   1307:                                        dynamicfwd = 1;
                   1308:                                else
                   1309:                                        fatal("%.200s line %d: Missing target "
                   1310:                                            "argument.", filename, linenum);
                   1311:                        } else {
                   1312:                                /* construct a string for parse_forward */
                   1313:                                snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
                   1314:                                    arg2);
                   1315:                        }
                   1316:                }
                   1317:                if (dynamicfwd)
1.168     stevesk  1318:                        strlcpy(fwdarg, arg, sizeof(fwdarg));
                   1319:
1.279     markus   1320:                if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
1.88      stevesk  1321:                        fatal("%.200s line %d: Bad forwarding specification.",
                   1322:                            filename, linenum);
1.135     djm      1323:
1.88      stevesk  1324:                if (*activep) {
1.279     markus   1325:                        if (remotefwd) {
                   1326:                                add_remote_forward(options, &fwd);
                   1327:                        } else {
1.135     djm      1328:                                add_local_forward(options, &fwd);
1.279     markus   1329:                        }
1.88      stevesk  1330:                }
1.17      markus   1331:                break;
1.71      markus   1332:
1.90      stevesk  1333:        case oClearAllForwardings:
                   1334:                intptr = &options->clear_forwardings;
                   1335:                goto parse_flag;
                   1336:
1.17      markus   1337:        case oHost:
1.206     djm      1338:                if (cmdline)
                   1339:                        fatal("Host directive not supported as a command-line "
                   1340:                            "option");
1.17      markus   1341:                *activep = 0;
1.191     djm      1342:                arg2 = NULL;
                   1343:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.252     djm      1344:                        if ((flags & SSHCONF_NEVERMATCH) != 0)
                   1345:                                break;
1.191     djm      1346:                        negated = *arg == '!';
                   1347:                        if (negated)
                   1348:                                arg++;
1.38      provos   1349:                        if (match_pattern(host, arg)) {
1.191     djm      1350:                                if (negated) {
                   1351:                                        debug("%.200s line %d: Skipping Host "
                   1352:                                            "block because of negated match "
                   1353:                                            "for %.100s", filename, linenum,
                   1354:                                            arg);
                   1355:                                        *activep = 0;
                   1356:                                        break;
                   1357:                                }
                   1358:                                if (!*activep)
                   1359:                                        arg2 = arg; /* logged below */
1.17      markus   1360:                                *activep = 1;
                   1361:                        }
1.191     djm      1362:                }
                   1363:                if (*activep)
                   1364:                        debug("%.200s line %d: Applying options for %.100s",
                   1365:                            filename, linenum, arg2);
1.42      provos   1366:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus   1367:                return 0;
                   1368:
1.206     djm      1369:        case oMatch:
                   1370:                if (cmdline)
                   1371:                        fatal("Host directive not supported as a command-line "
                   1372:                            "option");
1.221     djm      1373:                value = match_cfg_line(options, &s, pw, host, original_host,
1.302     djm      1374:                    flags & SSHCONF_FINAL, want_final_pass,
                   1375:                    filename, linenum);
1.206     djm      1376:                if (value < 0)
                   1377:                        fatal("%.200s line %d: Bad Match condition", filename,
                   1378:                            linenum);
1.252     djm      1379:                *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
1.206     djm      1380:                break;
                   1381:
1.17      markus   1382:        case oEscapeChar:
                   1383:                intptr = &options->escape_char;
1.42      provos   1384:                arg = strdelim(&s);
1.40      ho       1385:                if (!arg || *arg == '\0')
1.17      markus   1386:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.236     djm      1387:                if (strcmp(arg, "none") == 0)
                   1388:                        value = SSH_ESCAPECHAR_NONE;
                   1389:                else if (arg[1] == '\0')
                   1390:                        value = (u_char) arg[0];
                   1391:                else if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus   1392:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                   1393:                        value = (u_char) arg[1] & 31;
1.17      markus   1394:                else {
                   1395:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt  1396:                            filename, linenum);
1.17      markus   1397:                        /* NOTREACHED */
                   1398:                        value = 0;      /* Avoid compiler warning. */
                   1399:                }
                   1400:                if (*activep && *intptr == -1)
                   1401:                        *intptr = value;
1.112     djm      1402:                break;
                   1403:
                   1404:        case oAddressFamily:
1.114     djm      1405:                intptr = &options->address_family;
1.207     djm      1406:                multistate_ptr = multistate_addressfamily;
                   1407:                goto parse_multistate;
1.17      markus   1408:
1.101     markus   1409:        case oEnableSSHKeysign:
                   1410:                intptr = &options->enable_ssh_keysign;
                   1411:                goto parse_flag;
                   1412:
1.128     markus   1413:        case oIdentitiesOnly:
                   1414:                intptr = &options->identities_only;
                   1415:                goto parse_flag;
                   1416:
1.127     markus   1417:        case oServerAliveInterval:
                   1418:                intptr = &options->server_alive_interval;
                   1419:                goto parse_time;
                   1420:
                   1421:        case oServerAliveCountMax:
                   1422:                intptr = &options->server_alive_count_max;
                   1423:                goto parse_int;
                   1424:
1.130     djm      1425:        case oSendEnv:
                   1426:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1427:                        if (strchr(arg, '=') != NULL)
                   1428:                                fatal("%s line %d: Invalid environment name.",
                   1429:                                    filename, linenum);
1.137     djm      1430:                        if (!*activep)
                   1431:                                continue;
1.286     djm      1432:                        if (*arg == '-') {
                   1433:                                /* Removing an env var */
                   1434:                                rm_env(options, arg, filename, linenum);
                   1435:                                continue;
                   1436:                        } else {
                   1437:                                /* Adding an env var */
1.290     djm      1438:                                if (options->num_send_env >= INT_MAX)
1.286     djm      1439:                                        fatal("%s line %d: too many send env.",
                   1440:                                            filename, linenum);
1.290     djm      1441:                                options->send_env = xrecallocarray(
                   1442:                                    options->send_env, options->num_send_env,
1.291     djm      1443:                                    options->num_send_env + 1,
1.290     djm      1444:                                    sizeof(*options->send_env));
1.286     djm      1445:                                options->send_env[options->num_send_env++] =
                   1446:                                    xstrdup(arg);
                   1447:                        }
1.130     djm      1448:                }
                   1449:                break;
                   1450:
1.290     djm      1451:        case oSetEnv:
                   1452:                value = options->num_setenv;
                   1453:                while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
                   1454:                        if (strchr(arg, '=') == NULL)
                   1455:                                fatal("%s line %d: Invalid SetEnv.",
                   1456:                                    filename, linenum);
                   1457:                        if (!*activep || value != 0)
                   1458:                                continue;
                   1459:                        /* Adding a setenv var */
                   1460:                        if (options->num_setenv >= INT_MAX)
                   1461:                                fatal("%s line %d: too many SetEnv.",
                   1462:                                    filename, linenum);
                   1463:                        options->setenv = xrecallocarray(
                   1464:                            options->setenv, options->num_setenv,
                   1465:                            options->num_setenv + 1, sizeof(*options->setenv));
                   1466:                        options->setenv[options->num_setenv++] = xstrdup(arg);
                   1467:                }
                   1468:                break;
                   1469:
1.132     djm      1470:        case oControlPath:
                   1471:                charptr = &options->control_path;
                   1472:                goto parse_string;
                   1473:
                   1474:        case oControlMaster:
                   1475:                intptr = &options->control_master;
1.207     djm      1476:                multistate_ptr = multistate_controlmaster;
                   1477:                goto parse_multistate;
1.132     djm      1478:
1.187     djm      1479:        case oControlPersist:
                   1480:                /* no/false/yes/true, or a time spec */
                   1481:                intptr = &options->control_persist;
                   1482:                arg = strdelim(&s);
                   1483:                if (!arg || *arg == '\0')
                   1484:                        fatal("%.200s line %d: Missing ControlPersist"
                   1485:                            " argument.", filename, linenum);
                   1486:                value = 0;
                   1487:                value2 = 0;     /* timeout */
                   1488:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                   1489:                        value = 0;
                   1490:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                   1491:                        value = 1;
                   1492:                else if ((value2 = convtime(arg)) >= 0)
                   1493:                        value = 1;
                   1494:                else
                   1495:                        fatal("%.200s line %d: Bad ControlPersist argument.",
                   1496:                            filename, linenum);
                   1497:                if (*activep && *intptr == -1) {
                   1498:                        *intptr = value;
                   1499:                        options->control_persist_timeout = value2;
                   1500:                }
                   1501:                break;
                   1502:
1.136     djm      1503:        case oHashKnownHosts:
                   1504:                intptr = &options->hash_known_hosts;
                   1505:                goto parse_flag;
                   1506:
1.144     reyk     1507:        case oTunnel:
                   1508:                intptr = &options->tun_open;
1.207     djm      1509:                multistate_ptr = multistate_tunnel;
                   1510:                goto parse_multistate;
1.144     reyk     1511:
                   1512:        case oTunnelDevice:
                   1513:                arg = strdelim(&s);
                   1514:                if (!arg || *arg == '\0')
                   1515:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1516:                value = a2tun(arg, &value2);
1.145     reyk     1517:                if (value == SSH_TUNID_ERR)
1.144     reyk     1518:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                   1519:                if (*activep) {
                   1520:                        options->tun_local = value;
                   1521:                        options->tun_remote = value2;
                   1522:                }
                   1523:                break;
                   1524:
                   1525:        case oLocalCommand:
                   1526:                charptr = &options->local_command;
                   1527:                goto parse_command;
                   1528:
                   1529:        case oPermitLocalCommand:
                   1530:                intptr = &options->permit_local_command;
                   1531:                goto parse_flag;
                   1532:
1.277     bluhm    1533:        case oRemoteCommand:
                   1534:                charptr = &options->remote_command;
                   1535:                goto parse_command;
                   1536:
1.167     grunk    1537:        case oVisualHostKey:
                   1538:                intptr = &options->visual_host_key;
                   1539:                goto parse_flag;
                   1540:
1.252     djm      1541:        case oInclude:
                   1542:                if (cmdline)
                   1543:                        fatal("Include directive not supported as a "
                   1544:                            "command-line option");
                   1545:                value = 0;
                   1546:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1547:                        /*
                   1548:                         * Ensure all paths are anchored. User configuration
                   1549:                         * files may begin with '~/' but system configurations
                   1550:                         * must not. If the path is relative, then treat it
                   1551:                         * as living in ~/.ssh for user configurations or
                   1552:                         * /etc/ssh for system ones.
                   1553:                         */
                   1554:                        if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
                   1555:                                fatal("%.200s line %d: bad include path %s.",
                   1556:                                    filename, linenum, arg);
1.301     djm      1557:                        if (!path_absolute(arg) && *arg != '~') {
1.252     djm      1558:                                xasprintf(&arg2, "%s/%s",
                   1559:                                    (flags & SSHCONF_USERCONF) ?
                   1560:                                    "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
                   1561:                        } else
                   1562:                                arg2 = xstrdup(arg);
                   1563:                        memset(&gl, 0, sizeof(gl));
                   1564:                        r = glob(arg2, GLOB_TILDE, NULL, &gl);
                   1565:                        if (r == GLOB_NOMATCH) {
                   1566:                                debug("%.200s line %d: include %s matched no "
                   1567:                                    "files",filename, linenum, arg2);
1.269     dtucker  1568:                                free(arg2);
1.252     djm      1569:                                continue;
1.313     deraadt  1570:                        } else if (r != 0)
1.252     djm      1571:                                fatal("%.200s line %d: glob failed for %s.",
                   1572:                                    filename, linenum, arg2);
                   1573:                        free(arg2);
                   1574:                        oactive = *activep;
1.313     deraadt  1575:                        for (i = 0; i < gl.gl_pathc; i++) {
1.252     djm      1576:                                debug3("%.200s line %d: Including file %s "
                   1577:                                    "depth %d%s", filename, linenum,
                   1578:                                    gl.gl_pathv[i], depth,
                   1579:                                    oactive ? "" : " (parse only)");
                   1580:                                r = read_config_file_depth(gl.gl_pathv[i],
                   1581:                                    pw, host, original_host, options,
                   1582:                                    flags | SSHCONF_CHECKPERM |
                   1583:                                    (oactive ? 0 : SSHCONF_NEVERMATCH),
1.302     djm      1584:                                    activep, want_final_pass, depth + 1);
1.264     djm      1585:                                if (r != 1 && errno != ENOENT) {
1.263     djm      1586:                                        fatal("Can't open user config file "
                   1587:                                            "%.100s: %.100s", gl.gl_pathv[i],
                   1588:                                            strerror(errno));
                   1589:                                }
1.252     djm      1590:                                /*
                   1591:                                 * don't let Match in includes clobber the
                   1592:                                 * containing file's Match state.
                   1593:                                 */
                   1594:                                *activep = oactive;
                   1595:                                if (r != 1)
                   1596:                                        value = -1;
                   1597:                        }
                   1598:                        globfree(&gl);
                   1599:                }
                   1600:                if (value != 0)
                   1601:                        return value;
                   1602:                break;
                   1603:
1.190     djm      1604:        case oIPQoS:
                   1605:                arg = strdelim(&s);
                   1606:                if ((value = parse_ipqos(arg)) == -1)
                   1607:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1608:                            filename, linenum, arg);
                   1609:                arg = strdelim(&s);
                   1610:                if (arg == NULL)
                   1611:                        value2 = value;
                   1612:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1613:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1614:                            filename, linenum, arg);
                   1615:                if (*activep) {
                   1616:                        options->ip_qos_interactive = value;
                   1617:                        options->ip_qos_bulk = value2;
                   1618:                }
                   1619:                break;
                   1620:
1.192     djm      1621:        case oRequestTTY:
                   1622:                intptr = &options->request_tty;
1.207     djm      1623:                multistate_ptr = multistate_requesttty;
                   1624:                goto parse_multistate;
1.192     djm      1625:
1.199     djm      1626:        case oIgnoreUnknown:
                   1627:                charptr = &options->ignored_unknown;
                   1628:                goto parse_string;
                   1629:
1.205     djm      1630:        case oProxyUseFdpass:
                   1631:                intptr = &options->proxy_use_fdpass;
                   1632:                goto parse_flag;
                   1633:
1.208     djm      1634:        case oCanonicalDomains:
                   1635:                value = options->num_canonical_domains != 0;
                   1636:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.280     millert  1637:                        if (!valid_domain(arg, 1, &errstr)) {
                   1638:                                fatal("%s line %d: %s", filename, linenum,
                   1639:                                    errstr);
                   1640:                        }
1.208     djm      1641:                        if (!*activep || value)
                   1642:                                continue;
                   1643:                        if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
                   1644:                                fatal("%s line %d: too many hostname suffixes.",
                   1645:                                    filename, linenum);
                   1646:                        options->canonical_domains[
                   1647:                            options->num_canonical_domains++] = xstrdup(arg);
                   1648:                }
                   1649:                break;
                   1650:
1.209     djm      1651:        case oCanonicalizePermittedCNAMEs:
1.208     djm      1652:                value = options->num_permitted_cnames != 0;
                   1653:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1654:                        /* Either '*' for everything or 'list:list' */
                   1655:                        if (strcmp(arg, "*") == 0)
                   1656:                                arg2 = arg;
                   1657:                        else {
                   1658:                                lowercase(arg);
                   1659:                                if ((arg2 = strchr(arg, ':')) == NULL ||
                   1660:                                    arg2[1] == '\0') {
                   1661:                                        fatal("%s line %d: "
                   1662:                                            "Invalid permitted CNAME \"%s\"",
                   1663:                                            filename, linenum, arg);
                   1664:                                }
                   1665:                                *arg2 = '\0';
                   1666:                                arg2++;
                   1667:                        }
                   1668:                        if (!*activep || value)
                   1669:                                continue;
                   1670:                        if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
                   1671:                                fatal("%s line %d: too many permitted CNAMEs.",
                   1672:                                    filename, linenum);
                   1673:                        cname = options->permitted_cnames +
                   1674:                            options->num_permitted_cnames++;
                   1675:                        cname->source_list = xstrdup(arg);
                   1676:                        cname->target_list = xstrdup(arg2);
                   1677:                }
                   1678:                break;
                   1679:
1.209     djm      1680:        case oCanonicalizeHostname:
                   1681:                intptr = &options->canonicalize_hostname;
                   1682:                multistate_ptr = multistate_canonicalizehostname;
1.208     djm      1683:                goto parse_multistate;
                   1684:
1.209     djm      1685:        case oCanonicalizeMaxDots:
                   1686:                intptr = &options->canonicalize_max_dots;
1.208     djm      1687:                goto parse_int;
                   1688:
1.209     djm      1689:        case oCanonicalizeFallbackLocal:
                   1690:                intptr = &options->canonicalize_fallback_local;
1.208     djm      1691:                goto parse_flag;
                   1692:
1.220     millert  1693:        case oStreamLocalBindMask:
                   1694:                arg = strdelim(&s);
                   1695:                if (!arg || *arg == '\0')
                   1696:                        fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
                   1697:                /* Parse mode in octal format */
                   1698:                value = strtol(arg, &endofnumber, 8);
                   1699:                if (arg == endofnumber || value < 0 || value > 0777)
                   1700:                        fatal("%.200s line %d: Bad mask.", filename, linenum);
                   1701:                options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
                   1702:                break;
                   1703:
                   1704:        case oStreamLocalBindUnlink:
                   1705:                intptr = &options->fwd_opts.streamlocal_bind_unlink;
                   1706:                goto parse_flag;
                   1707:
1.223     djm      1708:        case oRevokedHostKeys:
                   1709:                charptr = &options->revoked_host_keys;
                   1710:                goto parse_string;
                   1711:
1.224     djm      1712:        case oFingerprintHash:
1.225     djm      1713:                intptr = &options->fingerprint_hash;
1.224     djm      1714:                arg = strdelim(&s);
                   1715:                if (!arg || *arg == '\0')
                   1716:                        fatal("%.200s line %d: Missing argument.",
                   1717:                            filename, linenum);
                   1718:                if ((value = ssh_digest_alg_by_name(arg)) == -1)
                   1719:                        fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
                   1720:                            filename, linenum, arg);
1.225     djm      1721:                if (*activep && *intptr == -1)
                   1722:                        *intptr = value;
1.224     djm      1723:                break;
                   1724:
1.229     djm      1725:        case oUpdateHostkeys:
                   1726:                intptr = &options->update_hostkeys;
1.232     djm      1727:                multistate_ptr = multistate_yesnoask;
                   1728:                goto parse_multistate;
1.229     djm      1729:
1.230     djm      1730:        case oHostbasedKeyTypes:
                   1731:                charptr = &options->hostbased_key_types;
1.238     markus   1732:                goto parse_keytypes;
                   1733:
                   1734:        case oPubkeyAcceptedKeyTypes:
                   1735:                charptr = &options->pubkey_key_types;
                   1736:                goto parse_keytypes;
1.230     djm      1737:
1.246     jcs      1738:        case oAddKeysToAgent:
                   1739:                intptr = &options->add_keys_to_agent;
                   1740:                multistate_ptr = multistate_yesnoaskconfirm;
                   1741:                goto parse_multistate;
                   1742:
1.253     markus   1743:        case oIdentityAgent:
                   1744:                charptr = &options->identity_agent;
1.299     djm      1745:                arg = strdelim(&s);
                   1746:                if (!arg || *arg == '\0')
                   1747:                        fatal("%.200s line %d: Missing argument.",
                   1748:                            filename, linenum);
1.319   ! djm      1749:   parse_agent_path:
1.299     djm      1750:                /* Extra validation if the string represents an env var. */
                   1751:                if (arg[0] == '$' && !valid_env_name(arg + 1)) {
                   1752:                        fatal("%.200s line %d: Invalid environment name %s.",
                   1753:                            filename, linenum, arg);
                   1754:                }
                   1755:                if (*activep && *charptr == NULL)
                   1756:                        *charptr = xstrdup(arg);
                   1757:                break;
1.253     markus   1758:
1.96      markus   1759:        case oDeprecated:
1.98      markus   1760:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   1761:                    filename, linenum, keyword);
1.98      markus   1762:                return 0;
1.96      markus   1763:
1.110     jakob    1764:        case oUnsupported:
                   1765:                error("%s line %d: Unsupported option \"%s\"",
                   1766:                    filename, linenum, keyword);
                   1767:                return 0;
                   1768:
1.17      markus   1769:        default:
1.221     djm      1770:                fatal("%s: Unimplemented opcode %d", __func__, opcode);
1.17      markus   1771:        }
                   1772:
                   1773:        /* Check that there is no garbage at end of line. */
1.57      djm      1774:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho       1775:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm      1776:                    filename, linenum, arg);
1.39      ho       1777:        }
1.17      markus   1778:        return 0;
1.1       deraadt  1779: }
                   1780:
1.19      markus   1781: /*
                   1782:  * Reads the config file and modifies the options accordingly.  Options
                   1783:  * should already be initialized before this call.  This never returns if
1.89      stevesk  1784:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   1785:  */
1.89      stevesk  1786: int
1.206     djm      1787: read_config_file(const char *filename, struct passwd *pw, const char *host,
1.302     djm      1788:     const char *original_host, Options *options, int flags,
                   1789:     int *want_final_pass)
1.1       deraadt  1790: {
1.252     djm      1791:        int active = 1;
                   1792:
                   1793:        return read_config_file_depth(filename, pw, host, original_host,
1.302     djm      1794:            options, flags, &active, want_final_pass, 0);
1.252     djm      1795: }
                   1796:
                   1797: #define READCONF_MAX_DEPTH     16
                   1798: static int
                   1799: read_config_file_depth(const char *filename, struct passwd *pw,
                   1800:     const char *host, const char *original_host, Options *options,
1.302     djm      1801:     int flags, int *activep, int *want_final_pass, int depth)
1.252     djm      1802: {
1.17      markus   1803:        FILE *f;
1.289     markus   1804:        char *line = NULL;
                   1805:        size_t linesize = 0;
1.252     djm      1806:        int linenum;
1.17      markus   1807:        int bad_options = 0;
                   1808:
1.252     djm      1809:        if (depth < 0 || depth > READCONF_MAX_DEPTH)
                   1810:                fatal("Too many recursive configuration includes");
                   1811:
1.129     djm      1812:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  1813:                return 0;
1.129     djm      1814:
1.196     dtucker  1815:        if (flags & SSHCONF_CHECKPERM) {
1.129     djm      1816:                struct stat sb;
1.134     deraadt  1817:
1.131     dtucker  1818:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      1819:                        fatal("fstat %s: %s", filename, strerror(errno));
                   1820:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  1821:                    (sb.st_mode & 022) != 0))
1.129     djm      1822:                        fatal("Bad owner or permissions on %s", filename);
                   1823:        }
1.17      markus   1824:
                   1825:        debug("Reading configuration data %.200s", filename);
                   1826:
1.19      markus   1827:        /*
                   1828:         * Mark that we are now processing the options.  This flag is turned
                   1829:         * on/off by Host specifications.
                   1830:         */
1.17      markus   1831:        linenum = 0;
1.289     markus   1832:        while (getline(&line, &linesize, f) != -1) {
1.17      markus   1833:                /* Update line number counter. */
                   1834:                linenum++;
1.252     djm      1835:                if (process_config_line_depth(options, pw, host, original_host,
1.302     djm      1836:                    line, filename, linenum, activep, flags, want_final_pass,
                   1837:                    depth) != 0)
1.17      markus   1838:                        bad_options++;
                   1839:        }
1.289     markus   1840:        free(line);
1.17      markus   1841:        fclose(f);
                   1842:        if (bad_options > 0)
1.64      millert  1843:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  1844:                    filename, bad_options);
1.89      stevesk  1845:        return 1;
1.1       deraadt  1846: }
                   1847:
1.218     djm      1848: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                   1849: int
                   1850: option_clear_or_none(const char *o)
                   1851: {
                   1852:        return o == NULL || strcasecmp(o, "none") == 0;
                   1853: }
                   1854:
1.19      markus   1855: /*
                   1856:  * Initializes options to special values that indicate that they have not yet
                   1857:  * been set.  Read_config_file will only set options with this value. Options
                   1858:  * are processed in the following order: command line, user config file,
                   1859:  * system config file.  Last, fill_default_options is called.
                   1860:  */
1.1       deraadt  1861:
1.26      markus   1862: void
1.17      markus   1863: initialize_options(Options * options)
1.1       deraadt  1864: {
1.17      markus   1865:        memset(options, 'X', sizeof(*options));
                   1866:        options->forward_agent = -1;
1.319   ! djm      1867:        options->forward_agent_sock_path = NULL;
1.17      markus   1868:        options->forward_x11 = -1;
1.123     markus   1869:        options->forward_x11_trusted = -1;
1.186     djm      1870:        options->forward_x11_timeout = -1;
1.255     dtucker  1871:        options->stdio_forward_host = NULL;
                   1872:        options->stdio_forward_port = 0;
1.256     dtucker  1873:        options->clear_forwardings = -1;
1.153     markus   1874:        options->exit_on_forward_failure = -1;
1.34      markus   1875:        options->xauth_location = NULL;
1.220     millert  1876:        options->fwd_opts.gateway_ports = -1;
                   1877:        options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
                   1878:        options->fwd_opts.streamlocal_bind_unlink = -1;
1.50      markus   1879:        options->pubkey_authentication = -1;
1.78      markus   1880:        options->challenge_response_authentication = -1;
1.118     markus   1881:        options->gss_authentication = -1;
                   1882:        options->gss_deleg_creds = -1;
1.17      markus   1883:        options->password_authentication = -1;
1.48      markus   1884:        options->kbd_interactive_authentication = -1;
                   1885:        options->kbd_interactive_devices = NULL;
1.72      markus   1886:        options->hostbased_authentication = -1;
1.17      markus   1887:        options->batch_mode = -1;
                   1888:        options->check_host_ip = -1;
                   1889:        options->strict_host_key_checking = -1;
                   1890:        options->compression = -1;
1.126     markus   1891:        options->tcp_keep_alive = -1;
1.17      markus   1892:        options->port = -1;
1.114     djm      1893:        options->address_family = -1;
1.17      markus   1894:        options->connection_attempts = -1;
1.111     djm      1895:        options->connection_timeout = -1;
1.17      markus   1896:        options->number_of_password_prompts = -1;
1.25      markus   1897:        options->ciphers = NULL;
1.62      markus   1898:        options->macs = NULL;
1.189     djm      1899:        options->kex_algorithms = NULL;
1.76      markus   1900:        options->hostkeyalgorithms = NULL;
1.298     djm      1901:        options->ca_sign_algorithms = NULL;
1.17      markus   1902:        options->num_identity_files = 0;
1.241     djm      1903:        options->num_certificate_files = 0;
1.17      markus   1904:        options->hostname = NULL;
1.52      markus   1905:        options->host_key_alias = NULL;
1.17      markus   1906:        options->proxy_command = NULL;
1.257     djm      1907:        options->jump_user = NULL;
                   1908:        options->jump_host = NULL;
                   1909:        options->jump_port = -1;
                   1910:        options->jump_extra = NULL;
1.17      markus   1911:        options->user = NULL;
                   1912:        options->escape_char = -1;
1.193     djm      1913:        options->num_system_hostfiles = 0;
                   1914:        options->num_user_hostfiles = 0;
1.185     djm      1915:        options->local_forwards = NULL;
1.17      markus   1916:        options->num_local_forwards = 0;
1.185     djm      1917:        options->remote_forwards = NULL;
1.17      markus   1918:        options->num_remote_forwards = 0;
1.271     dtucker  1919:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
1.95      markus   1920:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1921:        options->preferred_authentications = NULL;
1.77      markus   1922:        options->bind_address = NULL;
1.282     djm      1923:        options->bind_interface = NULL;
1.183     markus   1924:        options->pkcs11_provider = NULL;
1.310     djm      1925:        options->sk_provider = NULL;
1.101     markus   1926:        options->enable_ssh_keysign = - 1;
1.91      markus   1927:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1928:        options->identities_only = - 1;
1.105     markus   1929:        options->rekey_limit = - 1;
1.198     dtucker  1930:        options->rekey_interval = -1;
1.107     jakob    1931:        options->verify_host_key_dns = -1;
1.127     markus   1932:        options->server_alive_interval = -1;
                   1933:        options->server_alive_count_max = -1;
1.290     djm      1934:        options->send_env = NULL;
1.130     djm      1935:        options->num_send_env = 0;
1.290     djm      1936:        options->setenv = NULL;
                   1937:        options->num_setenv = 0;
1.132     djm      1938:        options->control_path = NULL;
                   1939:        options->control_master = -1;
1.187     djm      1940:        options->control_persist = -1;
                   1941:        options->control_persist_timeout = 0;
1.136     djm      1942:        options->hash_known_hosts = -1;
1.144     reyk     1943:        options->tun_open = -1;
                   1944:        options->tun_local = -1;
                   1945:        options->tun_remote = -1;
                   1946:        options->local_command = NULL;
                   1947:        options->permit_local_command = -1;
1.277     bluhm    1948:        options->remote_command = NULL;
1.246     jcs      1949:        options->add_keys_to_agent = -1;
1.253     markus   1950:        options->identity_agent = NULL;
1.167     grunk    1951:        options->visual_host_key = -1;
1.190     djm      1952:        options->ip_qos_interactive = -1;
                   1953:        options->ip_qos_bulk = -1;
1.192     djm      1954:        options->request_tty = -1;
1.205     djm      1955:        options->proxy_use_fdpass = -1;
1.199     djm      1956:        options->ignored_unknown = NULL;
1.208     djm      1957:        options->num_canonical_domains = 0;
                   1958:        options->num_permitted_cnames = 0;
1.209     djm      1959:        options->canonicalize_max_dots = -1;
                   1960:        options->canonicalize_fallback_local = -1;
                   1961:        options->canonicalize_hostname = -1;
1.223     djm      1962:        options->revoked_host_keys = NULL;
1.224     djm      1963:        options->fingerprint_hash = -1;
1.229     djm      1964:        options->update_hostkeys = -1;
1.230     djm      1965:        options->hostbased_key_types = NULL;
1.238     markus   1966:        options->pubkey_key_types = NULL;
1.1       deraadt  1967: }
                   1968:
1.19      markus   1969: /*
1.218     djm      1970:  * A petite version of fill_default_options() that just fills the options
                   1971:  * needed for hostname canonicalization to proceed.
                   1972:  */
                   1973: void
                   1974: fill_default_options_for_canonicalization(Options *options)
                   1975: {
                   1976:        if (options->canonicalize_max_dots == -1)
                   1977:                options->canonicalize_max_dots = 1;
                   1978:        if (options->canonicalize_fallback_local == -1)
                   1979:                options->canonicalize_fallback_local = 1;
                   1980:        if (options->canonicalize_hostname == -1)
                   1981:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
                   1982: }
                   1983:
                   1984: /*
1.19      markus   1985:  * Called after processing other sources of option data, this fills those
                   1986:  * options for which no value has been specified with their default values.
                   1987:  */
1.26      markus   1988: void
1.17      markus   1989: fill_default_options(Options * options)
1.1       deraadt  1990: {
1.298     djm      1991:        char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
1.297     djm      1992:        int r;
1.292     djm      1993:
1.17      markus   1994:        if (options->forward_agent == -1)
1.33      markus   1995:                options->forward_agent = 0;
1.17      markus   1996:        if (options->forward_x11 == -1)
1.23      markus   1997:                options->forward_x11 = 0;
1.123     markus   1998:        if (options->forward_x11_trusted == -1)
                   1999:                options->forward_x11_trusted = 0;
1.186     djm      2000:        if (options->forward_x11_timeout == -1)
                   2001:                options->forward_x11_timeout = 1200;
1.256     dtucker  2002:        /*
                   2003:         * stdio forwarding (-W) changes the default for these but we defer
                   2004:         * setting the values so they can be overridden.
                   2005:         */
1.153     markus   2006:        if (options->exit_on_forward_failure == -1)
1.256     dtucker  2007:                options->exit_on_forward_failure =
                   2008:                    options->stdio_forward_host != NULL ? 1 : 0;
                   2009:        if (options->clear_forwardings == -1)
                   2010:                options->clear_forwardings =
                   2011:                    options->stdio_forward_host != NULL ? 1 : 0;
                   2012:        if (options->clear_forwardings == 1)
                   2013:                clear_forwardings(options);
                   2014:
1.34      markus   2015:        if (options->xauth_location == NULL)
1.80      markus   2016:                options->xauth_location = _PATH_XAUTH;
1.220     millert  2017:        if (options->fwd_opts.gateway_ports == -1)
                   2018:                options->fwd_opts.gateway_ports = 0;
                   2019:        if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
                   2020:                options->fwd_opts.streamlocal_bind_mask = 0177;
                   2021:        if (options->fwd_opts.streamlocal_bind_unlink == -1)
                   2022:                options->fwd_opts.streamlocal_bind_unlink = 0;
1.50      markus   2023:        if (options->pubkey_authentication == -1)
                   2024:                options->pubkey_authentication = 1;
1.78      markus   2025:        if (options->challenge_response_authentication == -1)
1.83      markus   2026:                options->challenge_response_authentication = 1;
1.118     markus   2027:        if (options->gss_authentication == -1)
1.122     markus   2028:                options->gss_authentication = 0;
1.118     markus   2029:        if (options->gss_deleg_creds == -1)
                   2030:                options->gss_deleg_creds = 0;
1.17      markus   2031:        if (options->password_authentication == -1)
                   2032:                options->password_authentication = 1;
1.48      markus   2033:        if (options->kbd_interactive_authentication == -1)
1.59      markus   2034:                options->kbd_interactive_authentication = 1;
1.72      markus   2035:        if (options->hostbased_authentication == -1)
                   2036:                options->hostbased_authentication = 0;
1.17      markus   2037:        if (options->batch_mode == -1)
                   2038:                options->batch_mode = 0;
                   2039:        if (options->check_host_ip == -1)
                   2040:                options->check_host_ip = 1;
                   2041:        if (options->strict_host_key_checking == -1)
1.278     djm      2042:                options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
1.17      markus   2043:        if (options->compression == -1)
                   2044:                options->compression = 0;
1.126     markus   2045:        if (options->tcp_keep_alive == -1)
                   2046:                options->tcp_keep_alive = 1;
1.17      markus   2047:        if (options->port == -1)
                   2048:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      2049:        if (options->address_family == -1)
                   2050:                options->address_family = AF_UNSPEC;
1.17      markus   2051:        if (options->connection_attempts == -1)
1.84      markus   2052:                options->connection_attempts = 1;
1.17      markus   2053:        if (options->number_of_password_prompts == -1)
                   2054:                options->number_of_password_prompts = 3;
1.76      markus   2055:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.246     jcs      2056:        if (options->add_keys_to_agent == -1)
                   2057:                options->add_keys_to_agent = 0;
1.17      markus   2058:        if (options->num_identity_files == 0) {
1.273     djm      2059:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
                   2060:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
                   2061:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
                   2062:                add_identity_file(options, "~/",
1.310     djm      2063:                    _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
                   2064:                add_identity_file(options, "~/",
1.273     djm      2065:                    _PATH_SSH_CLIENT_ID_ED25519, 0);
1.311     markus   2066:                add_identity_file(options, "~/",
                   2067:                    _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
1.283     markus   2068:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
1.27      markus   2069:        }
1.17      markus   2070:        if (options->escape_char == -1)
                   2071:                options->escape_char = '~';
1.193     djm      2072:        if (options->num_system_hostfiles == 0) {
                   2073:                options->system_hostfiles[options->num_system_hostfiles++] =
                   2074:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
                   2075:                options->system_hostfiles[options->num_system_hostfiles++] =
                   2076:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
                   2077:        }
                   2078:        if (options->num_user_hostfiles == 0) {
                   2079:                options->user_hostfiles[options->num_user_hostfiles++] =
                   2080:                    xstrdup(_PATH_SSH_USER_HOSTFILE);
                   2081:                options->user_hostfiles[options->num_user_hostfiles++] =
                   2082:                    xstrdup(_PATH_SSH_USER_HOSTFILE2);
                   2083:        }
1.95      markus   2084:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   2085:                options->log_level = SYSLOG_LEVEL_INFO;
1.271     dtucker  2086:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
                   2087:                options->log_facility = SYSLOG_FACILITY_USER;
1.91      markus   2088:        if (options->no_host_authentication_for_localhost == - 1)
                   2089:                options->no_host_authentication_for_localhost = 0;
1.128     markus   2090:        if (options->identities_only == -1)
                   2091:                options->identities_only = 0;
1.101     markus   2092:        if (options->enable_ssh_keysign == -1)
                   2093:                options->enable_ssh_keysign = 0;
1.105     markus   2094:        if (options->rekey_limit == -1)
                   2095:                options->rekey_limit = 0;
1.198     dtucker  2096:        if (options->rekey_interval == -1)
                   2097:                options->rekey_interval = 0;
1.107     jakob    2098:        if (options->verify_host_key_dns == -1)
                   2099:                options->verify_host_key_dns = 0;
1.127     markus   2100:        if (options->server_alive_interval == -1)
                   2101:                options->server_alive_interval = 0;
                   2102:        if (options->server_alive_count_max == -1)
                   2103:                options->server_alive_count_max = 3;
1.132     djm      2104:        if (options->control_master == -1)
                   2105:                options->control_master = 0;
1.187     djm      2106:        if (options->control_persist == -1) {
                   2107:                options->control_persist = 0;
                   2108:                options->control_persist_timeout = 0;
                   2109:        }
1.136     djm      2110:        if (options->hash_known_hosts == -1)
                   2111:                options->hash_known_hosts = 0;
1.144     reyk     2112:        if (options->tun_open == -1)
1.145     reyk     2113:                options->tun_open = SSH_TUNMODE_NO;
                   2114:        if (options->tun_local == -1)
                   2115:                options->tun_local = SSH_TUNID_ANY;
                   2116:        if (options->tun_remote == -1)
                   2117:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     2118:        if (options->permit_local_command == -1)
                   2119:                options->permit_local_command = 0;
1.167     grunk    2120:        if (options->visual_host_key == -1)
                   2121:                options->visual_host_key = 0;
1.190     djm      2122:        if (options->ip_qos_interactive == -1)
1.284     job      2123:                options->ip_qos_interactive = IPTOS_DSCP_AF21;
1.190     djm      2124:        if (options->ip_qos_bulk == -1)
1.284     job      2125:                options->ip_qos_bulk = IPTOS_DSCP_CS1;
1.192     djm      2126:        if (options->request_tty == -1)
                   2127:                options->request_tty = REQUEST_TTY_AUTO;
1.205     djm      2128:        if (options->proxy_use_fdpass == -1)
                   2129:                options->proxy_use_fdpass = 0;
1.209     djm      2130:        if (options->canonicalize_max_dots == -1)
                   2131:                options->canonicalize_max_dots = 1;
                   2132:        if (options->canonicalize_fallback_local == -1)
                   2133:                options->canonicalize_fallback_local = 1;
                   2134:        if (options->canonicalize_hostname == -1)
                   2135:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.224     djm      2136:        if (options->fingerprint_hash == -1)
                   2137:                options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.229     djm      2138:        if (options->update_hostkeys == -1)
1.231     djm      2139:                options->update_hostkeys = 0;
1.310     djm      2140:        if (options->sk_provider == NULL)
1.314     djm      2141:                options->sk_provider = xstrdup("internal");
1.292     djm      2142:
                   2143:        /* Expand KEX name lists */
                   2144:        all_cipher = cipher_alg_list(',', 0);
                   2145:        all_mac = mac_alg_list(',');
                   2146:        all_kex = kex_alg_list(',');
                   2147:        all_key = sshkey_alg_list(0, 0, 1, ',');
1.298     djm      2148:        all_sig = sshkey_alg_list(0, 1, 1, ',');
1.297     djm      2149: #define ASSEMBLE(what, defaults, all) \
                   2150:        do { \
                   2151:                if ((r = kex_assemble_names(&options->what, \
                   2152:                    defaults, all)) != 0) \
                   2153:                        fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
                   2154:        } while (0)
1.303     djm      2155:        ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
                   2156:        ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
                   2157:        ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
1.297     djm      2158:        ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
1.315     djm      2159:        ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
1.298     djm      2160:        ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
1.297     djm      2161: #undef ASSEMBLE
1.292     djm      2162:        free(all_cipher);
                   2163:        free(all_mac);
                   2164:        free(all_kex);
                   2165:        free(all_key);
1.298     djm      2166:        free(all_sig);
1.224     djm      2167:
1.207     djm      2168: #define CLEAR_ON_NONE(v) \
                   2169:        do { \
1.218     djm      2170:                if (option_clear_or_none(v)) { \
1.207     djm      2171:                        free(v); \
                   2172:                        v = NULL; \
                   2173:                } \
                   2174:        } while(0)
                   2175:        CLEAR_ON_NONE(options->local_command);
1.277     bluhm    2176:        CLEAR_ON_NONE(options->remote_command);
1.207     djm      2177:        CLEAR_ON_NONE(options->proxy_command);
                   2178:        CLEAR_ON_NONE(options->control_path);
1.223     djm      2179:        CLEAR_ON_NONE(options->revoked_host_keys);
1.304     djm      2180:        CLEAR_ON_NONE(options->pkcs11_provider);
1.310     djm      2181:        CLEAR_ON_NONE(options->sk_provider);
1.287     djm      2182:        if (options->jump_host != NULL &&
                   2183:            strcmp(options->jump_host, "none") == 0 &&
                   2184:            options->jump_port == 0 && options->jump_user == NULL) {
                   2185:                free(options->jump_host);
                   2186:                options->jump_host = NULL;
                   2187:        }
1.254     markus   2188:        /* options->identity_agent distinguishes NULL from 'none' */
1.17      markus   2189:        /* options->user will be set in the main program if appropriate */
                   2190:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   2191:        /* options->host_key_alias should not be set by default */
1.67      markus   2192:        /* options->preferred_authentications will be set in ssh */
1.135     djm      2193: }
                   2194:
1.220     millert  2195: struct fwdarg {
                   2196:        char *arg;
                   2197:        int ispath;
                   2198: };
                   2199:
                   2200: /*
                   2201:  * parse_fwd_field
                   2202:  * parses the next field in a port forwarding specification.
                   2203:  * sets fwd to the parsed field and advances p past the colon
                   2204:  * or sets it to NULL at end of string.
                   2205:  * returns 0 on success, else non-zero.
                   2206:  */
                   2207: static int
                   2208: parse_fwd_field(char **p, struct fwdarg *fwd)
                   2209: {
                   2210:        char *ep, *cp = *p;
                   2211:        int ispath = 0;
                   2212:
                   2213:        if (*cp == '\0') {
                   2214:                *p = NULL;
                   2215:                return -1;      /* end of string */
                   2216:        }
                   2217:
                   2218:        /*
                   2219:         * A field escaped with square brackets is used literally.
                   2220:         * XXX - allow ']' to be escaped via backslash?
                   2221:         */
                   2222:        if (*cp == '[') {
                   2223:                /* find matching ']' */
                   2224:                for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
                   2225:                        if (*ep == '/')
                   2226:                                ispath = 1;
                   2227:                }
                   2228:                /* no matching ']' or not at end of field. */
                   2229:                if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
                   2230:                        return -1;
                   2231:                /* NUL terminate the field and advance p past the colon */
                   2232:                *ep++ = '\0';
                   2233:                if (*ep != '\0')
                   2234:                        *ep++ = '\0';
                   2235:                fwd->arg = cp + 1;
                   2236:                fwd->ispath = ispath;
                   2237:                *p = ep;
                   2238:                return 0;
                   2239:        }
                   2240:
                   2241:        for (cp = *p; *cp != '\0'; cp++) {
                   2242:                switch (*cp) {
                   2243:                case '\\':
                   2244:                        memmove(cp, cp + 1, strlen(cp + 1) + 1);
1.237     djm      2245:                        if (*cp == '\0')
                   2246:                                return -1;
1.220     millert  2247:                        break;
                   2248:                case '/':
                   2249:                        ispath = 1;
                   2250:                        break;
                   2251:                case ':':
                   2252:                        *cp++ = '\0';
                   2253:                        goto done;
                   2254:                }
                   2255:        }
                   2256: done:
                   2257:        fwd->arg = *p;
                   2258:        fwd->ispath = ispath;
                   2259:        *p = cp;
                   2260:        return 0;
                   2261: }
                   2262:
1.135     djm      2263: /*
                   2264:  * parse_forward
                   2265:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  2266:  *   dynamicfwd == 0
1.220     millert  2267:  *     [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
                   2268:  *     listenpath:connectpath
1.168     stevesk  2269:  *   dynamicfwd == 1
                   2270:  *     [listenhost:]listenport
1.135     djm      2271:  * returns number of arguments parsed or zero on error
                   2272:  */
                   2273: int
1.220     millert  2274: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      2275: {
1.220     millert  2276:        struct fwdarg fwdargs[4];
                   2277:        char *p, *cp;
1.135     djm      2278:        int i;
                   2279:
1.220     millert  2280:        memset(fwd, 0, sizeof(*fwd));
                   2281:        memset(fwdargs, 0, sizeof(fwdargs));
1.135     djm      2282:
                   2283:        cp = p = xstrdup(fwdspec);
                   2284:
                   2285:        /* skip leading spaces */
1.214     deraadt  2286:        while (isspace((u_char)*cp))
1.135     djm      2287:                cp++;
                   2288:
1.220     millert  2289:        for (i = 0; i < 4; ++i) {
                   2290:                if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135     djm      2291:                        break;
1.220     millert  2292:        }
1.135     djm      2293:
1.170     stevesk  2294:        /* Check for trailing garbage */
1.220     millert  2295:        if (cp != NULL && *cp != '\0') {
1.135     djm      2296:                i = 0;  /* failure */
1.220     millert  2297:        }
1.135     djm      2298:
                   2299:        switch (i) {
1.168     stevesk  2300:        case 1:
1.220     millert  2301:                if (fwdargs[0].ispath) {
                   2302:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2303:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2304:                } else {
                   2305:                        fwd->listen_host = NULL;
                   2306:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2307:                }
1.168     stevesk  2308:                fwd->connect_host = xstrdup("socks");
                   2309:                break;
                   2310:
                   2311:        case 2:
1.220     millert  2312:                if (fwdargs[0].ispath && fwdargs[1].ispath) {
                   2313:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2314:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2315:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2316:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2317:                } else if (fwdargs[1].ispath) {
                   2318:                        fwd->listen_host = NULL;
                   2319:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2320:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2321:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2322:                } else {
                   2323:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2324:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   2325:                        fwd->connect_host = xstrdup("socks");
                   2326:                }
1.168     stevesk  2327:                break;
                   2328:
1.135     djm      2329:        case 3:
1.220     millert  2330:                if (fwdargs[0].ispath) {
                   2331:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2332:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2333:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   2334:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   2335:                } else if (fwdargs[2].ispath) {
                   2336:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2337:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   2338:                        fwd->connect_path = xstrdup(fwdargs[2].arg);
                   2339:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2340:                } else {
                   2341:                        fwd->listen_host = NULL;
                   2342:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2343:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   2344:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   2345:                }
1.135     djm      2346:                break;
                   2347:
                   2348:        case 4:
1.220     millert  2349:                fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2350:                fwd->listen_port = a2port(fwdargs[1].arg);
                   2351:                fwd->connect_host = xstrdup(fwdargs[2].arg);
                   2352:                fwd->connect_port = a2port(fwdargs[3].arg);
1.135     djm      2353:                break;
                   2354:        default:
                   2355:                i = 0; /* failure */
                   2356:        }
                   2357:
1.202     djm      2358:        free(p);
1.135     djm      2359:
1.168     stevesk  2360:        if (dynamicfwd) {
                   2361:                if (!(i == 1 || i == 2))
                   2362:                        goto fail_free;
                   2363:        } else {
1.220     millert  2364:                if (!(i == 3 || i == 4)) {
                   2365:                        if (fwd->connect_path == NULL &&
                   2366:                            fwd->listen_path == NULL)
                   2367:                                goto fail_free;
                   2368:                }
                   2369:                if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168     stevesk  2370:                        goto fail_free;
                   2371:        }
                   2372:
1.220     millert  2373:        if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
                   2374:            (!remotefwd && fwd->listen_port == 0))
1.135     djm      2375:                goto fail_free;
                   2376:        if (fwd->connect_host != NULL &&
                   2377:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   2378:                goto fail_free;
1.220     millert  2379:        /* XXX - if connecting to a remote socket, max sun len may not match this host */
                   2380:        if (fwd->connect_path != NULL &&
                   2381:            strlen(fwd->connect_path) >= PATH_MAX_SUN)
                   2382:                goto fail_free;
1.176     djm      2383:        if (fwd->listen_host != NULL &&
                   2384:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   2385:                goto fail_free;
1.220     millert  2386:        if (fwd->listen_path != NULL &&
                   2387:            strlen(fwd->listen_path) >= PATH_MAX_SUN)
                   2388:                goto fail_free;
1.135     djm      2389:
                   2390:        return (i);
                   2391:
                   2392:  fail_free:
1.202     djm      2393:        free(fwd->connect_host);
                   2394:        fwd->connect_host = NULL;
1.220     millert  2395:        free(fwd->connect_path);
                   2396:        fwd->connect_path = NULL;
1.202     djm      2397:        free(fwd->listen_host);
                   2398:        fwd->listen_host = NULL;
1.220     millert  2399:        free(fwd->listen_path);
                   2400:        fwd->listen_path = NULL;
1.135     djm      2401:        return (0);
1.221     djm      2402: }
                   2403:
1.257     djm      2404: int
                   2405: parse_jump(const char *s, Options *o, int active)
                   2406: {
                   2407:        char *orig, *sdup, *cp;
                   2408:        char *host = NULL, *user = NULL;
1.258     naddy    2409:        int ret = -1, port = -1, first;
1.257     djm      2410:
                   2411:        active &= o->proxy_command == NULL && o->jump_host == NULL;
                   2412:
                   2413:        orig = sdup = xstrdup(s);
1.258     naddy    2414:        first = active;
1.259     djm      2415:        do {
1.287     djm      2416:                if (strcasecmp(s, "none") == 0)
                   2417:                        break;
1.259     djm      2418:                if ((cp = strrchr(sdup, ',')) == NULL)
                   2419:                        cp = sdup; /* last */
                   2420:                else
                   2421:                        *cp++ = '\0';
                   2422:
1.258     naddy    2423:                if (first) {
1.257     djm      2424:                        /* First argument and configuration is active */
1.280     millert  2425:                        if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
                   2426:                            parse_user_host_port(cp, &user, &host, &port) != 0)
1.257     djm      2427:                                goto out;
                   2428:                } else {
                   2429:                        /* Subsequent argument or inactive configuration */
1.280     millert  2430:                        if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
                   2431:                            parse_user_host_port(cp, NULL, NULL, NULL) != 0)
1.257     djm      2432:                                goto out;
                   2433:                }
1.258     naddy    2434:                first = 0; /* only check syntax for subsequent hosts */
1.259     djm      2435:        } while (cp != sdup);
1.257     djm      2436:        /* success */
1.258     naddy    2437:        if (active) {
1.287     djm      2438:                if (strcasecmp(s, "none") == 0) {
                   2439:                        o->jump_host = xstrdup("none");
                   2440:                        o->jump_port = 0;
                   2441:                } else {
                   2442:                        o->jump_user = user;
                   2443:                        o->jump_host = host;
                   2444:                        o->jump_port = port;
                   2445:                        o->proxy_command = xstrdup("none");
                   2446:                        user = host = NULL;
                   2447:                        if ((cp = strrchr(s, ',')) != NULL && cp != s) {
                   2448:                                o->jump_extra = xstrdup(s);
                   2449:                                o->jump_extra[cp - s] = '\0';
                   2450:                        }
1.259     djm      2451:                }
1.258     naddy    2452:        }
1.257     djm      2453:        ret = 0;
                   2454:  out:
1.258     naddy    2455:        free(orig);
1.257     djm      2456:        free(user);
                   2457:        free(host);
                   2458:        return ret;
1.280     millert  2459: }
                   2460:
                   2461: int
                   2462: parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
                   2463: {
                   2464:        char *path;
                   2465:        int r;
                   2466:
                   2467:        r = parse_uri("ssh", uri, userp, hostp, portp, &path);
                   2468:        if (r == 0 && path != NULL)
                   2469:                r = -1;         /* path not allowed */
                   2470:        return r;
1.257     djm      2471: }
                   2472:
1.221     djm      2473: /* XXX the following is a near-vebatim copy from servconf.c; refactor */
                   2474: static const char *
                   2475: fmt_multistate_int(int val, const struct multistate *m)
                   2476: {
                   2477:        u_int i;
                   2478:
                   2479:        for (i = 0; m[i].key != NULL; i++) {
                   2480:                if (m[i].value == val)
                   2481:                        return m[i].key;
                   2482:        }
                   2483:        return "UNKNOWN";
                   2484: }
                   2485:
                   2486: static const char *
                   2487: fmt_intarg(OpCodes code, int val)
                   2488: {
                   2489:        if (val == -1)
                   2490:                return "unset";
                   2491:        switch (code) {
                   2492:        case oAddressFamily:
                   2493:                return fmt_multistate_int(val, multistate_addressfamily);
                   2494:        case oVerifyHostKeyDNS:
1.232     djm      2495:        case oUpdateHostkeys:
1.221     djm      2496:                return fmt_multistate_int(val, multistate_yesnoask);
1.278     djm      2497:        case oStrictHostKeyChecking:
                   2498:                return fmt_multistate_int(val, multistate_strict_hostkey);
1.221     djm      2499:        case oControlMaster:
                   2500:                return fmt_multistate_int(val, multistate_controlmaster);
                   2501:        case oTunnel:
                   2502:                return fmt_multistate_int(val, multistate_tunnel);
                   2503:        case oRequestTTY:
                   2504:                return fmt_multistate_int(val, multistate_requesttty);
                   2505:        case oCanonicalizeHostname:
                   2506:                return fmt_multistate_int(val, multistate_canonicalizehostname);
1.285     djm      2507:        case oAddKeysToAgent:
                   2508:                return fmt_multistate_int(val, multistate_yesnoaskconfirm);
1.224     djm      2509:        case oFingerprintHash:
                   2510:                return ssh_digest_alg_name(val);
1.221     djm      2511:        default:
                   2512:                switch (val) {
                   2513:                case 0:
                   2514:                        return "no";
                   2515:                case 1:
                   2516:                        return "yes";
                   2517:                default:
                   2518:                        return "UNKNOWN";
                   2519:                }
                   2520:        }
                   2521: }
                   2522:
                   2523: static const char *
                   2524: lookup_opcode_name(OpCodes code)
                   2525: {
                   2526:        u_int i;
                   2527:
                   2528:        for (i = 0; keywords[i].name != NULL; i++)
                   2529:                if (keywords[i].opcode == code)
                   2530:                        return(keywords[i].name);
                   2531:        return "UNKNOWN";
                   2532: }
                   2533:
                   2534: static void
                   2535: dump_cfg_int(OpCodes code, int val)
                   2536: {
                   2537:        printf("%s %d\n", lookup_opcode_name(code), val);
                   2538: }
                   2539:
                   2540: static void
                   2541: dump_cfg_fmtint(OpCodes code, int val)
                   2542: {
                   2543:        printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
                   2544: }
                   2545:
                   2546: static void
                   2547: dump_cfg_string(OpCodes code, const char *val)
                   2548: {
                   2549:        if (val == NULL)
                   2550:                return;
                   2551:        printf("%s %s\n", lookup_opcode_name(code), val);
                   2552: }
                   2553:
                   2554: static void
                   2555: dump_cfg_strarray(OpCodes code, u_int count, char **vals)
                   2556: {
                   2557:        u_int i;
                   2558:
                   2559:        for (i = 0; i < count; i++)
                   2560:                printf("%s %s\n", lookup_opcode_name(code), vals[i]);
                   2561: }
                   2562:
                   2563: static void
                   2564: dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
                   2565: {
                   2566:        u_int i;
                   2567:
                   2568:        printf("%s", lookup_opcode_name(code));
                   2569:        for (i = 0; i < count; i++)
                   2570:                printf(" %s",  vals[i]);
                   2571:        printf("\n");
                   2572: }
                   2573:
                   2574: static void
                   2575: dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
                   2576: {
                   2577:        const struct Forward *fwd;
                   2578:        u_int i;
                   2579:
                   2580:        /* oDynamicForward */
                   2581:        for (i = 0; i < count; i++) {
                   2582:                fwd = &fwds[i];
1.265     djm      2583:                if (code == oDynamicForward && fwd->connect_host != NULL &&
1.221     djm      2584:                    strcmp(fwd->connect_host, "socks") != 0)
                   2585:                        continue;
1.265     djm      2586:                if (code == oLocalForward && fwd->connect_host != NULL &&
1.221     djm      2587:                    strcmp(fwd->connect_host, "socks") == 0)
                   2588:                        continue;
                   2589:                printf("%s", lookup_opcode_name(code));
                   2590:                if (fwd->listen_port == PORT_STREAMLOCAL)
                   2591:                        printf(" %s", fwd->listen_path);
                   2592:                else if (fwd->listen_host == NULL)
                   2593:                        printf(" %d", fwd->listen_port);
                   2594:                else {
                   2595:                        printf(" [%s]:%d",
                   2596:                            fwd->listen_host, fwd->listen_port);
                   2597:                }
                   2598:                if (code != oDynamicForward) {
                   2599:                        if (fwd->connect_port == PORT_STREAMLOCAL)
                   2600:                                printf(" %s", fwd->connect_path);
                   2601:                        else if (fwd->connect_host == NULL)
                   2602:                                printf(" %d", fwd->connect_port);
                   2603:                        else {
                   2604:                                printf(" [%s]:%d",
                   2605:                                    fwd->connect_host, fwd->connect_port);
                   2606:                        }
                   2607:                }
                   2608:                printf("\n");
                   2609:        }
                   2610: }
                   2611:
                   2612: void
                   2613: dump_client_config(Options *o, const char *host)
                   2614: {
                   2615:        int i;
1.292     djm      2616:        char buf[8], *all_key;
1.221     djm      2617:
1.240     djm      2618:        /* This is normally prepared in ssh_kex2 */
1.292     djm      2619:        all_key = sshkey_alg_list(0, 0, 1, ',');
                   2620:        if (kex_assemble_names( &o->hostkeyalgorithms,
                   2621:            KEX_DEFAULT_PK_ALG, all_key) != 0)
1.240     djm      2622:                fatal("%s: kex_assemble_names failed", __func__);
1.292     djm      2623:        free(all_key);
1.240     djm      2624:
1.221     djm      2625:        /* Most interesting options first: user, host, port */
                   2626:        dump_cfg_string(oUser, o->user);
1.306     jmc      2627:        dump_cfg_string(oHostname, host);
1.221     djm      2628:        dump_cfg_int(oPort, o->port);
                   2629:
                   2630:        /* Flag options */
1.285     djm      2631:        dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
1.221     djm      2632:        dump_cfg_fmtint(oAddressFamily, o->address_family);
                   2633:        dump_cfg_fmtint(oBatchMode, o->batch_mode);
                   2634:        dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
                   2635:        dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
                   2636:        dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
                   2637:        dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
                   2638:        dump_cfg_fmtint(oCompression, o->compression);
                   2639:        dump_cfg_fmtint(oControlMaster, o->control_master);
                   2640:        dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
1.256     dtucker  2641:        dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
1.221     djm      2642:        dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
1.224     djm      2643:        dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
1.221     djm      2644:        dump_cfg_fmtint(oForwardX11, o->forward_x11);
                   2645:        dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
                   2646:        dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
                   2647: #ifdef GSSAPI
                   2648:        dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
                   2649:        dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
                   2650: #endif /* GSSAPI */
                   2651:        dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
                   2652:        dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
                   2653:        dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
                   2654:        dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
                   2655:        dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
                   2656:        dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
                   2657:        dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
                   2658:        dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
                   2659:        dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
                   2660:        dump_cfg_fmtint(oRequestTTY, o->request_tty);
                   2661:        dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
                   2662:        dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
                   2663:        dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
                   2664:        dump_cfg_fmtint(oTunnel, o->tun_open);
                   2665:        dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
                   2666:        dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
1.229     djm      2667:        dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
1.221     djm      2668:
                   2669:        /* Integer options */
                   2670:        dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
                   2671:        dump_cfg_int(oConnectionAttempts, o->connection_attempts);
                   2672:        dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
                   2673:        dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
                   2674:        dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
                   2675:        dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
                   2676:
                   2677:        /* String options */
                   2678:        dump_cfg_string(oBindAddress, o->bind_address);
1.282     djm      2679:        dump_cfg_string(oBindInterface, o->bind_interface);
1.221     djm      2680:        dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
                   2681:        dump_cfg_string(oControlPath, o->control_path);
1.240     djm      2682:        dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
1.221     djm      2683:        dump_cfg_string(oHostKeyAlias, o->host_key_alias);
1.230     djm      2684:        dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
1.253     markus   2685:        dump_cfg_string(oIdentityAgent, o->identity_agent);
1.285     djm      2686:        dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
1.221     djm      2687:        dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
                   2688:        dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
1.298     djm      2689:        dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
1.221     djm      2690:        dump_cfg_string(oLocalCommand, o->local_command);
1.277     bluhm    2691:        dump_cfg_string(oRemoteCommand, o->remote_command);
1.221     djm      2692:        dump_cfg_string(oLogLevel, log_level_name(o->log_level));
                   2693:        dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
1.266     djm      2694: #ifdef ENABLE_PKCS11
1.221     djm      2695:        dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
1.266     djm      2696: #endif
1.310     djm      2697:        dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
1.221     djm      2698:        dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
1.242     djm      2699:        dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
1.230     djm      2700:        dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
1.221     djm      2701:        dump_cfg_string(oXAuthLocation, o->xauth_location);
                   2702:
1.230     djm      2703:        /* Forwards */
1.221     djm      2704:        dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
                   2705:        dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
                   2706:        dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
                   2707:
                   2708:        /* String array options */
                   2709:        dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
                   2710:        dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
1.285     djm      2711:        dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
1.221     djm      2712:        dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
                   2713:        dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
                   2714:        dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
1.290     djm      2715:        dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
1.221     djm      2716:
                   2717:        /* Special cases */
1.319   ! djm      2718:
        !          2719:        /* oForwardAgent */
        !          2720:        if (o->forward_agent_sock_path == NULL)
        !          2721:                dump_cfg_fmtint(oForwardAgent, o->forward_agent);
        !          2722:        else
        !          2723:                dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
1.221     djm      2724:
                   2725:        /* oConnectTimeout */
                   2726:        if (o->connection_timeout == -1)
                   2727:                printf("connecttimeout none\n");
                   2728:        else
                   2729:                dump_cfg_int(oConnectTimeout, o->connection_timeout);
                   2730:
                   2731:        /* oTunnelDevice */
                   2732:        printf("tunneldevice");
                   2733:        if (o->tun_local == SSH_TUNID_ANY)
                   2734:                printf(" any");
                   2735:        else
                   2736:                printf(" %d", o->tun_local);
                   2737:        if (o->tun_remote == SSH_TUNID_ANY)
                   2738:                printf(":any");
                   2739:        else
                   2740:                printf(":%d", o->tun_remote);
                   2741:        printf("\n");
                   2742:
                   2743:        /* oCanonicalizePermittedCNAMEs */
                   2744:        if ( o->num_permitted_cnames > 0) {
                   2745:                printf("canonicalizePermittedcnames");
                   2746:                for (i = 0; i < o->num_permitted_cnames; i++) {
                   2747:                        printf(" %s:%s", o->permitted_cnames[i].source_list,
                   2748:                            o->permitted_cnames[i].target_list);
                   2749:                }
                   2750:                printf("\n");
                   2751:        }
                   2752:
                   2753:        /* oControlPersist */
                   2754:        if (o->control_persist == 0 || o->control_persist_timeout == 0)
                   2755:                dump_cfg_fmtint(oControlPersist, o->control_persist);
                   2756:        else
                   2757:                dump_cfg_int(oControlPersist, o->control_persist_timeout);
                   2758:
                   2759:        /* oEscapeChar */
                   2760:        if (o->escape_char == SSH_ESCAPECHAR_NONE)
                   2761:                printf("escapechar none\n");
                   2762:        else {
1.257     djm      2763:                vis(buf, o->escape_char, VIS_WHITE, 0);
                   2764:                printf("escapechar %s\n", buf);
1.221     djm      2765:        }
                   2766:
                   2767:        /* oIPQoS */
                   2768:        printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
                   2769:        printf("%s\n", iptos2str(o->ip_qos_bulk));
                   2770:
                   2771:        /* oRekeyLimit */
1.249     dtucker  2772:        printf("rekeylimit %llu %d\n",
                   2773:            (unsigned long long)o->rekey_limit, o->rekey_interval);
1.221     djm      2774:
                   2775:        /* oStreamLocalBindMask */
                   2776:        printf("streamlocalbindmask 0%o\n",
                   2777:            o->fwd_opts.streamlocal_bind_mask);
1.285     djm      2778:
                   2779:        /* oLogFacility */
                   2780:        printf("syslogfacility %s\n", log_facility_name(o->log_facility));
1.257     djm      2781:
                   2782:        /* oProxyCommand / oProxyJump */
                   2783:        if (o->jump_host == NULL)
                   2784:                dump_cfg_string(oProxyCommand, o->proxy_command);
                   2785:        else {
                   2786:                /* Check for numeric addresses */
                   2787:                i = strchr(o->jump_host, ':') != NULL ||
                   2788:                    strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
                   2789:                snprintf(buf, sizeof(buf), "%d", o->jump_port);
                   2790:                printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
1.259     djm      2791:                    /* optional additional jump spec */
                   2792:                    o->jump_extra == NULL ? "" : o->jump_extra,
                   2793:                    o->jump_extra == NULL ? "" : ",",
1.257     djm      2794:                    /* optional user */
                   2795:                    o->jump_user == NULL ? "" : o->jump_user,
                   2796:                    o->jump_user == NULL ? "" : "@",
                   2797:                    /* opening [ if hostname is numeric */
                   2798:                    i ? "[" : "",
                   2799:                    /* mandatory hostname */
                   2800:                    o->jump_host,
                   2801:                    /* closing ] if hostname is numeric */
                   2802:                    i ? "]" : "",
                   2803:                    /* optional port number */
                   2804:                    o->jump_port <= 0 ? "" : ":",
1.259     djm      2805:                    o->jump_port <= 0 ? "" : buf);
1.257     djm      2806:        }
1.1       deraadt  2807: }