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

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