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

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