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

1.283   ! markus      1: /* $OpenBSD: readconf.c,v 1.282 2018/02/23 02:34:33 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.19      markus    672: /*
1.70      stevesk   673:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    674:  */
1.26      markus    675: static OpCodes
1.199     djm       676: parse_token(const char *cp, const char *filename, int linenum,
                    677:     const char *ignored_unknown)
1.1       deraadt   678: {
1.199     djm       679:        int i;
1.1       deraadt   680:
1.17      markus    681:        for (i = 0; keywords[i].name; i++)
1.199     djm       682:                if (strcmp(cp, keywords[i].name) == 0)
1.17      markus    683:                        return keywords[i].opcode;
1.235     djm       684:        if (ignored_unknown != NULL &&
                    685:            match_pattern_list(cp, ignored_unknown, 1) == 1)
1.199     djm       686:                return oIgnoredUnknownOption;
1.75      stevesk   687:        error("%s: line %d: Bad configuration option: %s",
                    688:            filename, linenum, cp);
1.17      markus    689:        return oBadOption;
1.1       deraadt   690: }
                    691:
1.207     djm       692: /* Multistate option parsing */
                    693: struct multistate {
                    694:        char *key;
                    695:        int value;
                    696: };
                    697: static const struct multistate multistate_flag[] = {
                    698:        { "true",                       1 },
                    699:        { "false",                      0 },
                    700:        { "yes",                        1 },
                    701:        { "no",                         0 },
                    702:        { NULL, -1 }
                    703: };
                    704: static const struct multistate multistate_yesnoask[] = {
                    705:        { "true",                       1 },
                    706:        { "false",                      0 },
                    707:        { "yes",                        1 },
                    708:        { "no",                         0 },
                    709:        { "ask",                        2 },
                    710:        { NULL, -1 }
                    711: };
1.278     djm       712: static const struct multistate multistate_strict_hostkey[] = {
                    713:        { "true",                       SSH_STRICT_HOSTKEY_YES },
                    714:        { "false",                      SSH_STRICT_HOSTKEY_OFF },
                    715:        { "yes",                        SSH_STRICT_HOSTKEY_YES },
                    716:        { "no",                         SSH_STRICT_HOSTKEY_OFF },
                    717:        { "ask",                        SSH_STRICT_HOSTKEY_ASK },
                    718:        { "off",                        SSH_STRICT_HOSTKEY_OFF },
                    719:        { "accept-new",                 SSH_STRICT_HOSTKEY_NEW },
                    720:        { NULL, -1 }
                    721: };
1.246     jcs       722: static const struct multistate multistate_yesnoaskconfirm[] = {
                    723:        { "true",                       1 },
                    724:        { "false",                      0 },
                    725:        { "yes",                        1 },
                    726:        { "no",                         0 },
                    727:        { "ask",                        2 },
                    728:        { "confirm",                    3 },
                    729:        { NULL, -1 }
                    730: };
1.207     djm       731: static const struct multistate multistate_addressfamily[] = {
                    732:        { "inet",                       AF_INET },
                    733:        { "inet6",                      AF_INET6 },
                    734:        { "any",                        AF_UNSPEC },
                    735:        { NULL, -1 }
                    736: };
                    737: static const struct multistate multistate_controlmaster[] = {
                    738:        { "true",                       SSHCTL_MASTER_YES },
                    739:        { "yes",                        SSHCTL_MASTER_YES },
                    740:        { "false",                      SSHCTL_MASTER_NO },
                    741:        { "no",                         SSHCTL_MASTER_NO },
                    742:        { "auto",                       SSHCTL_MASTER_AUTO },
                    743:        { "ask",                        SSHCTL_MASTER_ASK },
                    744:        { "autoask",                    SSHCTL_MASTER_AUTO_ASK },
                    745:        { NULL, -1 }
                    746: };
                    747: static const struct multistate multistate_tunnel[] = {
                    748:        { "ethernet",                   SSH_TUNMODE_ETHERNET },
                    749:        { "point-to-point",             SSH_TUNMODE_POINTOPOINT },
                    750:        { "true",                       SSH_TUNMODE_DEFAULT },
                    751:        { "yes",                        SSH_TUNMODE_DEFAULT },
                    752:        { "false",                      SSH_TUNMODE_NO },
                    753:        { "no",                         SSH_TUNMODE_NO },
                    754:        { NULL, -1 }
                    755: };
                    756: static const struct multistate multistate_requesttty[] = {
                    757:        { "true",                       REQUEST_TTY_YES },
                    758:        { "yes",                        REQUEST_TTY_YES },
                    759:        { "false",                      REQUEST_TTY_NO },
                    760:        { "no",                         REQUEST_TTY_NO },
                    761:        { "force",                      REQUEST_TTY_FORCE },
                    762:        { "auto",                       REQUEST_TTY_AUTO },
                    763:        { NULL, -1 }
                    764: };
1.209     djm       765: static const struct multistate multistate_canonicalizehostname[] = {
1.208     djm       766:        { "true",                       SSH_CANONICALISE_YES },
                    767:        { "false",                      SSH_CANONICALISE_NO },
                    768:        { "yes",                        SSH_CANONICALISE_YES },
                    769:        { "no",                         SSH_CANONICALISE_NO },
                    770:        { "always",                     SSH_CANONICALISE_ALWAYS },
                    771:        { NULL, -1 }
                    772: };
1.207     djm       773:
1.19      markus    774: /*
                    775:  * Processes a single option line as used in the configuration files. This
                    776:  * only sets those values that have not already been set.
                    777:  */
1.14      markus    778: int
1.206     djm       779: process_config_line(Options *options, struct passwd *pw, const char *host,
1.221     djm       780:     const char *original_host, char *line, const char *filename,
                    781:     int linenum, int *activep, int flags)
1.1       deraadt   782: {
1.252     djm       783:        return process_config_line_depth(options, pw, host, original_host,
                    784:            line, filename, linenum, activep, flags, 0);
                    785: }
                    786:
                    787: #define WHITESPACE " \t\r\n"
                    788: static int
                    789: process_config_line_depth(Options *options, struct passwd *pw, const char *host,
                    790:     const char *original_host, char *line, const char *filename,
                    791:     int linenum, int *activep, int flags, int depth)
                    792: {
1.193     djm       793:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
                    794:        char **cpptr, fwdarg[256];
1.199     djm       795:        u_int i, *uintptr, max_entries = 0;
1.252     djm       796:        int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
1.279     markus    797:        int remotefwd, dynamicfwd;
1.164     dtucker   798:        LogLevel *log_level_ptr;
1.271     dtucker   799:        SyslogFacility *log_facility_ptr;
1.201     dtucker   800:        long long val64;
1.102     markus    801:        size_t len;
1.220     millert   802:        struct Forward fwd;
1.207     djm       803:        const struct multistate *multistate_ptr;
1.208     djm       804:        struct allowed_cname *cname;
1.252     djm       805:        glob_t gl;
1.281     dtucker   806:        const char *errstr;
1.106     djm       807:
1.206     djm       808:        if (activep == NULL) { /* We are processing a command line directive */
                    809:                cmdline = 1;
                    810:                activep = &cmdline;
                    811:        }
                    812:
1.267     djm       813:        /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1.233     djm       814:        if ((len = strlen(line)) == 0)
                    815:                return 0;
                    816:        for (len--; len > 0; len--) {
1.267     djm       817:                if (strchr(WHITESPACE "\f", line[len]) == NULL)
1.106     djm       818:                        break;
                    819:                line[len] = '\0';
                    820:        }
1.1       deraadt   821:
1.42      provos    822:        s = line;
                    823:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       824:        if ((keyword = strdelim(&s)) == NULL)
                    825:                return 0;
1.42      provos    826:        /* Ignore leading whitespace. */
                    827:        if (*keyword == '\0')
1.43      markus    828:                keyword = strdelim(&s);
1.56      deraadt   829:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    830:                return 0;
1.199     djm       831:        /* Match lowercase keyword */
1.207     djm       832:        lowercase(keyword);
1.17      markus    833:
1.199     djm       834:        opcode = parse_token(keyword, filename, linenum,
                    835:            options->ignored_unknown);
1.17      markus    836:
                    837:        switch (opcode) {
                    838:        case oBadOption:
1.19      markus    839:                /* don't panic, but count bad options */
                    840:                return -1;
1.273     djm       841:        case oIgnore:
                    842:                return 0;
1.199     djm       843:        case oIgnoredUnknownOption:
                    844:                debug("%s line %d: Ignored unknown option \"%s\"",
                    845:                    filename, linenum, keyword);
                    846:                return 0;
1.111     djm       847:        case oConnectTimeout:
                    848:                intptr = &options->connection_timeout;
1.127     markus    849: parse_time:
1.111     djm       850:                arg = strdelim(&s);
                    851:                if (!arg || *arg == '\0')
                    852:                        fatal("%s line %d: missing time value.",
                    853:                            filename, linenum);
1.221     djm       854:                if (strcmp(arg, "none") == 0)
                    855:                        value = -1;
                    856:                else if ((value = convtime(arg)) == -1)
1.111     djm       857:                        fatal("%s line %d: invalid time value.",
                    858:                            filename, linenum);
1.160     dtucker   859:                if (*activep && *intptr == -1)
1.111     djm       860:                        *intptr = value;
                    861:                break;
                    862:
1.17      markus    863:        case oForwardAgent:
                    864:                intptr = &options->forward_agent;
1.207     djm       865:  parse_flag:
                    866:                multistate_ptr = multistate_flag;
                    867:  parse_multistate:
1.42      provos    868:                arg = strdelim(&s);
1.40      ho        869:                if (!arg || *arg == '\0')
1.207     djm       870:                        fatal("%s line %d: missing argument.",
                    871:                            filename, linenum);
                    872:                value = -1;
                    873:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                    874:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                    875:                                value = multistate_ptr[i].value;
                    876:                                break;
                    877:                        }
                    878:                }
                    879:                if (value == -1)
                    880:                        fatal("%s line %d: unsupported option \"%s\".",
                    881:                            filename, linenum, arg);
1.17      markus    882:                if (*activep && *intptr == -1)
                    883:                        *intptr = value;
                    884:                break;
                    885:
                    886:        case oForwardX11:
                    887:                intptr = &options->forward_x11;
                    888:                goto parse_flag;
                    889:
1.123     markus    890:        case oForwardX11Trusted:
                    891:                intptr = &options->forward_x11_trusted;
                    892:                goto parse_flag;
1.221     djm       893:
1.186     djm       894:        case oForwardX11Timeout:
                    895:                intptr = &options->forward_x11_timeout;
                    896:                goto parse_time;
1.123     markus    897:
1.17      markus    898:        case oGatewayPorts:
1.220     millert   899:                intptr = &options->fwd_opts.gateway_ports;
1.17      markus    900:                goto parse_flag;
                    901:
1.153     markus    902:        case oExitOnForwardFailure:
                    903:                intptr = &options->exit_on_forward_failure;
                    904:                goto parse_flag;
                    905:
1.17      markus    906:        case oUsePrivilegedPort:
                    907:                intptr = &options->use_privileged_port;
                    908:                goto parse_flag;
                    909:
                    910:        case oPasswordAuthentication:
                    911:                intptr = &options->password_authentication;
                    912:                goto parse_flag;
                    913:
1.48      markus    914:        case oKbdInteractiveAuthentication:
                    915:                intptr = &options->kbd_interactive_authentication;
                    916:                goto parse_flag;
                    917:
                    918:        case oKbdInteractiveDevices:
                    919:                charptr = &options->kbd_interactive_devices;
                    920:                goto parse_string;
                    921:
1.50      markus    922:        case oPubkeyAuthentication:
                    923:                intptr = &options->pubkey_authentication;
1.30      markus    924:                goto parse_flag;
                    925:
1.72      markus    926:        case oHostbasedAuthentication:
                    927:                intptr = &options->hostbased_authentication;
                    928:                goto parse_flag;
                    929:
1.59      markus    930:        case oChallengeResponseAuthentication:
1.78      markus    931:                intptr = &options->challenge_response_authentication;
1.17      markus    932:                goto parse_flag;
1.108     jakob     933:
1.118     markus    934:        case oGssAuthentication:
                    935:                intptr = &options->gss_authentication;
                    936:                goto parse_flag;
                    937:
                    938:        case oGssDelegateCreds:
                    939:                intptr = &options->gss_deleg_creds;
                    940:                goto parse_flag;
                    941:
1.17      markus    942:        case oBatchMode:
                    943:                intptr = &options->batch_mode;
                    944:                goto parse_flag;
                    945:
                    946:        case oCheckHostIP:
                    947:                intptr = &options->check_host_ip;
1.167     grunk     948:                goto parse_flag;
1.17      markus    949:
1.107     jakob     950:        case oVerifyHostKeyDNS:
                    951:                intptr = &options->verify_host_key_dns;
1.207     djm       952:                multistate_ptr = multistate_yesnoask;
                    953:                goto parse_multistate;
1.107     jakob     954:
1.17      markus    955:        case oStrictHostKeyChecking:
                    956:                intptr = &options->strict_host_key_checking;
1.278     djm       957:                multistate_ptr = multistate_strict_hostkey;
1.207     djm       958:                goto parse_multistate;
1.17      markus    959:
                    960:        case oCompression:
                    961:                intptr = &options->compression;
                    962:                goto parse_flag;
                    963:
1.126     markus    964:        case oTCPKeepAlive:
                    965:                intptr = &options->tcp_keep_alive;
1.17      markus    966:                goto parse_flag;
                    967:
1.91      markus    968:        case oNoHostAuthenticationForLocalhost:
                    969:                intptr = &options->no_host_authentication_for_localhost;
                    970:                goto parse_flag;
                    971:
1.17      markus    972:        case oNumberOfPasswordPrompts:
                    973:                intptr = &options->number_of_password_prompts;
                    974:                goto parse_int;
                    975:
1.105     markus    976:        case oRekeyLimit:
                    977:                arg = strdelim(&s);
                    978:                if (!arg || *arg == '\0')
1.198     dtucker   979:                        fatal("%.200s line %d: Missing argument.", filename,
                    980:                            linenum);
                    981:                if (strcmp(arg, "default") == 0) {
                    982:                        val64 = 0;
                    983:                } else {
1.200     dtucker   984:                        if (scan_scaled(arg, &val64) == -1)
                    985:                                fatal("%.200s line %d: Bad number '%s': %s",
                    986:                                    filename, linenum, arg, strerror(errno));
1.198     dtucker   987:                        if (val64 != 0 && val64 < 16)
                    988:                                fatal("%.200s line %d: RekeyLimit too small",
                    989:                                    filename, linenum);
1.105     markus    990:                }
1.165     djm       991:                if (*activep && options->rekey_limit == -1)
1.249     dtucker   992:                        options->rekey_limit = val64;
1.198     dtucker   993:                if (s != NULL) { /* optional rekey interval present */
                    994:                        if (strcmp(s, "none") == 0) {
                    995:                                (void)strdelim(&s);     /* discard */
                    996:                                break;
                    997:                        }
                    998:                        intptr = &options->rekey_interval;
                    999:                        goto parse_time;
                   1000:                }
1.105     markus   1001:                break;
                   1002:
1.17      markus   1003:        case oIdentityFile:
1.42      provos   1004:                arg = strdelim(&s);
1.40      ho       1005:                if (!arg || *arg == '\0')
1.17      markus   1006:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1007:                if (*activep) {
1.50      markus   1008:                        intptr = &options->num_identity_files;
1.27      markus   1009:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus   1010:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt  1011:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.221     djm      1012:                        add_identity_file(options, NULL,
                   1013:                            arg, flags & SSHCONF_USERCONF);
1.17      markus   1014:                }
                   1015:                break;
                   1016:
1.241     djm      1017:        case oCertificateFile:
                   1018:                arg = strdelim(&s);
                   1019:                if (!arg || *arg == '\0')
                   1020:                        fatal("%.200s line %d: Missing argument.",
                   1021:                            filename, linenum);
                   1022:                if (*activep) {
                   1023:                        intptr = &options->num_certificate_files;
                   1024:                        if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
                   1025:                                fatal("%.200s line %d: Too many certificate "
                   1026:                                    "files specified (max %d).",
                   1027:                                    filename, linenum,
                   1028:                                    SSH_MAX_CERTIFICATE_FILES);
                   1029:                        }
                   1030:                        add_certificate_file(options, arg,
                   1031:                            flags & SSHCONF_USERCONF);
                   1032:                }
                   1033:                break;
                   1034:
1.34      markus   1035:        case oXAuthLocation:
                   1036:                charptr=&options->xauth_location;
                   1037:                goto parse_string;
                   1038:
1.17      markus   1039:        case oUser:
                   1040:                charptr = &options->user;
                   1041: parse_string:
1.42      provos   1042:                arg = strdelim(&s);
1.40      ho       1043:                if (!arg || *arg == '\0')
1.193     djm      1044:                        fatal("%.200s line %d: Missing argument.",
                   1045:                            filename, linenum);
1.17      markus   1046:                if (*activep && *charptr == NULL)
1.38      provos   1047:                        *charptr = xstrdup(arg);
1.17      markus   1048:                break;
                   1049:
                   1050:        case oGlobalKnownHostsFile:
1.193     djm      1051:                cpptr = (char **)&options->system_hostfiles;
                   1052:                uintptr = &options->num_system_hostfiles;
                   1053:                max_entries = SSH_MAX_HOSTS_FILES;
                   1054: parse_char_array:
                   1055:                if (*activep && *uintptr == 0) {
                   1056:                        while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1057:                                if ((*uintptr) >= max_entries)
                   1058:                                        fatal("%s line %d: "
                   1059:                                            "too many authorized keys files.",
                   1060:                                            filename, linenum);
                   1061:                                cpptr[(*uintptr)++] = xstrdup(arg);
                   1062:                        }
                   1063:                }
                   1064:                return 0;
1.17      markus   1065:
                   1066:        case oUserKnownHostsFile:
1.193     djm      1067:                cpptr = (char **)&options->user_hostfiles;
                   1068:                uintptr = &options->num_user_hostfiles;
                   1069:                max_entries = SSH_MAX_HOSTS_FILES;
                   1070:                goto parse_char_array;
1.27      markus   1071:
1.17      markus   1072:        case oHostName:
                   1073:                charptr = &options->hostname;
                   1074:                goto parse_string;
                   1075:
1.52      markus   1076:        case oHostKeyAlias:
                   1077:                charptr = &options->host_key_alias;
                   1078:                goto parse_string;
                   1079:
1.67      markus   1080:        case oPreferredAuthentications:
                   1081:                charptr = &options->preferred_authentications;
                   1082:                goto parse_string;
                   1083:
1.77      markus   1084:        case oBindAddress:
                   1085:                charptr = &options->bind_address;
                   1086:                goto parse_string;
                   1087:
1.282     djm      1088:        case oBindInterface:
                   1089:                charptr = &options->bind_interface;
                   1090:                goto parse_string;
                   1091:
1.183     markus   1092:        case oPKCS11Provider:
                   1093:                charptr = &options->pkcs11_provider;
1.86      markus   1094:                goto parse_string;
1.85      jakob    1095:
1.17      markus   1096:        case oProxyCommand:
1.144     reyk     1097:                charptr = &options->proxy_command;
1.257     djm      1098:                /* Ignore ProxyCommand if ProxyJump already specified */
                   1099:                if (options->jump_host != NULL)
                   1100:                        charptr = &options->jump_host; /* Skip below */
1.144     reyk     1101: parse_command:
1.113     markus   1102:                if (s == NULL)
                   1103:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus   1104:                len = strspn(s, WHITESPACE "=");
1.17      markus   1105:                if (*activep && *charptr == NULL)
1.102     markus   1106:                        *charptr = xstrdup(s + len);
1.17      markus   1107:                return 0;
                   1108:
1.257     djm      1109:        case oProxyJump:
                   1110:                if (s == NULL) {
                   1111:                        fatal("%.200s line %d: Missing argument.",
                   1112:                            filename, linenum);
                   1113:                }
                   1114:                len = strspn(s, WHITESPACE "=");
                   1115:                if (parse_jump(s + len, options, *activep) == -1) {
                   1116:                        fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
                   1117:                            filename, linenum, s + len);
                   1118:                }
                   1119:                return 0;
                   1120:
1.17      markus   1121:        case oPort:
                   1122:                intptr = &options->port;
                   1123: parse_int:
1.42      provos   1124:                arg = strdelim(&s);
1.281     dtucker  1125:                if ((errstr = atoi_err(arg, &value)) != NULL)
                   1126:                        fatal("%s line %d: integer value %s.",
                   1127:                            filename, linenum, errstr);
1.17      markus   1128:                if (*activep && *intptr == -1)
                   1129:                        *intptr = value;
                   1130:                break;
                   1131:
                   1132:        case oConnectionAttempts:
                   1133:                intptr = &options->connection_attempts;
                   1134:                goto parse_int;
                   1135:
1.25      markus   1136:        case oCiphers:
1.42      provos   1137:                arg = strdelim(&s);
1.40      ho       1138:                if (!arg || *arg == '\0')
1.32      markus   1139:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.268     djm      1140:                if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
1.31      markus   1141:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt  1142:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1143:                if (*activep && options->ciphers == NULL)
1.38      provos   1144:                        options->ciphers = xstrdup(arg);
1.25      markus   1145:                break;
                   1146:
1.62      markus   1147:        case oMacs:
                   1148:                arg = strdelim(&s);
                   1149:                if (!arg || *arg == '\0')
                   1150:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.268     djm      1151:                if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
1.62      markus   1152:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt  1153:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus   1154:                if (*activep && options->macs == NULL)
                   1155:                        options->macs = xstrdup(arg);
                   1156:                break;
                   1157:
1.189     djm      1158:        case oKexAlgorithms:
                   1159:                arg = strdelim(&s);
                   1160:                if (!arg || *arg == '\0')
                   1161:                        fatal("%.200s line %d: Missing argument.",
                   1162:                            filename, linenum);
1.268     djm      1163:                if (*arg != '-' &&
                   1164:                    !kex_names_valid(*arg == '+' ? arg + 1 : arg))
1.189     djm      1165:                        fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
                   1166:                            filename, linenum, arg ? arg : "<NONE>");
                   1167:                if (*activep && options->kex_algorithms == NULL)
                   1168:                        options->kex_algorithms = xstrdup(arg);
                   1169:                break;
                   1170:
1.76      markus   1171:        case oHostKeyAlgorithms:
1.238     markus   1172:                charptr = &options->hostkeyalgorithms;
                   1173: parse_keytypes:
1.76      markus   1174:                arg = strdelim(&s);
                   1175:                if (!arg || *arg == '\0')
1.238     markus   1176:                        fatal("%.200s line %d: Missing argument.",
                   1177:                            filename, linenum);
1.268     djm      1178:                if (*arg != '-' &&
                   1179:                    !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
1.238     markus   1180:                        fatal("%s line %d: Bad key types '%s'.",
                   1181:                                filename, linenum, arg ? arg : "<NONE>");
                   1182:                if (*activep && *charptr == NULL)
                   1183:                        *charptr = xstrdup(arg);
1.76      markus   1184:                break;
                   1185:
1.17      markus   1186:        case oLogLevel:
1.164     dtucker  1187:                log_level_ptr = &options->log_level;
1.42      provos   1188:                arg = strdelim(&s);
1.38      provos   1189:                value = log_level_number(arg);
1.95      markus   1190:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert  1191:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt  1192:                            filename, linenum, arg ? arg : "<NONE>");
1.164     dtucker  1193:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                   1194:                        *log_level_ptr = (LogLevel) value;
1.17      markus   1195:                break;
                   1196:
1.271     dtucker  1197:        case oLogFacility:
                   1198:                log_facility_ptr = &options->log_facility;
                   1199:                arg = strdelim(&s);
                   1200:                value = log_facility_number(arg);
                   1201:                if (value == SYSLOG_FACILITY_NOT_SET)
                   1202:                        fatal("%.200s line %d: unsupported log facility '%s'",
                   1203:                            filename, linenum, arg ? arg : "<NONE>");
                   1204:                if (*log_facility_ptr == -1)
                   1205:                        *log_facility_ptr = (SyslogFacility) value;
                   1206:                break;
                   1207:
1.88      stevesk  1208:        case oLocalForward:
1.17      markus   1209:        case oRemoteForward:
1.168     stevesk  1210:        case oDynamicForward:
1.42      provos   1211:                arg = strdelim(&s);
1.135     djm      1212:                if (arg == NULL || *arg == '\0')
1.88      stevesk  1213:                        fatal("%.200s line %d: Missing port argument.",
                   1214:                            filename, linenum);
1.135     djm      1215:
1.279     markus   1216:                remotefwd = (opcode == oRemoteForward);
                   1217:                dynamicfwd = (opcode == oDynamicForward);
                   1218:
                   1219:                if (!dynamicfwd) {
1.168     stevesk  1220:                        arg2 = strdelim(&s);
1.279     markus   1221:                        if (arg2 == NULL || *arg2 == '\0') {
                   1222:                                if (remotefwd)
                   1223:                                        dynamicfwd = 1;
                   1224:                                else
                   1225:                                        fatal("%.200s line %d: Missing target "
                   1226:                                            "argument.", filename, linenum);
                   1227:                        } else {
                   1228:                                /* construct a string for parse_forward */
                   1229:                                snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
                   1230:                                    arg2);
                   1231:                        }
                   1232:                }
                   1233:                if (dynamicfwd)
1.168     stevesk  1234:                        strlcpy(fwdarg, arg, sizeof(fwdarg));
                   1235:
1.279     markus   1236:                if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
1.88      stevesk  1237:                        fatal("%.200s line %d: Bad forwarding specification.",
                   1238:                            filename, linenum);
1.135     djm      1239:
1.88      stevesk  1240:                if (*activep) {
1.279     markus   1241:                        if (remotefwd) {
                   1242:                                add_remote_forward(options, &fwd);
                   1243:                        } else {
1.135     djm      1244:                                add_local_forward(options, &fwd);
1.279     markus   1245:                        }
1.88      stevesk  1246:                }
1.17      markus   1247:                break;
1.71      markus   1248:
1.90      stevesk  1249:        case oClearAllForwardings:
                   1250:                intptr = &options->clear_forwardings;
                   1251:                goto parse_flag;
                   1252:
1.17      markus   1253:        case oHost:
1.206     djm      1254:                if (cmdline)
                   1255:                        fatal("Host directive not supported as a command-line "
                   1256:                            "option");
1.17      markus   1257:                *activep = 0;
1.191     djm      1258:                arg2 = NULL;
                   1259:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.252     djm      1260:                        if ((flags & SSHCONF_NEVERMATCH) != 0)
                   1261:                                break;
1.191     djm      1262:                        negated = *arg == '!';
                   1263:                        if (negated)
                   1264:                                arg++;
1.38      provos   1265:                        if (match_pattern(host, arg)) {
1.191     djm      1266:                                if (negated) {
                   1267:                                        debug("%.200s line %d: Skipping Host "
                   1268:                                            "block because of negated match "
                   1269:                                            "for %.100s", filename, linenum,
                   1270:                                            arg);
                   1271:                                        *activep = 0;
                   1272:                                        break;
                   1273:                                }
                   1274:                                if (!*activep)
                   1275:                                        arg2 = arg; /* logged below */
1.17      markus   1276:                                *activep = 1;
                   1277:                        }
1.191     djm      1278:                }
                   1279:                if (*activep)
                   1280:                        debug("%.200s line %d: Applying options for %.100s",
                   1281:                            filename, linenum, arg2);
1.42      provos   1282:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus   1283:                return 0;
                   1284:
1.206     djm      1285:        case oMatch:
                   1286:                if (cmdline)
                   1287:                        fatal("Host directive not supported as a command-line "
                   1288:                            "option");
1.221     djm      1289:                value = match_cfg_line(options, &s, pw, host, original_host,
                   1290:                    flags & SSHCONF_POSTCANON, filename, linenum);
1.206     djm      1291:                if (value < 0)
                   1292:                        fatal("%.200s line %d: Bad Match condition", filename,
                   1293:                            linenum);
1.252     djm      1294:                *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
1.206     djm      1295:                break;
                   1296:
1.17      markus   1297:        case oEscapeChar:
                   1298:                intptr = &options->escape_char;
1.42      provos   1299:                arg = strdelim(&s);
1.40      ho       1300:                if (!arg || *arg == '\0')
1.17      markus   1301:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.236     djm      1302:                if (strcmp(arg, "none") == 0)
                   1303:                        value = SSH_ESCAPECHAR_NONE;
                   1304:                else if (arg[1] == '\0')
                   1305:                        value = (u_char) arg[0];
                   1306:                else if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus   1307:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                   1308:                        value = (u_char) arg[1] & 31;
1.17      markus   1309:                else {
                   1310:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt  1311:                            filename, linenum);
1.17      markus   1312:                        /* NOTREACHED */
                   1313:                        value = 0;      /* Avoid compiler warning. */
                   1314:                }
                   1315:                if (*activep && *intptr == -1)
                   1316:                        *intptr = value;
1.112     djm      1317:                break;
                   1318:
                   1319:        case oAddressFamily:
1.114     djm      1320:                intptr = &options->address_family;
1.207     djm      1321:                multistate_ptr = multistate_addressfamily;
                   1322:                goto parse_multistate;
1.17      markus   1323:
1.101     markus   1324:        case oEnableSSHKeysign:
                   1325:                intptr = &options->enable_ssh_keysign;
                   1326:                goto parse_flag;
                   1327:
1.128     markus   1328:        case oIdentitiesOnly:
                   1329:                intptr = &options->identities_only;
                   1330:                goto parse_flag;
                   1331:
1.127     markus   1332:        case oServerAliveInterval:
                   1333:                intptr = &options->server_alive_interval;
                   1334:                goto parse_time;
                   1335:
                   1336:        case oServerAliveCountMax:
                   1337:                intptr = &options->server_alive_count_max;
                   1338:                goto parse_int;
                   1339:
1.130     djm      1340:        case oSendEnv:
                   1341:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1342:                        if (strchr(arg, '=') != NULL)
                   1343:                                fatal("%s line %d: Invalid environment name.",
                   1344:                                    filename, linenum);
1.137     djm      1345:                        if (!*activep)
                   1346:                                continue;
1.130     djm      1347:                        if (options->num_send_env >= MAX_SEND_ENV)
                   1348:                                fatal("%s line %d: too many send env.",
                   1349:                                    filename, linenum);
                   1350:                        options->send_env[options->num_send_env++] =
                   1351:                            xstrdup(arg);
                   1352:                }
                   1353:                break;
                   1354:
1.132     djm      1355:        case oControlPath:
                   1356:                charptr = &options->control_path;
                   1357:                goto parse_string;
                   1358:
                   1359:        case oControlMaster:
                   1360:                intptr = &options->control_master;
1.207     djm      1361:                multistate_ptr = multistate_controlmaster;
                   1362:                goto parse_multistate;
1.132     djm      1363:
1.187     djm      1364:        case oControlPersist:
                   1365:                /* no/false/yes/true, or a time spec */
                   1366:                intptr = &options->control_persist;
                   1367:                arg = strdelim(&s);
                   1368:                if (!arg || *arg == '\0')
                   1369:                        fatal("%.200s line %d: Missing ControlPersist"
                   1370:                            " argument.", filename, linenum);
                   1371:                value = 0;
                   1372:                value2 = 0;     /* timeout */
                   1373:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                   1374:                        value = 0;
                   1375:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                   1376:                        value = 1;
                   1377:                else if ((value2 = convtime(arg)) >= 0)
                   1378:                        value = 1;
                   1379:                else
                   1380:                        fatal("%.200s line %d: Bad ControlPersist argument.",
                   1381:                            filename, linenum);
                   1382:                if (*activep && *intptr == -1) {
                   1383:                        *intptr = value;
                   1384:                        options->control_persist_timeout = value2;
                   1385:                }
                   1386:                break;
                   1387:
1.136     djm      1388:        case oHashKnownHosts:
                   1389:                intptr = &options->hash_known_hosts;
                   1390:                goto parse_flag;
                   1391:
1.144     reyk     1392:        case oTunnel:
                   1393:                intptr = &options->tun_open;
1.207     djm      1394:                multistate_ptr = multistate_tunnel;
                   1395:                goto parse_multistate;
