[BACK]Return to readconf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/readconf.c, Revision 1.237

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