[BACK]Return to readconf.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / crypto / external / bsd / openssh / dist

Annotation of src/crypto/external/bsd/openssh/dist/readconf.c, Revision 1.32

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

CVSweb <webmaster@jp.NetBSD.org>