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

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