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

1.219   ! djm         1: /* $OpenBSD: readconf.c,v 1.218 2014/02/23 20:11:36 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.152     stevesk    19:
                     20: #include <netinet/in.h>
1.190     djm        21: #include <netinet/in_systm.h>
                     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.200     dtucker    34: #include <util.h>
1.1       deraadt    35:
1.159     deraadt    36: #include "xmalloc.h"
1.1       deraadt    37: #include "ssh.h"
1.25      markus     38: #include "compat.h"
1.58      markus     39: #include "cipher.h"
1.55      markus     40: #include "pathnames.h"
1.58      markus     41: #include "log.h"
1.159     deraadt    42: #include "key.h"
1.58      markus     43: #include "readconf.h"
                     44: #include "match.h"
                     45: #include "misc.h"
1.159     deraadt    46: #include "buffer.h"
1.62      markus     47: #include "kex.h"
                     48: #include "mac.h"
1.206     djm        49: #include "uidswap.h"
1.1       deraadt    50:
                     51: /* Format of the configuration file:
                     52:
                     53:    # Configuration data is parsed as follows:
                     54:    #  1. command line options
                     55:    #  2. user-specific file
                     56:    #  3. system-wide file
                     57:    # Any configuration value is only changed the first time it is set.
                     58:    # Thus, host-specific definitions should be at the beginning of the
                     59:    # configuration file, and defaults at the end.
                     60:
                     61:    # Host-specific declarations.  These may override anything above.  A single
                     62:    # host may match multiple declarations; these are processed in the order
                     63:    # that they are given in.
                     64:
                     65:    Host *.ngs.fi ngs.fi
1.96      markus     66:      User foo
1.1       deraadt    67:
                     68:    Host fake.com
                     69:      HostName another.host.name.real.org
                     70:      User blaah
                     71:      Port 34289
                     72:      ForwardX11 no
                     73:      ForwardAgent no
                     74:
                     75:    Host books.com
                     76:      RemoteForward 9999 shadows.cs.hut.fi:9999
                     77:      Cipher 3des
                     78:
                     79:    Host fascist.blob.com
                     80:      Port 23123
                     81:      User tylonen
                     82:      PasswordAuthentication no
                     83:
                     84:    Host puukko.hut.fi
                     85:      User t35124p
                     86:      ProxyCommand ssh-proxy %h %p
                     87:
                     88:    Host *.fr
1.96      markus     89:      PublicKeyAuthentication no
1.1       deraadt    90:
                     91:    Host *.su
                     92:      Cipher none
                     93:      PasswordAuthentication no
                     94:
1.144     reyk       95:    Host vpn.fake.com
                     96:      Tunnel yes
                     97:      TunnelDevice 3
                     98:
1.1       deraadt    99:    # Defaults for various options
                    100:    Host *
                    101:      ForwardAgent no
1.50      markus    102:      ForwardX11 no
1.1       deraadt   103:      PasswordAuthentication yes
                    104:      RSAAuthentication yes
                    105:      RhostsRSAAuthentication yes
                    106:      StrictHostKeyChecking yes
1.126     markus    107:      TcpKeepAlive no
1.1       deraadt   108:      IdentityFile ~/.ssh/identity
                    109:      Port 22
                    110:      EscapeChar ~
                    111:
                    112: */
                    113:
                    114: /* Keyword tokens. */
                    115:
1.17      markus    116: typedef enum {
                    117:        oBadOption,
1.206     djm       118:        oHost, oMatch,
1.186     djm       119:        oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
                    120:        oGatewayPorts, oExitOnForwardFailure,
1.100     deraadt   121:        oPasswordAuthentication, oRSAAuthentication,
1.59      markus    122:        oChallengeResponseAuthentication, oXAuthLocation,
1.17      markus    123:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
1.206     djm       124:        oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
1.17      markus    125:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    126:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126     markus    127:        oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62      markus    128:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50      markus    129:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67      markus    130:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    131:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.183     markus    132:        oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
1.96      markus    133:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       134:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    135:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    136:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.187     djm       137:        oSendEnv, oControlPath, oControlMaster, oControlPersist,
                    138:        oHashKnownHosts,
1.144     reyk      139:        oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
1.216     djm       140:        oVisualHostKey, oUseRoaming,
1.205     djm       141:        oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
1.209     djm       142:        oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
                    143:        oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
1.199     djm       144:        oIgnoredUnknownOption, oDeprecated, oUnsupported
1.1       deraadt   145: } OpCodes;
                    146:
                    147: /* Textual representations of the tokens. */
                    148:
1.17      markus    149: static struct {
                    150:        const char *name;
                    151:        OpCodes opcode;
                    152: } keywords[] = {
                    153:        { "forwardagent", oForwardAgent },
                    154:        { "forwardx11", oForwardX11 },
1.123     markus    155:        { "forwardx11trusted", oForwardX11Trusted },
1.186     djm       156:        { "forwardx11timeout", oForwardX11Timeout },
1.153     markus    157:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    158:        { "xauthlocation", oXAuthLocation },
1.17      markus    159:        { "gatewayports", oGatewayPorts },
                    160:        { "useprivilegedport", oUsePrivilegedPort },
1.116     markus    161:        { "rhostsauthentication", oDeprecated },
1.17      markus    162:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    163:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    164:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17      markus    165:        { "rsaauthentication", oRSAAuthentication },
1.50      markus    166:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    167:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.72      markus    168:        { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73      markus    169:        { "hostbasedauthentication", oHostbasedAuthentication },
1.59      markus    170:        { "challengeresponseauthentication", oChallengeResponseAuthentication },
                    171:        { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
                    172:        { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
1.110     jakob     173:        { "kerberosauthentication", oUnsupported },
                    174:        { "kerberostgtpassing", oUnsupported },
                    175:        { "afstokenpassing", oUnsupported },
1.118     markus    176: #if defined(GSSAPI)
                    177:        { "gssapiauthentication", oGssAuthentication },
                    178:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    179: #else
                    180:        { "gssapiauthentication", oUnsupported },
                    181:        { "gssapidelegatecredentials", oUnsupported },
                    182: #endif
1.96      markus    183:        { "fallbacktorsh", oDeprecated },
                    184:        { "usersh", oDeprecated },
1.17      markus    185:        { "identityfile", oIdentityFile },
1.174     stevesk   186:        { "identityfile2", oIdentityFile },                     /* obsolete */
1.128     markus    187:        { "identitiesonly", oIdentitiesOnly },
1.17      markus    188:        { "hostname", oHostName },
1.52      markus    189:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    190:        { "proxycommand", oProxyCommand },
                    191:        { "port", oPort },
                    192:        { "cipher", oCipher },
1.25      markus    193:        { "ciphers", oCiphers },
1.62      markus    194:        { "macs", oMacs },
1.25      markus    195:        { "protocol", oProtocol },
1.17      markus    196:        { "remoteforward", oRemoteForward },
                    197:        { "localforward", oLocalForward },
                    198:        { "user", oUser },
                    199:        { "host", oHost },
1.206     djm       200:        { "match", oMatch },
1.17      markus    201:        { "escapechar", oEscapeChar },
                    202:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.193     djm       203:        { "globalknownhostsfile2", oDeprecated },
1.174     stevesk   204:        { "userknownhostsfile", oUserKnownHostsFile },
1.193     djm       205:        { "userknownhostsfile2", oDeprecated },
1.17      markus    206:        { "connectionattempts", oConnectionAttempts },
                    207:        { "batchmode", oBatchMode },
                    208:        { "checkhostip", oCheckHostIP },
                    209:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    210:        { "compression", oCompression },
                    211:        { "compressionlevel", oCompressionLevel },
1.126     markus    212:        { "tcpkeepalive", oTCPKeepAlive },
                    213:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    214:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
                    215:        { "loglevel", oLogLevel },
1.71      markus    216:        { "dynamicforward", oDynamicForward },
1.67      markus    217:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    218:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77      markus    219:        { "bindaddress", oBindAddress },
1.183     markus    220: #ifdef ENABLE_PKCS11
                    221:        { "smartcarddevice", oPKCS11Provider },
                    222:        { "pkcs11provider", oPKCS11Provider },
1.110     jakob     223: #else
                    224:        { "smartcarddevice", oUnsupported },
1.183     markus    225:        { "pkcs11provider", oUnsupported },
1.110     jakob     226: #endif
1.93      deraadt   227:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    228:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     229:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   230:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    231:        { "rekeylimit", oRekeyLimit },
1.111     djm       232:        { "connecttimeout", oConnectTimeout },
1.112     djm       233:        { "addressfamily", oAddressFamily },
1.127     markus    234:        { "serveraliveinterval", oServerAliveInterval },
                    235:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       236:        { "sendenv", oSendEnv },
1.132     djm       237:        { "controlpath", oControlPath },
                    238:        { "controlmaster", oControlMaster },
1.187     djm       239:        { "controlpersist", oControlPersist },
1.136     djm       240:        { "hashknownhosts", oHashKnownHosts },
1.144     reyk      241:        { "tunnel", oTunnel },
                    242:        { "tunneldevice", oTunnelDevice },
                    243:        { "localcommand", oLocalCommand },
                    244:        { "permitlocalcommand", oPermitLocalCommand },
1.167     grunk     245:        { "visualhostkey", oVisualHostKey },
1.177     andreas   246:        { "useroaming", oUseRoaming },
1.189     djm       247:        { "kexalgorithms", oKexAlgorithms },
1.190     djm       248:        { "ipqos", oIPQoS },
1.192     djm       249:        { "requesttty", oRequestTTY },
1.205     djm       250:        { "proxyusefdpass", oProxyUseFdpass },
1.208     djm       251:        { "canonicaldomains", oCanonicalDomains },
1.209     djm       252:        { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
                    253:        { "canonicalizehostname", oCanonicalizeHostname },
                    254:        { "canonicalizemaxdots", oCanonicalizeMaxDots },
                    255:        { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
1.199     djm       256:        { "ignoreunknown", oIgnoreUnknown },
1.171     djm       257:
1.92      stevesk   258:        { NULL, oBadOption }
1.13      markus    259: };
                    260:
1.19      markus    261: /*
                    262:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    263:  * error.
                    264:  */
1.1       deraadt   265:
1.26      markus    266: void
1.135     djm       267: add_local_forward(Options *options, const Forward *newfwd)
1.1       deraadt   268: {
1.17      markus    269:        Forward *fwd;
                    270:        extern uid_t original_real_uid;
1.185     djm       271:
1.135     djm       272:        if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
1.64      millert   273:                fatal("Privileged ports can only be forwarded by root.");
1.185     djm       274:        options->local_forwards = xrealloc(options->local_forwards,
                    275:            options->num_local_forwards + 1,
                    276:            sizeof(*options->local_forwards));
1.17      markus    277:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       278:
1.172     stevesk   279:        fwd->listen_host = newfwd->listen_host;
1.135     djm       280:        fwd->listen_port = newfwd->listen_port;
1.172     stevesk   281:        fwd->connect_host = newfwd->connect_host;
1.135     djm       282:        fwd->connect_port = newfwd->connect_port;
1.1       deraadt   283: }
                    284:
1.19      markus    285: /*
                    286:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    287:  * an error.
                    288:  */
1.1       deraadt   289:
1.26      markus    290: void
1.135     djm       291: add_remote_forward(Options *options, const Forward *newfwd)
1.1       deraadt   292: {
1.17      markus    293:        Forward *fwd;
1.185     djm       294:
                    295:        options->remote_forwards = xrealloc(options->remote_forwards,
                    296:            options->num_remote_forwards + 1,
                    297:            sizeof(*options->remote_forwards));
1.17      markus    298:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       299:
1.172     stevesk   300:        fwd->listen_host = newfwd->listen_host;
1.135     djm       301:        fwd->listen_port = newfwd->listen_port;
1.172     stevesk   302:        fwd->connect_host = newfwd->connect_host;
1.135     djm       303:        fwd->connect_port = newfwd->connect_port;
1.194     markus    304:        fwd->handle = newfwd->handle;
1.184     markus    305:        fwd->allocated_port = 0;
1.1       deraadt   306: }
                    307:
1.90      stevesk   308: static void
                    309: clear_forwardings(Options *options)
                    310: {
                    311:        int i;
                    312:
1.135     djm       313:        for (i = 0; i < options->num_local_forwards; i++) {
1.202     djm       314:                free(options->local_forwards[i].listen_host);
                    315:                free(options->local_forwards[i].connect_host);
1.135     djm       316:        }
1.185     djm       317:        if (options->num_local_forwards > 0) {
1.202     djm       318:                free(options->local_forwards);
1.185     djm       319:                options->local_forwards = NULL;
                    320:        }
1.90      stevesk   321:        options->num_local_forwards = 0;
1.135     djm       322:        for (i = 0; i < options->num_remote_forwards; i++) {
1.202     djm       323:                free(options->remote_forwards[i].listen_host);
                    324:                free(options->remote_forwards[i].connect_host);
1.135     djm       325:        }
1.185     djm       326:        if (options->num_remote_forwards > 0) {
1.202     djm       327:                free(options->remote_forwards);
1.185     djm       328:                options->remote_forwards = NULL;
                    329:        }
1.90      stevesk   330:        options->num_remote_forwards = 0;
1.145     reyk      331:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   332: }
                    333:
1.195     dtucker   334: void
                    335: add_identity_file(Options *options, const char *dir, const char *filename,
                    336:     int userprovided)
                    337: {
                    338:        char *path;
1.219   ! djm       339:        int i;
1.195     dtucker   340:
                    341:        if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
                    342:                fatal("Too many identity files specified (max %d)",
                    343:                    SSH_MAX_IDENTITY_FILES);
                    344:
                    345:        if (dir == NULL) /* no dir, filename is absolute */
                    346:                path = xstrdup(filename);
                    347:        else
                    348:                (void)xasprintf(&path, "%.100s%.100s", dir, filename);
1.219   ! djm       349:
        !           350:        /* Avoid registering duplicates */
        !           351:        for (i = 0; i < options->num_identity_files; i++) {
        !           352:                if (options->identity_file_userprovided[i] == userprovided &&
        !           353:                    strcmp(options->identity_files[i], path) == 0) {
        !           354:                        debug2("%s: ignoring duplicate key %s", __func__, path);
        !           355:                        free(path);
        !           356:                        return;
        !           357:                }
        !           358:        }
1.195     dtucker   359:
                    360:        options->identity_file_userprovided[options->num_identity_files] =
                    361:            userprovided;
                    362:        options->identity_files[options->num_identity_files++] = path;
                    363: }
                    364:
1.206     djm       365: int
                    366: default_ssh_port(void)
                    367: {
                    368:        static int port;
                    369:        struct servent *sp;
                    370:
                    371:        if (port == 0) {
                    372:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    373:                port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
                    374:        }
                    375:        return port;
                    376: }
                    377:
                    378: /*
                    379:  * Execute a command in a shell.
                    380:  * Return its exit status or -1 on abnormal exit.
                    381:  */
                    382: static int
                    383: execute_in_shell(const char *cmd)
                    384: {
                    385:        char *shell, *command_string;
                    386:        pid_t pid;
                    387:        int devnull, status;
                    388:        extern uid_t original_real_uid;
                    389:
                    390:        if ((shell = getenv("SHELL")) == NULL)
                    391:                shell = _PATH_BSHELL;
                    392:
                    393:        /*
                    394:         * Use "exec" to avoid "sh -c" processes on some platforms
                    395:         * (e.g. Solaris)
                    396:         */
                    397:        xasprintf(&command_string, "exec %s", cmd);
                    398:
                    399:        /* Need this to redirect subprocess stdin/out */
                    400:        if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
                    401:                fatal("open(/dev/null): %s", strerror(errno));
                    402:
                    403:        debug("Executing command: '%.500s'", cmd);
                    404:
                    405:        /* Fork and execute the command. */
                    406:        if ((pid = fork()) == 0) {
                    407:                char *argv[4];
                    408:
                    409:                /* Child.  Permanently give up superuser privileges. */
                    410:                permanently_drop_suid(original_real_uid);
                    411:
                    412:                /* Redirect child stdin and stdout. Leave stderr */
                    413:                if (dup2(devnull, STDIN_FILENO) == -1)
                    414:                        fatal("dup2: %s", strerror(errno));
                    415:                if (dup2(devnull, STDOUT_FILENO) == -1)
                    416:                        fatal("dup2: %s", strerror(errno));
                    417:                if (devnull > STDERR_FILENO)
                    418:                        close(devnull);
                    419:                closefrom(STDERR_FILENO + 1);
                    420:
                    421:                argv[0] = shell;
                    422:                argv[1] = "-c";
                    423:                argv[2] = command_string;
                    424:                argv[3] = NULL;
                    425:
                    426:                execv(argv[0], argv);
                    427:                error("Unable to execute '%.100s': %s", cmd, strerror(errno));
                    428:                /* Die with signal to make this error apparent to parent. */
                    429:                signal(SIGTERM, SIG_DFL);
                    430:                kill(getpid(), SIGTERM);
                    431:                _exit(1);
                    432:        }
                    433:        /* Parent. */
                    434:        if (pid < 0)
                    435:                fatal("%s: fork: %.100s", __func__, strerror(errno));
                    436:
                    437:        close(devnull);
                    438:        free(command_string);
                    439:
                    440:        while (waitpid(pid, &status, 0) == -1) {
                    441:                if (errno != EINTR && errno != EAGAIN)
                    442:                        fatal("%s: waitpid: %s", __func__, strerror(errno));
                    443:        }
                    444:        if (!WIFEXITED(status)) {
                    445:                error("command '%.100s' exited abnormally", cmd);
                    446:                return -1;
                    447:        }
                    448:        debug3("command returned status %d", WEXITSTATUS(status));
                    449:        return WEXITSTATUS(status);
                    450: }
                    451:
                    452: /*
                    453:  * Parse and execute a Match directive.
                    454:  */
                    455: static int
                    456: match_cfg_line(Options *options, char **condition, struct passwd *pw,
                    457:     const char *host_arg, const char *filename, int linenum)
                    458: {
1.211     djm       459:        char *arg, *attrib, *cmd, *cp = *condition, *host;
                    460:        const char *ruser;
1.213     dtucker   461:        int r, port, result = 1, attributes = 0;
1.206     djm       462:        size_t len;
                    463:        char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
                    464:
                    465:        /*
                    466:         * Configuration is likely to be incomplete at this point so we
                    467:         * must be prepared to use default values.
                    468:         */
                    469:        port = options->port <= 0 ? default_ssh_port() : options->port;
                    470:        ruser = options->user == NULL ? pw->pw_name : options->user;
1.211     djm       471:        if (options->hostname != NULL) {
1.212     djm       472:                /* NB. Please keep in sync with ssh.c:main() */
1.211     djm       473:                host = percent_expand(options->hostname,
                    474:                    "h", host_arg, (char *)NULL);
                    475:        } else
                    476:                host = xstrdup(host_arg);
1.206     djm       477:
                    478:        debug3("checking match for '%s' host %s", cp, host);
                    479:        while ((attrib = strdelim(&cp)) && *attrib != '\0') {
1.213     dtucker   480:                attributes++;
                    481:                if (strcasecmp(attrib, "all") == 0) {
                    482:                        if (attributes != 1 ||
                    483:                            ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
                    484:                                error("'all' cannot be combined with other "
                    485:                                    "Match attributes");
                    486:                                result = -1;
                    487:                                goto out;
                    488:                        }
                    489:                        *condition = cp;
                    490:                        result = 1;
                    491:                        goto out;
                    492:                }
1.206     djm       493:                if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
                    494:                        error("Missing Match criteria for %s", attrib);
1.211     djm       495:                        result = -1;
                    496:                        goto out;
1.206     djm       497:                }
                    498:                len = strlen(arg);
                    499:                if (strcasecmp(attrib, "host") == 0) {
                    500:                        if (match_hostname(host, arg, len) != 1)
                    501:                                result = 0;
                    502:                        else
                    503:                                debug("%.200s line %d: matched 'Host %.100s' ",
                    504:                                    filename, linenum, host);
                    505:                } else if (strcasecmp(attrib, "originalhost") == 0) {
                    506:                        if (match_hostname(host_arg, arg, len) != 1)
                    507:                                result = 0;
                    508:                        else
                    509:                                debug("%.200s line %d: matched "
                    510:                                    "'OriginalHost %.100s' ",
                    511:                                    filename, linenum, host_arg);
                    512:                } else if (strcasecmp(attrib, "user") == 0) {
                    513:                        if (match_pattern_list(ruser, arg, len, 0) != 1)
                    514:                                result = 0;
                    515:                        else
                    516:                                debug("%.200s line %d: matched 'User %.100s' ",
                    517:                                    filename, linenum, ruser);
                    518:                } else if (strcasecmp(attrib, "localuser") == 0) {
                    519:                        if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
                    520:                                result = 0;
                    521:                        else
                    522:                                debug("%.200s line %d: matched "
                    523:                                    "'LocalUser %.100s' ",
                    524:                                    filename, linenum, pw->pw_name);
1.210     djm       525:                } else if (strcasecmp(attrib, "exec") == 0) {
1.206     djm       526:                        if (gethostname(thishost, sizeof(thishost)) == -1)
                    527:                                fatal("gethostname: %s", strerror(errno));
                    528:                        strlcpy(shorthost, thishost, sizeof(shorthost));
                    529:                        shorthost[strcspn(thishost, ".")] = '\0';
                    530:                        snprintf(portstr, sizeof(portstr), "%d", port);
                    531:
                    532:                        cmd = percent_expand(arg,
                    533:                            "L", shorthost,
                    534:                            "d", pw->pw_dir,
                    535:                            "h", host,
                    536:                            "l", thishost,
                    537:                            "n", host_arg,
                    538:                            "p", portstr,
                    539:                            "r", ruser,
                    540:                            "u", pw->pw_name,
                    541:                            (char *)NULL);
1.217     djm       542:                        if (result != 1) {
                    543:                                /* skip execution if prior predicate failed */
                    544:                                debug("%.200s line %d: skipped exec \"%.100s\"",
1.206     djm       545:                                    filename, linenum, cmd);
1.217     djm       546:                        } else {
                    547:                                r = execute_in_shell(cmd);
                    548:                                if (r == -1) {
                    549:                                        fatal("%.200s line %d: match exec "
                    550:                                            "'%.100s' error", filename,
                    551:                                            linenum, cmd);
                    552:                                } else if (r == 0) {
                    553:                                        debug("%.200s line %d: matched "
                    554:                                            "'exec \"%.100s\"'", filename,
                    555:                                            linenum, cmd);
                    556:                                } else {
                    557:                                        debug("%.200s line %d: no match "
                    558:                                            "'exec \"%.100s\"'", filename,
                    559:                                            linenum, cmd);
                    560:                                        result = 0;
                    561:                                }
                    562:                        }
1.206     djm       563:                        free(cmd);
                    564:                } else {
                    565:                        error("Unsupported Match attribute %s", attrib);
1.211     djm       566:                        result = -1;
                    567:                        goto out;
1.206     djm       568:                }
1.213     dtucker   569:        }
                    570:        if (attributes == 0) {
                    571:                error("One or more attributes required for Match");
                    572:                result = -1;
                    573:                goto out;
1.206     djm       574:        }
                    575:        debug3("match %sfound", result ? "" : "not ");
                    576:        *condition = cp;
1.211     djm       577:  out:
                    578:        free(host);
1.206     djm       579:        return result;
                    580: }
                    581:
1.208     djm       582: /* Check and prepare a domain name: removes trailing '.' and lowercases */
                    583: static void
                    584: valid_domain(char *name, const char *filename, int linenum)
                    585: {
                    586:        size_t i, l = strlen(name);
                    587:        u_char c, last = '\0';
                    588:
                    589:        if (l == 0)
                    590:                fatal("%s line %d: empty hostname suffix", filename, linenum);
                    591:        if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
                    592:                fatal("%s line %d: hostname suffix \"%.100s\" "
                    593:                    "starts with invalid character", filename, linenum, name);
                    594:        for (i = 0; i < l; i++) {
                    595:                c = tolower((u_char)name[i]);
                    596:                name[i] = (char)c;
                    597:                if (last == '.' && c == '.')
                    598:                        fatal("%s line %d: hostname suffix \"%.100s\" contains "
                    599:                            "consecutive separators", filename, linenum, name);
                    600:                if (c != '.' && c != '-' && !isalnum(c) &&
                    601:                    c != '_') /* technically invalid, but common */
                    602:                        fatal("%s line %d: hostname suffix \"%.100s\" contains "
                    603:                            "invalid characters", filename, linenum, name);
                    604:                last = c;
                    605:        }
                    606:        if (name[l - 1] == '.')
                    607:                name[l - 1] = '\0';
                    608: }
                    609:
1.19      markus    610: /*
1.70      stevesk   611:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    612:  */
1.26      markus    613: static OpCodes
1.199     djm       614: parse_token(const char *cp, const char *filename, int linenum,
                    615:     const char *ignored_unknown)
1.1       deraadt   616: {
1.199     djm       617:        int i;
1.1       deraadt   618:
1.17      markus    619:        for (i = 0; keywords[i].name; i++)
1.199     djm       620:                if (strcmp(cp, keywords[i].name) == 0)
1.17      markus    621:                        return keywords[i].opcode;
1.199     djm       622:        if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
                    623:            strlen(ignored_unknown), 1) == 1)
                    624:                return oIgnoredUnknownOption;
1.75      stevesk   625:        error("%s: line %d: Bad configuration option: %s",
                    626:            filename, linenum, cp);
1.17      markus    627:        return oBadOption;
1.1       deraadt   628: }
                    629:
1.207     djm       630: /* Multistate option parsing */
                    631: struct multistate {
                    632:        char *key;
                    633:        int value;
                    634: };
                    635: static const struct multistate multistate_flag[] = {
                    636:        { "true",                       1 },
                    637:        { "false",                      0 },
                    638:        { "yes",                        1 },
                    639:        { "no",                         0 },
                    640:        { NULL, -1 }
                    641: };
                    642: static const struct multistate multistate_yesnoask[] = {
                    643:        { "true",                       1 },
                    644:        { "false",                      0 },
                    645:        { "yes",                        1 },
                    646:        { "no",                         0 },
                    647:        { "ask",                        2 },
                    648:        { NULL, -1 }
                    649: };
                    650: static const struct multistate multistate_addressfamily[] = {
                    651:        { "inet",                       AF_INET },
                    652:        { "inet6",                      AF_INET6 },
                    653:        { "any",                        AF_UNSPEC },
                    654:        { NULL, -1 }
                    655: };
                    656: static const struct multistate multistate_controlmaster[] = {
                    657:        { "true",                       SSHCTL_MASTER_YES },
                    658:        { "yes",                        SSHCTL_MASTER_YES },
                    659:        { "false",                      SSHCTL_MASTER_NO },
                    660:        { "no",                         SSHCTL_MASTER_NO },
                    661:        { "auto",                       SSHCTL_MASTER_AUTO },
                    662:        { "ask",                        SSHCTL_MASTER_ASK },
                    663:        { "autoask",                    SSHCTL_MASTER_AUTO_ASK },
                    664:        { NULL, -1 }
                    665: };
                    666: static const struct multistate multistate_tunnel[] = {
                    667:        { "ethernet",                   SSH_TUNMODE_ETHERNET },
                    668:        { "point-to-point",             SSH_TUNMODE_POINTOPOINT },
                    669:        { "true",                       SSH_TUNMODE_DEFAULT },
                    670:        { "yes",                        SSH_TUNMODE_DEFAULT },
                    671:        { "false",                      SSH_TUNMODE_NO },
                    672:        { "no",                         SSH_TUNMODE_NO },
                    673:        { NULL, -1 }
                    674: };
                    675: static const struct multistate multistate_requesttty[] = {
                    676:        { "true",                       REQUEST_TTY_YES },
                    677:        { "yes",                        REQUEST_TTY_YES },
                    678:        { "false",                      REQUEST_TTY_NO },
                    679:        { "no",                         REQUEST_TTY_NO },
                    680:        { "force",                      REQUEST_TTY_FORCE },
                    681:        { "auto",                       REQUEST_TTY_AUTO },
                    682:        { NULL, -1 }
                    683: };
1.209     djm       684: static const struct multistate multistate_canonicalizehostname[] = {
1.208     djm       685:        { "true",                       SSH_CANONICALISE_YES },
                    686:        { "false",                      SSH_CANONICALISE_NO },
                    687:        { "yes",                        SSH_CANONICALISE_YES },
                    688:        { "no",                         SSH_CANONICALISE_NO },
                    689:        { "always",                     SSH_CANONICALISE_ALWAYS },
                    690:        { NULL, -1 }
                    691: };
1.207     djm       692:
1.19      markus    693: /*
                    694:  * Processes a single option line as used in the configuration files. This
                    695:  * only sets those values that have not already been set.
                    696:  */
1.102     markus    697: #define WHITESPACE " \t\r\n"
1.14      markus    698: int
1.206     djm       699: process_config_line(Options *options, struct passwd *pw, const char *host,
                    700:     char *line, const char *filename, int linenum, int *activep, int userconfig)
1.1       deraadt   701: {
1.193     djm       702:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
                    703:        char **cpptr, fwdarg[256];
1.199     djm       704:        u_int i, *uintptr, max_entries = 0;
1.206     djm       705:        int negated, opcode, *intptr, value, value2, cmdline = 0;
1.164     dtucker   706:        LogLevel *log_level_ptr;
1.201     dtucker   707:        long long val64;
1.102     markus    708:        size_t len;
1.135     djm       709:        Forward fwd;
1.207     djm       710:        const struct multistate *multistate_ptr;
1.208     djm       711:        struct allowed_cname *cname;
1.106     djm       712:
1.206     djm       713:        if (activep == NULL) { /* We are processing a command line directive */
                    714:                cmdline = 1;
                    715:                activep = &cmdline;
                    716:        }
                    717:
1.106     djm       718:        /* Strip trailing whitespace */
1.139     deraadt   719:        for (len = strlen(line) - 1; len > 0; len--) {
1.106     djm       720:                if (strchr(WHITESPACE, line[len]) == NULL)
                    721:                        break;
                    722:                line[len] = '\0';
                    723:        }
1.1       deraadt   724:
1.42      provos    725:        s = line;
                    726:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       727:        if ((keyword = strdelim(&s)) == NULL)
                    728:                return 0;
1.42      provos    729:        /* Ignore leading whitespace. */
                    730:        if (*keyword == '\0')
1.43      markus    731:                keyword = strdelim(&s);
1.56      deraadt   732:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    733:                return 0;
1.199     djm       734:        /* Match lowercase keyword */
1.207     djm       735:        lowercase(keyword);
1.17      markus    736:
1.199     djm       737:        opcode = parse_token(keyword, filename, linenum,
                    738:            options->ignored_unknown);
1.17      markus    739:
                    740:        switch (opcode) {
                    741:        case oBadOption:
1.19      markus    742:                /* don't panic, but count bad options */
                    743:                return -1;
1.17      markus    744:                /* NOTREACHED */
1.199     djm       745:        case oIgnoredUnknownOption:
                    746:                debug("%s line %d: Ignored unknown option \"%s\"",
                    747:                    filename, linenum, keyword);
                    748:                return 0;
1.111     djm       749:        case oConnectTimeout:
                    750:                intptr = &options->connection_timeout;
1.127     markus    751: parse_time:
1.111     djm       752:                arg = strdelim(&s);
                    753:                if (!arg || *arg == '\0')
                    754:                        fatal("%s line %d: missing time value.",
                    755:                            filename, linenum);
                    756:                if ((value = convtime(arg)) == -1)
                    757:                        fatal("%s line %d: invalid time value.",
                    758:                            filename, linenum);
1.160     dtucker   759:                if (*activep && *intptr == -1)
1.111     djm       760:                        *intptr = value;
                    761:                break;
                    762:
1.17      markus    763:        case oForwardAgent:
                    764:                intptr = &options->forward_agent;
1.207     djm       765:  parse_flag:
                    766:                multistate_ptr = multistate_flag;
                    767:  parse_multistate:
1.42      provos    768:                arg = strdelim(&s);
1.40      ho        769:                if (!arg || *arg == '\0')
1.207     djm       770:                        fatal("%s line %d: missing argument.",
                    771:                            filename, linenum);
                    772:                value = -1;
                    773:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                    774:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                    775:                                value = multistate_ptr[i].value;
                    776:                                break;
                    777:                        }
                    778:                }
                    779:                if (value == -1)
                    780:                        fatal("%s line %d: unsupported option \"%s\".",
                    781:                            filename, linenum, arg);
1.17      markus    782:                if (*activep && *intptr == -1)
                    783:                        *intptr = value;
                    784:                break;
                    785:
                    786:        case oForwardX11:
                    787:                intptr = &options->forward_x11;
                    788:                goto parse_flag;
                    789:
1.123     markus    790:        case oForwardX11Trusted:
                    791:                intptr = &options->forward_x11_trusted;
                    792:                goto parse_flag;
1.186     djm       793:
                    794:        case oForwardX11Timeout:
                    795:                intptr = &options->forward_x11_timeout;
                    796:                goto parse_time;
1.123     markus    797:
1.17      markus    798:        case oGatewayPorts:
                    799:                intptr = &options->gateway_ports;
                    800:                goto parse_flag;
                    801:
1.153     markus    802:        case oExitOnForwardFailure:
                    803:                intptr = &options->exit_on_forward_failure;
                    804:                goto parse_flag;
                    805:
1.17      markus    806:        case oUsePrivilegedPort:
                    807:                intptr = &options->use_privileged_port;
                    808:                goto parse_flag;
                    809:
                    810:        case oPasswordAuthentication:
                    811:                intptr = &options->password_authentication;
                    812:                goto parse_flag;
                    813:
1.48      markus    814:        case oKbdInteractiveAuthentication:
                    815:                intptr = &options->kbd_interactive_authentication;
                    816:                goto parse_flag;
                    817:
                    818:        case oKbdInteractiveDevices:
                    819:                charptr = &options->kbd_interactive_devices;
                    820:                goto parse_string;
                    821:
1.50      markus    822:        case oPubkeyAuthentication:
                    823:                intptr = &options->pubkey_authentication;
1.30      markus    824:                goto parse_flag;
                    825:
1.17      markus    826:        case oRSAAuthentication:
                    827:                intptr = &options->rsa_authentication;
                    828:                goto parse_flag;
                    829:
                    830:        case oRhostsRSAAuthentication:
                    831:                intptr = &options->rhosts_rsa_authentication;
                    832:                goto parse_flag;
                    833:
1.72      markus    834:        case oHostbasedAuthentication:
                    835:                intptr = &options->hostbased_authentication;
                    836:                goto parse_flag;
                    837:
1.59      markus    838:        case oChallengeResponseAuthentication:
1.78      markus    839:                intptr = &options->challenge_response_authentication;
1.17      markus    840:                goto parse_flag;
1.108     jakob     841:
1.118     markus    842:        case oGssAuthentication:
                    843:                intptr = &options->gss_authentication;
                    844:                goto parse_flag;
                    845:
                    846:        case oGssDelegateCreds:
                    847:                intptr = &options->gss_deleg_creds;
                    848:                goto parse_flag;
                    849:
1.17      markus    850:        case oBatchMode:
                    851:                intptr = &options->batch_mode;
                    852:                goto parse_flag;
                    853:
                    854:        case oCheckHostIP:
                    855:                intptr = &options->check_host_ip;
1.167     grunk     856:                goto parse_flag;
1.17      markus    857:
1.107     jakob     858:        case oVerifyHostKeyDNS:
                    859:                intptr = &options->verify_host_key_dns;
1.207     djm       860:                multistate_ptr = multistate_yesnoask;
                    861:                goto parse_multistate;
1.107     jakob     862:
1.17      markus    863:        case oStrictHostKeyChecking:
                    864:                intptr = &options->strict_host_key_checking;
1.207     djm       865:                multistate_ptr = multistate_yesnoask;
                    866:                goto parse_multistate;
1.17      markus    867:
                    868:        case oCompression:
                    869:                intptr = &options->compression;
                    870:                goto parse_flag;
                    871:
1.126     markus    872:        case oTCPKeepAlive:
                    873:                intptr = &options->tcp_keep_alive;
1.17      markus    874:                goto parse_flag;
                    875:
1.91      markus    876:        case oNoHostAuthenticationForLocalhost:
                    877:                intptr = &options->no_host_authentication_for_localhost;
                    878:                goto parse_flag;
                    879:
1.17      markus    880:        case oNumberOfPasswordPrompts:
                    881:                intptr = &options->number_of_password_prompts;
                    882:                goto parse_int;
                    883:
                    884:        case oCompressionLevel:
                    885:                intptr = &options->compression_level;
                    886:                goto parse_int;
                    887:
1.105     markus    888:        case oRekeyLimit:
                    889:                arg = strdelim(&s);
                    890:                if (!arg || *arg == '\0')
1.198     dtucker   891:                        fatal("%.200s line %d: Missing argument.", filename,
                    892:                            linenum);
                    893:                if (strcmp(arg, "default") == 0) {
                    894:                        val64 = 0;
                    895:                } else {
1.200     dtucker   896:                        if (scan_scaled(arg, &val64) == -1)
                    897:                                fatal("%.200s line %d: Bad number '%s': %s",
                    898:                                    filename, linenum, arg, strerror(errno));
                    899:                        /* check for too-large or too-small limits */
                    900:                        if (val64 > UINT_MAX)
1.198     dtucker   901:                                fatal("%.200s line %d: RekeyLimit too large",
                    902:                                    filename, linenum);
                    903:                        if (val64 != 0 && val64 < 16)
                    904:                                fatal("%.200s line %d: RekeyLimit too small",
                    905:                                    filename, linenum);
1.105     markus    906:                }
1.165     djm       907:                if (*activep && options->rekey_limit == -1)
                    908:                        options->rekey_limit = (u_int32_t)val64;
1.198     dtucker   909:                if (s != NULL) { /* optional rekey interval present */
                    910:                        if (strcmp(s, "none") == 0) {
                    911:                                (void)strdelim(&s);     /* discard */
                    912:                                break;
                    913:                        }
                    914:                        intptr = &options->rekey_interval;
                    915:                        goto parse_time;
                    916:                }
1.105     markus    917:                break;
                    918:
1.17      markus    919:        case oIdentityFile:
1.42      provos    920:                arg = strdelim(&s);
1.40      ho        921:                if (!arg || *arg == '\0')
1.17      markus    922:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    923:                if (*activep) {
1.50      markus    924:                        intptr = &options->num_identity_files;
1.27      markus    925:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    926:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt   927:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.196     dtucker   928:                        add_identity_file(options, NULL, arg, userconfig);
1.17      markus    929:                }
                    930:                break;
                    931:
1.34      markus    932:        case oXAuthLocation:
                    933:                charptr=&options->xauth_location;
                    934:                goto parse_string;
                    935:
1.17      markus    936:        case oUser:
                    937:                charptr = &options->user;
                    938: parse_string:
1.42      provos    939:                arg = strdelim(&s);
1.40      ho        940:                if (!arg || *arg == '\0')
1.193     djm       941:                        fatal("%.200s line %d: Missing argument.",
                    942:                            filename, linenum);
1.17      markus    943:                if (*activep && *charptr == NULL)
1.38      provos    944:                        *charptr = xstrdup(arg);
1.17      markus    945:                break;
                    946:
                    947:        case oGlobalKnownHostsFile:
1.193     djm       948:                cpptr = (char **)&options->system_hostfiles;
                    949:                uintptr = &options->num_system_hostfiles;
                    950:                max_entries = SSH_MAX_HOSTS_FILES;
                    951: parse_char_array:
                    952:                if (*activep && *uintptr == 0) {
                    953:                        while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                    954:                                if ((*uintptr) >= max_entries)
                    955:                                        fatal("%s line %d: "
                    956:                                            "too many authorized keys files.",
                    957:                                            filename, linenum);
                    958:                                cpptr[(*uintptr)++] = xstrdup(arg);
                    959:                        }
                    960:                }
                    961:                return 0;
1.17      markus    962:
                    963:        case oUserKnownHostsFile:
1.193     djm       964:                cpptr = (char **)&options->user_hostfiles;
                    965:                uintptr = &options->num_user_hostfiles;
                    966:                max_entries = SSH_MAX_HOSTS_FILES;
                    967:                goto parse_char_array;
1.27      markus    968:
1.17      markus    969:        case oHostName:
                    970:                charptr = &options->hostname;
                    971:                goto parse_string;
                    972:
1.52      markus    973:        case oHostKeyAlias:
                    974:                charptr = &options->host_key_alias;
                    975:                goto parse_string;
                    976:
1.67      markus    977:        case oPreferredAuthentications:
                    978:                charptr = &options->preferred_authentications;
                    979:                goto parse_string;
                    980:
1.77      markus    981:        case oBindAddress:
                    982:                charptr = &options->bind_address;
                    983:                goto parse_string;
                    984:
1.183     markus    985:        case oPKCS11Provider:
                    986:                charptr = &options->pkcs11_provider;
1.86      markus    987:                goto parse_string;
1.85      jakob     988:
1.17      markus    989:        case oProxyCommand:
1.144     reyk      990:                charptr = &options->proxy_command;
                    991: parse_command:
1.113     markus    992:                if (s == NULL)
                    993:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus    994:                len = strspn(s, WHITESPACE "=");
1.17      markus    995:                if (*activep && *charptr == NULL)
1.102     markus    996:                        *charptr = xstrdup(s + len);
1.17      markus    997:                return 0;
                    998:
                    999:        case oPort:
                   1000:                intptr = &options->port;
                   1001: parse_int:
1.42      provos   1002:                arg = strdelim(&s);
1.40      ho       1003:                if (!arg || *arg == '\0')
1.17      markus   1004:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1005:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus   1006:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus   1007:
                   1008:                /* Octal, decimal, or hex format? */
1.38      provos   1009:                value = strtol(arg, &endofnumber, 0);
                   1010:                if (arg == endofnumber)
1.21      markus   1011:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus   1012:                if (*activep && *intptr == -1)
                   1013:                        *intptr = value;
                   1014:                break;
                   1015:
                   1016:        case oConnectionAttempts:
                   1017:                intptr = &options->connection_attempts;
                   1018:                goto parse_int;
                   1019:
                   1020:        case oCipher:
                   1021:                intptr = &options->cipher;
1.42      provos   1022:                arg = strdelim(&s);
1.40      ho       1023:                if (!arg || *arg == '\0')
1.32      markus   1024:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1025:                value = cipher_number(arg);
1.17      markus   1026:                if (value == -1)
                   1027:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.93      deraadt  1028:                            filename, linenum, arg ? arg : "<NONE>");
