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

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