1.144     reyk     1396:
                   1397:        case oTunnelDevice:
                   1398:                arg = strdelim(&s);
                   1399:                if (!arg || *arg == '\0')
                   1400:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1401:                value = a2tun(arg, &value2);
1.145     reyk     1402:                if (value == SSH_TUNID_ERR)
1.144     reyk     1403:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                   1404:                if (*activep) {
                   1405:                        options->tun_local = value;
                   1406:                        options->tun_remote = value2;
                   1407:                }
                   1408:                break;
                   1409:
                   1410:        case oLocalCommand:
                   1411:                charptr = &options->local_command;
                   1412:                goto parse_command;
                   1413:
                   1414:        case oPermitLocalCommand:
                   1415:                intptr = &options->permit_local_command;
                   1416:                goto parse_flag;
                   1417:
1.277     bluhm    1418:        case oRemoteCommand:
                   1419:                charptr = &options->remote_command;
                   1420:                goto parse_command;
                   1421:
1.167     grunk    1422:        case oVisualHostKey:
                   1423:                intptr = &options->visual_host_key;
                   1424:                goto parse_flag;
                   1425:
1.252     djm      1426:        case oInclude:
                   1427:                if (cmdline)
                   1428:                        fatal("Include directive not supported as a "
                   1429:                            "command-line option");
                   1430:                value = 0;
                   1431:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1432:                        /*
                   1433:                         * Ensure all paths are anchored. User configuration
                   1434:                         * files may begin with '~/' but system configurations
                   1435:                         * must not. If the path is relative, then treat it
                   1436:                         * as living in ~/.ssh for user configurations or
                   1437:                         * /etc/ssh for system ones.
                   1438:                         */
                   1439:                        if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
                   1440:                                fatal("%.200s line %d: bad include path %s.",
                   1441:                                    filename, linenum, arg);
                   1442:                        if (*arg != '/' && *arg != '~') {
                   1443:                                xasprintf(&arg2, "%s/%s",
                   1444:                                    (flags & SSHCONF_USERCONF) ?
                   1445:                                    "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
                   1446:                        } else
                   1447:                                arg2 = xstrdup(arg);
                   1448:                        memset(&gl, 0, sizeof(gl));
                   1449:                        r = glob(arg2, GLOB_TILDE, NULL, &gl);
                   1450:                        if (r == GLOB_NOMATCH) {
                   1451:                                debug("%.200s line %d: include %s matched no "
                   1452:                                    "files",filename, linenum, arg2);
1.269     dtucker  1453:                                free(arg2);
1.252     djm      1454:                                continue;
                   1455:                        } else if (r != 0 || gl.gl_pathc < 0)
                   1456:                                fatal("%.200s line %d: glob failed for %s.",
                   1457:                                    filename, linenum, arg2);
                   1458:                        free(arg2);
                   1459:                        oactive = *activep;
                   1460:                        for (i = 0; i < (u_int)gl.gl_pathc; i++) {
                   1461:                                debug3("%.200s line %d: Including file %s "
                   1462:                                    "depth %d%s", filename, linenum,
                   1463:                                    gl.gl_pathv[i], depth,
                   1464:                                    oactive ? "" : " (parse only)");
                   1465:                                r = read_config_file_depth(gl.gl_pathv[i],
                   1466:                                    pw, host, original_host, options,
                   1467:                                    flags | SSHCONF_CHECKPERM |
                   1468:                                    (oactive ? 0 : SSHCONF_NEVERMATCH),
                   1469:                                    activep, depth + 1);
1.264     djm      1470:                                if (r != 1 && errno != ENOENT) {
1.263     djm      1471:                                        fatal("Can't open user config file "
                   1472:                                            "%.100s: %.100s", gl.gl_pathv[i],
                   1473:                                            strerror(errno));
                   1474:                                }
1.252     djm      1475:                                /*
                   1476:                                 * don't let Match in includes clobber the
                   1477:                                 * containing file's Match state.
                   1478:                                 */
                   1479:                                *activep = oactive;
                   1480:                                if (r != 1)
                   1481:                                        value = -1;
                   1482:                        }
                   1483:                        globfree(&gl);
                   1484:                }
                   1485:                if (value != 0)
                   1486:                        return value;
                   1487:                break;
                   1488:
1.190     djm      1489:        case oIPQoS:
                   1490:                arg = strdelim(&s);
                   1491:                if ((value = parse_ipqos(arg)) == -1)
                   1492:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1493:                            filename, linenum, arg);
                   1494:                arg = strdelim(&s);
                   1495:                if (arg == NULL)
                   1496:                        value2 = value;
                   1497:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1498:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1499:                            filename, linenum, arg);
                   1500:                if (*activep) {
                   1501:                        options->ip_qos_interactive = value;
                   1502:                        options->ip_qos_bulk = value2;
                   1503:                }
                   1504:                break;
                   1505:
1.192     djm      1506:        case oRequestTTY:
                   1507:                intptr = &options->request_tty;
1.207     djm      1508:                multistate_ptr = multistate_requesttty;
                   1509:                goto parse_multistate;
1.192     djm      1510:
1.199     djm      1511:        case oIgnoreUnknown:
                   1512:                charptr = &options->ignored_unknown;
                   1513:                goto parse_string;
                   1514:
1.205     djm      1515:        case oProxyUseFdpass:
                   1516:                intptr = &options->proxy_use_fdpass;
                   1517:                goto parse_flag;
                   1518:
1.208     djm      1519:        case oCanonicalDomains:
                   1520:                value = options->num_canonical_domains != 0;
                   1521:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.280     millert  1522:                        if (!valid_domain(arg, 1, &errstr)) {
                   1523:                                fatal("%s line %d: %s", filename, linenum,
                   1524:                                    errstr);
                   1525:                        }
1.208     djm      1526:                        if (!*activep || value)
                   1527:                                continue;
                   1528:                        if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
                   1529:                                fatal("%s line %d: too many hostname suffixes.",
                   1530:                                    filename, linenum);
                   1531:                        options->canonical_domains[
                   1532:                            options->num_canonical_domains++] = xstrdup(arg);
                   1533:                }
                   1534:                break;
                   1535:
1.209     djm      1536:        case oCanonicalizePermittedCNAMEs:
1.208     djm      1537:                value = options->num_permitted_cnames != 0;
                   1538:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1539:                        /* Either '*' for everything or 'list:list' */
                   1540:                        if (strcmp(arg, "*") == 0)
                   1541:                                arg2 = arg;
                   1542:                        else {
                   1543:                                lowercase(arg);
                   1544:                                if ((arg2 = strchr(arg, ':')) == NULL ||
                   1545:                                    arg2[1] == '\0') {
                   1546:                                        fatal("%s line %d: "
                   1547:                                            "Invalid permitted CNAME \"%s\"",
                   1548:                                            filename, linenum, arg);
                   1549:                                }
                   1550:                                *arg2 = '\0';
                   1551:                                arg2++;
                   1552:                        }
                   1553:                        if (!*activep || value)
                   1554:                                continue;
                   1555:                        if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
                   1556:                                fatal("%s line %d: too many permitted CNAMEs.",
                   1557:                                    filename, linenum);
                   1558:                        cname = options->permitted_cnames +
                   1559:                            options->num_permitted_cnames++;
                   1560:                        cname->source_list = xstrdup(arg);
                   1561:                        cname->target_list = xstrdup(arg2);
                   1562:                }
                   1563:                break;
                   1564:
1.209     djm      1565:        case oCanonicalizeHostname:
                   1566:                intptr = &options->canonicalize_hostname;
                   1567:                multistate_ptr = multistate_canonicalizehostname;
1.208     djm      1568:                goto parse_multistate;
                   1569:
1.209     djm      1570:        case oCanonicalizeMaxDots:
                   1571:                intptr = &options->canonicalize_max_dots;
1.208     djm      1572:                goto parse_int;
                   1573:
1.209     djm      1574:        case oCanonicalizeFallbackLocal:
                   1575:                intptr = &options->canonicalize_fallback_local;
1.208     djm      1576:                goto parse_flag;
                   1577:
1.220     millert  1578:        case oStreamLocalBindMask:
                   1579:                arg = strdelim(&s);
                   1580:                if (!arg || *arg == '\0')
                   1581:                        fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
                   1582:                /* Parse mode in octal format */
                   1583:                value = strtol(arg, &endofnumber, 8);
                   1584:                if (arg == endofnumber || value < 0 || value > 0777)
                   1585:                        fatal("%.200s line %d: Bad mask.", filename, linenum);
                   1586:                options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
                   1587:                break;
                   1588:
                   1589:        case oStreamLocalBindUnlink:
                   1590:                intptr = &options->fwd_opts.streamlocal_bind_unlink;
                   1591:                goto parse_flag;
                   1592:
1.223     djm      1593:        case oRevokedHostKeys:
                   1594:                charptr = &options->revoked_host_keys;
                   1595:                goto parse_string;
                   1596:
1.224     djm      1597:        case oFingerprintHash:
1.225     djm      1598:                intptr = &options->fingerprint_hash;
1.224     djm      1599:                arg = strdelim(&s);
                   1600:                if (!arg || *arg == '\0')
                   1601:                        fatal("%.200s line %d: Missing argument.",
                   1602:                            filename, linenum);
                   1603:                if ((value = ssh_digest_alg_by_name(arg)) == -1)
                   1604:                        fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
                   1605:                            filename, linenum, arg);
1.225     djm      1606:                if (*activep && *intptr == -1)
                   1607:                        *intptr = value;
1.224     djm      1608:                break;
                   1609:
1.229     djm      1610:        case oUpdateHostkeys:
                   1611:                intptr = &options->update_hostkeys;
1.232     djm      1612:                multistate_ptr = multistate_yesnoask;
                   1613:                goto parse_multistate;
1.229     djm      1614:
1.230     djm      1615:        case oHostbasedKeyTypes:
                   1616:                charptr = &options->hostbased_key_types;
1.238     markus   1617:                goto parse_keytypes;
                   1618:
                   1619:        case oPubkeyAcceptedKeyTypes:
                   1620:                charptr = &options->pubkey_key_types;
                   1621:                goto parse_keytypes;
1.230     djm      1622:
1.246     jcs      1623:        case oAddKeysToAgent:
                   1624:                intptr = &options->add_keys_to_agent;
                   1625:                multistate_ptr = multistate_yesnoaskconfirm;
                   1626:                goto parse_multistate;
                   1627:
1.253     markus   1628:        case oIdentityAgent:
                   1629:                charptr = &options->identity_agent;
                   1630:                goto parse_string;
                   1631:
1.96      markus   1632:        case oDeprecated:
1.98      markus   1633:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   1634:                    filename, linenum, keyword);
1.98      markus   1635:                return 0;
1.96      markus   1636:
1.110     jakob    1637:        case oUnsupported:
                   1638:                error("%s line %d: Unsupported option \"%s\"",
                   1639:                    filename, linenum, keyword);
                   1640:                return 0;
                   1641:
1.17      markus   1642:        default:
1.221     djm      1643:                fatal("%s: Unimplemented opcode %d", __func__, opcode);
1.17      markus   1644:        }
                   1645:
                   1646:        /* Check that there is no garbage at end of line. */
1.57      djm      1647:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho       1648:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm      1649:                    filename, linenum, arg);
1.39      ho       1650:        }
1.17      markus   1651:        return 0;
1.1       deraadt  1652: }
                   1653:
1.19      markus   1654: /*
                   1655:  * Reads the config file and modifies the options accordingly.  Options
                   1656:  * should already be initialized before this call.  This never returns if
1.89      stevesk  1657:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   1658:  */
1.89      stevesk  1659: int
1.206     djm      1660: read_config_file(const char *filename, struct passwd *pw, const char *host,
1.221     djm      1661:     const char *original_host, Options *options, int flags)
1.1       deraadt  1662: {
1.252     djm      1663:        int active = 1;
                   1664:
                   1665:        return read_config_file_depth(filename, pw, host, original_host,
                   1666:            options, flags, &active, 0);
                   1667: }
                   1668:
                   1669: #define READCONF_MAX_DEPTH     16
                   1670: static int
                   1671: read_config_file_depth(const char *filename, struct passwd *pw,
                   1672:     const char *host, const char *original_host, Options *options,
                   1673:     int flags, int *activep, int depth)
                   1674: {
1.17      markus   1675:        FILE *f;
1.270     djm      1676:        char line[4096];
1.252     djm      1677:        int linenum;
1.17      markus   1678:        int bad_options = 0;
                   1679:
1.252     djm      1680:        if (depth < 0 || depth > READCONF_MAX_DEPTH)
                   1681:                fatal("Too many recursive configuration includes");
                   1682:
1.129     djm      1683:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  1684:                return 0;
1.129     djm      1685:
1.196     dtucker  1686:        if (flags & SSHCONF_CHECKPERM) {
1.129     djm      1687:                struct stat sb;
1.134     deraadt  1688:
1.131     dtucker  1689:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      1690:                        fatal("fstat %s: %s", filename, strerror(errno));
                   1691:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  1692:                    (sb.st_mode & 022) != 0))
1.129     djm      1693:                        fatal("Bad owner or permissions on %s", filename);
                   1694:        }
1.17      markus   1695:
                   1696:        debug("Reading configuration data %.200s", filename);
                   1697:
1.19      markus   1698:        /*
                   1699:         * Mark that we are now processing the options.  This flag is turned
                   1700:         * on/off by Host specifications.
                   1701:         */
1.17      markus   1702:        linenum = 0;
                   1703:        while (fgets(line, sizeof(line), f)) {
                   1704:                /* Update line number counter. */
                   1705:                linenum++;
1.270     djm      1706:                if (strlen(line) == sizeof(line) - 1)
                   1707:                        fatal("%s line %d too long", filename, linenum);
1.252     djm      1708:                if (process_config_line_depth(options, pw, host, original_host,
                   1709:                    line, filename, linenum, activep, flags, depth) != 0)
1.17      markus   1710:                        bad_options++;
                   1711:        }
                   1712:        fclose(f);
                   1713:        if (bad_options > 0)
1.64      millert  1714:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  1715:                    filename, bad_options);
1.89      stevesk  1716:        return 1;
1.1       deraadt  1717: }
                   1718:
1.218     djm      1719: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                   1720: int
                   1721: option_clear_or_none(const char *o)
                   1722: {
                   1723:        return o == NULL || strcasecmp(o, "none") == 0;
                   1724: }
                   1725:
1.19      markus   1726: /*
                   1727:  * Initializes options to special values that indicate that they have not yet
                   1728:  * been set.  Read_config_file will only set options with this value. Options
                   1729:  * are processed in the following order: command line, user config file,
                   1730:  * system config file.  Last, fill_default_options is called.
                   1731:  */
1.1       deraadt  1732:
1.26      markus   1733: void
1.17      markus   1734: initialize_options(Options * options)
1.1       deraadt  1735: {
1.17      markus   1736:        memset(options, 'X', sizeof(*options));
                   1737:        options->forward_agent = -1;
                   1738:        options->forward_x11 = -1;
1.123     markus   1739:        options->forward_x11_trusted = -1;
1.186     djm      1740:        options->forward_x11_timeout = -1;
1.255     dtucker  1741:        options->stdio_forward_host = NULL;
                   1742:        options->stdio_forward_port = 0;
1.256     dtucker  1743:        options->clear_forwardings = -1;
1.153     markus   1744:        options->exit_on_forward_failure = -1;
1.34      markus   1745:        options->xauth_location = NULL;
1.220     millert  1746:        options->fwd_opts.gateway_ports = -1;
                   1747:        options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
                   1748:        options->fwd_opts.streamlocal_bind_unlink = -1;
1.17      markus   1749:        options->use_privileged_port = -1;
1.50      markus   1750:        options->pubkey_authentication = -1;
1.78      markus   1751:        options->challenge_response_authentication = -1;
1.118     markus   1752:        options->gss_authentication = -1;
                   1753:        options->gss_deleg_creds = -1;
1.17      markus   1754:        options->password_authentication = -1;
1.48      markus   1755:        options->kbd_interactive_authentication = -1;
                   1756:        options->kbd_interactive_devices = NULL;
1.72      markus   1757:        options->hostbased_authentication = -1;
1.17      markus   1758:        options->batch_mode = -1;
                   1759:        options->check_host_ip = -1;
                   1760:        options->strict_host_key_checking = -1;
                   1761:        options->compression = -1;
1.126     markus   1762:        options->tcp_keep_alive = -1;
1.17      markus   1763:        options->port = -1;
1.114     djm      1764:        options->address_family = -1;
1.17      markus   1765:        options->connection_attempts = -1;
1.111     djm      1766:        options->connection_timeout = -1;
1.17      markus   1767:        options->number_of_password_prompts = -1;
1.25      markus   1768:        options->ciphers = NULL;
1.62      markus   1769:        options->macs = NULL;
1.189     djm      1770:        options->kex_algorithms = NULL;
1.76      markus   1771:        options->hostkeyalgorithms = NULL;
1.17      markus   1772:        options->num_identity_files = 0;
1.241     djm      1773:        options->num_certificate_files = 0;
1.17      markus   1774:        options->hostname = NULL;
1.52      markus   1775:        options->host_key_alias = NULL;
1.17      markus   1776:        options->proxy_command = NULL;
1.257     djm      1777:        options->jump_user = NULL;
                   1778:        options->jump_host = NULL;
                   1779:        options->jump_port = -1;
                   1780:        options->jump_extra = NULL;
1.17      markus   1781:        options->user = NULL;
                   1782:        options->escape_char = -1;
1.193     djm      1783:        options->num_system_hostfiles = 0;
                   1784:        options->num_user_hostfiles = 0;
1.185     djm      1785:        options->local_forwards = NULL;
1.17      markus   1786:        options->num_local_forwards = 0;
1.185     djm      1787:        options->remote_forwards = NULL;
1.17      markus   1788:        options->num_remote_forwards = 0;
1.271     dtucker  1789:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
1.95      markus   1790:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1791:        options->preferred_authentications = NULL;
1.77      markus   1792:        options->bind_address = NULL;
1.282     djm      1793:        options->bind_interface = NULL;
1.183     markus   1794:        options->pkcs11_provider = NULL;
1.101     markus   1795:        options->enable_ssh_keysign = - 1;
1.91      markus   1796:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1797:        options->identities_only = - 1;
1.105     markus   1798:        options->rekey_limit = - 1;
1.198     dtucker  1799:        options->rekey_interval = -1;
1.107     jakob    1800:        options->verify_host_key_dns = -1;
1.127     markus   1801:        options->server_alive_interval = -1;
                   1802:        options->server_alive_count_max = -1;
1.130     djm      1803:        options->num_send_env = 0;
1.132     djm      1804:        options->control_path = NULL;
                   1805:        options->control_master = -1;
1.187     djm      1806:        options->control_persist = -1;
                   1807:        options->control_persist_timeout = 0;
1.136     djm      1808:        options->hash_known_hosts = -1;
1.144     reyk     1809:        options->tun_open = -1;
                   1810:        options->tun_local = -1;
                   1811:        options->tun_remote = -1;
                   1812:        options->local_command = NULL;
                   1813:        options->permit_local_command = -1;
1.277     bluhm    1814:        options->remote_command = NULL;
1.246     jcs      1815:        options->add_keys_to_agent = -1;
1.253     markus   1816:        options->identity_agent = NULL;
1.167     grunk    1817:        options->visual_host_key = -1;
1.190     djm      1818:        options->ip_qos_interactive = -1;
                   1819:        options->ip_qos_bulk = -1;
1.192     djm      1820:        options->request_tty = -1;
1.205     djm      1821:        options->proxy_use_fdpass = -1;
1.199     djm      1822:        options->ignored_unknown = NULL;
1.208     djm      1823:        options->num_canonical_domains = 0;
                   1824:        options->num_permitted_cnames = 0;
1.209     djm      1825:        options->canonicalize_max_dots = -1;
                   1826:        options->canonicalize_fallback_local = -1;
                   1827:        options->canonicalize_hostname = -1;
1.223     djm      1828:        options->revoked_host_keys = NULL;
1.224     djm      1829:        options->fingerprint_hash = -1;
1.229     djm      1830:        options->update_hostkeys = -1;
1.230     djm      1831:        options->hostbased_key_types = NULL;
1.238     markus   1832:        options->pubkey_key_types = NULL;
1.1       deraadt  1833: }
                   1834:
1.19      markus   1835: /*
1.218     djm      1836:  * A petite version of fill_default_options() that just fills the options
                   1837:  * needed for hostname canonicalization to proceed.
                   1838:  */
                   1839: void
                   1840: fill_default_options_for_canonicalization(Options *options)
                   1841: {
                   1842:        if (options->canonicalize_max_dots == -1)
                   1843:                options->canonicalize_max_dots = 1;
                   1844:        if (options->canonicalize_fallback_local == -1)
                   1845:                options->canonicalize_fallback_local = 1;
                   1846:        if (options->canonicalize_hostname == -1)
                   1847:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
                   1848: }
                   1849:
                   1850: /*
1.19      markus   1851:  * Called after processing other sources of option data, this fills those
                   1852:  * options for which no value has been specified with their default values.
                   1853:  */
