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

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.112   ! djm        15: RCSID("$OpenBSD: readconf.c,v 1.111 2003/05/15 14:55:25 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:      RhostsAuthentication no
                     61:      PasswordAuthentication no
                     62:
                     63:    Host puukko.hut.fi
                     64:      User t35124p
                     65:      ProxyCommand ssh-proxy %h %p
                     66:
                     67:    Host *.fr
1.96      markus     68:      PublicKeyAuthentication no
1.1       deraadt    69:
                     70:    Host *.su
                     71:      Cipher none
                     72:      PasswordAuthentication no
                     73:
                     74:    # Defaults for various options
                     75:    Host *
                     76:      ForwardAgent no
1.50      markus     77:      ForwardX11 no
1.1       deraadt    78:      RhostsAuthentication yes
                     79:      PasswordAuthentication yes
                     80:      RSAAuthentication yes
                     81:      RhostsRSAAuthentication yes
                     82:      StrictHostKeyChecking yes
                     83:      KeepAlives no
                     84:      IdentityFile ~/.ssh/identity
                     85:      Port 22
                     86:      EscapeChar ~
                     87:
                     88: */
                     89:
                     90: /* Keyword tokens. */
                     91:
1.17      markus     92: typedef enum {
                     93:        oBadOption,
                     94:        oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
1.100     deraadt    95:        oPasswordAuthentication, oRSAAuthentication,
1.59      markus     96:        oChallengeResponseAuthentication, oXAuthLocation,
1.108     jakob      97:        oKerberosAuthentication, oKerberosTgtPassing, oAFSTokenPassing,
1.17      markus     98:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
                     99:        oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
                    100:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    101:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.59      markus    102:        oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts,
1.62      markus    103:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50      markus    104:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67      markus    105:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    106:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.90      stevesk   107:        oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
1.96      markus    108:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       109:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.112   ! djm       110:        oAddressFamily,
1.110     jakob     111:        oDeprecated, oUnsupported
1.1       deraadt   112: } OpCodes;
                    113:
                    114: /* Textual representations of the tokens. */
                    115:
1.17      markus    116: static struct {
                    117:        const char *name;
                    118:        OpCodes opcode;
                    119: } keywords[] = {
                    120:        { "forwardagent", oForwardAgent },
                    121:        { "forwardx11", oForwardX11 },
1.34      markus    122:        { "xauthlocation", oXAuthLocation },
1.17      markus    123:        { "gatewayports", oGatewayPorts },
                    124:        { "useprivilegedport", oUsePrivilegedPort },
                    125:        { "rhostsauthentication", oRhostsAuthentication },
                    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: #if defined(KRB4) || defined(KRB5)
1.17      markus    138:        { "kerberosauthentication", oKerberosAuthentication },
1.82      dugsong   139:        { "kerberostgtpassing", oKerberosTgtPassing },
1.110     jakob     140: #else
                    141:        { "kerberosauthentication", oUnsupported },
                    142:        { "kerberostgtpassing", oUnsupported },
                    143: #endif
                    144: #if defined(AFS)
1.17      markus    145:        { "afstokenpassing", oAFSTokenPassing },
1.110     jakob     146: #else
                    147:        { "afstokenpassing", oUnsupported },
                    148: #endif
1.96      markus    149:        { "fallbacktorsh", oDeprecated },
                    150:        { "usersh", oDeprecated },
1.17      markus    151:        { "identityfile", oIdentityFile },
1.50      markus    152:        { "identityfile2", oIdentityFile },                     /* alias */
1.17      markus    153:        { "hostname", oHostName },
1.52      markus    154:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    155:        { "proxycommand", oProxyCommand },
                    156:        { "port", oPort },
                    157:        { "cipher", oCipher },
1.25      markus    158:        { "ciphers", oCiphers },
1.62      markus    159:        { "macs", oMacs },
1.25      markus    160:        { "protocol", oProtocol },
1.17      markus    161:        { "remoteforward", oRemoteForward },
                    162:        { "localforward", oLocalForward },
                    163:        { "user", oUser },
                    164:        { "host", oHost },
                    165:        { "escapechar", oEscapeChar },
                    166:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.81      markus    167:        { "userknownhostsfile", oUserKnownHostsFile },          /* obsolete */
1.27      markus    168:        { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
1.81      markus    169:        { "userknownhostsfile2", oUserKnownHostsFile2 },        /* obsolete */
1.17      markus    170:        { "connectionattempts", oConnectionAttempts },
                    171:        { "batchmode", oBatchMode },
                    172:        { "checkhostip", oCheckHostIP },
                    173:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    174:        { "compression", oCompression },
                    175:        { "compressionlevel", oCompressionLevel },
                    176:        { "keepalive", oKeepAlives },
                    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.110     jakob     190: #ifdef DNS
1.107     jakob     191:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.110     jakob     192: #else
                    193:        { "verifyhostkeydns", oUnsupported },
                    194: #endif
1.93      deraadt   195:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    196:        { "rekeylimit", oRekeyLimit },
1.111     djm       197:        { "connecttimeout", oConnectTimeout },
1.112   ! djm       198:        { "addressfamily", oAddressFamily },
1.92      stevesk   199:        { NULL, oBadOption }
1.13      markus    200: };
                    201:
1.19      markus    202: /*
                    203:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    204:  * error.
                    205:  */
1.1       deraadt   206:
1.26      markus    207: void
1.22      markus    208: add_local_forward(Options *options, u_short port, const char *host,
                    209:                  u_short host_port)
1.1       deraadt   210: {
1.17      markus    211:        Forward *fwd;
                    212:        extern uid_t original_real_uid;
                    213:        if (port < IPPORT_RESERVED && original_real_uid != 0)
1.64      millert   214:                fatal("Privileged ports can only be forwarded by root.");
1.17      markus    215:        if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    216:                fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
                    217:        fwd = &options->local_forwards[options->num_local_forwards++];
                    218:        fwd->port = port;
                    219:        fwd->host = xstrdup(host);
                    220:        fwd->host_port = host_port;
1.1       deraadt   221: }
                    222:
1.19      markus    223: /*
                    224:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    225:  * an error.
                    226:  */
1.1       deraadt   227:
1.26      markus    228: void
1.22      markus    229: add_remote_forward(Options *options, u_short port, const char *host,
                    230:                   u_short host_port)
1.1       deraadt   231: {
1.17      markus    232:        Forward *fwd;
                    233:        if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    234:                fatal("Too many remote forwards (max %d).",
1.93      deraadt   235:                    SSH_MAX_FORWARDS_PER_DIRECTION);
1.17      markus    236:        fwd = &options->remote_forwards[options->num_remote_forwards++];
                    237:        fwd->port = port;
                    238:        fwd->host = xstrdup(host);
                    239:        fwd->host_port = host_port;
1.1       deraadt   240: }
                    241:
1.90      stevesk   242: static void
                    243: clear_forwardings(Options *options)
                    244: {
                    245:        int i;
                    246:
                    247:        for (i = 0; i < options->num_local_forwards; i++)
                    248:                xfree(options->local_forwards[i].host);
                    249:        options->num_local_forwards = 0;
                    250:        for (i = 0; i < options->num_remote_forwards; i++)
                    251:                xfree(options->remote_forwards[i].host);
                    252:        options->num_remote_forwards = 0;
                    253: }
                    254:
1.19      markus    255: /*
1.70      stevesk   256:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    257:  */
1.1       deraadt   258:
1.26      markus    259: static OpCodes
1.17      markus    260: parse_token(const char *cp, const char *filename, int linenum)
1.1       deraadt   261: {
1.51      markus    262:        u_int i;
1.1       deraadt   263:
1.17      markus    264:        for (i = 0; keywords[i].name; i++)
1.20      markus    265:                if (strcasecmp(cp, keywords[i].name) == 0)
1.17      markus    266:                        return keywords[i].opcode;
                    267:
1.75      stevesk   268:        error("%s: line %d: Bad configuration option: %s",
                    269:            filename, linenum, cp);
1.17      markus    270:        return oBadOption;
1.1       deraadt   271: }
                    272:
1.19      markus    273: /*
                    274:  * Processes a single option line as used in the configuration files. This
                    275:  * only sets those values that have not already been set.
                    276:  */
1.102     markus    277: #define WHITESPACE " \t\r\n"
1.1       deraadt   278:
1.14      markus    279: int
                    280: process_config_line(Options *options, const char *host,
1.17      markus    281:                    char *line, const char *filename, int linenum,
                    282:                    int *activep)
1.1       deraadt   283: {
1.102     markus    284:        char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
1.22      markus    285:        int opcode, *intptr, value;
1.102     markus    286:        size_t len;
1.22      markus    287:        u_short fwd_port, fwd_host_port;
1.88      stevesk   288:        char sfwd_host_port[6];
1.112   ! djm       289:        extern int IPv4or6;
1.106     djm       290:
                    291:        /* Strip trailing whitespace */
                    292:        for(len = strlen(line) - 1; len > 0; len--) {
                    293:                if (strchr(WHITESPACE, line[len]) == NULL)
                    294:                        break;
                    295:                line[len] = '\0';
                    296:        }
1.1       deraadt   297:
1.42      provos    298:        s = line;
                    299:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
                    300:        keyword = strdelim(&s);
                    301:        /* Ignore leading whitespace. */
                    302:        if (*keyword == '\0')
1.43      markus    303:                keyword = strdelim(&s);
1.56      deraadt   304:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    305:                return 0;
                    306:
1.38      provos    307:        opcode = parse_token(keyword, filename, linenum);
1.17      markus    308:
                    309:        switch (opcode) {
                    310:        case oBadOption:
1.19      markus    311:                /* don't panic, but count bad options */
                    312:                return -1;
1.17      markus    313:                /* NOTREACHED */
1.111     djm       314:        case oConnectTimeout:
                    315:                intptr = &options->connection_timeout;
                    316: /* parse_time: */
                    317:                arg = strdelim(&s);
                    318:                if (!arg || *arg == '\0')
                    319:                        fatal("%s line %d: missing time value.",
                    320:                            filename, linenum);
                    321:                if ((value = convtime(arg)) == -1)
                    322:                        fatal("%s line %d: invalid time value.",
                    323:                            filename, linenum);
                    324:                if (*intptr == -1)
                    325:                        *intptr = value;
                    326:                break;
                    327:
1.17      markus    328:        case oForwardAgent:
                    329:                intptr = &options->forward_agent;
                    330: parse_flag:
1.42      provos    331:                arg = strdelim(&s);
1.40      ho        332:                if (!arg || *arg == '\0')
1.17      markus    333:                        fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
                    334:                value = 0;      /* To avoid compiler warning... */
1.38      provos    335:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    336:                        value = 1;
1.38      provos    337:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    338:                        value = 0;
                    339:                else
                    340:                        fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
                    341:                if (*activep && *intptr == -1)
                    342:                        *intptr = value;
                    343:                break;
                    344:
                    345:        case oForwardX11:
                    346:                intptr = &options->forward_x11;
                    347:                goto parse_flag;
                    348:
                    349:        case oGatewayPorts:
                    350:                intptr = &options->gateway_ports;
                    351:                goto parse_flag;
                    352:
                    353:        case oUsePrivilegedPort:
                    354:                intptr = &options->use_privileged_port;
                    355:                goto parse_flag;
                    356:
                    357:        case oRhostsAuthentication:
                    358:                intptr = &options->rhosts_authentication;
                    359:                goto parse_flag;
                    360:
                    361:        case oPasswordAuthentication:
                    362:                intptr = &options->password_authentication;
                    363:                goto parse_flag;
                    364:
1.48      markus    365:        case oKbdInteractiveAuthentication:
                    366:                intptr = &options->kbd_interactive_authentication;
                    367:                goto parse_flag;
                    368:
                    369:        case oKbdInteractiveDevices:
                    370:                charptr = &options->kbd_interactive_devices;
                    371:                goto parse_string;
                    372:
1.50      markus    373:        case oPubkeyAuthentication:
                    374:                intptr = &options->pubkey_authentication;
1.30      markus    375:                goto parse_flag;
                    376:
1.17      markus    377:        case oRSAAuthentication:
                    378:                intptr = &options->rsa_authentication;
                    379:                goto parse_flag;
                    380:
                    381:        case oRhostsRSAAuthentication:
                    382:                intptr = &options->rhosts_rsa_authentication;
                    383:                goto parse_flag;
                    384:
1.72      markus    385:        case oHostbasedAuthentication:
                    386:                intptr = &options->hostbased_authentication;
                    387:                goto parse_flag;
                    388:
1.59      markus    389:        case oChallengeResponseAuthentication:
1.78      markus    390:                intptr = &options->challenge_response_authentication;
1.17      markus    391:                goto parse_flag;
1.108     jakob     392:
1.17      markus    393:        case oKerberosAuthentication:
                    394:                intptr = &options->kerberos_authentication;
                    395:                goto parse_flag;
1.108     jakob     396:
1.17      markus    397:        case oKerberosTgtPassing:
                    398:                intptr = &options->kerberos_tgt_passing;
                    399:                goto parse_flag;
1.108     jakob     400:
1.17      markus    401:        case oAFSTokenPassing:
                    402:                intptr = &options->afs_token_passing;
                    403:                goto parse_flag;
1.108     jakob     404:
1.17      markus    405:        case oBatchMode:
                    406:                intptr = &options->batch_mode;
                    407:                goto parse_flag;
                    408:
                    409:        case oCheckHostIP:
                    410:                intptr = &options->check_host_ip;
                    411:                goto parse_flag;
                    412:
1.107     jakob     413:        case oVerifyHostKeyDNS:
                    414:                intptr = &options->verify_host_key_dns;
                    415:                goto parse_flag;
                    416:
1.17      markus    417:        case oStrictHostKeyChecking:
                    418:                intptr = &options->strict_host_key_checking;
1.42      provos    419:                arg = strdelim(&s);
1.40      ho        420:                if (!arg || *arg == '\0')
1.60      stevesk   421:                        fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93      deraadt   422:                            filename, linenum);
1.17      markus    423:                value = 0;      /* To avoid compiler warning... */
1.38      provos    424:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    425:                        value = 1;
1.38      provos    426:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    427:                        value = 0;
1.38      provos    428:                else if (strcmp(arg, "ask") == 0)
1.17      markus    429:                        value = 2;
                    430:                else
                    431:                        fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
                    432:                if (*activep && *intptr == -1)
                    433:                        *intptr = value;
                    434:                break;
                    435:
                    436:        case oCompression:
                    437:                intptr = &options->compression;
                    438:                goto parse_flag;
                    439:
                    440:        case oKeepAlives:
                    441:                intptr = &options->keepalives;
                    442:                goto parse_flag;
                    443:
1.91      markus    444:        case oNoHostAuthenticationForLocalhost:
                    445:                intptr = &options->no_host_authentication_for_localhost;
                    446:                goto parse_flag;
                    447:
1.17      markus    448:        case oNumberOfPasswordPrompts:
                    449:                intptr = &options->number_of_password_prompts;
                    450:                goto parse_int;
                    451:
                    452:        case oCompressionLevel:
                    453:                intptr = &options->compression_level;
                    454:                goto parse_int;
                    455:
1.105     markus    456:        case oRekeyLimit:
                    457:                intptr = &options->rekey_limit;
                    458:                arg = strdelim(&s);
                    459:                if (!arg || *arg == '\0')
                    460:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    461:                if (arg[0] < '0' || arg[0] > '9')
                    462:                        fatal("%.200s line %d: Bad number.", filename, linenum);
                    463:                value = strtol(arg, &endofnumber, 10);
                    464:                if (arg == endofnumber)
                    465:                        fatal("%.200s line %d: Bad number.", filename, linenum);
                    466:                switch (toupper(*endofnumber)) {
                    467:                case 'K':
                    468:                        value *= 1<<10;
                    469:                        break;
                    470:                case 'M':
                    471:                        value *= 1<<20;
                    472:                        break;
                    473:                case 'G':
                    474:                        value *= 1<<30;
                    475:                        break;
                    476:                }
                    477:                if (*activep && *intptr == -1)
                    478:                        *intptr = value;
                    479:                break;
                    480:
1.17      markus    481:        case oIdentityFile:
1.42      provos    482:                arg = strdelim(&s);
1.40      ho        483:                if (!arg || *arg == '\0')
1.17      markus    484:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    485:                if (*activep) {
1.50      markus    486:                        intptr = &options->num_identity_files;
1.27      markus    487:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    488:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt   489:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50      markus    490:                        charptr =  &options->identity_files[*intptr];
1.38      provos    491:                        *charptr = xstrdup(arg);
1.27      markus    492:                        *intptr = *intptr + 1;
1.17      markus    493:                }
                    494:                break;
                    495:
1.34      markus    496:        case oXAuthLocation:
                    497:                charptr=&options->xauth_location;
                    498:                goto parse_string;
                    499:
1.17      markus    500:        case oUser:
                    501:                charptr = &options->user;
                    502: parse_string:
1.42      provos    503:                arg = strdelim(&s);
1.40      ho        504:                if (!arg || *arg == '\0')
1.17      markus    505:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    506:                if (*activep && *charptr == NULL)
1.38      provos    507:                        *charptr = xstrdup(arg);
1.17      markus    508:                break;
                    509:
                    510:        case oGlobalKnownHostsFile:
                    511:                charptr = &options->system_hostfile;
                    512:                goto parse_string;
                    513:
                    514:        case oUserKnownHostsFile:
                    515:                charptr = &options->user_hostfile;
                    516:                goto parse_string;
                    517:
1.27      markus    518:        case oGlobalKnownHostsFile2:
                    519:                charptr = &options->system_hostfile2;
                    520:                goto parse_string;
                    521:
                    522:        case oUserKnownHostsFile2:
                    523:                charptr = &options->user_hostfile2;
                    524:                goto parse_string;
                    525:
1.17      markus    526:        case oHostName:
                    527:                charptr = &options->hostname;
                    528:                goto parse_string;
                    529:
1.52      markus    530:        case oHostKeyAlias:
                    531:                charptr = &options->host_key_alias;
                    532:                goto parse_string;
                    533:
1.67      markus    534:        case oPreferredAuthentications:
                    535:                charptr = &options->preferred_authentications;
                    536:                goto parse_string;
                    537:
1.77      markus    538:        case oBindAddress:
                    539:                charptr = &options->bind_address;
                    540:                goto parse_string;
                    541:
1.85      jakob     542:        case oSmartcardDevice:
1.86      markus    543:                charptr = &options->smartcard_device;
                    544:                goto parse_string;
1.85      jakob     545:
1.17      markus    546:        case oProxyCommand:
                    547:                charptr = &options->proxy_command;
1.102     markus    548:                len = strspn(s, WHITESPACE "=");
1.17      markus    549:                if (*activep && *charptr == NULL)
1.102     markus    550:                        *charptr = xstrdup(s + len);
1.17      markus    551:                return 0;
                    552:
                    553:        case oPort:
                    554:                intptr = &options->port;
                    555: parse_int:
1.42      provos    556:                arg = strdelim(&s);
1.40      ho        557:                if (!arg || *arg == '\0')
1.17      markus    558:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    559:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    560:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus    561:
                    562:                /* Octal, decimal, or hex format? */
1.38      provos    563:                value = strtol(arg, &endofnumber, 0);
                    564:                if (arg == endofnumber)
1.21      markus    565:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus    566:                if (*activep && *intptr == -1)
                    567:                        *intptr = value;
                    568:                break;
                    569:
                    570:        case oConnectionAttempts:
                    571:                intptr = &options->connection_attempts;
                    572:                goto parse_int;
                    573:
                    574:        case oCipher:
                    575:                intptr = &options->cipher;
1.42      provos    576:                arg = strdelim(&s);
1.40      ho        577:                if (!arg || *arg == '\0')
1.32      markus    578:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    579:                value = cipher_number(arg);
1.17      markus    580:                if (value == -1)
                    581:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.93      deraadt   582:                            filename, linenum, arg ? arg : "<NONE>");
1.17      markus    583:                if (*activep && *intptr == -1)
                    584:                        *intptr = value;
                    585:                break;
                    586:
1.25      markus    587:        case oCiphers:
1.42      provos    588:                arg = strdelim(&s);
1.40      ho        589:                if (!arg || *arg == '\0')
1.32      markus    590:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    591:                if (!ciphers_valid(arg))
1.31      markus    592:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt   593:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    594:                if (*activep && options->ciphers == NULL)
1.38      provos    595:                        options->ciphers = xstrdup(arg);
1.25      markus    596:                break;
                    597:
1.62      markus    598:        case oMacs:
                    599:                arg = strdelim(&s);
                    600:                if (!arg || *arg == '\0')
                    601:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    602:                if (!mac_valid(arg))
                    603:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt   604:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus    605:                if (*activep && options->macs == NULL)
                    606:                        options->macs = xstrdup(arg);
                    607:                break;
                    608:
1.76      markus    609:        case oHostKeyAlgorithms:
                    610:                arg = strdelim(&s);
                    611:                if (!arg || *arg == '\0')
                    612:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    613:                if (!key_names_valid2(arg))
                    614:                        fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93      deraadt   615:                            filename, linenum, arg ? arg : "<NONE>");
1.76      markus    616:                if (*activep && options->hostkeyalgorithms == NULL)
                    617:                        options->hostkeyalgorithms = xstrdup(arg);
                    618:                break;
                    619:
1.25      markus    620:        case oProtocol:
                    621:                intptr = &options->protocol;
1.42      provos    622:                arg = strdelim(&s);
1.40      ho        623:                if (!arg || *arg == '\0')
1.32      markus    624:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    625:                value = proto_spec(arg);
1.25      markus    626:                if (value == SSH_PROTO_UNKNOWN)
                    627:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93      deraadt   628:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    629:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                    630:                        *intptr = value;
                    631:                break;
                    632:
1.17      markus    633:        case oLogLevel:
                    634:                intptr = (int *) &options->log_level;
1.42      provos    635:                arg = strdelim(&s);
1.38      provos    636:                value = log_level_number(arg);
1.95      markus    637:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert   638:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt   639:                            filename, linenum, arg ? arg : "<NONE>");
1.95      markus    640:                if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
1.17      markus    641:                        *intptr = (LogLevel) value;
                    642:                break;
                    643:
1.88      stevesk   644:        case oLocalForward:
1.17      markus    645:        case oRemoteForward:
1.42      provos    646:                arg = strdelim(&s);
1.40      ho        647:                if (!arg || *arg == '\0')
1.88      stevesk   648:                        fatal("%.200s line %d: Missing port argument.",
                    649:                            filename, linenum);
                    650:                if ((fwd_port = a2port(arg)) == 0)
                    651:                        fatal("%.200s line %d: Bad listen port.",
                    652:                            filename, linenum);
1.42      provos    653:                arg = strdelim(&s);
1.40      ho        654:                if (!arg || *arg == '\0')
1.17      markus    655:                        fatal("%.200s line %d: Missing second argument.",
1.88      stevesk   656:                            filename, linenum);
                    657:                if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
                    658:                    sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
                    659:                        fatal("%.200s line %d: Bad forwarding specification.",
                    660:                            filename, linenum);
                    661:                if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
                    662:                        fatal("%.200s line %d: Bad forwarding port.",
                    663:                            filename, linenum);
                    664:                if (*activep) {
                    665:                        if (opcode == oLocalForward)
                    666:                                add_local_forward(options, fwd_port, buf,
                    667:                                    fwd_host_port);
                    668:                        else if (opcode == oRemoteForward)
                    669:                                add_remote_forward(options, fwd_port, buf,
                    670:                                    fwd_host_port);
                    671:                }
