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

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.50    ! markus     15: RCSID("$OpenBSD: readconf.c,v 1.49 2000/10/11 20:27:23 markus Exp $");
1.1       deraadt    16:
                     17: #include "ssh.h"
                     18: #include "readconf.h"
1.24      markus     19: #include "match.h"
1.1       deraadt    20: #include "xmalloc.h"
1.25      markus     21: #include "compat.h"
1.1       deraadt    22:
                     23: /* Format of the configuration file:
                     24:
                     25:    # Configuration data is parsed as follows:
                     26:    #  1. command line options
                     27:    #  2. user-specific file
                     28:    #  3. system-wide file
                     29:    # Any configuration value is only changed the first time it is set.
                     30:    # Thus, host-specific definitions should be at the beginning of the
                     31:    # configuration file, and defaults at the end.
                     32:
                     33:    # Host-specific declarations.  These may override anything above.  A single
                     34:    # host may match multiple declarations; these are processed in the order
                     35:    # that they are given in.
                     36:
                     37:    Host *.ngs.fi ngs.fi
                     38:      FallBackToRsh no
                     39:
                     40:    Host fake.com
                     41:      HostName another.host.name.real.org
                     42:      User blaah
                     43:      Port 34289
                     44:      ForwardX11 no
                     45:      ForwardAgent no
                     46:
                     47:    Host books.com
                     48:      RemoteForward 9999 shadows.cs.hut.fi:9999
                     49:      Cipher 3des
                     50:
                     51:    Host fascist.blob.com
                     52:      Port 23123
                     53:      User tylonen
                     54:      RhostsAuthentication no
                     55:      PasswordAuthentication no
                     56:
                     57:    Host puukko.hut.fi
                     58:      User t35124p
                     59:      ProxyCommand ssh-proxy %h %p
                     60:
                     61:    Host *.fr
                     62:      UseRsh yes
                     63:
                     64:    Host *.su
                     65:      Cipher none
                     66:      PasswordAuthentication no
                     67:
                     68:    # Defaults for various options
                     69:    Host *
                     70:      ForwardAgent no
1.50    ! markus     71:      ForwardX11 no
1.1       deraadt    72:      RhostsAuthentication yes
                     73:      PasswordAuthentication yes
                     74:      RSAAuthentication yes
                     75:      RhostsRSAAuthentication yes
                     76:      FallBackToRsh no
                     77:      UseRsh no
                     78:      StrictHostKeyChecking yes
                     79:      KeepAlives no
                     80:      IdentityFile ~/.ssh/identity
                     81:      Port 22
                     82:      EscapeChar ~
                     83:
                     84: */
                     85:
                     86: /* Keyword tokens. */
                     87:
1.17      markus     88: typedef enum {
                     89:        oBadOption,
                     90:        oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
                     91:        oPasswordAuthentication, oRSAAuthentication, oFallBackToRsh, oUseRsh,
1.34      markus     92:        oSkeyAuthentication, oXAuthLocation,
1.1       deraadt    93: #ifdef KRB4
1.17      markus     94:        oKerberosAuthentication,
1.1       deraadt    95: #endif /* KRB4 */
                     96: #ifdef AFS
1.17      markus     97:        oKerberosTgtPassing, oAFSTokenPassing,
1.1       deraadt    98: #endif
1.17      markus     99:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
                    100:        oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
                    101:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    102:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
                    103:        oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts, oTISAuthentication,
1.50    ! markus    104:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol,
        !           105:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.48      markus    106:        oKbdInteractiveAuthentication, oKbdInteractiveDevices
1.1       deraadt   107: } OpCodes;
                    108:
                    109: /* Textual representations of the tokens. */
                    110:
1.17      markus    111: static struct {
                    112:        const char *name;
                    113:        OpCodes opcode;
                    114: } keywords[] = {
                    115:        { "forwardagent", oForwardAgent },
                    116:        { "forwardx11", oForwardX11 },
1.34      markus    117:        { "xauthlocation", oXAuthLocation },
1.17      markus    118:        { "gatewayports", oGatewayPorts },
                    119:        { "useprivilegedport", oUsePrivilegedPort },
                    120:        { "rhostsauthentication", oRhostsAuthentication },
                    121:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    122:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    123:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17      markus    124:        { "rsaauthentication", oRSAAuthentication },
1.50    ! markus    125:        { "pubkeyauthentication", oPubkeyAuthentication },
        !           126:        { "dsaauthentication", oPubkeyAuthentication },         /* alias */
1.17      markus    127:        { "skeyauthentication", oSkeyAuthentication },
1.1       deraadt   128: #ifdef KRB4
1.17      markus    129:        { "kerberosauthentication", oKerberosAuthentication },
1.1       deraadt   130: #endif /* KRB4 */
1.5       dugsong   131: #ifdef AFS
1.17      markus    132:        { "kerberostgtpassing", oKerberosTgtPassing },
                    133:        { "afstokenpassing", oAFSTokenPassing },
1.1       deraadt   134: #endif
1.17      markus    135:        { "fallbacktorsh", oFallBackToRsh },
                    136:        { "usersh", oUseRsh },
                    137:        { "identityfile", oIdentityFile },
1.50    ! markus    138:        { "identityfile2", oIdentityFile },                     /* alias */
1.17      markus    139:        { "hostname", oHostName },
                    140:        { "proxycommand", oProxyCommand },
                    141:        { "port", oPort },
                    142:        { "cipher", oCipher },
1.25      markus    143:        { "ciphers", oCiphers },
                    144:        { "protocol", oProtocol },
1.17      markus    145:        { "remoteforward", oRemoteForward },
                    146:        { "localforward", oLocalForward },
                    147:        { "user", oUser },
                    148:        { "host", oHost },
                    149:        { "escapechar", oEscapeChar },
                    150:        { "rhostsrsaauthentication", oRhostsRSAAuthentication },
                    151:        { "globalknownhostsfile", oGlobalKnownHostsFile },
                    152:        { "userknownhostsfile", oUserKnownHostsFile },
1.27      markus    153:        { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
                    154:        { "userknownhostsfile2", oUserKnownHostsFile2 },
1.17      markus    155:        { "connectionattempts", oConnectionAttempts },
                    156:        { "batchmode", oBatchMode },
                    157:        { "checkhostip", oCheckHostIP },
                    158:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    159:        { "compression", oCompression },
                    160:        { "compressionlevel", oCompressionLevel },
                    161:        { "keepalive", oKeepAlives },
                    162:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
                    163:        { "tisauthentication", oTISAuthentication },
                    164:        { "loglevel", oLogLevel },
                    165:        { NULL, 0 }
1.13      markus    166: };
                    167:
1.19      markus    168: /*
                    169:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    170:  * error.
                    171:  */
1.1       deraadt   172:
1.26      markus    173: void
1.22      markus    174: add_local_forward(Options *options, u_short port, const char *host,
                    175:                  u_short host_port)
1.1       deraadt   176: {
1.17      markus    177:        Forward *fwd;
                    178:        extern uid_t original_real_uid;
                    179:        if (port < IPPORT_RESERVED && original_real_uid != 0)
                    180:                fatal("Privileged ports can only be forwarded by root.\n");
                    181:        if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    182:                fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
                    183:        fwd = &options->local_forwards[options->num_local_forwards++];
                    184:        fwd->port = port;
                    185:        fwd->host = xstrdup(host);
                    186:        fwd->host_port = host_port;
1.1       deraadt   187: }
                    188:
1.19      markus    189: /*
                    190:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    191:  * an error.
                    192:  */
1.1       deraadt   193:
1.26      markus    194: void
1.22      markus    195: add_remote_forward(Options *options, u_short port, const char *host,
                    196:                   u_short host_port)
1.1       deraadt   197: {
1.17      markus    198:        Forward *fwd;
                    199:        if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    200:                fatal("Too many remote forwards (max %d).",
                    201:                      SSH_MAX_FORWARDS_PER_DIRECTION);
                    202:        fwd = &options->remote_forwards[options->num_remote_forwards++];
                    203:        fwd->port = port;
                    204:        fwd->host = xstrdup(host);
                    205:        fwd->host_port = host_port;
1.1       deraadt   206: }
                    207:
1.19      markus    208: /*
                    209:  * Returns the number of the token pointed to by cp of length len. Never
                    210:  * returns if the token is not known.
                    211:  */
1.1       deraadt   212:
1.26      markus    213: static OpCodes
1.17      markus    214: parse_token(const char *cp, const char *filename, int linenum)
1.1       deraadt   215: {
1.17      markus    216:        unsigned int i;
1.1       deraadt   217:
1.17      markus    218:        for (i = 0; keywords[i].name; i++)
1.20      markus    219:                if (strcasecmp(cp, keywords[i].name) == 0)
1.17      markus    220:                        return keywords[i].opcode;
                    221:
                    222:        fprintf(stderr, "%s: line %d: Bad configuration option: %s\n",
                    223:                filename, linenum, cp);
                    224:        return oBadOption;
1.1       deraadt   225: }
                    226:
1.19      markus    227: /*
                    228:  * Processes a single option line as used in the configuration files. This
                    229:  * only sets those values that have not already been set.
                    230:  */
1.1       deraadt   231:
1.14      markus    232: int
                    233: process_config_line(Options *options, const char *host,
1.17      markus    234:                    char *line, const char *filename, int linenum,
                    235:                    int *activep)
1.1       deraadt   236: {
1.38      provos    237:        char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
1.22      markus    238:        int opcode, *intptr, value;
                    239:        u_short fwd_port, fwd_host_port;
1.1       deraadt   240:
1.42      provos    241:        s = line;
                    242:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
                    243:        keyword = strdelim(&s);
                    244:        /* Ignore leading whitespace. */
                    245:        if (*keyword == '\0')
1.43      markus    246:                keyword = strdelim(&s);
1.42      provos    247:        if (!*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    248:                return 0;
                    249:
1.38      provos    250:        opcode = parse_token(keyword, filename, linenum);
1.17      markus    251:
                    252:        switch (opcode) {
                    253:        case oBadOption:
1.19      markus    254:                /* don't panic, but count bad options */
                    255:                return -1;
1.17      markus    256:                /* NOTREACHED */
                    257:        case oForwardAgent:
                    258:                intptr = &options->forward_agent;
                    259: parse_flag:
1.42      provos    260:                arg = strdelim(&s);
1.40      ho        261:                if (!arg || *arg == '\0')
1.17      markus    262:                        fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
                    263:                value = 0;      /* To avoid compiler warning... */
1.38      provos    264:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    265:                        value = 1;
1.38      provos    266:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    267:                        value = 0;
                    268:                else
                    269:                        fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
                    270:                if (*activep && *intptr == -1)
                    271:                        *intptr = value;
                    272:                break;
                    273:
                    274:        case oForwardX11:
                    275:                intptr = &options->forward_x11;
                    276:                goto parse_flag;
                    277:
                    278:        case oGatewayPorts:
                    279:                intptr = &options->gateway_ports;
                    280:                goto parse_flag;
                    281:
                    282:        case oUsePrivilegedPort:
                    283:                intptr = &options->use_privileged_port;
                    284:                goto parse_flag;
                    285:
                    286:        case oRhostsAuthentication:
                    287:                intptr = &options->rhosts_authentication;
                    288:                goto parse_flag;
                    289:
                    290:        case oPasswordAuthentication:
                    291:                intptr = &options->password_authentication;
                    292:                goto parse_flag;
                    293:
1.48      markus    294:        case oKbdInteractiveAuthentication:
                    295:                intptr = &options->kbd_interactive_authentication;
                    296:                goto parse_flag;
                    297:
                    298:        case oKbdInteractiveDevices:
                    299:                charptr = &options->kbd_interactive_devices;
                    300:                goto parse_string;
                    301:
1.50    ! markus    302:        case oPubkeyAuthentication:
        !           303:                intptr = &options->pubkey_authentication;
1.30      markus    304:                goto parse_flag;
                    305:
1.17      markus    306:        case oRSAAuthentication:
                    307:                intptr = &options->rsa_authentication;
                    308:                goto parse_flag;
                    309:
                    310:        case oRhostsRSAAuthentication:
                    311:                intptr = &options->rhosts_rsa_authentication;
                    312:                goto parse_flag;
                    313:
                    314:        case oTISAuthentication:
                    315:                /* fallthrough, there is no difference on the client side */
                    316:        case oSkeyAuthentication:
                    317:                intptr = &options->skey_authentication;
                    318:                goto parse_flag;
1.16      markus    319:
1.1       deraadt   320: #ifdef KRB4
1.17      markus    321:        case oKerberosAuthentication:
                    322:                intptr = &options->kerberos_authentication;
                    323:                goto parse_flag;
1.1       deraadt   324: #endif /* KRB4 */
                    325:
1.5       dugsong   326: #ifdef AFS
1.17      markus    327:        case oKerberosTgtPassing:
                    328:                intptr = &options->kerberos_tgt_passing;
                    329:                goto parse_flag;
                    330:
                    331:        case oAFSTokenPassing:
                    332:                intptr = &options->afs_token_passing;
                    333:                goto parse_flag;
1.1       deraadt   334: #endif
1.17      markus    335:
                    336:        case oFallBackToRsh:
                    337:                intptr = &options->fallback_to_rsh;
                    338:                goto parse_flag;
                    339:
                    340:        case oUseRsh:
                    341:                intptr = &options->use_rsh;
                    342:                goto parse_flag;
                    343:
                    344:        case oBatchMode:
                    345:                intptr = &options->batch_mode;
                    346:                goto parse_flag;
                    347:
                    348:        case oCheckHostIP:
                    349:                intptr = &options->check_host_ip;
                    350:                goto parse_flag;
                    351:
                    352:        case oStrictHostKeyChecking:
                    353:                intptr = &options->strict_host_key_checking;
1.42      provos    354:                arg = strdelim(&s);
1.40      ho        355:                if (!arg || *arg == '\0')
1.17      markus    356:                        fatal("%.200s line %d: Missing yes/no argument.",
                    357:                              filename, linenum);
                    358:                value = 0;      /* To avoid compiler warning... */
1.38      provos    359:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    360:                        value = 1;
1.38      provos    361:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    362:                        value = 0;
1.38      provos    363:                else if (strcmp(arg, "ask") == 0)
1.17      markus    364:                        value = 2;
                    365:                else
                    366:                        fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
                    367:                if (*activep && *intptr == -1)
                    368:                        *intptr = value;
                    369:                break;
                    370:
                    371:        case oCompression:
                    372:                intptr = &options->compression;
                    373:                goto parse_flag;
                    374:
                    375:        case oKeepAlives:
                    376:                intptr = &options->keepalives;
                    377:                goto parse_flag;
                    378:
                    379:        case oNumberOfPasswordPrompts:
                    380:                intptr = &options->number_of_password_prompts;
                    381:                goto parse_int;
                    382:
                    383:        case oCompressionLevel:
                    384:                intptr = &options->compression_level;
                    385:                goto parse_int;
                    386:
                    387:        case oIdentityFile:
1.42      provos    388:                arg = strdelim(&s);
1.40      ho        389:                if (!arg || *arg == '\0')
1.17      markus    390:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    391:                if (*activep) {
1.50    ! markus    392:                        intptr = &options->num_identity_files;
1.27      markus    393:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    394:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
                    395:                                      filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50    ! markus    396:                        charptr =  &options->identity_files[*intptr];
1.38      provos    397:                        *charptr = xstrdup(arg);
1.27      markus    398:                        *intptr = *intptr + 1;
1.17      markus    399:                }
                    400:                break;
                    401:
1.34      markus    402:        case oXAuthLocation:
                    403:                charptr=&options->xauth_location;
                    404:                goto parse_string;
                    405:
1.17      markus    406:        case oUser:
                    407:                charptr = &options->user;
                    408: parse_string:
1.42      provos    409:                arg = strdelim(&s);
1.40      ho        410:                if (!arg || *arg == '\0')
1.17      markus    411:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    412:                if (*activep && *charptr == NULL)
1.38      provos    413:                        *charptr = xstrdup(arg);
1.17      markus    414:                break;
                    415:
                    416:        case oGlobalKnownHostsFile:
                    417:                charptr = &options->system_hostfile;
                    418:                goto parse_string;
                    419:
                    420:        case oUserKnownHostsFile:
                    421:                charptr = &options->user_hostfile;
                    422:                goto parse_string;
                    423:
1.27      markus    424:        case oGlobalKnownHostsFile2:
                    425:                charptr = &options->system_hostfile2;
                    426:                goto parse_string;
                    427:
                    428:        case oUserKnownHostsFile2:
                    429:                charptr = &options->user_hostfile2;
                    430:                goto parse_string;
                    431:
1.17      markus    432:        case oHostName:
                    433:                charptr = &options->hostname;
                    434:                goto parse_string;
                    435:
                    436:        case oProxyCommand:
                    437:                charptr = &options->proxy_command;
                    438:                string = xstrdup("");
1.42      provos    439:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.38      provos    440:                        string = xrealloc(string, strlen(string) + strlen(arg) + 2);
1.17      markus    441:                        strcat(string, " ");
1.38      provos    442:                        strcat(string, arg);
1.17      markus    443:                }
                    444:                if (*activep && *charptr == NULL)
                    445:                        *charptr = string;
                    446:                else
                    447:                        xfree(string);
                    448:                return 0;
                    449:
                    450:        case oPort:
                    451:                intptr = &options->port;
                    452: parse_int:
1.42      provos    453:                arg = strdelim(&s);
1.40      ho        454:                if (!arg || *arg == '\0')
1.17      markus    455:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    456:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    457:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus    458:
                    459:                /* Octal, decimal, or hex format? */
1.38      provos    460:                value = strtol(arg, &endofnumber, 0);
                    461:                if (arg == endofnumber)
1.21      markus    462:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus    463:                if (*activep && *intptr == -1)
                    464:                        *intptr = value;
                    465:                break;
                    466:
                    467:        case oConnectionAttempts:
                    468:                intptr = &options->connection_attempts;
                    469:                goto parse_int;
                    470:
                    471:        case oCipher:
                    472:                intptr = &options->cipher;
1.42      provos    473:                arg = strdelim(&s);
1.40      ho        474:                if (!arg || *arg == '\0')
1.32      markus    475:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    476:                value = cipher_number(arg);
1.17      markus    477:                if (value == -1)
                    478:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.38      provos    479:                              filename, linenum, arg ? arg : "<NONE>");
1.17      markus    480:                if (*activep && *intptr == -1)
                    481:                        *intptr = value;
                    482:                break;
                    483:
1.25      markus    484:        case oCiphers:
1.42      provos    485:                arg = strdelim(&s);
1.40      ho        486:                if (!arg || *arg == '\0')
1.32      markus    487:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    488:                if (!ciphers_valid(arg))
1.31      markus    489:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.38      provos    490:                              filename, linenum, arg ? arg : "<NONE>");
1.25      markus    491:                if (*activep && options->ciphers == NULL)
1.38      provos    492:                        options->ciphers = xstrdup(arg);
1.25      markus    493:                break;
                    494:
                    495:        case oProtocol:
                    496:                intptr = &options->protocol;
1.42      provos    497:                arg = strdelim(&s);
1.40      ho        498:                if (!arg || *arg == '\0')
1.32      markus    499:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    500:                value = proto_spec(arg);
1.25      markus    501:                if (value == SSH_PROTO_UNKNOWN)
                    502:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.38      provos    503:                              filename, linenum, arg ? arg : "<NONE>");
