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

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