1.17      markus    672:                break;
1.71      markus    673:
                    674:        case oDynamicForward:
                    675:                arg = strdelim(&s);
                    676:                if (!arg || *arg == '\0')
                    677:                        fatal("%.200s line %d: Missing port argument.",
                    678:                            filename, linenum);
1.74      stevesk   679:                fwd_port = a2port(arg);
                    680:                if (fwd_port == 0)
1.71      markus    681:                        fatal("%.200s line %d: Badly formatted port number.",
                    682:                            filename, linenum);
1.87      markus    683:                if (*activep)
                    684:                        add_local_forward(options, fwd_port, "socks4", 0);
1.72      markus    685:                break;
1.17      markus    686:
1.90      stevesk   687:        case oClearAllForwardings:
                    688:                intptr = &options->clear_forwardings;
                    689:                goto parse_flag;
                    690:
1.17      markus    691:        case oHost:
                    692:                *activep = 0;
1.42      provos    693:                while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38      provos    694:                        if (match_pattern(host, arg)) {
                    695:                                debug("Applying options for %.100s", arg);
1.17      markus    696:                                *activep = 1;
                    697:                                break;
                    698:                        }
1.42      provos    699:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus    700:                return 0;
                    701:
                    702:        case oEscapeChar:
                    703:                intptr = &options->escape_char;
