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

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