1.17      markus   1029:                if (*activep && *intptr == -1)
                   1030:                        *intptr = value;
                   1031:                break;
                   1032:
1.25      markus   1033:        case oCiphers:
1.42      provos   1034:                arg = strdelim(&s);
1.40      ho       1035:                if (!arg || *arg == '\0')
1.32      markus   1036:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1037:                if (!ciphers_valid(arg))
1.31      markus   1038:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt  1039:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1040:                if (*activep && options->ciphers == NULL)
1.38      provos   1041:                        options->ciphers = xstrdup(arg);
1.25      markus   1042:                break;
                   1043:
1.62      markus   1044:        case oMacs:
                   1045:                arg = strdelim(&s);
                   1046:                if (!arg || *arg == '\0')
                   1047:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1048:                if (!mac_valid(arg))
                   1049:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt  1050:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus   1051:                if (*activep && options->macs == NULL)
                   1052:                        options->macs = xstrdup(arg);
                   1053:                break;
                   1054:
1.189     djm      1055:        case oKexAlgorithms:
                   1056:                arg = strdelim(&s);
                   1057:                if (!arg || *arg == '\0')
                   1058:                        fatal("%.200s line %d: Missing argument.",
                   1059:                            filename, linenum);
                   1060:                if (!kex_names_valid(arg))
                   1061:                        fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
                   1062:                            filename, linenum, arg ? arg : "<NONE>");
                   1063:                if (*activep && options->kex_algorithms == NULL)
                   1064:                        options->kex_algorithms = xstrdup(arg);
                   1065:                break;
                   1066:
1.76      markus   1067:        case oHostKeyAlgorithms:
                   1068:                arg = strdelim(&s);
                   1069:                if (!arg || *arg == '\0')
                   1070:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1071:                if (!key_names_valid2(arg))
                   1072:                        fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93      deraadt  1073:                            filename, linenum, arg ? arg : "<NONE>");
1.76      markus   1074:                if (*activep && options->hostkeyalgorithms == NULL)
                   1075:                        options->hostkeyalgorithms = xstrdup(arg);
                   1076:                break;
                   1077:
1.25      markus   1078:        case oProtocol:
                   1079:                intptr = &options->protocol;
1.42      provos   1080:                arg = strdelim(&s);
1.40      ho       1081:                if (!arg || *arg == '\0')
1.32      markus   1082:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1083:                value = proto_spec(arg);
1.25      markus   1084:                if (value == SSH_PROTO_UNKNOWN)
                   1085:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93      deraadt  1086:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1087:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                   1088:                        *intptr = value;
                   1089:                break;
                   1090:
1.17      markus   1091:        case oLogLevel:
1.164     dtucker  1092:                log_level_ptr = &options->log_level;
1.42      provos   1093:                arg = strdelim(&s);
1.38      provos   1094:                value = log_level_number(arg);
1.95      markus   1095:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert  1096:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt  1097:                            filename, linenum, arg ? arg : "<NONE>");
1.164     dtucker  1098:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                   1099:                        *log_level_ptr = (LogLevel) value;
1.17      markus   1100:                break;
                   1101:
1.88      stevesk  1102:        case oLocalForward:
1.17      markus   1103:        case oRemoteForward:
1.168     stevesk  1104:        case oDynamicForward:
1.42      provos   1105:                arg = strdelim(&s);
1.135     djm      1106:                if (arg == NULL || *arg == '\0')
1.88      stevesk  1107:                        fatal("%.200s line %d: Missing port argument.",
                   1108:                            filename, linenum);
1.135     djm      1109:
1.168     stevesk  1110:                if (opcode == oLocalForward ||
                   1111:                    opcode == oRemoteForward) {
                   1112:                        arg2 = strdelim(&s);
                   1113:                        if (arg2 == NULL || *arg2 == '\0')
                   1114:                                fatal("%.200s line %d: Missing target argument.",
                   1115:                                    filename, linenum);
1.135     djm      1116:
1.168     stevesk  1117:                        /* construct a string for parse_forward */
                   1118:                        snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
                   1119:                } else if (opcode == oDynamicForward) {
                   1120:                        strlcpy(fwdarg, arg, sizeof(fwdarg));
                   1121:                }
                   1122:
                   1123:                if (parse_forward(&fwd, fwdarg,
1.176     djm      1124:                    opcode == oDynamicForward ? 1 : 0,
                   1125:                    opcode == oRemoteForward ? 1 : 0) == 0)
1.88      stevesk  1126:                        fatal("%.200s line %d: Bad forwarding specification.",
                   1127:                            filename, linenum);
1.135     djm      1128:
1.88      stevesk  1129:                if (*activep) {
1.168     stevesk  1130:                        if (opcode == oLocalForward ||
                   1131:                            opcode == oDynamicForward)
1.135     djm      1132:                                add_local_forward(options, &fwd);
1.88      stevesk  1133:                        else if (opcode == oRemoteForward)
1.135     djm      1134:                                add_remote_forward(options, &fwd);
1.88      stevesk  1135:                }
1.17      markus   1136:                break;
1.71      markus   1137:
1.90      stevesk  1138:        case oClearAllForwardings:
                   1139:                intptr = &options->clear_forwardings;
                   1140:                goto parse_flag;
                   1141:
1.17      markus   1142:        case oHost:
1.206     djm      1143:                if (cmdline)
                   1144:                        fatal("Host directive not supported as a command-line "
                   1145:                            "option");
1.17      markus   1146:                *activep = 0;
1.191     djm      1147:                arg2 = NULL;
                   1148:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1149:                        negated = *arg == '!';
                   1150:                        if (negated)
                   1151:                                arg++;
1.38      provos   1152:                        if (match_pattern(host, arg)) {
1.191     djm      1153:                                if (negated) {
                   1154:                                        debug("%.200s line %d: Skipping Host "
                   1155:                                            "block because of negated match "
                   1156:                                            "for %.100s", filename, linenum,
                   1157:                                            arg);
                   1158:                                        *activep = 0;
                   1159:                                        break;
                   1160:                                }
                   1161:                                if (!*activep)
                   1162:                                        arg2 = arg; /* logged below */
1.17      markus   1163:                                *activep = 1;
                   1164:                        }
1.191     djm      1165:                }
                   1166:                if (*activep)
                   1167:                        debug("%.200s line %d: Applying options for %.100s",
                   1168:                            filename, linenum, arg2);
1.42      provos   1169:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus   1170:                return 0;
                   1171:
1.206     djm      1172:        case oMatch:
                   1173:                if (cmdline)
                   1174:                        fatal("Host directive not supported as a command-line "
                   1175:                            "option");
                   1176:                value = match_cfg_line(options, &s, pw, host,
                   1177:                    filename, linenum);
                   1178:                if (value < 0)
                   1179:                        fatal("%.200s line %d: Bad Match condition", filename,
                   1180:                            linenum);
                   1181:                *activep = value;
                   1182:                break;
                   1183:
1.17      markus   1184:        case oEscapeChar:
                   1185:                intptr = &options->escape_char;
