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

1.158   ! stevesk     1: /* $OpenBSD: readconf.c,v 1.157 2006/07/22 20:48:23 stevesk 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:
                     15: #include "includes.h"
1.147     stevesk    16:
                     17: #include <sys/types.h>
                     18: #include <sys/stat.h>
1.152     stevesk    19: #include <sys/socket.h>
                     20:
                     21: #include <netinet/in.h>
1.148     stevesk    22:
                     23: #include <ctype.h>
1.154     stevesk    24: #include <errno.h>
1.155     stevesk    25: #include <netdb.h>
1.158   ! stevesk    26: #include <stdio.h>
1.157     stevesk    27: #include <string.h>
1.156     stevesk    28: #include <unistd.h>
1.1       deraadt    29:
                     30: #include "ssh.h"
                     31: #include "xmalloc.h"
1.25      markus     32: #include "compat.h"
1.58      markus     33: #include "cipher.h"
1.55      markus     34: #include "pathnames.h"
1.58      markus     35: #include "log.h"
                     36: #include "readconf.h"
                     37: #include "match.h"
                     38: #include "misc.h"
1.62      markus     39: #include "kex.h"
                     40: #include "mac.h"
1.1       deraadt    41:
                     42: /* Format of the configuration file:
                     43:
                     44:    # Configuration data is parsed as follows:
                     45:    #  1. command line options
                     46:    #  2. user-specific file
                     47:    #  3. system-wide file
                     48:    # Any configuration value is only changed the first time it is set.
                     49:    # Thus, host-specific definitions should be at the beginning of the
                     50:    # configuration file, and defaults at the end.
                     51:
                     52:    # Host-specific declarations.  These may override anything above.  A single
                     53:    # host may match multiple declarations; these are processed in the order
                     54:    # that they are given in.
                     55:
                     56:    Host *.ngs.fi ngs.fi
1.96      markus     57:      User foo
1.1       deraadt    58:
                     59:    Host fake.com
                     60:      HostName another.host.name.real.org
                     61:      User blaah
                     62:      Port 34289
                     63:      ForwardX11 no
                     64:      ForwardAgent no
                     65:
                     66:    Host books.com
                     67:      RemoteForward 9999 shadows.cs.hut.fi:9999
                     68:      Cipher 3des
                     69:
                     70:    Host fascist.blob.com
                     71:      Port 23123
                     72:      User tylonen
                     73:      PasswordAuthentication no
                     74:
                     75:    Host puukko.hut.fi
                     76:      User t35124p
                     77:      ProxyCommand ssh-proxy %h %p
                     78:
                     79:    Host *.fr
1.96      markus     80:      PublicKeyAuthentication no
1.1       deraadt    81:
                     82:    Host *.su
                     83:      Cipher none
                     84:      PasswordAuthentication no
                     85:
1.144     reyk       86:    Host vpn.fake.com
                     87:      Tunnel yes
                     88:      TunnelDevice 3
                     89:
1.1       deraadt    90:    # Defaults for various options
                     91:    Host *
                     92:      ForwardAgent no
1.50      markus     93:      ForwardX11 no
1.1       deraadt    94:      PasswordAuthentication yes
                     95:      RSAAuthentication yes
                     96:      RhostsRSAAuthentication yes
                     97:      StrictHostKeyChecking yes
1.126     markus     98:      TcpKeepAlive no
1.1       deraadt    99:      IdentityFile ~/.ssh/identity
                    100:      Port 22
                    101:      EscapeChar ~
                    102:
                    103: */
                    104:
                    105: /* Keyword tokens. */
                    106:
1.17      markus    107: typedef enum {
                    108:        oBadOption,
1.123     markus    109:        oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
1.153     markus    110:        oExitOnForwardFailure,
1.100     deraadt   111:        oPasswordAuthentication, oRSAAuthentication,
1.59      markus    112:        oChallengeResponseAuthentication, oXAuthLocation,
1.17      markus    113:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
                    114:        oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
                    115:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    116:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126     markus    117:        oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62      markus    118:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50      markus    119:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67      markus    120:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    121:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.90      stevesk   122:        oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
1.96      markus    123:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       124:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    125:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    126:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.136     djm       127:        oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
1.144     reyk      128:        oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
1.110     jakob     129:        oDeprecated, oUnsupported
1.1       deraadt   130: } OpCodes;
                    131:
                    132: /* Textual representations of the tokens. */
                    133:
1.17      markus    134: static struct {
                    135:        const char *name;
                    136:        OpCodes opcode;
                    137: } keywords[] = {
                    138:        { "forwardagent", oForwardAgent },
                    139:        { "forwardx11", oForwardX11 },
1.123     markus    140:        { "forwardx11trusted", oForwardX11Trusted },
1.153     markus    141:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    142:        { "xauthlocation", oXAuthLocation },
1.17      markus    143:        { "gatewayports", oGatewayPorts },
                    144:        { "useprivilegedport", oUsePrivilegedPort },
1.116     markus    145:        { "rhostsauthentication", oDeprecated },
1.17      markus    146:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    147:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    148:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17      markus    149:        { "rsaauthentication", oRSAAuthentication },
1.50      markus    150:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    151:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.72      markus    152:        { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73      markus    153:        { "hostbasedauthentication", oHostbasedAuthentication },
1.59      markus    154:        { "challengeresponseauthentication", oChallengeResponseAuthentication },
                    155:        { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
                    156:        { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
1.110     jakob     157:        { "kerberosauthentication", oUnsupported },
                    158:        { "kerberostgtpassing", oUnsupported },
                    159:        { "afstokenpassing", oUnsupported },
1.118     markus    160: #if defined(GSSAPI)
                    161:        { "gssapiauthentication", oGssAuthentication },
                    162:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    163: #else
                    164:        { "gssapiauthentication", oUnsupported },
                    165:        { "gssapidelegatecredentials", oUnsupported },
                    166: #endif
1.96      markus    167:        { "fallbacktorsh", oDeprecated },
                    168:        { "usersh", oDeprecated },
1.17      markus    169:        { "identityfile", oIdentityFile },
1.50      markus    170:        { "identityfile2", oIdentityFile },                     /* alias */
1.128     markus    171:        { "identitiesonly", oIdentitiesOnly },
1.17      markus    172:        { "hostname", oHostName },
1.52      markus    173:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    174:        { "proxycommand", oProxyCommand },
                    175:        { "port", oPort },
                    176:        { "cipher", oCipher },
1.25      markus    177:        { "ciphers", oCiphers },
1.62      markus    178:        { "macs", oMacs },
1.25      markus    179:        { "protocol", oProtocol },
1.17      markus    180:        { "remoteforward", oRemoteForward },
                    181:        { "localforward", oLocalForward },
                    182:        { "user", oUser },
                    183:        { "host", oHost },
                    184:        { "escapechar", oEscapeChar },
                    185:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.81      markus    186:        { "userknownhostsfile", oUserKnownHostsFile },          /* obsolete */
1.27      markus    187:        { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
1.81      markus    188:        { "userknownhostsfile2", oUserKnownHostsFile2 },        /* obsolete */
1.17      markus    189:        { "connectionattempts", oConnectionAttempts },
                    190:        { "batchmode", oBatchMode },
                    191:        { "checkhostip", oCheckHostIP },
                    192:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    193:        { "compression", oCompression },
                    194:        { "compressionlevel", oCompressionLevel },
1.126     markus    195:        { "tcpkeepalive", oTCPKeepAlive },
                    196:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    197:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
                    198:        { "loglevel", oLogLevel },
1.71      markus    199:        { "dynamicforward", oDynamicForward },
1.67      markus    200:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    201:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77      markus    202:        { "bindaddress", oBindAddress },
1.110     jakob     203: #ifdef SMARTCARD
1.85      jakob     204:        { "smartcarddevice", oSmartcardDevice },
1.110     jakob     205: #else
                    206:        { "smartcarddevice", oUnsupported },
                    207: #endif
1.93      deraadt   208:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    209:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     210:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   211:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    212:        { "rekeylimit", oRekeyLimit },
1.111     djm       213:        { "connecttimeout", oConnectTimeout },
1.112     djm       214:        { "addressfamily", oAddressFamily },
1.127     markus    215:        { "serveraliveinterval", oServerAliveInterval },
                    216:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       217:        { "sendenv", oSendEnv },
1.132     djm       218:        { "controlpath", oControlPath },
                    219:        { "controlmaster", oControlMaster },
1.136     djm       220:        { "hashknownhosts", oHashKnownHosts },
1.144     reyk      221:        { "tunnel", oTunnel },
                    222:        { "tunneldevice", oTunnelDevice },
                    223:        { "localcommand", oLocalCommand },
                    224:        { "permitlocalcommand", oPermitLocalCommand },
1.92      stevesk   225:        { NULL, oBadOption }
1.13      markus    226: };
                    227:
1.19      markus    228: /*
                    229:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    230:  * error.
                    231:  */
1.1       deraadt   232:
1.26      markus    233: void
1.135     djm       234: add_local_forward(Options *options, const Forward *newfwd)
1.1       deraadt   235: {
1.17      markus    236:        Forward *fwd;
                    237:        extern uid_t original_real_uid;
1.135     djm       238:        if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
1.64      millert   239:                fatal("Privileged ports can only be forwarded by root.");
1.17      markus    240:        if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    241:                fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
                    242:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       243:
                    244:        fwd->listen_host = (newfwd->listen_host == NULL) ?
                    245:            NULL : xstrdup(newfwd->listen_host);
                    246:        fwd->listen_port = newfwd->listen_port;
                    247:        fwd->connect_host = xstrdup(newfwd->connect_host);
                    248:        fwd->connect_port = newfwd->connect_port;
1.1       deraadt   249: }
                    250:
1.19      markus    251: /*
                    252:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    253:  * an error.
                    254:  */
1.1       deraadt   255:
1.26      markus    256: void
1.135     djm       257: add_remote_forward(Options *options, const Forward *newfwd)
1.1       deraadt   258: {
1.17      markus    259:        Forward *fwd;
                    260:        if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    261:                fatal("Too many remote forwards (max %d).",
1.93      deraadt   262:                    SSH_MAX_FORWARDS_PER_DIRECTION);
1.17      markus    263:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       264:
                    265:        fwd->listen_host = (newfwd->listen_host == NULL) ?
                    266:            NULL : xstrdup(newfwd->listen_host);
                    267:        fwd->listen_port = newfwd->listen_port;
                    268:        fwd->connect_host = xstrdup(newfwd->connect_host);
                    269:        fwd->connect_port = newfwd->connect_port;
1.1       deraadt   270: }
                    271:
1.90      stevesk   272: static void
                    273: clear_forwardings(Options *options)
                    274: {
                    275:        int i;
                    276:
1.135     djm       277:        for (i = 0; i < options->num_local_forwards; i++) {
1.138     dtucker   278:                if (options->local_forwards[i].listen_host != NULL)
                    279:                        xfree(options->local_forwards[i].listen_host);
1.135     djm       280:                xfree(options->local_forwards[i].connect_host);
                    281:        }
1.90      stevesk   282:        options->num_local_forwards = 0;
1.135     djm       283:        for (i = 0; i < options->num_remote_forwards; i++) {
1.138     dtucker   284:                if (options->remote_forwards[i].listen_host != NULL)
                    285:                        xfree(options->remote_forwards[i].listen_host);
1.135     djm       286:                xfree(options->remote_forwards[i].connect_host);
                    287:        }
1.90      stevesk   288:        options->num_remote_forwards = 0;
1.145     reyk      289:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   290: }
                    291:
1.19      markus    292: /*
1.70      stevesk   293:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    294:  */
1.1       deraadt   295:
1.26      markus    296: static OpCodes
1.17      markus    297: parse_token(const char *cp, const char *filename, int linenum)
1.1       deraadt   298: {
1.51      markus    299:        u_int i;
1.1       deraadt   300:
1.17      markus    301:        for (i = 0; keywords[i].name; i++)
1.20      markus    302:                if (strcasecmp(cp, keywords[i].name) == 0)
1.17      markus    303:                        return keywords[i].opcode;
                    304:
1.75      stevesk   305:        error("%s: line %d: Bad configuration option: %s",
                    306:            filename, linenum, cp);
1.17      markus    307:        return oBadOption;
1.1       deraadt   308: }
                    309:
1.19      markus    310: /*
                    311:  * Processes a single option line as used in the configuration files. This
                    312:  * only sets those values that have not already been set.
                    313:  */
1.102     markus    314: #define WHITESPACE " \t\r\n"
1.1       deraadt   315:
1.14      markus    316: int
                    317: process_config_line(Options *options, const char *host,
1.17      markus    318:                    char *line, const char *filename, int linenum,
                    319:                    int *activep)
1.1       deraadt   320: {
1.135     djm       321:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
1.146     djm       322:        int opcode, *intptr, value, value2, scale;
                    323:        long long orig, val64;
1.102     markus    324:        size_t len;
1.135     djm       325:        Forward fwd;
1.106     djm       326:
                    327:        /* Strip trailing whitespace */
1.139     deraadt   328:        for (len = strlen(line) - 1; len > 0; len--) {
1.106     djm       329:                if (strchr(WHITESPACE, line[len]) == NULL)
                    330:                        break;
                    331:                line[len] = '\0';
                    332:        }
1.1       deraadt   333:
1.42      provos    334:        s = line;
                    335:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       336:        if ((keyword = strdelim(&s)) == NULL)
                    337:                return 0;
1.42      provos    338:        /* Ignore leading whitespace. */
                    339:        if (*keyword == '\0')
1.43      markus    340:                keyword = strdelim(&s);
1.56      deraadt   341:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    342:                return 0;
                    343:
1.38      provos    344:        opcode = parse_token(keyword, filename, linenum);
1.17      markus    345:
                    346:        switch (opcode) {
                    347:        case oBadOption:
1.19      markus    348:                /* don't panic, but count bad options */
                    349:                return -1;
1.17      markus    350:                /* NOTREACHED */
1.111     djm       351:        case oConnectTimeout:
                    352:                intptr = &options->connection_timeout;
1.127     markus    353: parse_time:
1.111     djm       354:                arg = strdelim(&s);
                    355:                if (!arg || *arg == '\0')
                    356:                        fatal("%s line %d: missing time value.",
                    357:                            filename, linenum);
                    358:                if ((value = convtime(arg)) == -1)
                    359:                        fatal("%s line %d: invalid time value.",
                    360:                            filename, linenum);
                    361:                if (*intptr == -1)
                    362:                        *intptr = value;
                    363:                break;
                    364:
1.17      markus    365:        case oForwardAgent:
                    366:                intptr = &options->forward_agent;
                    367: parse_flag:
1.42      provos    368:                arg = strdelim(&s);
1.40      ho        369:                if (!arg || *arg == '\0')
1.17      markus    370:                        fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
                    371:                value = 0;      /* To avoid compiler warning... */
1.38      provos    372:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    373:                        value = 1;
1.38      provos    374:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    375:                        value = 0;
                    376:                else
                    377:                        fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
                    378:                if (*activep && *intptr == -1)
                    379:                        *intptr = value;
                    380:                break;
                    381:
                    382:        case oForwardX11:
                    383:                intptr = &options->forward_x11;
                    384:                goto parse_flag;
                    385:
1.123     markus    386:        case oForwardX11Trusted:
                    387:                intptr = &options->forward_x11_trusted;
                    388:                goto parse_flag;
                    389:
1.17      markus    390:        case oGatewayPorts:
                    391:                intptr = &options->gateway_ports;
                    392:                goto parse_flag;
                    393:
1.153     markus    394:        case oExitOnForwardFailure:
                    395:                intptr = &options->exit_on_forward_failure;
                    396:                goto parse_flag;
                    397:
1.17      markus    398:        case oUsePrivilegedPort:
                    399:                intptr = &options->use_privileged_port;
                    400:                goto parse_flag;
                    401:
                    402:        case oPasswordAuthentication:
                    403:                intptr = &options->password_authentication;
                    404:                goto parse_flag;
                    405:
1.48      markus    406:        case oKbdInteractiveAuthentication:
                    407:                intptr = &options->kbd_interactive_authentication;
                    408:                goto parse_flag;
                    409:
                    410:        case oKbdInteractiveDevices:
                    411:                charptr = &options->kbd_interactive_devices;
                    412:                goto parse_string;
                    413:
1.50      markus    414:        case oPubkeyAuthentication:
                    415:                intptr = &options->pubkey_authentication;
1.30      markus    416:                goto parse_flag;
                    417:
1.17      markus    418:        case oRSAAuthentication:
                    419:                intptr = &options->rsa_authentication;
                    420:                goto parse_flag;
                    421:
                    422:        case oRhostsRSAAuthentication:
                    423:                intptr = &options->rhosts_rsa_authentication;
                    424:                goto parse_flag;
                    425:
1.72      markus    426:        case oHostbasedAuthentication:
                    427:                intptr = &options->hostbased_authentication;
                    428:                goto parse_flag;
                    429:
1.59      markus    430:        case oChallengeResponseAuthentication:
1.78      markus    431:                intptr = &options->challenge_response_authentication;
1.17      markus    432:                goto parse_flag;
1.108     jakob     433:
1.118     markus    434:        case oGssAuthentication:
                    435:                intptr = &options->gss_authentication;
                    436:                goto parse_flag;
                    437:
                    438:        case oGssDelegateCreds:
                    439:                intptr = &options->gss_deleg_creds;
                    440:                goto parse_flag;
                    441:
1.17      markus    442:        case oBatchMode:
                    443:                intptr = &options->batch_mode;
                    444:                goto parse_flag;
                    445:
                    446:        case oCheckHostIP:
                    447:                intptr = &options->check_host_ip;
                    448:                goto parse_flag;
                    449:
1.107     jakob     450:        case oVerifyHostKeyDNS:
                    451:                intptr = &options->verify_host_key_dns;
1.125     jakob     452:                goto parse_yesnoask;
1.107     jakob     453:
1.17      markus    454:        case oStrictHostKeyChecking:
                    455:                intptr = &options->strict_host_key_checking;
1.125     jakob     456: parse_yesnoask:
1.42      provos    457:                arg = strdelim(&s);
1.40      ho        458:                if (!arg || *arg == '\0')
1.60      stevesk   459:                        fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93      deraadt   460:                            filename, linenum);
1.17      markus    461:                value = 0;      /* To avoid compiler warning... */
1.38      provos    462:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    463:                        value = 1;
1.38      provos    464:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    465:                        value = 0;
1.38      provos    466:                else if (strcmp(arg, "ask") == 0)
1.17      markus    467:                        value = 2;
                    468:                else
                    469:                        fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
                    470:                if (*activep && *intptr == -1)
                    471:                        *intptr = value;
                    472:                break;
                    473:
                    474:        case oCompression:
                    475:                intptr = &options->compression;
                    476:                goto parse_flag;
                    477:
1.126     markus    478:        case oTCPKeepAlive:
                    479:                intptr = &options->tcp_keep_alive;
1.17      markus    480:                goto parse_flag;
                    481:
1.91      markus    482:        case oNoHostAuthenticationForLocalhost:
                    483:                intptr = &options->no_host_authentication_for_localhost;
                    484:                goto parse_flag;
                    485:
1.17      markus    486:        case oNumberOfPasswordPrompts:
                    487:                intptr = &options->number_of_password_prompts;
                    488:                goto parse_int;
                    489:
                    490:        case oCompressionLevel:
                    491:                intptr = &options->compression_level;
                    492:                goto parse_int;
                    493:
1.105     markus    494:        case oRekeyLimit:
                    495:                intptr = &options->rekey_limit;
                    496:                arg = strdelim(&s);
                    497:                if (!arg || *arg == '\0')
                    498:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    499:                if (arg[0] < '0' || arg[0] > '9')
                    500:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.146     djm       501:                orig = val64 = strtoll(arg, &endofnumber, 10);
1.105     markus    502:                if (arg == endofnumber)
                    503:                        fatal("%.200s line %d: Bad number.", filename, linenum);
                    504:                switch (toupper(*endofnumber)) {
1.146     djm       505:                case '\0':
                    506:                        scale = 1;
                    507:                        break;
1.105     markus    508:                case 'K':
1.146     djm       509:                        scale = 1<<10;
1.105     markus    510:                        break;
                    511:                case 'M':
1.146     djm       512:                        scale = 1<<20;
1.105     markus    513:                        break;
                    514:                case 'G':
1.146     djm       515:                        scale = 1<<30;
1.105     markus    516:                        break;
1.146     djm       517:                default:
                    518:                        fatal("%.200s line %d: Invalid RekeyLimit suffix",
                    519:                            filename, linenum);
1.105     markus    520:                }
1.146     djm       521:                val64 *= scale;
                    522:                /* detect integer wrap and too-large limits */
                    523:                if ((val64 / scale) != orig || val64 > INT_MAX)
                    524:                        fatal("%.200s line %d: RekeyLimit too large",
                    525:                            filename, linenum);
                    526:                if (val64 < 16)
                    527:                        fatal("%.200s line %d: RekeyLimit too small",
                    528:                            filename, linenum);
1.105     markus    529:                if (*activep && *intptr == -1)
1.146     djm       530:                        *intptr = (int)val64;
1.105     markus    531:                break;
                    532:
1.17      markus    533:        case oIdentityFile:
1.42      provos    534:                arg = strdelim(&s);
1.40      ho        535:                if (!arg || *arg == '\0')
1.17      markus    536:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    537:                if (*activep) {
1.50      markus    538:                        intptr = &options->num_identity_files;
1.27      markus    539:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    540:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt   541:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50      markus    542:                        charptr =  &options->identity_files[*intptr];
1.38      provos    543:                        *charptr = xstrdup(arg);
1.27      markus    544:                        *intptr = *intptr + 1;
1.17      markus    545:                }
                    546:                break;
                    547:
1.34      markus    548:        case oXAuthLocation:
                    549:                charptr=&options->xauth_location;
                    550:                goto parse_string;
                    551:
1.17      markus    552:        case oUser:
                    553:                charptr = &options->user;
                    554: parse_string:
1.42      provos    555:                arg = strdelim(&s);
1.40      ho        556:                if (!arg || *arg == '\0')
1.17      markus    557:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    558:                if (*activep && *charptr == NULL)
1.38      provos    559:                        *charptr = xstrdup(arg);
1.17      markus    560:                break;
                    561:
                    562:        case oGlobalKnownHostsFile:
                    563:                charptr = &options->system_hostfile;
                    564:                goto parse_string;
                    565:
                    566:        case oUserKnownHostsFile:
                    567:                charptr = &options->user_hostfile;
                    568:                goto parse_string;
                    569:
1.27      markus    570:        case oGlobalKnownHostsFile2:
                    571:                charptr = &options->system_hostfile2;
                    572:                goto parse_string;
                    573:
                    574:        case oUserKnownHostsFile2:
                    575:                charptr = &options->user_hostfile2;
                    576:                goto parse_string;
                    577:
1.17      markus    578:        case oHostName:
                    579:                charptr = &options->hostname;
                    580:                goto parse_string;
                    581:
1.52      markus    582:        case oHostKeyAlias:
                    583:                charptr = &options->host_key_alias;
                    584:                goto parse_string;
                    585:
1.67      markus    586:        case oPreferredAuthentications:
                    587:                charptr = &options->preferred_authentications;
                    588:                goto parse_string;
                    589:
1.77      markus    590:        case oBindAddress:
                    591:                charptr = &options->bind_address;
                    592:                goto parse_string;
                    593:
1.85      jakob     594:        case oSmartcardDevice:
1.86      markus    595:                charptr = &options->smartcard_device;
                    596:                goto parse_string;
1.85      jakob     597:
1.17      markus    598:        case oProxyCommand:
1.144     reyk      599:                charptr = &options->proxy_command;
                    600: parse_command:
1.113     markus    601:                if (s == NULL)
                    602:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus    603:                len = strspn(s, WHITESPACE "=");
1.17      markus    604:                if (*activep && *charptr == NULL)
1.102     markus    605:                        *charptr = xstrdup(s + len);
1.17      markus    606:                return 0;
                    607:
                    608:        case oPort:
                    609:                intptr = &options->port;
                    610: parse_int:
1.42      provos    611:                arg = strdelim(&s);
1.40      ho        612:                if (!arg || *arg == '\0')
1.17      markus    613:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    614:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    615:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus    616:
                    617:                /* Octal, decimal, or hex format? */
1.38      provos    618:                value = strtol(arg, &endofnumber, 0);
                    619:                if (arg == endofnumber)
1.21      markus    620:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus    621:                if (*activep && *intptr == -1)
                    622:                        *intptr = value;
                    623:                break;
                    624:
                    625:        case oConnectionAttempts:
                    626:                intptr = &options->connection_attempts;
                    627:                goto parse_int;
                    628:
                    629:        case oCipher:
                    630:                intptr = &options->cipher;
1.42      provos    631:                arg = strdelim(&s);
1.40      ho        632:                if (!arg || *arg == '\0')
1.32      markus    633:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    634:                value = cipher_number(arg);
1.17      markus    635:                if (value == -1)
                    636:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.93      deraadt   637:                            filename, linenum, arg ? arg : "<NONE>");
1.17      markus    638:                if (*activep && *intptr == -1)
                    639:                        *intptr = value;
                    640:                break;
                    641:
1.25      markus    642:        case oCiphers:
1.42      provos    643:                arg = strdelim(&s);
1.40      ho        644:                if (!arg || *arg == '\0')
1.32      markus    645:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    646:                if (!ciphers_valid(arg))
1.31      markus    647:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt   648:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    649:                if (*activep && options->ciphers == NULL)
1.38      provos    650:                        options->ciphers = xstrdup(arg);
1.25      markus    651:                break;
                    652:
1.62      markus    653:        case oMacs:
                    654:                arg = strdelim(&s);
                    655:                if (!arg || *arg == '\0')
                    656:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    657:                if (!mac_valid(arg))
                    658:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt   659:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus    660:                if (*activep && options->macs == NULL)
                    661:                        options->macs = xstrdup(arg);
                    662:                break;
                    663:
1.76      markus    664:        case oHostKeyAlgorithms:
                    665:                arg = strdelim(&s);
                    666:                if (!arg || *arg == '\0')
                    667:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    668:                if (!key_names_valid2(arg))
                    669:                        fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93      deraadt   670:                            filename, linenum, arg ? arg : "<NONE>");
1.76      markus    671:                if (*activep && options->hostkeyalgorithms == NULL)
                    672:                        options->hostkeyalgorithms = xstrdup(arg);
                    673:                break;
                    674:
1.25      markus    675:        case oProtocol:
                    676:                intptr = &options->protocol;
1.42      provos    677:                arg = strdelim(&s);
1.40      ho        678:                if (!arg || *arg == '\0')
1.32      markus    679:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    680:                value = proto_spec(arg);
1.25      markus    681:                if (value == SSH_PROTO_UNKNOWN)
                    682:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93      deraadt   683:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    684:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                    685:                        *intptr = value;
                    686:                break;
                    687:
1.17      markus    688:        case oLogLevel:
                    689:                intptr = (int *) &options->log_level;
1.42      provos    690:                arg = strdelim(&s);
1.38      provos    691:                value = log_level_number(arg);
1.95      markus    692:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert   693:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt   694:                            filename, linenum, arg ? arg : "<NONE>");
1.95      markus    695:                if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
1.17      markus    696:                        *intptr = (LogLevel) value;
                    697:                break;
                    698:
1.88      stevesk   699:        case oLocalForward:
1.17      markus    700:        case oRemoteForward:
1.42      provos    701:                arg = strdelim(&s);
1.135     djm       702:                if (arg == NULL || *arg == '\0')
1.88      stevesk   703:                        fatal("%.200s line %d: Missing port argument.",
                    704:                            filename, linenum);
1.135     djm       705:                arg2 = strdelim(&s);
                    706:                if (arg2 == NULL || *arg2 == '\0')
                    707:                        fatal("%.200s line %d: Missing target argument.",
1.88      stevesk   708:                            filename, linenum);
1.135     djm       709:
                    710:                /* construct a string for parse_forward */
                    711:                snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
                    712:
                    713:                if (parse_forward(&fwd, fwdarg) == 0)
1.88      stevesk   714:                        fatal("%.200s line %d: Bad forwarding specification.",
                    715:                            filename, linenum);
1.135     djm       716:
1.88      stevesk   717:                if (*activep) {
                    718:                        if (opcode == oLocalForward)
1.135     djm       719:                                add_local_forward(options, &fwd);
1.88      stevesk   720:                        else if (opcode == oRemoteForward)
1.135     djm       721:                                add_remote_forward(options, &fwd);
1.88      stevesk   722:                }
1.17      markus    723:                break;
1.71      markus    724:
                    725:        case oDynamicForward:
                    726:                arg = strdelim(&s);
                    727:                if (!arg || *arg == '\0')
                    728:                        fatal("%.200s line %d: Missing port argument.",
                    729:                            filename, linenum);
1.135     djm       730:                memset(&fwd, '\0', sizeof(fwd));
                    731:                fwd.connect_host = "socks";
                    732:                fwd.listen_host = hpdelim(&arg);
                    733:                if (fwd.listen_host == NULL ||
                    734:                    strlen(fwd.listen_host) >= NI_MAXHOST)
                    735:                        fatal("%.200s line %d: Bad forwarding specification.",
                    736:                            filename, linenum);
                    737:                if (arg) {
                    738:                        fwd.listen_port = a2port(arg);
                    739:                        fwd.listen_host = cleanhostname(fwd.listen_host);
                    740:                } else {
                    741:                        fwd.listen_port = a2port(fwd.listen_host);
1.143     djm       742:                        fwd.listen_host = NULL;
1.135     djm       743:                }
                    744:                if (fwd.listen_port == 0)
1.71      markus    745:                        fatal("%.200s line %d: Badly formatted port number.",
                    746:                            filename, linenum);
1.87      markus    747:                if (*activep)
1.135     djm       748:                        add_local_forward(options, &fwd);
1.72      markus    749:                break;
1.17      markus    750:
1.90      stevesk   751:        case oClearAllForwardings:
                    752:                intptr = &options->clear_forwardings;
                    753:                goto parse_flag;
                    754:
1.17      markus    755:        case oHost:
                    756:                *activep = 0;
1.42      provos    757:                while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38      provos    758:                        if (match_pattern(host, arg)) {
                    759:                                debug("Applying options for %.100s", arg);
1.17      markus    760:                                *activep = 1;
                    761:                                break;
                    762:                        }
1.42      provos    763:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus    764:                return 0;
                    765:
                    766:        case oEscapeChar:
                    767:                intptr = &options->escape_char;
1.42      provos    768:                arg = strdelim(&s);
1.40      ho        769:                if (!arg || *arg == '\0')
1.17      markus    770:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    771:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus    772:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                    773:                        value = (u_char) arg[1] & 31;
1.38      provos    774:                else if (strlen(arg) == 1)
1.51      markus    775:                        value = (u_char) arg[0];
1.38      provos    776:                else if (strcmp(arg, "none") == 0)
1.79      stevesk   777:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus    778:                else {
                    779:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt   780:                            filename, linenum);
1.17      markus    781:                        /* NOTREACHED */
                    782:                        value = 0;      /* Avoid compiler warning. */
                    783:                }
                    784:                if (*activep && *intptr == -1)
                    785:                        *intptr = value;
1.112     djm       786:                break;
                    787:
                    788:        case oAddressFamily:
                    789:                arg = strdelim(&s);
1.140     markus    790:                if (!arg || *arg == '\0')
                    791:                        fatal("%s line %d: missing address family.",
                    792:                            filename, linenum);
1.114     djm       793:                intptr = &options->address_family;
1.112     djm       794:                if (strcasecmp(arg, "inet") == 0)
1.114     djm       795:                        value = AF_INET;
1.112     djm       796:                else if (strcasecmp(arg, "inet6") == 0)
1.114     djm       797:                        value = AF_INET6;
1.112     djm       798:                else if (strcasecmp(arg, "any") == 0)
1.114     djm       799:                        value = AF_UNSPEC;
1.112     djm       800:                else
                    801:                        fatal("Unsupported AddressFamily \"%s\"", arg);
1.114     djm       802:                if (*activep && *intptr == -1)
                    803:                        *intptr = value;
1.17      markus    804:                break;
                    805:
1.101     markus    806:        case oEnableSSHKeysign:
                    807:                intptr = &options->enable_ssh_keysign;
                    808:                goto parse_flag;
                    809:
1.128     markus    810:        case oIdentitiesOnly:
                    811:                intptr = &options->identities_only;
                    812:                goto parse_flag;
                    813:
1.127     markus    814:        case oServerAliveInterval:
                    815:                intptr = &options->server_alive_interval;
                    816:                goto parse_time;
                    817:
                    818:        case oServerAliveCountMax:
                    819:                intptr = &options->server_alive_count_max;
                    820:                goto parse_int;
                    821:
1.130     djm       822:        case oSendEnv:
                    823:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                    824:                        if (strchr(arg, '=') != NULL)
                    825:                                fatal("%s line %d: Invalid environment name.",
                    826:                                    filename, linenum);
1.137     djm       827:                        if (!*activep)
                    828:                                continue;
1.130     djm       829:                        if (options->num_send_env >= MAX_SEND_ENV)
                    830:                                fatal("%s line %d: too many send env.",
                    831:                                    filename, linenum);
                    832:                        options->send_env[options->num_send_env++] =
                    833:                            xstrdup(arg);
                    834:                }
                    835:                break;
                    836:
1.132     djm       837:        case oControlPath:
                    838:                charptr = &options->control_path;
                    839:                goto parse_string;
                    840:
                    841:        case oControlMaster:
                    842:                intptr = &options->control_master;
1.141     djm       843:                arg = strdelim(&s);
                    844:                if (!arg || *arg == '\0')
                    845:                        fatal("%.200s line %d: Missing ControlMaster argument.",
                    846:                            filename, linenum);
                    847:                value = 0;      /* To avoid compiler warning... */
                    848:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                    849:                        value = SSHCTL_MASTER_YES;
                    850:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                    851:                        value = SSHCTL_MASTER_NO;
                    852:                else if (strcmp(arg, "auto") == 0)
                    853:                        value = SSHCTL_MASTER_AUTO;
                    854:                else if (strcmp(arg, "ask") == 0)
                    855:                        value = SSHCTL_MASTER_ASK;
                    856:                else if (strcmp(arg, "autoask") == 0)
                    857:                        value = SSHCTL_MASTER_AUTO_ASK;
                    858:                else
                    859:                        fatal("%.200s line %d: Bad ControlMaster argument.",
                    860:                            filename, linenum);
                    861:                if (*activep && *intptr == -1)
                    862:                        *intptr = value;
                    863:                break;
1.132     djm       864:
1.136     djm       865:        case oHashKnownHosts:
                    866:                intptr = &options->hash_known_hosts;
                    867:                goto parse_flag;
                    868:
1.144     reyk      869:        case oTunnel:
                    870:                intptr = &options->tun_open;
1.145     reyk      871:                arg = strdelim(&s);
                    872:                if (!arg || *arg == '\0')
                    873:                        fatal("%s line %d: Missing yes/point-to-point/"
                    874:                            "ethernet/no argument.", filename, linenum);
                    875:                value = 0;      /* silence compiler */
                    876:                if (strcasecmp(arg, "ethernet") == 0)
                    877:                        value = SSH_TUNMODE_ETHERNET;
                    878:                else if (strcasecmp(arg, "point-to-point") == 0)
                    879:                        value = SSH_TUNMODE_POINTOPOINT;
                    880:                else if (strcasecmp(arg, "yes") == 0)
                    881:                        value = SSH_TUNMODE_DEFAULT;
                    882:                else if (strcasecmp(arg, "no") == 0)
                    883:                        value = SSH_TUNMODE_NO;
                    884:                else
                    885:                        fatal("%s line %d: Bad yes/point-to-point/ethernet/"
                    886:                            "no argument: %s", filename, linenum, arg);
                    887:                if (*activep)
                    888:                        *intptr = value;
                    889:                break;
1.144     reyk      890:
                    891:        case oTunnelDevice:
                    892:                arg = strdelim(&s);
                    893:                if (!arg || *arg == '\0')
                    894:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    895:                value = a2tun(arg, &value2);
