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

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