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

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