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

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