1.42      provos   1186:                arg = strdelim(&s);
1.40      ho       1187:                if (!arg || *arg == '\0')
1.17      markus   1188:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1189:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus   1190:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                   1191:                        value = (u_char) arg[1] & 31;
1.38      provos   1192:                else if (strlen(arg) == 1)
1.51      markus   1193:                        value = (u_char) arg[0];
1.38      provos   1194:                else if (strcmp(arg, "none") == 0)
1.79      stevesk  1195:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus   1196:                else {
                   1197:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt  1198:                            filename, linenum);
1.17      markus   1199:                        /* NOTREACHED */
                   1200:                        value = 0;      /* Avoid compiler warning. */
                   1201:                }
                   1202:                if (*activep && *intptr == -1)
                   1203:                        *intptr = value;
1.112     djm      1204:                break;
                   1205:
                   1206:        case oAddressFamily:
1.114     djm      1207:                intptr = &options->address_family;
1.207     djm      1208:                multistate_ptr = multistate_addressfamily;
                   1209:                goto parse_multistate;
1.17      markus   1210:
1.101     markus   1211:        case oEnableSSHKeysign:
                   1212:                intptr = &options->enable_ssh_keysign;
                   1213:                goto parse_flag;
                   1214:
1.128     markus   1215:        case oIdentitiesOnly:
                   1216:                intptr = &options->identities_only;
                   1217:                goto parse_flag;
                   1218:
1.127     markus   1219:        case oServerAliveInterval:
                   1220:                intptr = &options->server_alive_interval;
                   1221:                goto parse_time;
                   1222:
                   1223:        case oServerAliveCountMax:
                   1224:                intptr = &options->server_alive_count_max;
                   1225:                goto parse_int;
                   1226:
1.130     djm      1227:        case oSendEnv:
                   1228:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1229:                        if (strchr(arg, '=') != NULL)
                   1230:                                fatal("%s line %d: Invalid environment name.",
                   1231:                                    filename, linenum);
1.137     djm      1232:                        if (!*activep)
                   1233:                                continue;
1.130     djm      1234:                        if (options->num_send_env >= MAX_SEND_ENV)
                   1235:                                fatal("%s line %d: too many send env.",
                   1236:                                    filename, linenum);
                   1237:                        options->send_env[options->num_send_env++] =
                   1238:                            xstrdup(arg);
                   1239:                }
                   1240:                break;
                   1241:
1.132     djm      1242:        case oControlPath:
                   1243:                charptr = &options->control_path;
                   1244:                goto parse_string;
                   1245:
                   1246:        case oControlMaster:
                   1247:                intptr = &options->control_master;
1.207     djm      1248:                multistate_ptr = multistate_controlmaster;
                   1249:                goto parse_multistate;
1.132     djm      1250:
1.187     djm      1251:        case oControlPersist:
                   1252:                /* no/false/yes/true, or a time spec */
                   1253:                intptr = &options->control_persist;
                   1254:                arg = strdelim(&s);
                   1255:                if (!arg || *arg == '\0')
                   1256:                        fatal("%.200s line %d: Missing ControlPersist"
                   1257:                            " argument.", filename, linenum);
                   1258:                value = 0;
                   1259:                value2 = 0;     /* timeout */
                   1260:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                   1261:                        value = 0;
                   1262:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                   1263:                        value = 1;
                   1264:                else if ((value2 = convtime(arg)) >= 0)
                   1265:                        value = 1;
                   1266:                else
                   1267:                        fatal("%.200s line %d: Bad ControlPersist argument.",
                   1268:                            filename, linenum);
                   1269:                if (*activep && *intptr == -1) {
                   1270:                        *intptr = value;
                   1271:                        options->control_persist_timeout = value2;
                   1272:                }
                   1273:                break;
                   1274:
1.136     djm      1275:        case oHashKnownHosts:
                   1276:                intptr = &options->hash_known_hosts;
                   1277:                goto parse_flag;
                   1278:
1.144     reyk     1279:        case oTunnel:
                   1280:                intptr = &options->tun_open;
1.207     djm      1281:                multistate_ptr = multistate_tunnel;
                   1282:                goto parse_multistate;
1.144     reyk     1283:
                   1284:        case oTunnelDevice:
                   1285:                arg = strdelim(&s);
                   1286:                if (!arg || *arg == '\0')
                   1287:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1288:                value = a2tun(arg, &value2);
1.145     reyk     1289:                if (value == SSH_TUNID_ERR)
1.144     reyk     1290:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                   1291:                if (*activep) {
                   1292:                        options->tun_local = value;
                   1293:                        options->tun_remote = value2;
                   1294:                }
                   1295:                break;
                   1296:
                   1297:        case oLocalCommand:
                   1298:                charptr = &options->local_command;
                   1299:                goto parse_command;
                   1300:
                   1301:        case oPermitLocalCommand:
                   1302:                intptr = &options->permit_local_command;
                   1303:                goto parse_flag;
                   1304:
1.167     grunk    1305:        case oVisualHostKey:
                   1306:                intptr = &options->visual_host_key;
                   1307:                goto parse_flag;
                   1308:
1.190     djm      1309:        case oIPQoS:
                   1310:                arg = strdelim(&s);
                   1311:                if ((value = parse_ipqos(arg)) == -1)
                   1312:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1313:                            filename, linenum, arg);
                   1314:                arg = strdelim(&s);
                   1315:                if (arg == NULL)
                   1316:                        value2 = value;
                   1317:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1318:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1319:                            filename, linenum, arg);
                   1320:                if (*activep) {
                   1321:                        options->ip_qos_interactive = value;
                   1322:                        options->ip_qos_bulk = value2;
                   1323:                }
                   1324:                break;
                   1325:
1.177     andreas  1326:        case oUseRoaming:
                   1327:                intptr = &options->use_roaming;
                   1328:                goto parse_flag;
                   1329:
1.192     djm      1330:        case oRequestTTY:
                   1331:                intptr = &options->request_tty;
1.207     djm      1332:                multistate_ptr = multistate_requesttty;
                   1333:                goto parse_multistate;
1.192     djm      1334:
1.199     djm      1335:        case oIgnoreUnknown:
                   1336:                charptr = &options->ignored_unknown;
                   1337:                goto parse_string;
                   1338:
1.205     djm      1339:        case oProxyUseFdpass:
                   1340:                intptr = &options->proxy_use_fdpass;
                   1341:                goto parse_flag;
                   1342:
1.208     djm      1343:        case oCanonicalDomains:
                   1344:                value = options->num_canonical_domains != 0;
                   1345:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1346:                        valid_domain(arg, filename, linenum);
                   1347:                        if (!*activep || value)
                   1348:                                continue;
                   1349:                        if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
                   1350:                                fatal("%s line %d: too many hostname suffixes.",
                   1351:                                    filename, linenum);
                   1352:                        options->canonical_domains[
                   1353:                            options->num_canonical_domains++] = xstrdup(arg);
                   1354:                }
                   1355:                break;
                   1356:
1.209     djm      1357:        case oCanonicalizePermittedCNAMEs:
1.208     djm      1358:                value = options->num_permitted_cnames != 0;
                   1359:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1360:                        /* Either '*' for everything or 'list:list' */
                   1361:                        if (strcmp(arg, "*") == 0)
                   1362:                                arg2 = arg;
                   1363:                        else {
                   1364:                                lowercase(arg);
                   1365:                                if ((arg2 = strchr(arg, ':')) == NULL ||
                   1366:                                    arg2[1] == '\0') {
                   1367:                                        fatal("%s line %d: "
                   1368:                                            "Invalid permitted CNAME \"%s\"",
                   1369:                                            filename, linenum, arg);
                   1370:                                }
                   1371:                                *arg2 = '\0';
                   1372:                                arg2++;
                   1373:                        }
                   1374:                        if (!*activep || value)
                   1375:                                continue;
                   1376:                        if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
                   1377:                                fatal("%s line %d: too many permitted CNAMEs.",
                   1378:                                    filename, linenum);
                   1379:                        cname = options->permitted_cnames +
                   1380:                            options->num_permitted_cnames++;
                   1381:                        cname->source_list = xstrdup(arg);
                   1382:                        cname->target_list = xstrdup(arg2);
                   1383:                }
                   1384:                break;
                   1385:
1.209     djm      1386:        case oCanonicalizeHostname:
                   1387:                intptr = &options->canonicalize_hostname;
                   1388:                multistate_ptr = multistate_canonicalizehostname;
1.208     djm      1389:                goto parse_multistate;
                   1390:
1.209     djm      1391:        case oCanonicalizeMaxDots:
                   1392:                intptr = &options->canonicalize_max_dots;
1.208     djm      1393:                goto parse_int;
                   1394:
1.209     djm      1395:        case oCanonicalizeFallbackLocal:
                   1396:                intptr = &options->canonicalize_fallback_local;
1.208     djm      1397:                goto parse_flag;
                   1398:
1.96      markus   1399:        case oDeprecated:
1.98      markus   1400:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   1401:                    filename, linenum, keyword);
1.98      markus   1402:                return 0;
1.96      markus   1403:
1.110     jakob    1404:        case oUnsupported:
                   1405:                error("%s line %d: Unsupported option \"%s\"",
                   1406:                    filename, linenum, keyword);
                   1407:                return 0;
                   1408:
1.17      markus   1409:        default:
                   1410:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                   1411:        }
                   1412:
                   1413:        /* Check that there is no garbage at end of line. */
1.57      djm      1414:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho       1415:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm      1416:                    filename, linenum, arg);
1.39      ho       1417:        }
1.17      markus   1418:        return 0;
1.1       deraadt  1419: }
                   1420:
                   1421:
1.19      markus   1422: /*
                   1423:  * Reads the config file and modifies the options accordingly.  Options
                   1424:  * should already be initialized before this call.  This never returns if
1.89      stevesk  1425:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   1426:  */
1.1       deraadt  1427:
1.89      stevesk  1428: int
1.206     djm      1429: read_config_file(const char *filename, struct passwd *pw, const char *host,
                   1430:     Options *options, int flags)
