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

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