1.25      markus    504:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                    505:                        *intptr = value;
                    506:                break;
                    507:
1.17      markus    508:        case oLogLevel:
                    509:                intptr = (int *) &options->log_level;
1.42      provos    510:                arg = strdelim(&s);
1.38      provos    511:                value = log_level_number(arg);
1.17      markus    512:                if (value == (LogLevel) - 1)
                    513:                        fatal("%.200s line %d: unsupported log level '%s'\n",
1.38      provos    514:                              filename, linenum, arg ? arg : "<NONE>");
1.17      markus    515:                if (*activep && (LogLevel) * intptr == -1)
                    516:                        *intptr = (LogLevel) value;
                    517:                break;
                    518:
                    519:        case oRemoteForward:
1.42      provos    520:                arg = strdelim(&s);
1.40      ho        521:                if (!arg || *arg == '\0')
1.17      markus    522:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    523:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    524:                        fatal("%.200s line %d: Badly formatted port number.",
                    525:                              filename, linenum);
1.38      provos    526:                fwd_port = atoi(arg);
1.42      provos    527:                arg = strdelim(&s);
1.40      ho        528:                if (!arg || *arg == '\0')
1.17      markus    529:                        fatal("%.200s line %d: Missing second argument.",
                    530:                              filename, linenum);