1.42      provos    704:                arg = strdelim(&s);
1.40      ho        705:                if (!arg || *arg == '\0')
1.17      markus    706:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    707:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus    708:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                    709:                        value = (u_char) arg[1] & 31;
1.38      provos    710:                else if (strlen(arg) == 1)
1.51      markus    711:                        value = (u_char) arg[0];
1.38      provos    712:                else if (strcmp(arg, "none") == 0)
1.79      stevesk   713:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus    714:                else {
                    715:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt   716:                            filename, linenum);
1.17      markus    717:                        /* NOTREACHED */
                    718:                        value = 0;      /* Avoid compiler warning. */
                    719:                }
                    720:                if (*activep && *intptr == -1)
                    721:                        *intptr = value;
1.112   ! djm       722:                break;
        !           723:
        !           724:        case oAddressFamily:
        !           725:                arg = strdelim(&s);
        !           726:                if (strcasecmp(arg, "inet") == 0)
        !           727:                        IPv4or6 = AF_INET;
        !           728:                else if (strcasecmp(arg, "inet6") == 0)
        !           729:                        IPv4or6 = AF_INET6;
        !           730:                else if (strcasecmp(arg, "any") == 0)
        !           731:                        IPv4or6 = AF_UNSPEC;
        !           732:                else
        !           733:                        fatal("Unsupported AddressFamily \"%s\"", arg);
