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

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