1.38      provos    531:                if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17      markus    532:                        fatal("%.200s line %d: Badly formatted host:port.",
                    533:                              filename, linenum);
                    534:                if (*activep)
                    535:                        add_remote_forward(options, fwd_port, buf, fwd_host_port);
                    536:                break;
                    537:
                    538:        case oLocalForward:
1.42      provos    539:                arg = strdelim(&s);
1.40      ho        540:                if (!arg || *arg == '\0')
1.17      markus    541:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    542:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    543:                        fatal("%.200s line %d: Badly formatted port number.",
                    544:                              filename, linenum);
1.38      provos    545:                fwd_port = atoi(arg);
1.42      provos    546:                arg = strdelim(&s);
1.40      ho        547:                if (!arg || *arg == '\0')
1.17      markus    548:                        fatal("%.200s line %d: Missing second argument.",
                    549:                              filename, linenum);
1.38      provos    550:                if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17      markus    551:                        fatal("%.200s line %d: Badly formatted host:port.",
                    552:                              filename, linenum);
                    553:                if (*activep)
                    554:                        add_local_forward(options, fwd_port, buf, fwd_host_port);
                    555:                break;
                    556:
                    557:        case oHost:
                    558:                *activep = 0;
1.42      provos    559:                while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38      provos    560:                        if (match_pattern(host, arg)) {
                    561:                                debug("Applying options for %.100s", arg);
1.17      markus    562:                                *activep = 1;
                    563:                                break;
                    564:                        }