1.17      markus    734:                break;
                    735:
1.101     markus    736:        case oEnableSSHKeysign:
                    737:                intptr = &options->enable_ssh_keysign;
                    738:                goto parse_flag;
                    739:
1.96      markus    740:        case oDeprecated:
1.98      markus    741:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus    742:                    filename, linenum, keyword);
1.98      markus    743:                return 0;
1.96      markus    744:
1.110     jakob     745:        case oUnsupported:
                    746:                error("%s line %d: Unsupported option \"%s\"",
                    747:                    filename, linenum, keyword);
                    748:                return 0;
                    749:
1.17      markus    750:        default:
                    751:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                    752:        }
                    753:
                    754:        /* Check that there is no garbage at end of line. */
1.57      djm       755:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho        756:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.93      deraadt   757:                     filename, linenum, arg);
1.39      ho        758:        }
1.17      markus    759:        return 0;
1.1       deraadt   760: }
                    761:
                    762:
1.19      markus    763: /*
                    764:  * Reads the config file and modifies the options accordingly.  Options
                    765:  * should already be initialized before this call.  This never returns if
1.89      stevesk   766:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus    767:  */
1.1       deraadt   768:
1.89      stevesk   769: int
1.17      markus    770: read_config_file(const char *filename, const char *host, Options *options)
1.1       deraadt   771: {
1.17      markus    772:        FILE *f;
                    773:        char line[1024];
                    774:        int active, linenum;
                    775:        int bad_options = 0;
                    776:
                    777:        /* Open the file. */
                    778:        f = fopen(filename, "r");
                    779:        if (!f)
1.89      stevesk   780:                return 0;
1.17      markus    781:
                    782:        debug("Reading configuration data %.200s", filename);
                    783:
1.19      markus    784:        /*
                    785:         * Mark that we are now processing the options.  This flag is turned
                    786:         * on/off by Host specifications.
                    787:         */
1.17      markus    788:        active = 1;
                    789:        linenum = 0;
                    790:        while (fgets(line, sizeof(line), f)) {
                    791:                /* Update line number counter. */
                    792:                linenum++;
                    793:                if (process_config_line(options, host, line, filename, linenum, &active) != 0)
                    794:                        bad_options++;
                    795:        }
                    796:        fclose(f);
                    797:        if (bad_options > 0)
1.64      millert   798:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt   799:                    filename, bad_options);
1.89      stevesk   800:        return 1;
1.1       deraadt   801: }
                    802:
1.19      markus    803: /*
                    804:  * Initializes options to special values that indicate that they have not yet
                    805:  * been set.  Read_config_file will only set options with this value. Options
                    806:  * are processed in the following order: command line, user config file,
                    807:  * system config file.  Last, fill_default_options is called.
                    808:  */
1.1       deraadt   809:
1.26      markus    810: void
1.17      markus    811: initialize_options(Options * options)
1.1       deraadt   812: {
1.17      markus    813:        memset(options, 'X', sizeof(*options));
                    814:        options->forward_agent = -1;
                    815:        options->forward_x11 = -1;
1.34      markus    816:        options->xauth_location = NULL;
1.17      markus    817:        options->gateway_ports = -1;
                    818:        options->use_privileged_port = -1;
                    819:        options->rhosts_authentication = -1;
                    820:        options->rsa_authentication = -1;
1.50      markus    821:        options->pubkey_authentication = -1;
1.78      markus    822:        options->challenge_response_authentication = -1;
1.17      markus    823:        options->kerberos_authentication = -1;
1.82      dugsong   824:        options->kerberos_tgt_passing = -1;
1.17      markus    825:        options->afs_token_passing = -1;
                    826:        options->password_authentication = -1;
1.48      markus    827:        options->kbd_interactive_authentication = -1;
                    828:        options->kbd_interactive_devices = NULL;
1.17      markus    829:        options->rhosts_rsa_authentication = -1;
1.72      markus    830:        options->hostbased_authentication = -1;
1.17      markus    831:        options->batch_mode = -1;
                    832:        options->check_host_ip = -1;
                    833:        options->strict_host_key_checking = -1;
                    834:        options->compression = -1;
                    835:        options->keepalives = -1;
                    836:        options->compression_level = -1;
                    837:        options->port = -1;
                    838:        options->connection_attempts = -1;
1.111     djm       839:        options->connection_timeout = -1;
1.17      markus    840:        options->number_of_password_prompts = -1;
                    841:        options->cipher = -1;
1.25      markus    842:        options->ciphers = NULL;
1.62      markus    843:        options->macs = NULL;
1.76      markus    844:        options->hostkeyalgorithms = NULL;
1.25      markus    845:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus    846:        options->num_identity_files = 0;
                    847:        options->hostname = NULL;
1.52      markus    848:        options->host_key_alias = NULL;
1.17      markus    849:        options->proxy_command = NULL;
                    850:        options->user = NULL;
                    851:        options->escape_char = -1;
                    852:        options->system_hostfile = NULL;
                    853:        options->user_hostfile = NULL;
1.27      markus    854:        options->system_hostfile2 = NULL;
                    855:        options->user_hostfile2 = NULL;
1.17      markus    856:        options->num_local_forwards = 0;
                    857:        options->num_remote_forwards = 0;
1.90      stevesk   858:        options->clear_forwardings = -1;
1.95      markus    859:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus    860:        options->preferred_authentications = NULL;
1.77      markus    861:        options->bind_address = NULL;
1.86      markus    862:        options->smartcard_device = NULL;
1.101     markus    863:        options->enable_ssh_keysign = - 1;
1.91      markus    864:        options->no_host_authentication_for_localhost = - 1;
1.105     markus    865:        options->rekey_limit = - 1;
1.107     jakob     866:        options->verify_host_key_dns = -1;
1.1       deraadt   867: }
                    868:
1.19      markus    869: /*
                    870:  * Called after processing other sources of option data, this fills those
                    871:  * options for which no value has been specified with their default values.
                    872:  */
1.1       deraadt   873:
1.26      markus    874: void
1.17      markus    875: fill_default_options(Options * options)
1.1       deraadt   876: {
1.61      deraadt   877:        int len;
                    878:
1.17      markus    879:        if (options->forward_agent == -1)
1.33      markus    880:                options->forward_agent = 0;
1.17      markus    881:        if (options->forward_x11 == -1)
1.23      markus    882:                options->forward_x11 = 0;
1.34      markus    883:        if (options->xauth_location == NULL)
1.80      markus    884:                options->xauth_location = _PATH_XAUTH;
1.17      markus    885:        if (options->gateway_ports == -1)
                    886:                options->gateway_ports = 0;
                    887:        if (options->use_privileged_port == -1)
1.65      markus    888:                options->use_privileged_port = 0;
1.17      markus    889:        if (options->rhosts_authentication == -1)
1.99      stevesk   890:                options->rhosts_authentication = 0;
1.17      markus    891:        if (options->rsa_authentication == -1)
                    892:                options->rsa_authentication = 1;
1.50      markus    893:        if (options->pubkey_authentication == -1)
                    894:                options->pubkey_authentication = 1;
1.78      markus    895:        if (options->challenge_response_authentication == -1)
1.83      markus    896:                options->challenge_response_authentication = 1;
1.17      markus    897:        if (options->kerberos_authentication == -1)
1.45      provos    898:                options->kerberos_authentication = 1;
1.17      markus    899:        if (options->kerberos_tgt_passing == -1)
                    900:                options->kerberos_tgt_passing = 1;
                    901:        if (options->afs_token_passing == -1)
                    902:                options->afs_token_passing = 1;
                    903:        if (options->password_authentication == -1)
                    904:                options->password_authentication = 1;
1.48      markus    905:        if (options->kbd_interactive_authentication == -1)
1.59      markus    906:                options->kbd_interactive_authentication = 1;
1.17      markus    907:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk   908:                options->rhosts_rsa_authentication = 0;
1.72      markus    909:        if (options->hostbased_authentication == -1)
                    910:                options->hostbased_authentication = 0;
1.17      markus    911:        if (options->batch_mode == -1)
                    912:                options->batch_mode = 0;
                    913:        if (options->check_host_ip == -1)
                    914:                options->check_host_ip = 1;
                    915:        if (options->strict_host_key_checking == -1)
                    916:                options->strict_host_key_checking = 2;  /* 2 is default */
                    917:        if (options->compression == -1)
                    918:                options->compression = 0;
                    919:        if (options->keepalives == -1)
                    920:                options->keepalives = 1;
                    921:        if (options->compression_level == -1)
                    922:                options->compression_level = 6;
                    923:        if (options->port == -1)
                    924:                options->port = 0;      /* Filled in ssh_connect. */
                    925:        if (options->connection_attempts == -1)
1.84      markus    926:                options->connection_attempts = 1;
1.17      markus    927:        if (options->number_of_password_prompts == -1)
                    928:                options->number_of_password_prompts = 3;
                    929:        /* Selected in ssh_login(). */
                    930:        if (options->cipher == -1)
                    931:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus    932:        /* options->ciphers, default set in myproposals.h */
1.62      markus    933:        /* options->macs, default set in myproposals.h */
1.76      markus    934:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus    935:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.69      markus    936:                options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17      markus    937:        if (options->num_identity_files == 0) {
1.50      markus    938:                if (options->protocol & SSH_PROTO_1) {
1.61      deraadt   939:                        len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50      markus    940:                        options->identity_files[options->num_identity_files] =
1.61      deraadt   941:                            xmalloc(len);
                    942:                        snprintf(options->identity_files[options->num_identity_files++],
                    943:                            len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50      markus    944:                }
                    945:                if (options->protocol & SSH_PROTO_2) {
1.63      deraadt   946:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
                    947:                        options->identity_files[options->num_identity_files] =
                    948:                            xmalloc(len);
                    949:                        snprintf(options->identity_files[options->num_identity_files++],
                    950:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
                    951:
1.61      deraadt   952:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50      markus    953:                        options->identity_files[options->num_identity_files] =
1.61      deraadt   954:                            xmalloc(len);
                    955:                        snprintf(options->identity_files[options->num_identity_files++],
                    956:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50      markus    957:                }
1.27      markus    958:        }
1.17      markus    959:        if (options->escape_char == -1)
                    960:                options->escape_char = '~';
                    961:        if (options->system_hostfile == NULL)
1.55      markus    962:                options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17      markus    963:        if (options->user_hostfile == NULL)
1.55      markus    964:                options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27      markus    965:        if (options->system_hostfile2 == NULL)
1.55      markus    966:                options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27      markus    967:        if (options->user_hostfile2 == NULL)
1.55      markus    968:                options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95      markus    969:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus    970:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk   971:        if (options->clear_forwardings == 1)
                    972:                clear_forwardings(options);
1.91      markus    973:        if (options->no_host_authentication_for_localhost == - 1)
                    974:                options->no_host_authentication_for_localhost = 0;
1.101     markus    975:        if (options->enable_ssh_keysign == -1)
                    976:                options->enable_ssh_keysign = 0;
1.105     markus    977:        if (options->rekey_limit == -1)
                    978:                options->rekey_limit = 0;
1.107     jakob     979:        if (options->verify_host_key_dns == -1)
                    980:                options->verify_host_key_dns = 0;
1.17      markus    981:        /* options->proxy_command should not be set by default */
                    982:        /* options->user will be set in the main program if appropriate */
                    983:        /* options->hostname will be set in the main program if appropriate */
1.52      markus    984:        /* options->host_key_alias should not be set by default */
1.67      markus    985:        /* options->preferred_authentications will be set in ssh */
1.1       deraadt   986: }