1.145     reyk      896:                if (value == SSH_TUNID_ERR)
1.144     reyk      897:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                    898:                if (*activep) {
                    899:                        options->tun_local = value;
                    900:                        options->tun_remote = value2;
                    901:                }
                    902:                break;
                    903:
                    904:        case oLocalCommand:
                    905:                charptr = &options->local_command;
                    906:                goto parse_command;
                    907:
                    908:        case oPermitLocalCommand:
                    909:                intptr = &options->permit_local_command;
                    910:                goto parse_flag;
                    911:
1.96      markus    912:        case oDeprecated:
1.98      markus    913:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus    914:                    filename, linenum, keyword);
1.98      markus    915:                return 0;
1.96      markus    916:
1.110     jakob     917:        case oUnsupported:
                    918:                error("%s line %d: Unsupported option \"%s\"",
                    919:                    filename, linenum, keyword);
                    920:                return 0;
                    921:
1.17      markus    922:        default:
                    923:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                    924:        }
                    925:
                    926:        /* Check that there is no garbage at end of line. */
1.57      djm       927:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho        928:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm       929:                    filename, linenum, arg);
1.39      ho        930:        }
1.17      markus    931:        return 0;
1.1       deraadt   932: }
                    933:
                    934:
1.19      markus    935: /*
                    936:  * Reads the config file and modifies the options accordingly.  Options
                    937:  * should already be initialized before this call.  This never returns if
1.89      stevesk   938:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus    939:  */
1.1       deraadt   940:
1.89      stevesk   941: int
1.134     deraadt   942: read_config_file(const char *filename, const char *host, Options *options,
1.129     djm       943:     int checkperm)
1.1       deraadt   944: {
1.17      markus    945:        FILE *f;
                    946:        char line[1024];
                    947:        int active, linenum;
                    948:        int bad_options = 0;
                    949:
                    950:        /* Open the file. */
1.129     djm       951:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk   952:                return 0;
1.129     djm       953:
                    954:        if (checkperm) {
                    955:                struct stat sb;
1.134     deraadt   956:
1.131     dtucker   957:                if (fstat(fileno(f), &sb) == -1)
1.129     djm       958:                        fatal("fstat %s: %s", filename, strerror(errno));
                    959:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker   960:                    (sb.st_mode & 022) != 0))