1.42      provos    565:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus    566:                return 0;
                    567:
                    568:        case oEscapeChar:
                    569:                intptr = &options->escape_char;
1.42      provos    570:                arg = strdelim(&s);
1.40      ho        571:                if (!arg || *arg == '\0')
1.17      markus    572:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    573:                if (arg[0] == '^' && arg[2] == 0 &&
                    574:                    (unsigned char) arg[1] >= 64 && (unsigned char) arg[1] < 128)
                    575:                        value = (unsigned char) arg[1] & 31;
                    576:                else if (strlen(arg) == 1)
                    577:                        value = (unsigned char) arg[0];
                    578:                else if (strcmp(arg, "none") == 0)
1.17      markus    579:                        value = -2;
                    580:                else {
                    581:                        fatal("%.200s line %d: Bad escape character.",
                    582:                              filename, linenum);
                    583:                        /* NOTREACHED */
                    584:                        value = 0;      /* Avoid compiler warning. */
                    585:                }
                    586:                if (*activep && *intptr == -1)
                    587:                        *intptr = value;
                    588:                break;
                    589:
                    590:        default:
                    591:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                    592:        }
                    593:
                    594:        /* Check that there is no garbage at end of line. */
1.42      provos    595:        if ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.39      ho        596:        {
                    597:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
                    598:                      filename, linenum, arg);
                    599:        }
