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

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