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

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