1.17      markus    600:        return 0;
1.1       deraadt   601: }
                    602:
                    603:
1.19      markus    604: /*
                    605:  * Reads the config file and modifies the options accordingly.  Options
                    606:  * should already be initialized before this call.  This never returns if
                    607:  * there is an error.  If the file does not exist, this returns immediately.
                    608:  */
1.1       deraadt   609:
1.26      markus    610: void
1.17      markus    611: read_config_file(const char *filename, const char *host, Options *options)
1.1       deraadt   612: {
1.17      markus    613:        FILE *f;
                    614:        char line[1024];
                    615:        int active, linenum;
                    616:        int bad_options = 0;
                    617:
                    618:        /* Open the file. */
                    619:        f = fopen(filename, "r");
                    620:        if (!f)
                    621:                return;
                    622:
                    623:        debug("Reading configuration data %.200s", filename);
                    624:
1.19      markus    625:        /*
                    626:         * Mark that we are now processing the options.  This flag is turned
                    627:         * on/off by Host specifications.
                    628:         */
1.17      markus    629:        active = 1;
                    630:        linenum = 0;
                    631:        while (fgets(line, sizeof(line), f)) {
                    632:                /* Update line number counter. */
                    633:                linenum++;
                    634:                if (process_config_line(options, host, line, filename, linenum, &active) != 0)
                    635:                        bad_options++;
                    636:        }
                    637:        fclose(f);
                    638:        if (bad_options > 0)
                    639:                fatal("%s: terminating, %d bad configuration options\n",
                    640:                      filename, bad_options);
1.1       deraadt   641: }
                    642:
1.19      markus    643: /*
                    644:  * Initializes options to special values that indicate that they have not yet
                    645:  * been set.  Read_config_file will only set options with this value. Options
                    646:  * are processed in the following order: command line, user config file,
                    647:  * system config file.  Last, fill_default_options is called.
                    648:  */
1.1       deraadt   649:
1.26      markus    650: void
1.17      markus    651: initialize_options(Options * options)
1.1       deraadt   652: {
1.17      markus    653:        memset(options, 'X', sizeof(*options));
                    654:        options->forward_agent = -1;
                    655:        options->forward_x11 = -1;
1.34      markus    656:        options->xauth_location = NULL;
1.17      markus    657:        options->gateway_ports = -1;
                    658:        options->use_privileged_port = -1;
                    659:        options->rhosts_authentication = -1;
                    660:        options->rsa_authentication = -1;
1.50    ! markus    661:        options->pubkey_authentication = -1;
1.17      markus    662:        options->skey_authentication = -1;
1.1       deraadt   663: #ifdef KRB4
1.17      markus    664:        options->kerberos_authentication = -1;
1.1       deraadt   665: #endif
1.5       dugsong   666: #ifdef AFS
1.17      markus    667:        options->kerberos_tgt_passing = -1;
                    668:        options->afs_token_passing = -1;
1.1       deraadt   669: #endif
1.17      markus    670:        options->password_authentication = -1;
1.48      markus    671:        options->kbd_interactive_authentication = -1;
                    672:        options->kbd_interactive_devices = NULL;
1.17      markus    673:        options->rhosts_rsa_authentication = -1;
                    674:        options->fallback_to_rsh = -1;
                    675:        options->use_rsh = -1;
                    676:        options->batch_mode = -1;
                    677:        options->check_host_ip = -1;
                    678:        options->strict_host_key_checking = -1;
                    679:        options->compression = -1;
                    680:        options->keepalives = -1;
                    681:        options->compression_level = -1;
                    682:        options->port = -1;
                    683:        options->connection_attempts = -1;
                    684:        options->number_of_password_prompts = -1;
                    685:        options->cipher = -1;
1.25      markus    686:        options->ciphers = NULL;
                    687:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus    688:        options->num_identity_files = 0;
                    689:        options->hostname = NULL;
                    690:        options->proxy_command = NULL;
                    691:        options->user = NULL;
                    692:        options->escape_char = -1;
                    693:        options->system_hostfile = NULL;
                    694:        options->user_hostfile = NULL;
1.27      markus    695:        options->system_hostfile2 = NULL;
                    696:        options->user_hostfile2 = NULL;
1.17      markus    697:        options->num_local_forwards = 0;
                    698:        options->num_remote_forwards = 0;
                    699:        options->log_level = (LogLevel) - 1;
1.1       deraadt   700: }
                    701:
1.19      markus    702: /*
                    703:  * Called after processing other sources of option data, this fills those
                    704:  * options for which no value has been specified with their default values.
                    705:  */