1.129     djm       961:                        fatal("Bad owner or permissions on %s", filename);
                    962:        }
1.17      markus    963:
                    964:        debug("Reading configuration data %.200s", filename);
                    965:
1.19      markus    966:        /*
                    967:         * Mark that we are now processing the options.  This flag is turned
                    968:         * on/off by Host specifications.
                    969:         */
1.17      markus    970:        active = 1;
                    971:        linenum = 0;
                    972:        while (fgets(line, sizeof(line), f)) {
                    973:                /* Update line number counter. */
                    974:                linenum++;
                    975:                if (process_config_line(options, host, line, filename, linenum, &active) != 0)
                    976:                        bad_options++;
                    977:        }
                    978:        fclose(f);
                    979:        if (bad_options > 0)
1.64      millert   980:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt   981:                    filename, bad_options);
1.89      stevesk   982:        return 1;
1.1       deraadt   983: }
                    984:
1.19      markus    985: /*
                    986:  * Initializes options to special values that indicate that they have not yet
                    987:  * been set.  Read_config_file will only set options with this value. Options
                    988:  * are processed in the following order: command line, user config file,
                    989:  * system config file.  Last, fill_default_options is called.
                    990:  */
1.1       deraadt   991:
1.26      markus    992: void
1.17      markus    993: initialize_options(Options * options)
1.1       deraadt   994: {
1.17      markus    995:        memset(options, 'X', sizeof(*options));
                    996:        options->forward_agent = -1;
                    997:        options->forward_x11 = -1;
1.123     markus    998:        options->forward_x11_trusted = -1;
1.153     markus    999:        options->exit_on_forward_failure = -1;
1.34      markus   1000:        options->xauth_location = NULL;
1.17      markus   1001:        options->gateway_ports = -1;
                   1002:        options->use_privileged_port = -1;
                   1003:        options->rsa_authentication = -1;
1.50      markus   1004:        options->pubkey_authentication = -1;
1.78      markus   1005:        options->challenge_response_authentication = -1;
1.118     markus   1006:        options->gss_authentication = -1;
                   1007:        options->gss_deleg_creds = -1;
1.17      markus   1008:        options->password_authentication = -1;
1.48      markus   1009:        options->kbd_interactive_authentication = -1;
                   1010:        options->kbd_interactive_devices = NULL;
1.17      markus   1011:        options->rhosts_rsa_authentication = -1;
1.72      markus   1012:        options->hostbased_authentication = -1;
1.17      markus   1013:        options->batch_mode = -1;
                   1014:        options->check_host_ip = -1;
                   1015:        options->strict_host_key_checking = -1;
                   1016:        options->compression = -1;
1.126     markus   1017:        options->tcp_keep_alive = -1;
1.17      markus   1018:        options->compression_level = -1;
                   1019:        options->port = -1;
1.114     djm      1020:        options->address_family = -1;
1.17      markus   1021:        options->connection_attempts = -1;
1.111     djm      1022:        options->connection_timeout = -1;
1.17      markus   1023:        options->number_of_password_prompts = -1;
                   1024:        options->cipher = -1;
1.25      markus   1025:        options->ciphers = NULL;
1.62      markus   1026:        options->macs = NULL;
1.76      markus   1027:        options->hostkeyalgorithms = NULL;
1.25      markus   1028:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus   1029:        options->num_identity_files = 0;
                   1030:        options->hostname = NULL;
1.52      markus   1031:        options->host_key_alias = NULL;
1.17      markus   1032:        options->proxy_command = NULL;
                   1033:        options->user = NULL;
                   1034:        options->escape_char = -1;
                   1035:        options->system_hostfile = NULL;
                   1036:        options->user_hostfile = NULL;
1.27      markus   1037:        options->system_hostfile2 = NULL;
                   1038:        options->user_hostfile2 = NULL;
1.17      markus   1039:        options->num_local_forwards = 0;
                   1040:        options->num_remote_forwards = 0;
1.90      stevesk  1041:        options->clear_forwardings = -1;
1.95      markus   1042:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1043:        options->preferred_authentications = NULL;
1.77      markus   1044:        options->bind_address = NULL;
1.86      markus   1045:        options->smartcard_device = NULL;
1.101     markus   1046:        options->enable_ssh_keysign = - 1;
1.91      markus   1047:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1048:        options->identities_only = - 1;
1.105     markus   1049:        options->rekey_limit = - 1;
1.107     jakob    1050:        options->verify_host_key_dns = -1;
1.127     markus   1051:        options->server_alive_interval = -1;
                   1052:        options->server_alive_count_max = -1;
1.130     djm      1053:        options->num_send_env = 0;
1.132     djm      1054:        options->control_path = NULL;
                   1055:        options->control_master = -1;
1.136     djm      1056:        options->hash_known_hosts = -1;
1.144     reyk     1057:        options->tun_open = -1;
                   1058:        options->tun_local = -1;
                   1059:        options->tun_remote = -1;
                   1060:        options->local_command = NULL;
                   1061:        options->permit_local_command = -1;
1.1       deraadt  1062: }
                   1063:
1.19      markus   1064: /*
                   1065:  * Called after processing other sources of option data, this fills those
                   1066:  * options for which no value has been specified with their default values.
                   1067:  */
1.1       deraadt  1068:
1.26      markus   1069: void
1.17      markus   1070: fill_default_options(Options * options)
1.1       deraadt  1071: {
1.61      deraadt  1072:        int len;
                   1073:
1.17      markus   1074:        if (options->forward_agent == -1)
1.33      markus   1075:                options->forward_agent = 0;
1.17      markus   1076:        if (options->forward_x11 == -1)
1.23      markus   1077:                options->forward_x11 = 0;
1.123     markus   1078:        if (options->forward_x11_trusted == -1)
                   1079:                options->forward_x11_trusted = 0;
1.153     markus   1080:        if (options->exit_on_forward_failure == -1)
                   1081:                options->exit_on_forward_failure = 0;
1.34      markus   1082:        if (options->xauth_location == NULL)
1.80      markus   1083:                options->xauth_location = _PATH_XAUTH;
1.17      markus   1084:        if (options->gateway_ports == -1)
                   1085:                options->gateway_ports = 0;
                   1086:        if (options->use_privileged_port == -1)
1.65      markus   1087:                options->use_privileged_port = 0;
1.17      markus   1088:        if (options->rsa_authentication == -1)
                   1089:                options->rsa_authentication = 1;
1.50      markus   1090:        if (options->pubkey_authentication == -1)
                   1091:                options->pubkey_authentication = 1;
1.78      markus   1092:        if (options->challenge_response_authentication == -1)
1.83      markus   1093:                options->challenge_response_authentication = 1;
1.118     markus   1094:        if (options->gss_authentication == -1)
1.122     markus   1095:                options->gss_authentication = 0;
1.118     markus   1096:        if (options->gss_deleg_creds == -1)
                   1097:                options->gss_deleg_creds = 0;
1.17      markus   1098:        if (options->password_authentication == -1)
                   1099:                options->password_authentication = 1;
1.48      markus   1100:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1101:                options->kbd_interactive_authentication = 1;
1.17      markus   1102:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk  1103:                options->rhosts_rsa_authentication = 0;
1.72      markus   1104:        if (options->hostbased_authentication == -1)
                   1105:                options->hostbased_authentication = 0;
1.17      markus   1106:        if (options->batch_mode == -1)
                   1107:                options->batch_mode = 0;
                   1108:        if (options->check_host_ip == -1)
                   1109:                options->check_host_ip = 1;
                   1110:        if (options->strict_host_key_checking == -1)
                   1111:                options->strict_host_key_checking = 2;  /* 2 is default */
                   1112:        if (options->compression == -1)
                   1113:                options->compression = 0;
1.126     markus   1114:        if (options->tcp_keep_alive == -1)
                   1115:                options->tcp_keep_alive = 1;
1.17      markus   1116:        if (options->compression_level == -1)
                   1117:                options->compression_level = 6;
                   1118:        if (options->port == -1)
                   1119:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1120:        if (options->address_family == -1)
                   1121:                options->address_family = AF_UNSPEC;
1.17      markus   1122:        if (options->connection_attempts == -1)
1.84      markus   1123:                options->connection_attempts = 1;
1.17      markus   1124:        if (options->number_of_password_prompts == -1)
                   1125:                options->number_of_password_prompts = 3;
                   1126:        /* Selected in ssh_login(). */
                   1127:        if (options->cipher == -1)
                   1128:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus   1129:        /* options->ciphers, default set in myproposals.h */
1.62      markus   1130:        /* options->macs, default set in myproposals.h */
1.76      markus   1131:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus   1132:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.69      markus   1133:                options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17      markus   1134:        if (options->num_identity_files == 0) {
1.50      markus   1135:                if (options->protocol & SSH_PROTO_1) {
1.61      deraadt  1136:                        len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50      markus   1137:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1138:                            xmalloc(len);
                   1139:                        snprintf(options->identity_files[options->num_identity_files++],
                   1140:                            len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50      markus   1141:                }
                   1142:                if (options->protocol & SSH_PROTO_2) {
1.63      deraadt  1143:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
                   1144:                        options->identity_files[options->num_identity_files] =
                   1145:                            xmalloc(len);
                   1146:                        snprintf(options->identity_files[options->num_identity_files++],
                   1147:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
                   1148:
1.61      deraadt  1149:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50      markus   1150:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1151:                            xmalloc(len);
                   1152:                        snprintf(options->identity_files[options->num_identity_files++],
                   1153:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50      markus   1154:                }
1.27      markus   1155:        }
1.17      markus   1156:        if (options->escape_char == -1)
                   1157:                options->escape_char = '~';
                   1158:        if (options->system_hostfile == NULL)
1.55      markus   1159:                options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17      markus   1160:        if (options->user_hostfile == NULL)
1.55      markus   1161:                options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27      markus   1162:        if (options->system_hostfile2 == NULL)
1.55      markus   1163:                options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27      markus   1164:        if (options->user_hostfile2 == NULL)
1.55      markus   1165:                options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95      markus   1166:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1167:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk  1168:        if (options->clear_forwardings == 1)
                   1169:                clear_forwardings(options);
1.91      markus   1170:        if (options->no_host_authentication_for_localhost == - 1)
                   1171:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1172:        if (options->identities_only == -1)
                   1173:                options->identities_only = 0;
1.101     markus   1174:        if (options->enable_ssh_keysign == -1)
                   1175:                options->enable_ssh_keysign = 0;
1.105     markus   1176:        if (options->rekey_limit == -1)
                   1177:                options->rekey_limit = 0;
1.107     jakob    1178:        if (options->verify_host_key_dns == -1)
                   1179:                options->verify_host_key_dns = 0;
1.127     markus   1180:        if (options->server_alive_interval == -1)
                   1181:                options->server_alive_interval = 0;
                   1182:        if (options->server_alive_count_max == -1)
                   1183:                options->server_alive_count_max = 3;
1.132     djm      1184:        if (options->control_master == -1)
                   1185:                options->control_master = 0;
1.136     djm      1186:        if (options->hash_known_hosts == -1)
                   1187:                options->hash_known_hosts = 0;
1.144     reyk     1188:        if (options->tun_open == -1)
1.145     reyk     1189:                options->tun_open = SSH_TUNMODE_NO;
                   1190:        if (options->tun_local == -1)
                   1191:                options->tun_local = SSH_TUNID_ANY;
                   1192:        if (options->tun_remote == -1)
                   1193:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1194:        if (options->permit_local_command == -1)
                   1195:                options->permit_local_command = 0;
                   1196:        /* options->local_command should not be set by default */
1.17      markus   1197:        /* options->proxy_command should not be set by default */
                   1198:        /* options->user will be set in the main program if appropriate */
                   1199:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   1200:        /* options->host_key_alias should not be set by default */
1.67      markus   1201:        /* options->preferred_authentications will be set in ssh */
1.135     djm      1202: }
                   1203:
                   1204: /*
                   1205:  * parse_forward
                   1206:  * parses a string containing a port forwarding specification of the form:
                   1207:  *     [listenhost:]listenport:connecthost:connectport
                   1208:  * returns number of arguments parsed or zero on error
                   1209:  */
                   1210: int
                   1211: parse_forward(Forward *fwd, const char *fwdspec)
                   1212: {
                   1213:        int i;
                   1214:        char *p, *cp, *fwdarg[4];
                   1215:
                   1216:        memset(fwd, '\0', sizeof(*fwd));
                   1217:
                   1218:        cp = p = xstrdup(fwdspec);
                   1219:
                   1220:        /* skip leading spaces */
                   1221:        while (*cp && isspace(*cp))
                   1222:                cp++;
                   1223:
                   1224:        for (i = 0; i < 4; ++i)
                   1225:                if ((fwdarg[i] = hpdelim(&cp)) == NULL)
                   1226:                        break;
                   1227:
                   1228:        /* Check for trailing garbage in 4-arg case*/
                   1229:        if (cp != NULL)
                   1230:                i = 0;  /* failure */
                   1231:
                   1232:        switch (i) {
                   1233:        case 3:
                   1234:                fwd->listen_host = NULL;
                   1235:                fwd->listen_port = a2port(fwdarg[0]);
                   1236:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
                   1237:                fwd->connect_port = a2port(fwdarg[2]);
                   1238:                break;
                   1239:
                   1240:        case 4:
                   1241:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1242:                fwd->listen_port = a2port(fwdarg[1]);
                   1243:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
                   1244:                fwd->connect_port = a2port(fwdarg[3]);
                   1245:                break;
                   1246:        default:
                   1247:                i = 0; /* failure */
                   1248:        }
                   1249:
                   1250:        xfree(p);
                   1251:
                   1252:        if (fwd->listen_port == 0 && fwd->connect_port == 0)
                   1253:                goto fail_free;
                   1254:
                   1255:        if (fwd->connect_host != NULL &&
                   1256:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   1257:                goto fail_free;
                   1258:
                   1259:        return (i);
                   1260:
                   1261:  fail_free:
                   1262:        if (fwd->connect_host != NULL)
                   1263:                xfree(fwd->connect_host);
                   1264:        if (fwd->listen_host != NULL)
                   1265:                xfree(fwd->listen_host);
                   1266:        return (0);
1.1       deraadt  1267: }