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

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