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

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