1.1       deraadt   706:
1.26      markus    707: void
1.17      markus    708: fill_default_options(Options * options)
1.1       deraadt   709: {
1.17      markus    710:        if (options->forward_agent == -1)
1.33      markus    711:                options->forward_agent = 0;
1.17      markus    712:        if (options->forward_x11 == -1)
1.23      markus    713:                options->forward_x11 = 0;
1.34      markus    714: #ifdef XAUTH_PATH
                    715:        if (options->xauth_location == NULL)
1.35      markus    716:                options->xauth_location = XAUTH_PATH;
1.34      markus    717: #endif /* XAUTH_PATH */
1.17      markus    718:        if (options->gateway_ports == -1)
                    719:                options->gateway_ports = 0;
                    720:        if (options->use_privileged_port == -1)
                    721:                options->use_privileged_port = 1;
                    722:        if (options->rhosts_authentication == -1)
                    723:                options->rhosts_authentication = 1;
                    724:        if (options->rsa_authentication == -1)
                    725:                options->rsa_authentication = 1;
1.50    ! markus    726:        if (options->pubkey_authentication == -1)
        !           727:                options->pubkey_authentication = 1;
1.17      markus    728:        if (options->skey_authentication == -1)
                    729:                options->skey_authentication = 0;
1.1       deraadt   730: #ifdef KRB4
1.17      markus    731:        if (options->kerberos_authentication == -1)
1.45      provos    732:                options->kerberos_authentication = 1;
1.5       dugsong   733: #endif /* KRB4 */
                    734: #ifdef AFS
1.17      markus    735:        if (options->kerberos_tgt_passing == -1)
                    736:                options->kerberos_tgt_passing = 1;
                    737:        if (options->afs_token_passing == -1)
                    738:                options->afs_token_passing = 1;
1.5       dugsong   739: #endif /* AFS */
1.17      markus    740:        if (options->password_authentication == -1)
                    741:                options->password_authentication = 1;
1.48      markus    742:        if (options->kbd_interactive_authentication == -1)
                    743:                options->kbd_interactive_authentication = 0;
1.17      markus    744:        if (options->rhosts_rsa_authentication == -1)
                    745:                options->rhosts_rsa_authentication = 1;
                    746:        if (options->fallback_to_rsh == -1)
1.41      deraadt   747:                options->fallback_to_rsh = 0;
1.17      markus    748:        if (options->use_rsh == -1)
                    749:                options->use_rsh = 0;
                    750:        if (options->batch_mode == -1)
                    751:                options->batch_mode = 0;
                    752:        if (options->check_host_ip == -1)
                    753:                options->check_host_ip = 1;
                    754:        if (options->strict_host_key_checking == -1)
                    755:                options->strict_host_key_checking = 2;  /* 2 is default */
                    756:        if (options->compression == -1)
                    757:                options->compression = 0;
                    758:        if (options->keepalives == -1)
                    759:                options->keepalives = 1;
                    760:        if (options->compression_level == -1)
                    761:                options->compression_level = 6;
                    762:        if (options->port == -1)
                    763:                options->port = 0;      /* Filled in ssh_connect. */
                    764:        if (options->connection_attempts == -1)
                    765:                options->connection_attempts = 4;
                    766:        if (options->number_of_password_prompts == -1)
                    767:                options->number_of_password_prompts = 3;
                    768:        /* Selected in ssh_login(). */
                    769:        if (options->cipher == -1)
                    770:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus    771:        /* options->ciphers, default set in myproposals.h */
1.25      markus    772:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.28      markus    773:                options->protocol = SSH_PROTO_1|SSH_PROTO_2|SSH_PROTO_1_PREFERRED;
1.17      markus    774:        if (options->num_identity_files == 0) {
1.50    ! markus    775:                if (options->protocol & SSH_PROTO_1) {
        !           776:                        options->identity_files[options->num_identity_files] =
        !           777:                            xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
        !           778:                        sprintf(options->identity_files[options->num_identity_files++],
        !           779:                            "~/%.100s", SSH_CLIENT_IDENTITY);
        !           780:                }
        !           781:                if (options->protocol & SSH_PROTO_2) {
        !           782:                        options->identity_files[options->num_identity_files] =
        !           783:                            xmalloc(2 + strlen(SSH_CLIENT_ID_DSA) + 1);
        !           784:                        sprintf(options->identity_files[options->num_identity_files++],
        !           785:                            "~/%.100s", SSH_CLIENT_ID_DSA);
        !           786:                }
1.27      markus    787:        }
1.17      markus    788:        if (options->escape_char == -1)
                    789:                options->escape_char = '~';
                    790:        if (options->system_hostfile == NULL)
                    791:                options->system_hostfile = SSH_SYSTEM_HOSTFILE;
                    792:        if (options->user_hostfile == NULL)
                    793:                options->user_hostfile = SSH_USER_HOSTFILE;
1.27      markus    794:        if (options->system_hostfile2 == NULL)
                    795:                options->system_hostfile2 = SSH_SYSTEM_HOSTFILE2;
                    796:        if (options->user_hostfile2 == NULL)
                    797:                options->user_hostfile2 = SSH_USER_HOSTFILE2;
1.17      markus    798:        if (options->log_level == (LogLevel) - 1)
                    799:                options->log_level = SYSLOG_LEVEL_INFO;
                    800:        /* options->proxy_command should not be set by default */
                    801:        /* options->user will be set in the main program if appropriate */
                    802:        /* options->hostname will be set in the main program if appropriate */
1.1       deraadt   803: }