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

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