1.1       deraadt  1431: {
1.17      markus   1432:        FILE *f;
                   1433:        char line[1024];
                   1434:        int active, linenum;
                   1435:        int bad_options = 0;
                   1436:
1.129     djm      1437:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  1438:                return 0;
1.129     djm      1439:
1.196     dtucker  1440:        if (flags & SSHCONF_CHECKPERM) {
1.129     djm      1441:                struct stat sb;
1.134     deraadt  1442:
1.131     dtucker  1443:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      1444:                        fatal("fstat %s: %s", filename, strerror(errno));
                   1445:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  1446:                    (sb.st_mode & 022) != 0))
1.129     djm      1447:                        fatal("Bad owner or permissions on %s", filename);
                   1448:        }
1.17      markus   1449:
                   1450:        debug("Reading configuration data %.200s", filename);
                   1451:
1.19      markus   1452:        /*
                   1453:         * Mark that we are now processing the options.  This flag is turned
                   1454:         * on/off by Host specifications.
                   1455:         */
1.17      markus   1456:        active = 1;
                   1457:        linenum = 0;
                   1458:        while (fgets(line, sizeof(line), f)) {
                   1459:                /* Update line number counter. */
                   1460:                linenum++;
1.206     djm      1461:                if (process_config_line(options, pw, host, line, filename,
                   1462:                    linenum, &active, flags & SSHCONF_USERCONF) != 0)
1.17      markus   1463:                        bad_options++;
                   1464:        }
                   1465:        fclose(f);
                   1466:        if (bad_options > 0)
1.64      millert  1467:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  1468:                    filename, bad_options);
1.89      stevesk  1469:        return 1;
1.1       deraadt  1470: }
                   1471:
1.218     djm      1472: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                   1473: int
                   1474: option_clear_or_none(const char *o)
                   1475: {
                   1476:        return o == NULL || strcasecmp(o, "none") == 0;
                   1477: }
                   1478:
1.19      markus   1479: /*
                   1480:  * Initializes options to special values that indicate that they have not yet
                   1481:  * been set.  Read_config_file will only set options with this value. Options
                   1482:  * are processed in the following order: command line, user config file,
                   1483:  * system config file.  Last, fill_default_options is called.
                   1484:  */
1.1       deraadt  1485:
1.26      markus   1486: void
1.17      markus   1487: initialize_options(Options * options)
1.1       deraadt  1488: {
1.17      markus   1489:        memset(options, 'X', sizeof(*options));
                   1490:        options->forward_agent = -1;
                   1491:        options->forward_x11 = -1;
1.123     markus   1492:        options->forward_x11_trusted = -1;
1.186     djm      1493:        options->forward_x11_timeout = -1;
1.153     markus   1494:        options->exit_on_forward_failure = -1;
1.34      markus   1495:        options->xauth_location = NULL;
1.17      markus   1496:        options->gateway_ports = -1;
                   1497:        options->use_privileged_port = -1;
                   1498:        options->rsa_authentication = -1;
1.50      markus   1499:        options->pubkey_authentication = -1;
1.78      markus   1500:        options->challenge_response_authentication = -1;
1.118     markus   1501:        options->gss_authentication = -1;
                   1502:        options->gss_deleg_creds = -1;
1.17      markus   1503:        options->password_authentication = -1;
1.48      markus   1504:        options->kbd_interactive_authentication = -1;
                   1505:        options->kbd_interactive_devices = NULL;
1.17      markus   1506:        options->rhosts_rsa_authentication = -1;
1.72      markus   1507:        options->hostbased_authentication = -1;
1.17      markus   1508:        options->batch_mode = -1;
                   1509:        options->check_host_ip = -1;
                   1510:        options->strict_host_key_checking = -1;
                   1511:        options->compression = -1;
1.126     markus   1512:        options->tcp_keep_alive = -1;
1.17      markus   1513:        options->compression_level = -1;
                   1514:        options->port = -1;
1.114     djm      1515:        options->address_family = -1;
1.17      markus   1516:        options->connection_attempts = -1;
1.111     djm      1517:        options->connection_timeout = -1;
1.17      markus   1518:        options->number_of_password_prompts = -1;
                   1519:        options->cipher = -1;
1.25      markus   1520:        options->ciphers = NULL;
1.62      markus   1521:        options->macs = NULL;
1.189     djm      1522:        options->kex_algorithms = NULL;
1.76      markus   1523:        options->hostkeyalgorithms = NULL;
1.25      markus   1524:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus   1525:        options->num_identity_files = 0;
                   1526:        options->hostname = NULL;
1.52      markus   1527:        options->host_key_alias = NULL;
1.17      markus   1528:        options->proxy_command = NULL;
                   1529:        options->user = NULL;
                   1530:        options->escape_char = -1;
1.193     djm      1531:        options->num_system_hostfiles = 0;
                   1532:        options->num_user_hostfiles = 0;
1.185     djm      1533:        options->local_forwards = NULL;
1.17      markus   1534:        options->num_local_forwards = 0;
1.185     djm      1535:        options->remote_forwards = NULL;
1.17      markus   1536:        options->num_remote_forwards = 0;
1.90      stevesk  1537:        options->clear_forwardings = -1;
1.95      markus   1538:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1539:        options->preferred_authentications = NULL;
1.77      markus   1540:        options->bind_address = NULL;
1.183     markus   1541:        options->pkcs11_provider = NULL;
1.101     markus   1542:        options->enable_ssh_keysign = - 1;
1.91      markus   1543:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1544:        options->identities_only = - 1;
1.105     markus   1545:        options->rekey_limit = - 1;
1.198     dtucker  1546:        options->rekey_interval = -1;
1.107     jakob    1547:        options->verify_host_key_dns = -1;
1.127     markus   1548:        options->server_alive_interval = -1;
                   1549:        options->server_alive_count_max = -1;
1.130     djm      1550:        options->num_send_env = 0;
1.132     djm      1551:        options->control_path = NULL;
                   1552:        options->control_master = -1;
1.187     djm      1553:        options->control_persist = -1;
                   1554:        options->control_persist_timeout = 0;
1.136     djm      1555:        options->hash_known_hosts = -1;
1.144     reyk     1556:        options->tun_open = -1;
                   1557:        options->tun_local = -1;
                   1558:        options->tun_remote = -1;
                   1559:        options->local_command = NULL;
                   1560:        options->permit_local_command = -1;
1.177     andreas  1561:        options->use_roaming = -1;
1.167     grunk    1562:        options->visual_host_key = -1;
1.190     djm      1563:        options->ip_qos_interactive = -1;
                   1564:        options->ip_qos_bulk = -1;
1.192     djm      1565:        options->request_tty = -1;
1.205     djm      1566:        options->proxy_use_fdpass = -1;
1.199     djm      1567:        options->ignored_unknown = NULL;
1.208     djm      1568:        options->num_canonical_domains = 0;
                   1569:        options->num_permitted_cnames = 0;
1.209     djm      1570:        options->canonicalize_max_dots = -1;
                   1571:        options->canonicalize_fallback_local = -1;
                   1572:        options->canonicalize_hostname = -1;
1.1       deraadt  1573: }
                   1574:
1.19      markus   1575: /*
1.218     djm      1576:  * A petite version of fill_default_options() that just fills the options
                   1577:  * needed for hostname canonicalization to proceed.
                   1578:  */
                   1579: void
                   1580: fill_default_options_for_canonicalization(Options *options)
                   1581: {
                   1582:        if (options->canonicalize_max_dots == -1)
                   1583:                options->canonicalize_max_dots = 1;
                   1584:        if (options->canonicalize_fallback_local == -1)
                   1585:                options->canonicalize_fallback_local = 1;
                   1586:        if (options->canonicalize_hostname == -1)
                   1587:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
                   1588: }
                   1589:
                   1590: /*
1.19      markus   1591:  * Called after processing other sources of option data, this fills those
                   1592:  * options for which no value has been specified with their default values.
                   1593:  */
