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

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