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

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