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

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