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

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