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

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