1.26      markus   1594: void
1.17      markus   1595: fill_default_options(Options * options)
1.1       deraadt  1596: {
1.17      markus   1597:        if (options->forward_agent == -1)
1.33      markus   1598:                options->forward_agent = 0;
1.17      markus   1599:        if (options->forward_x11 == -1)
1.23      markus   1600:                options->forward_x11 = 0;
1.123     markus   1601:        if (options->forward_x11_trusted == -1)
                   1602:                options->forward_x11_trusted = 0;
1.186     djm      1603:        if (options->forward_x11_timeout == -1)
                   1604:                options->forward_x11_timeout = 1200;
1.153     markus   1605:        if (options->exit_on_forward_failure == -1)
                   1606:                options->exit_on_forward_failure = 0;
1.34      markus   1607:        if (options->xauth_location == NULL)
1.80      markus   1608:                options->xauth_location = _PATH_XAUTH;
1.17      markus   1609:        if (options->gateway_ports == -1)
                   1610:                options->gateway_ports = 0;
                   1611:        if (options->use_privileged_port == -1)
1.65      markus   1612:                options->use_privileged_port = 0;
1.17      markus   1613:        if (options->rsa_authentication == -1)
                   1614:                options->rsa_authentication = 1;
1.50      markus   1615:        if (options->pubkey_authentication == -1)
                   1616:                options->pubkey_authentication = 1;
1.78      markus   1617:        if (options->challenge_response_authentication == -1)
1.83      markus   1618:                options->challenge_response_authentication = 1;
1.118     markus   1619:        if (options->gss_authentication == -1)
1.122     markus   1620:                options->gss_authentication = 0;
1.118     markus   1621:        if (options->gss_deleg_creds == -1)
                   1622:                options->gss_deleg_creds = 0;
1.17      markus   1623:        if (options->password_authentication == -1)
                   1624:                options->password_authentication = 1;
1.48      markus   1625:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1626:                options->kbd_interactive_authentication = 1;
1.17      markus   1627:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk  1628:                options->rhosts_rsa_authentication = 0;
1.72      markus   1629:        if (options->hostbased_authentication == -1)
                   1630:                options->hostbased_authentication = 0;
1.17      markus   1631:        if (options->batch_mode == -1)
                   1632:                options->batch_mode = 0;
                   1633:        if (options->check_host_ip == -1)
                   1634:                options->check_host_ip = 1;
                   1635:        if (options->strict_host_key_checking == -1)
                   1636:                options->strict_host_key_checking = 2;  /* 2 is default */
                   1637:        if (options->compression == -1)
                   1638:                options->compression = 0;
1.126     markus   1639:        if (options->tcp_keep_alive == -1)
                   1640:                options->tcp_keep_alive = 1;
1.17      markus   1641:        if (options->compression_level == -1)
                   1642:                options->compression_level = 6;
                   1643:        if (options->port == -1)
                   1644:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1645:        if (options->address_family == -1)
                   1646:                options->address_family = AF_UNSPEC;
1.17      markus   1647:        if (options->connection_attempts == -1)
1.84      markus   1648:                options->connection_attempts = 1;
1.17      markus   1649:        if (options->number_of_password_prompts == -1)
                   1650:                options->number_of_password_prompts = 3;
                   1651:        /* Selected in ssh_login(). */
                   1652:        if (options->cipher == -1)
                   1653:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus   1654:        /* options->ciphers, default set in myproposals.h */
1.62      markus   1655:        /* options->macs, default set in myproposals.h */
1.189     djm      1656:        /* options->kex_algorithms, default set in myproposals.h */
1.76      markus   1657:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus   1658:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.178     markus   1659:                options->protocol = SSH_PROTO_2;
1.17      markus   1660:        if (options->num_identity_files == 0) {
1.50      markus   1661:                if (options->protocol & SSH_PROTO_1) {
1.195     dtucker  1662:                        add_identity_file(options, "~/",
                   1663:                            _PATH_SSH_CLIENT_IDENTITY, 0);
1.50      markus   1664:                }
                   1665:                if (options->protocol & SSH_PROTO_2) {
1.195     dtucker  1666:                        add_identity_file(options, "~/",
                   1667:                            _PATH_SSH_CLIENT_ID_RSA, 0);
                   1668:                        add_identity_file(options, "~/",
                   1669:                            _PATH_SSH_CLIENT_ID_DSA, 0);
                   1670:                        add_identity_file(options, "~/",
                   1671:                            _PATH_SSH_CLIENT_ID_ECDSA, 0);
1.215     markus   1672:                        add_identity_file(options, "~/",
                   1673:                            _PATH_SSH_CLIENT_ID_ED25519, 0);
1.50      markus   1674:                }
1.27      markus   1675:        }
1.17      markus   1676:        if (options->escape_char == -1)
                   1677:                options->escape_char = '~';
1.193     djm      1678:        if (options->num_system_hostfiles == 0) {
                   1679:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1680:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
                   1681:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1682:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
                   1683:        }
                   1684:        if (options->num_user_hostfiles == 0) {
                   1685:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1686:                    xstrdup(_PATH_SSH_USER_HOSTFILE);
                   1687:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1688:                    xstrdup(_PATH_SSH_USER_HOSTFILE2);
                   1689:        }
1.95      markus   1690:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1691:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk  1692:        if (options->clear_forwardings == 1)
                   1693:                clear_forwardings(options);
1.91      markus   1694:        if (options->no_host_authentication_for_localhost == - 1)
                   1695:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1696:        if (options->identities_only == -1)
                   1697:                options->identities_only = 0;
1.101     markus   1698:        if (options->enable_ssh_keysign == -1)
                   1699:                options->enable_ssh_keysign = 0;
1.105     markus   1700:        if (options->rekey_limit == -1)
                   1701:                options->rekey_limit = 0;
1.198     dtucker  1702:        if (options->rekey_interval == -1)
                   1703:                options->rekey_interval = 0;
1.107     jakob    1704:        if (options->verify_host_key_dns == -1)
                   1705:                options->verify_host_key_dns = 0;
1.127     markus   1706:        if (options->server_alive_interval == -1)
                   1707:                options->server_alive_interval = 0;
                   1708:        if (options->server_alive_count_max == -1)
                   1709:                options->server_alive_count_max = 3;
1.132     djm      1710:        if (options->control_master == -1)
                   1711:                options->control_master = 0;
1.187     djm      1712:        if (options->control_persist == -1) {
                   1713:                options->control_persist = 0;
                   1714:                options->control_persist_timeout = 0;
                   1715:        }
1.136     djm      1716:        if (options->hash_known_hosts == -1)
                   1717:                options->hash_known_hosts = 0;
1.144     reyk     1718:        if (options->tun_open == -1)
1.145     reyk     1719:                options->tun_open = SSH_TUNMODE_NO;
                   1720:        if (options->tun_local == -1)
                   1721:                options->tun_local = SSH_TUNID_ANY;
                   1722:        if (options->tun_remote == -1)
                   1723:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1724:        if (options->permit_local_command == -1)
                   1725:                options->permit_local_command = 0;
1.177     andreas  1726:        if (options->use_roaming == -1)
                   1727:                options->use_roaming = 1;
1.167     grunk    1728:        if (options->visual_host_key == -1)
                   1729:                options->visual_host_key = 0;
1.190     djm      1730:        if (options->ip_qos_interactive == -1)
                   1731:                options->ip_qos_interactive = IPTOS_LOWDELAY;
                   1732:        if (options->ip_qos_bulk == -1)
                   1733:                options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.192     djm      1734:        if (options->request_tty == -1)
                   1735:                options->request_tty = REQUEST_TTY_AUTO;
1.205     djm      1736:        if (options->proxy_use_fdpass == -1)
                   1737:                options->proxy_use_fdpass = 0;
1.209     djm      1738:        if (options->canonicalize_max_dots == -1)
                   1739:                options->canonicalize_max_dots = 1;
                   1740:        if (options->canonicalize_fallback_local == -1)
                   1741:                options->canonicalize_fallback_local = 1;
                   1742:        if (options->canonicalize_hostname == -1)
                   1743:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.207     djm      1744: #define CLEAR_ON_NONE(v) \
                   1745:        do { \
1.218     djm      1746:                if (option_clear_or_none(v)) { \
1.207     djm      1747:                        free(v); \
                   1748:                        v = NULL; \
                   1749:                } \
                   1750:        } while(0)
                   1751:        CLEAR_ON_NONE(options->local_command);
                   1752:        CLEAR_ON_NONE(options->proxy_command);
                   1753:        CLEAR_ON_NONE(options->control_path);
1.17      markus   1754:        /* options->user will be set in the main program if appropriate */
                   1755:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   1756:        /* options->host_key_alias should not be set by default */
1.67      markus   1757:        /* options->preferred_authentications will be set in ssh */
1.135     djm      1758: }
                   1759:
                   1760: /*
                   1761:  * parse_forward
                   1762:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  1763:  *   dynamicfwd == 0
1.135     djm      1764:  *     [listenhost:]listenport:connecthost:connectport
1.168     stevesk  1765:  *   dynamicfwd == 1
                   1766:  *     [listenhost:]listenport
1.135     djm      1767:  * returns number of arguments parsed or zero on error
                   1768:  */
                   1769: int
1.176     djm      1770: parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      1771: {
                   1772:        int i;
                   1773:        char *p, *cp, *fwdarg[4];
                   1774:
                   1775:        memset(fwd, '\0', sizeof(*fwd));
                   1776:
                   1777:        cp = p = xstrdup(fwdspec);
                   1778:
                   1779:        /* skip leading spaces */
1.214     deraadt  1780:        while (isspace((u_char)*cp))
1.135     djm      1781:                cp++;
                   1782:
                   1783:        for (i = 0; i < 4; ++i)
                   1784:                if ((fwdarg[i] = hpdelim(&cp)) == NULL)
                   1785:                        break;
                   1786:
1.170     stevesk  1787:        /* Check for trailing garbage */
1.135     djm      1788:        if (cp != NULL)
                   1789:                i = 0;  /* failure */
                   1790:
                   1791:        switch (i) {
1.168     stevesk  1792:        case 1:
                   1793:                fwd->listen_host = NULL;
                   1794:                fwd->listen_port = a2port(fwdarg[0]);
                   1795:                fwd->connect_host = xstrdup("socks");
                   1796:                break;
                   1797:
                   1798:        case 2:
                   1799:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1800:                fwd->listen_port = a2port(fwdarg[1]);
                   1801:                fwd->connect_host = xstrdup("socks");
                   1802:                break;
                   1803:
1.135     djm      1804:        case 3:
                   1805:                fwd->listen_host = NULL;
                   1806:                fwd->listen_port = a2port(fwdarg[0]);
                   1807:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
                   1808:                fwd->connect_port = a2port(fwdarg[2]);
                   1809:                break;
                   1810:
                   1811:        case 4:
                   1812:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1813:                fwd->listen_port = a2port(fwdarg[1]);
                   1814:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
                   1815:                fwd->connect_port = a2port(fwdarg[3]);
                   1816:                break;
                   1817:        default:
                   1818:                i = 0; /* failure */
                   1819:        }
                   1820:
1.202     djm      1821:        free(p);
1.135     djm      1822:
1.168     stevesk  1823:        if (dynamicfwd) {
                   1824:                if (!(i == 1 || i == 2))
                   1825:                        goto fail_free;
                   1826:        } else {
                   1827:                if (!(i == 3 || i == 4))
                   1828:                        goto fail_free;
1.175     djm      1829:                if (fwd->connect_port <= 0)
1.168     stevesk  1830:                        goto fail_free;
                   1831:        }
                   1832:
1.176     djm      1833:        if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
1.135     djm      1834:                goto fail_free;
                   1835:
                   1836:        if (fwd->connect_host != NULL &&
                   1837:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   1838:                goto fail_free;
1.176     djm      1839:        if (fwd->listen_host != NULL &&
                   1840:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   1841:                goto fail_free;
                   1842:
1.135     djm      1843:
                   1844:        return (i);
                   1845:
                   1846:  fail_free:
1.202     djm      1847:        free(fwd->connect_host);
                   1848:        fwd->connect_host = NULL;
                   1849:        free(fwd->listen_host);
                   1850:        fwd->listen_host = NULL;
1.135     djm      1851:        return (0);
1.1       deraadt  1852: }