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

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