1.26      markus   1854: void
1.17      markus   1855: fill_default_options(Options * options)
1.1       deraadt  1856: {
1.17      markus   1857:        if (options->forward_agent == -1)
1.33      markus   1858:                options->forward_agent = 0;
1.17      markus   1859:        if (options->forward_x11 == -1)
1.23      markus   1860:                options->forward_x11 = 0;
1.123     markus   1861:        if (options->forward_x11_trusted == -1)
                   1862:                options->forward_x11_trusted = 0;
1.186     djm      1863:        if (options->forward_x11_timeout == -1)
                   1864:                options->forward_x11_timeout = 1200;
1.256     dtucker  1865:        /*
                   1866:         * stdio forwarding (-W) changes the default for these but we defer
                   1867:         * setting the values so they can be overridden.
                   1868:         */
1.153     markus   1869:        if (options->exit_on_forward_failure == -1)
1.256     dtucker  1870:                options->exit_on_forward_failure =
                   1871:                    options->stdio_forward_host != NULL ? 1 : 0;
                   1872:        if (options->clear_forwardings == -1)
                   1873:                options->clear_forwardings =
                   1874:                    options->stdio_forward_host != NULL ? 1 : 0;
                   1875:        if (options->clear_forwardings == 1)
                   1876:                clear_forwardings(options);
                   1877:
1.34      markus   1878:        if (options->xauth_location == NULL)
1.80      markus   1879:                options->xauth_location = _PATH_XAUTH;
1.220     millert  1880:        if (options->fwd_opts.gateway_ports == -1)
                   1881:                options->fwd_opts.gateway_ports = 0;
                   1882:        if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
                   1883:                options->fwd_opts.streamlocal_bind_mask = 0177;
                   1884:        if (options->fwd_opts.streamlocal_bind_unlink == -1)
                   1885:                options->fwd_opts.streamlocal_bind_unlink = 0;
1.17      markus   1886:        if (options->use_privileged_port == -1)
1.65      markus   1887:                options->use_privileged_port = 0;
1.50      markus   1888:        if (options->pubkey_authentication == -1)
                   1889:                options->pubkey_authentication = 1;
1.78      markus   1890:        if (options->challenge_response_authentication == -1)
1.83      markus   1891:                options->challenge_response_authentication = 1;
1.118     markus   1892:        if (options->gss_authentication == -1)
1.122     markus   1893:                options->gss_authentication = 0;
1.118     markus   1894:        if (options->gss_deleg_creds == -1)
                   1895:                options->gss_deleg_creds = 0;
1.17      markus   1896:        if (options->password_authentication == -1)
                   1897:                options->password_authentication = 1;
1.48      markus   1898:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1899:                options->kbd_interactive_authentication = 1;
1.72      markus   1900:        if (options->hostbased_authentication == -1)
                   1901:                options->hostbased_authentication = 0;
1.17      markus   1902:        if (options->batch_mode == -1)
                   1903:                options->batch_mode = 0;
                   1904:        if (options->check_host_ip == -1)
                   1905:                options->check_host_ip = 1;
                   1906:        if (options->strict_host_key_checking == -1)
1.278     djm      1907:                options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
1.17      markus   1908:        if (options->compression == -1)
                   1909:                options->compression = 0;
1.126     markus   1910:        if (options->tcp_keep_alive == -1)
                   1911:                options->tcp_keep_alive = 1;
1.17      markus   1912:        if (options->port == -1)
                   1913:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1914:        if (options->address_family == -1)
                   1915:                options->address_family = AF_UNSPEC;
1.17      markus   1916:        if (options->connection_attempts == -1)
1.84      markus   1917:                options->connection_attempts = 1;
1.17      markus   1918:        if (options->number_of_password_prompts == -1)
                   1919:                options->number_of_password_prompts = 3;
1.76      markus   1920:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.246     jcs      1921:        if (options->add_keys_to_agent == -1)
                   1922:                options->add_keys_to_agent = 0;
1.17      markus   1923:        if (options->num_identity_files == 0) {
1.273     djm      1924:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
                   1925:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
                   1926:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
                   1927:                add_identity_file(options, "~/",
                   1928:                    _PATH_SSH_CLIENT_ID_ED25519, 0);
1.283   ! markus   1929:                add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
1.27      markus   1930:        }
1.17      markus   1931:        if (options->escape_char == -1)
                   1932:                options->escape_char = '~';
1.193     djm      1933:        if (options->num_system_hostfiles == 0) {
                   1934:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1935:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
                   1936:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1937:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
                   1938:        }
                   1939:        if (options->num_user_hostfiles == 0) {
                   1940:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1941:                    xstrdup(_PATH_SSH_USER_HOSTFILE);
                   1942:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1943:                    xstrdup(_PATH_SSH_USER_HOSTFILE2);
                   1944:        }
1.95      markus   1945:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1946:                options->log_level = SYSLOG_LEVEL_INFO;
1.271     dtucker  1947:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
                   1948:                options->log_facility = SYSLOG_FACILITY_USER;
1.91      markus   1949:        if (options->no_host_authentication_for_localhost == - 1)
                   1950:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1951:        if (options->identities_only == -1)
                   1952:                options->identities_only = 0;
1.101     markus   1953:        if (options->enable_ssh_keysign == -1)
                   1954:                options->enable_ssh_keysign = 0;
1.105     markus   1955:        if (options->rekey_limit == -1)
                   1956:                options->rekey_limit = 0;
1.198     dtucker  1957:        if (options->rekey_interval == -1)
                   1958:                options->rekey_interval = 0;
1.107     jakob    1959:        if (options->verify_host_key_dns == -1)
                   1960:                options->verify_host_key_dns = 0;
1.127     markus   1961:        if (options->server_alive_interval == -1)
                   1962:                options->server_alive_interval = 0;
                   1963:        if (options->server_alive_count_max == -1)
                   1964:                options->server_alive_count_max = 3;
1.132     djm      1965:        if (options->control_master == -1)
                   1966:                options->control_master = 0;
1.187     djm      1967:        if (options->control_persist == -1) {
                   1968:                options->control_persist = 0;
                   1969:                options->control_persist_timeout = 0;
                   1970:        }
1.136     djm      1971:        if (options->hash_known_hosts == -1)
                   1972:                options->hash_known_hosts = 0;
1.144     reyk     1973:        if (options->tun_open == -1)
1.145     reyk     1974:                options->tun_open = SSH_TUNMODE_NO;
                   1975:        if (options->tun_local == -1)
                   1976:                options->tun_local = SSH_TUNID_ANY;
                   1977:        if (options->tun_remote == -1)
                   1978:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1979:        if (options->permit_local_command == -1)
                   1980:                options->permit_local_command = 0;
1.167     grunk    1981:        if (options->visual_host_key == -1)
                   1982:                options->visual_host_key = 0;
1.190     djm      1983:        if (options->ip_qos_interactive == -1)
                   1984:                options->ip_qos_interactive = IPTOS_LOWDELAY;
                   1985:        if (options->ip_qos_bulk == -1)
                   1986:                options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.192     djm      1987:        if (options->request_tty == -1)
                   1988:                options->request_tty = REQUEST_TTY_AUTO;
1.205     djm      1989:        if (options->proxy_use_fdpass == -1)
                   1990:                options->proxy_use_fdpass = 0;
1.209     djm      1991:        if (options->canonicalize_max_dots == -1)
                   1992:                options->canonicalize_max_dots = 1;
                   1993:        if (options->canonicalize_fallback_local == -1)
                   1994:                options->canonicalize_fallback_local = 1;
                   1995:        if (options->canonicalize_hostname == -1)
                   1996:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.224     djm      1997:        if (options->fingerprint_hash == -1)
                   1998:                options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.229     djm      1999:        if (options->update_hostkeys == -1)
1.231     djm      2000:                options->update_hostkeys = 0;
1.239     djm      2001:        if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
                   2002:            kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
                   2003:            kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
                   2004:            kex_assemble_names(KEX_DEFAULT_PK_ALG,
                   2005:            &options->hostbased_key_types) != 0 ||
                   2006:            kex_assemble_names(KEX_DEFAULT_PK_ALG,
                   2007:            &options->pubkey_key_types) != 0)
                   2008:                fatal("%s: kex_assemble_names failed", __func__);
1.224     djm      2009:
1.207     djm      2010: #define CLEAR_ON_NONE(v) \
                   2011:        do { \
1.218     djm      2012:                if (option_clear_or_none(v)) { \
1.207     djm      2013:                        free(v); \
                   2014:                        v = NULL; \
                   2015:                } \
                   2016:        } while(0)
                   2017:        CLEAR_ON_NONE(options->local_command);
1.277     bluhm    2018:        CLEAR_ON_NONE(options->remote_command);
1.207     djm      2019:        CLEAR_ON_NONE(options->proxy_command);
                   2020:        CLEAR_ON_NONE(options->control_path);
1.223     djm      2021:        CLEAR_ON_NONE(options->revoked_host_keys);
1.254     markus   2022:        /* options->identity_agent distinguishes NULL from 'none' */
1.17      markus   2023:        /* options->user will be set in the main program if appropriate */
                   2024:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   2025:        /* options->host_key_alias should not be set by default */
1.67      markus   2026:        /* options->preferred_authentications will be set in ssh */
1.135     djm      2027: }
                   2028:
1.220     millert  2029: struct fwdarg {
                   2030:        char *arg;
                   2031:        int ispath;
                   2032: };
                   2033:
                   2034: /*
                   2035:  * parse_fwd_field
                   2036:  * parses the next field in a port forwarding specification.
                   2037:  * sets fwd to the parsed field and advances p past the colon
                   2038:  * or sets it to NULL at end of string.
                   2039:  * returns 0 on success, else non-zero.
                   2040:  */
                   2041: static int
                   2042: parse_fwd_field(char **p, struct fwdarg *fwd)
                   2043: {
                   2044:        char *ep, *cp = *p;
                   2045:        int ispath = 0;
                   2046:
                   2047:        if (*cp == '\0') {
                   2048:                *p = NULL;
                   2049:                return -1;      /* end of string */
                   2050:        }
                   2051:
                   2052:        /*
                   2053:         * A field escaped with square brackets is used literally.
                   2054:         * XXX - allow ']' to be escaped via backslash?
                   2055:         */
                   2056:        if (*cp == '[') {
                   2057:                /* find matching ']' */
                   2058:                for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
                   2059:                        if (*ep == '/')
                   2060:                                ispath = 1;
                   2061:                }
                   2062:                /* no matching ']' or not at end of field. */
                   2063:                if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
                   2064:                        return -1;
                   2065:                /* NUL terminate the field and advance p past the colon */
                   2066:                *ep++ = '\0';
                   2067:                if (*ep != '\0')
                   2068:                        *ep++ = '\0';
                   2069:                fwd->arg = cp + 1;
                   2070:                fwd->ispath = ispath;
                   2071:                *p = ep;
                   2072:                return 0;
                   2073:        }
                   2074:
                   2075:        for (cp = *p; *cp != '\0'; cp++) {
                   2076:                switch (*cp) {
                   2077:                case '\\':
                   2078:                        memmove(cp, cp + 1, strlen(cp + 1) + 1);
1.237     djm      2079:                        if (*cp == '\0')
                   2080:                                return -1;
1.220     millert  2081:                        break;
                   2082:                case '/':
                   2083:                        ispath = 1;
                   2084:                        break;
                   2085:                case ':':
                   2086:                        *cp++ = '\0';
                   2087:                        goto done;
                   2088:                }
                   2089:        }
                   2090: done:
                   2091:        fwd->arg = *p;
                   2092:        fwd->ispath = ispath;
                   2093:        *p = cp;
                   2094:        return 0;
                   2095: }
                   2096:
1.135     djm      2097: /*
                   2098:  * parse_forward
                   2099:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  2100:  *   dynamicfwd == 0
1.220     millert  2101:  *     [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
                   2102:  *     listenpath:connectpath
1.168     stevesk  2103:  *   dynamicfwd == 1
                   2104:  *     [listenhost:]listenport
1.135     djm      2105:  * returns number of arguments parsed or zero on error
                   2106:  */
                   2107: int
1.220     millert  2108: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      2109: {
1.220     millert  2110:        struct fwdarg fwdargs[4];
                   2111:        char *p, *cp;
1.135     djm      2112:        int i;
                   2113:
1.220     millert  2114:        memset(fwd, 0, sizeof(*fwd));
                   2115:        memset(fwdargs, 0, sizeof(fwdargs));
1.135     djm      2116:
                   2117:        cp = p = xstrdup(fwdspec);
                   2118:
                   2119:        /* skip leading spaces */
1.214     deraadt  2120:        while (isspace((u_char)*cp))
1.135     djm      2121:                cp++;
                   2122:
1.220     millert  2123:        for (i = 0; i < 4; ++i) {
                   2124:                if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135     djm      2125:                        break;
1.220     millert  2126:        }
1.135     djm      2127:
1.170     stevesk  2128:        /* Check for trailing garbage */
1.220     millert  2129:        if (cp != NULL && *cp != '\0') {
1.135     djm      2130:                i = 0;  /* failure */
1.220     millert  2131:        }
1.135     djm      2132:
                   2133:        switch (i) {
1.168     stevesk  2134:        case 1:
1.220     millert  2135:                if (fwdargs[0].ispath) {
                   2136:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2137:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2138:                } else {
                   2139:                        fwd->listen_host = NULL;
                   2140:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2141:                }
1.168     stevesk  2142:                fwd->connect_host = xstrdup("socks");
                   2143:                break;
                   2144:
                   2145:        case 2:
1.220     millert  2146:                if (fwdargs[0].ispath && fwdargs[1].ispath) {
                   2147:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2148:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2149:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2150:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2151:                } else if (fwdargs[1].ispath) {
                   2152:                        fwd->listen_host = NULL;
                   2153:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2154:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
                   2155:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2156:                } else {
                   2157:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2158:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   2159:                        fwd->connect_host = xstrdup("socks");
                   2160:                }
1.168     stevesk  2161:                break;
                   2162:
1.135     djm      2163:        case 3:
1.220     millert  2164:                if (fwdargs[0].ispath) {
                   2165:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
                   2166:                        fwd->listen_port = PORT_STREAMLOCAL;
                   2167:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   2168:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   2169:                } else if (fwdargs[2].ispath) {
                   2170:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2171:                        fwd->listen_port = a2port(fwdargs[1].arg);
                   2172:                        fwd->connect_path = xstrdup(fwdargs[2].arg);
                   2173:                        fwd->connect_port = PORT_STREAMLOCAL;
                   2174:                } else {
                   2175:                        fwd->listen_host = NULL;
                   2176:                        fwd->listen_port = a2port(fwdargs[0].arg);
                   2177:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
                   2178:                        fwd->connect_port = a2port(fwdargs[2].arg);
                   2179:                }
1.135     djm      2180:                break;
                   2181:
                   2182:        case 4:
1.220     millert  2183:                fwd->listen_host = xstrdup(fwdargs[0].arg);
                   2184:                fwd->listen_port = a2port(fwdargs[1].arg);
                   2185:                fwd->connect_host = xstrdup(fwdargs[2].arg);
                   2186:                fwd->connect_port = a2port(fwdargs[3].arg);
1.135     djm      2187:                break;
                   2188:        default:
                   2189:                i = 0; /* failure */
                   2190:        }
                   2191:
1.202     djm      2192:        free(p);
1.135     djm      2193:
1.168     stevesk  2194:        if (dynamicfwd) {
                   2195:                if (!(i == 1 || i == 2))
                   2196:                        goto fail_free;
                   2197:        } else {
1.220     millert  2198:                if (!(i == 3 || i == 4)) {
                   2199:                        if (fwd->connect_path == NULL &&
                   2200:                            fwd->listen_path == NULL)
                   2201:                                goto fail_free;
                   2202:                }
                   2203:                if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168     stevesk  2204:                        goto fail_free;
                   2205:        }
                   2206:
1.220     millert  2207:        if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
                   2208:            (!remotefwd && fwd->listen_port == 0))
1.135     djm      2209:                goto fail_free;
                   2210:        if (fwd->connect_host != NULL &&
                   2211:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   2212:                goto fail_free;
1.220     millert  2213:        /* XXX - if connecting to a remote socket, max sun len may not match this host */
                   2214:        if (fwd->connect_path != NULL &&
                   2215:            strlen(fwd->connect_path) >= PATH_MAX_SUN)
                   2216:                goto fail_free;
1.176     djm      2217:        if (fwd->listen_host != NULL &&
                   2218:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   2219:                goto fail_free;
1.220     millert  2220:        if (fwd->listen_path != NULL &&
                   2221:            strlen(fwd->listen_path) >= PATH_MAX_SUN)
                   2222:                goto fail_free;
1.135     djm      2223:
                   2224:        return (i);
                   2225:
                   2226:  fail_free:
1.202     djm      2227:        free(fwd->connect_host);
                   2228:        fwd->connect_host = NULL;
1.220     millert  2229:        free(fwd->connect_path);
                   2230:        fwd->connect_path = NULL;
1.202     djm      2231:        free(fwd->listen_host);
                   2232:        fwd->listen_host = NULL;
1.220     millert  2233:        free(fwd->listen_path);
                   2234:        fwd->listen_path = NULL;
1.135     djm      2235:        return (0);
1.221     djm      2236: }
                   2237:
1.257     djm      2238: int
                   2239: parse_jump(const char *s, Options *o, int active)
                   2240: {
                   2241:        char *orig, *sdup, *cp;
                   2242:        char *host = NULL, *user = NULL;
1.258     naddy    2243:        int ret = -1, port = -1, first;
1.257     djm      2244:
                   2245:        active &= o->proxy_command == NULL && o->jump_host == NULL;
                   2246:
                   2247:        orig = sdup = xstrdup(s);
1.258     naddy    2248:        first = active;
1.259     djm      2249:        do {
                   2250:                if ((cp = strrchr(sdup, ',')) == NULL)
                   2251:                        cp = sdup; /* last */
                   2252:                else
                   2253:                        *cp++ = '\0';
                   2254:
1.258     naddy    2255:                if (first) {
1.257     djm      2256:                        /* First argument and configuration is active */
1.280     millert  2257:                        if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
                   2258:                            parse_user_host_port(cp, &user, &host, &port) != 0)
1.257     djm      2259:                                goto out;
                   2260:                } else {
                   2261:                        /* Subsequent argument or inactive configuration */
1.280     millert  2262:                        if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
                   2263:                            parse_user_host_port(cp, NULL, NULL, NULL) != 0)
1.257     djm      2264:                                goto out;
                   2265:                }
1.258     naddy    2266:                first = 0; /* only check syntax for subsequent hosts */
1.259     djm      2267:        } while (cp != sdup);
1.257     djm      2268:        /* success */
1.258     naddy    2269:        if (active) {
                   2270:                o->jump_user = user;
                   2271:                o->jump_host = host;
                   2272:                o->jump_port = port;
                   2273:                o->proxy_command = xstrdup("none");
                   2274:                user = host = NULL;
1.259     djm      2275:                if ((cp = strrchr(s, ',')) != NULL && cp != s) {
                   2276:                        o->jump_extra = xstrdup(s);
                   2277:                        o->jump_extra[cp - s] = '\0';
                   2278:                }
1.258     naddy    2279:        }
1.257     djm      2280:        ret = 0;
                   2281:  out:
1.258     naddy    2282:        free(orig);
1.257     djm      2283:        free(user);
                   2284:        free(host);
                   2285:        return ret;
1.280     millert  2286: }
                   2287:
                   2288: int
                   2289: parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
                   2290: {
                   2291:        char *path;
                   2292:        int r;
                   2293:
                   2294:        r = parse_uri("ssh", uri, userp, hostp, portp, &path);
                   2295:        if (r == 0 && path != NULL)
                   2296:                r = -1;         /* path not allowed */
                   2297:        return r;
1.257     djm      2298: }
                   2299:
1.221     djm      2300: /* XXX the following is a near-vebatim copy from servconf.c; refactor */
                   2301: static const char *
                   2302: fmt_multistate_int(int val, const struct multistate *m)
                   2303: {
                   2304:        u_int i;
                   2305:
                   2306:        for (i = 0; m[i].key != NULL; i++) {
                   2307:                if (m[i].value == val)
                   2308:                        return m[i].key;
                   2309:        }
                   2310:        return "UNKNOWN";
                   2311: }
                   2312:
                   2313: static const char *
                   2314: fmt_intarg(OpCodes code, int val)
                   2315: {
                   2316:        if (val == -1)
                   2317:                return "unset";
                   2318:        switch (code) {
                   2319:        case oAddressFamily:
                   2320:                return fmt_multistate_int(val, multistate_addressfamily);
                   2321:        case oVerifyHostKeyDNS:
1.232     djm      2322:        case oUpdateHostkeys:
1.221     djm      2323:                return fmt_multistate_int(val, multistate_yesnoask);
1.278     djm      2324:        case oStrictHostKeyChecking:
                   2325:                return fmt_multistate_int(val, multistate_strict_hostkey);
1.221     djm      2326:        case oControlMaster:
                   2327:                return fmt_multistate_int(val, multistate_controlmaster);
                   2328:        case oTunnel:
                   2329:                return fmt_multistate_int(val, multistate_tunnel);
                   2330:        case oRequestTTY:
                   2331:                return fmt_multistate_int(val, multistate_requesttty);
                   2332:        case oCanonicalizeHostname:
                   2333:                return fmt_multistate_int(val, multistate_canonicalizehostname);
1.224     djm      2334:        case oFingerprintHash:
                   2335:                return ssh_digest_alg_name(val);
1.221     djm      2336:        default:
                   2337:                switch (val) {
                   2338:                case 0:
                   2339:                        return "no";
                   2340:                case 1:
                   2341:                        return "yes";
                   2342:                default:
                   2343:                        return "UNKNOWN";
                   2344:                }
                   2345:        }
                   2346: }
                   2347:
                   2348: static const char *
                   2349: lookup_opcode_name(OpCodes code)
                   2350: {
                   2351:        u_int i;
                   2352:
                   2353:        for (i = 0; keywords[i].name != NULL; i++)
                   2354:                if (keywords[i].opcode == code)
                   2355:                        return(keywords[i].name);
                   2356:        return "UNKNOWN";
                   2357: }
                   2358:
                   2359: static void
                   2360: dump_cfg_int(OpCodes code, int val)
                   2361: {
                   2362:        printf("%s %d\n", lookup_opcode_name(code), val);
                   2363: }
                   2364:
                   2365: static void
                   2366: dump_cfg_fmtint(OpCodes code, int val)
                   2367: {
                   2368:        printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
                   2369: }
                   2370:
                   2371: static void
                   2372: dump_cfg_string(OpCodes code, const char *val)
                   2373: {
                   2374:        if (val == NULL)
                   2375:                return;
                   2376:        printf("%s %s\n", lookup_opcode_name(code), val);
                   2377: }
                   2378:
                   2379: static void
                   2380: dump_cfg_strarray(OpCodes code, u_int count, char **vals)
                   2381: {
                   2382:        u_int i;
                   2383:
                   2384:        for (i = 0; i < count; i++)
                   2385:                printf("%s %s\n", lookup_opcode_name(code), vals[i]);
                   2386: }
                   2387:
                   2388: static void
                   2389: dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
                   2390: {
                   2391:        u_int i;
                   2392:
                   2393:        printf("%s", lookup_opcode_name(code));
                   2394:        for (i = 0; i < count; i++)
                   2395:                printf(" %s",  vals[i]);
                   2396:        printf("\n");
                   2397: }
                   2398:
                   2399: static void
                   2400: dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
                   2401: {
                   2402:        const struct Forward *fwd;
                   2403:        u_int i;
                   2404:
                   2405:        /* oDynamicForward */
                   2406:        for (i = 0; i < count; i++) {
                   2407:                fwd = &fwds[i];
1.265     djm      2408:                if (code == oDynamicForward && fwd->connect_host != NULL &&
1.221     djm      2409:                    strcmp(fwd->connect_host, "socks") != 0)
                   2410:                        continue;
1.265     djm      2411:                if (code == oLocalForward && fwd->connect_host != NULL &&
1.221     djm      2412:                    strcmp(fwd->connect_host, "socks") == 0)
                   2413:                        continue;
                   2414:                printf("%s", lookup_opcode_name(code));
                   2415:                if (fwd->listen_port == PORT_STREAMLOCAL)
                   2416:                        printf(" %s", fwd->listen_path);
                   2417:                else if (fwd->listen_host == NULL)
                   2418:                        printf(" %d", fwd->listen_port);
                   2419:                else {
                   2420:                        printf(" [%s]:%d",
                   2421:                            fwd->listen_host, fwd->listen_port);
                   2422:                }
                   2423:                if (code != oDynamicForward) {
                   2424:                        if (fwd->connect_port == PORT_STREAMLOCAL)
                   2425:                                printf(" %s", fwd->connect_path);
                   2426:                        else if (fwd->connect_host == NULL)
                   2427:                                printf(" %d", fwd->connect_port);
                   2428:                        else {
                   2429:                                printf(" [%s]:%d",
                   2430:                                    fwd->connect_host, fwd->connect_port);
                   2431:                        }
                   2432:                }
                   2433:                printf("\n");
                   2434:        }
                   2435: }
                   2436:
                   2437: void
                   2438: dump_client_config(Options *o, const char *host)
                   2439: {
                   2440:        int i;
1.257     djm      2441:        char buf[8];
1.221     djm      2442:
1.240     djm      2443:        /* This is normally prepared in ssh_kex2 */
                   2444:        if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
                   2445:                fatal("%s: kex_assemble_names failed", __func__);
                   2446:
1.221     djm      2447:        /* Most interesting options first: user, host, port */
                   2448:        dump_cfg_string(oUser, o->user);
                   2449:        dump_cfg_string(oHostName, host);
                   2450:        dump_cfg_int(oPort, o->port);
                   2451:
                   2452:        /* Flag options */
                   2453:        dump_cfg_fmtint(oAddressFamily, o->address_family);
                   2454:        dump_cfg_fmtint(oBatchMode, o->batch_mode);
                   2455:        dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
                   2456:        dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
                   2457:        dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
                   2458:        dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
                   2459:        dump_cfg_fmtint(oCompression, o->compression);
                   2460:        dump_cfg_fmtint(oControlMaster, o->control_master);
                   2461:        dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
1.256     dtucker  2462:        dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
1.221     djm      2463:        dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
1.224     djm      2464:        dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
1.221     djm      2465:        dump_cfg_fmtint(oForwardAgent, o->forward_agent);
                   2466:        dump_cfg_fmtint(oForwardX11, o->forward_x11);
                   2467:        dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
                   2468:        dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
                   2469: #ifdef GSSAPI
                   2470:        dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
                   2471:        dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
                   2472: #endif /* GSSAPI */
                   2473:        dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
                   2474:        dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
                   2475:        dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
                   2476:        dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
                   2477:        dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
                   2478:        dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
                   2479:        dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
                   2480:        dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
                   2481:        dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
                   2482:        dump_cfg_fmtint(oRequestTTY, o->request_tty);
                   2483:        dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
                   2484:        dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
                   2485:        dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
                   2486:        dump_cfg_fmtint(oTunnel, o->tun_open);
                   2487:        dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
                   2488:        dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
                   2489:        dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
1.229     djm      2490:        dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
1.221     djm      2491:
                   2492:        /* Integer options */
                   2493:        dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
                   2494:        dump_cfg_int(oConnectionAttempts, o->connection_attempts);
                   2495:        dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
                   2496:        dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
                   2497:        dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
                   2498:        dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
                   2499:
                   2500:        /* String options */
                   2501:        dump_cfg_string(oBindAddress, o->bind_address);
1.282     djm      2502:        dump_cfg_string(oBindInterface, o->bind_interface);
1.221     djm      2503:        dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
                   2504:        dump_cfg_string(oControlPath, o->control_path);
1.240     djm      2505:        dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
1.221     djm      2506:        dump_cfg_string(oHostKeyAlias, o->host_key_alias);
1.230     djm      2507:        dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
1.253     markus   2508:        dump_cfg_string(oIdentityAgent, o->identity_agent);
1.221     djm      2509:        dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
                   2510:        dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
                   2511:        dump_cfg_string(oLocalCommand, o->local_command);
1.277     bluhm    2512:        dump_cfg_string(oRemoteCommand, o->remote_command);
1.221     djm      2513:        dump_cfg_string(oLogLevel, log_level_name(o->log_level));
                   2514:        dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
1.266     djm      2515: #ifdef ENABLE_PKCS11
1.221     djm      2516:        dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
1.266     djm      2517: #endif
1.221     djm      2518:        dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
1.242     djm      2519:        dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
1.230     djm      2520:        dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
1.221     djm      2521:        dump_cfg_string(oXAuthLocation, o->xauth_location);
                   2522:
1.230     djm      2523:        /* Forwards */
1.221     djm      2524:        dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
                   2525:        dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
                   2526:        dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
                   2527:
                   2528:        /* String array options */
                   2529:        dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
                   2530:        dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
                   2531:        dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
                   2532:        dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
                   2533:        dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
                   2534:
                   2535:        /* Special cases */
                   2536:
                   2537:        /* oConnectTimeout */
                   2538:        if (o->connection_timeout == -1)
                   2539:                printf("connecttimeout none\n");
                   2540:        else
                   2541:                dump_cfg_int(oConnectTimeout, o->connection_timeout);
                   2542:
                   2543:        /* oTunnelDevice */
                   2544:        printf("tunneldevice");
                   2545:        if (o->tun_local == SSH_TUNID_ANY)
                   2546:                printf(" any");
                   2547:        else
                   2548:                printf(" %d", o->tun_local);
                   2549:        if (o->tun_remote == SSH_TUNID_ANY)
                   2550:                printf(":any");
                   2551:        else
                   2552:                printf(":%d", o->tun_remote);
                   2553:        printf("\n");
                   2554:
                   2555:        /* oCanonicalizePermittedCNAMEs */
                   2556:        if ( o->num_permitted_cnames > 0) {
                   2557:                printf("canonicalizePermittedcnames");
                   2558:                for (i = 0; i < o->num_permitted_cnames; i++) {
                   2559:                        printf(" %s:%s", o->permitted_cnames[i].source_list,
                   2560:                            o->permitted_cnames[i].target_list);
                   2561:                }
                   2562:                printf("\n");
                   2563:        }
                   2564:
                   2565:        /* oControlPersist */
                   2566:        if (o->control_persist == 0 || o->control_persist_timeout == 0)
                   2567:                dump_cfg_fmtint(oControlPersist, o->control_persist);
                   2568:        else
                   2569:                dump_cfg_int(oControlPersist, o->control_persist_timeout);
                   2570:
                   2571:        /* oEscapeChar */
                   2572:        if (o->escape_char == SSH_ESCAPECHAR_NONE)
                   2573:                printf("escapechar none\n");
                   2574:        else {
1.257     djm      2575:                vis(buf, o->escape_char, VIS_WHITE, 0);
                   2576:                printf("escapechar %s\n", buf);
1.221     djm      2577:        }
                   2578:
                   2579:        /* oIPQoS */
                   2580:        printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
                   2581:        printf("%s\n", iptos2str(o->ip_qos_bulk));
                   2582:
                   2583:        /* oRekeyLimit */
1.249     dtucker  2584:        printf("rekeylimit %llu %d\n",
                   2585:            (unsigned long long)o->rekey_limit, o->rekey_interval);
1.221     djm      2586:
                   2587:        /* oStreamLocalBindMask */
                   2588:        printf("streamlocalbindmask 0%o\n",
                   2589:            o->fwd_opts.streamlocal_bind_mask);
1.257     djm      2590:
                   2591:        /* oProxyCommand / oProxyJump */
                   2592:        if (o->jump_host == NULL)
                   2593:                dump_cfg_string(oProxyCommand, o->proxy_command);
                   2594:        else {
                   2595:                /* Check for numeric addresses */
                   2596:                i = strchr(o->jump_host, ':') != NULL ||
                   2597:                    strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
                   2598:                snprintf(buf, sizeof(buf), "%d", o->jump_port);
                   2599:                printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
1.259     djm      2600:                    /* optional additional jump spec */
                   2601:                    o->jump_extra == NULL ? "" : o->jump_extra,
                   2602:                    o->jump_extra == NULL ? "" : ",",
1.257     djm      2603:                    /* optional user */
                   2604:                    o->jump_user == NULL ? "" : o->jump_user,
                   2605:                    o->jump_user == NULL ? "" : "@",
                   2606:                    /* opening [ if hostname is numeric */
                   2607:                    i ? "[" : "",
                   2608:                    /* mandatory hostname */
                   2609:                    o->jump_host,
                   2610:                    /* closing ] if hostname is numeric */
                   2611:                    i ? "]" : "",
                   2612:                    /* optional port number */
                   2613:                    o->jump_port <= 0 ? "" : ":",
1.259     djm      2614:                    o->jump_port <= 0 ? "" : buf);
1.257     djm      2615:        }
1.1       deraadt  2616: }