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

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