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

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