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

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