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

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