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

1.166   ! grunk       1: /* $OpenBSD: readconf.c,v 1.165 2008/01/19 23:09:49 djm Exp $ */
1.1       deraadt     2: /*
1.18      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for reading the configuration files.
1.26      markus      7:  *
1.46      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.18      deraadt    13:  */
1.1       deraadt    14:
1.147     stevesk    15: #include <sys/types.h>
                     16: #include <sys/stat.h>
1.152     stevesk    17: #include <sys/socket.h>
                     18:
                     19: #include <netinet/in.h>
1.148     stevesk    20:
                     21: #include <ctype.h>
1.154     stevesk    22: #include <errno.h>
1.155     stevesk    23: #include <netdb.h>
1.159     deraadt    24: #include <signal.h>
1.158     stevesk    25: #include <stdio.h>
1.157     stevesk    26: #include <string.h>
1.156     stevesk    27: #include <unistd.h>
1.1       deraadt    28:
1.159     deraadt    29: #include "xmalloc.h"
1.1       deraadt    30: #include "ssh.h"
1.25      markus     31: #include "compat.h"
1.58      markus     32: #include "cipher.h"
1.55      markus     33: #include "pathnames.h"
1.58      markus     34: #include "log.h"
1.159     deraadt    35: #include "key.h"
1.58      markus     36: #include "readconf.h"
                     37: #include "match.h"
                     38: #include "misc.h"
1.159     deraadt    39: #include "buffer.h"
1.62      markus     40: #include "kex.h"
                     41: #include "mac.h"
1.1       deraadt    42:
                     43: /* Format of the configuration file:
                     44:
                     45:    # Configuration data is parsed as follows:
                     46:    #  1. command line options
                     47:    #  2. user-specific file
                     48:    #  3. system-wide file
                     49:    # Any configuration value is only changed the first time it is set.
                     50:    # Thus, host-specific definitions should be at the beginning of the
                     51:    # configuration file, and defaults at the end.
                     52:
                     53:    # Host-specific declarations.  These may override anything above.  A single
                     54:    # host may match multiple declarations; these are processed in the order
                     55:    # that they are given in.
                     56:
                     57:    Host *.ngs.fi ngs.fi
1.96      markus     58:      User foo
1.1       deraadt    59:
                     60:    Host fake.com
                     61:      HostName another.host.name.real.org
                     62:      User blaah
                     63:      Port 34289
                     64:      ForwardX11 no
                     65:      ForwardAgent no
                     66:
                     67:    Host books.com
                     68:      RemoteForward 9999 shadows.cs.hut.fi:9999
                     69:      Cipher 3des
                     70:
                     71:    Host fascist.blob.com
                     72:      Port 23123
                     73:      User tylonen
                     74:      PasswordAuthentication no
                     75:
                     76:    Host puukko.hut.fi
                     77:      User t35124p
                     78:      ProxyCommand ssh-proxy %h %p
                     79:
                     80:    Host *.fr
1.96      markus     81:      PublicKeyAuthentication no
1.1       deraadt    82:
                     83:    Host *.su
                     84:      Cipher none
                     85:      PasswordAuthentication no
                     86:
1.144     reyk       87:    Host vpn.fake.com
                     88:      Tunnel yes
                     89:      TunnelDevice 3
                     90:
1.1       deraadt    91:    # Defaults for various options
                     92:    Host *
                     93:      ForwardAgent no
1.50      markus     94:      ForwardX11 no
1.1       deraadt    95:      PasswordAuthentication yes
                     96:      RSAAuthentication yes
                     97:      RhostsRSAAuthentication yes
                     98:      StrictHostKeyChecking yes
1.126     markus     99:      TcpKeepAlive no
1.1       deraadt   100:      IdentityFile ~/.ssh/identity
                    101:      Port 22
                    102:      EscapeChar ~
                    103:
                    104: */
                    105:
                    106: /* Keyword tokens. */
                    107:
1.17      markus    108: typedef enum {
                    109:        oBadOption,
1.123     markus    110:        oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
1.153     markus    111:        oExitOnForwardFailure,
1.100     deraadt   112:        oPasswordAuthentication, oRSAAuthentication,
1.59      markus    113:        oChallengeResponseAuthentication, oXAuthLocation,
1.17      markus    114:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
                    115:        oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
                    116:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    117:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126     markus    118:        oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62      markus    119:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50      markus    120:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67      markus    121:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    122:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.90      stevesk   123:        oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
1.96      markus    124:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       125:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    126:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    127:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.136     djm       128:        oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
1.144     reyk      129:        oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
1.110     jakob     130:        oDeprecated, oUnsupported
1.1       deraadt   131: } OpCodes;
                    132:
                    133: /* Textual representations of the tokens. */
                    134:
1.17      markus    135: static struct {
                    136:        const char *name;
                    137:        OpCodes opcode;
                    138: } keywords[] = {
                    139:        { "forwardagent", oForwardAgent },
                    140:        { "forwardx11", oForwardX11 },
1.123     markus    141:        { "forwardx11trusted", oForwardX11Trusted },
1.153     markus    142:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    143:        { "xauthlocation", oXAuthLocation },
1.17      markus    144:        { "gatewayports", oGatewayPorts },
                    145:        { "useprivilegedport", oUsePrivilegedPort },
1.116     markus    146:        { "rhostsauthentication", oDeprecated },
1.17      markus    147:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    148:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    149:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17      markus    150:        { "rsaauthentication", oRSAAuthentication },
1.50      markus    151:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    152:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.72      markus    153:        { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73      markus    154:        { "hostbasedauthentication", oHostbasedAuthentication },
1.59      markus    155:        { "challengeresponseauthentication", oChallengeResponseAuthentication },
                    156:        { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
                    157:        { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
1.110     jakob     158:        { "kerberosauthentication", oUnsupported },
                    159:        { "kerberostgtpassing", oUnsupported },
                    160:        { "afstokenpassing", oUnsupported },
1.118     markus    161: #if defined(GSSAPI)
                    162:        { "gssapiauthentication", oGssAuthentication },
                    163:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    164: #else
                    165:        { "gssapiauthentication", oUnsupported },
                    166:        { "gssapidelegatecredentials", oUnsupported },
                    167: #endif
1.96      markus    168:        { "fallbacktorsh", oDeprecated },
                    169:        { "usersh", oDeprecated },
1.17      markus    170:        { "identityfile", oIdentityFile },
1.50      markus    171:        { "identityfile2", oIdentityFile },                     /* alias */
1.128     markus    172:        { "identitiesonly", oIdentitiesOnly },
1.17      markus    173:        { "hostname", oHostName },
1.52      markus    174:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    175:        { "proxycommand", oProxyCommand },
                    176:        { "port", oPort },
                    177:        { "cipher", oCipher },
1.25      markus    178:        { "ciphers", oCiphers },
1.62      markus    179:        { "macs", oMacs },
1.25      markus    180:        { "protocol", oProtocol },
1.17      markus    181:        { "remoteforward", oRemoteForward },
                    182:        { "localforward", oLocalForward },
                    183:        { "user", oUser },
                    184:        { "host", oHost },
                    185:        { "escapechar", oEscapeChar },
                    186:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.81      markus    187:        { "userknownhostsfile", oUserKnownHostsFile },          /* obsolete */
1.27      markus    188:        { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
1.81      markus    189:        { "userknownhostsfile2", oUserKnownHostsFile2 },        /* obsolete */
1.17      markus    190:        { "connectionattempts", oConnectionAttempts },
                    191:        { "batchmode", oBatchMode },
                    192:        { "checkhostip", oCheckHostIP },
                    193:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    194:        { "compression", oCompression },
                    195:        { "compressionlevel", oCompressionLevel },
1.126     markus    196:        { "tcpkeepalive", oTCPKeepAlive },
                    197:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    198:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
                    199:        { "loglevel", oLogLevel },
1.71      markus    200:        { "dynamicforward", oDynamicForward },
1.67      markus    201:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    202:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77      markus    203:        { "bindaddress", oBindAddress },
1.110     jakob     204: #ifdef SMARTCARD
1.85      jakob     205:        { "smartcarddevice", oSmartcardDevice },
1.110     jakob     206: #else
                    207:        { "smartcarddevice", oUnsupported },
                    208: #endif
1.93      deraadt   209:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    210:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     211:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   212:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    213:        { "rekeylimit", oRekeyLimit },
1.111     djm       214:        { "connecttimeout", oConnectTimeout },
1.112     djm       215:        { "addressfamily", oAddressFamily },
1.127     markus    216:        { "serveraliveinterval", oServerAliveInterval },
                    217:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       218:        { "sendenv", oSendEnv },
1.132     djm       219:        { "controlpath", oControlPath },
                    220:        { "controlmaster", oControlMaster },
1.136     djm       221:        { "hashknownhosts", oHashKnownHosts },
1.144     reyk      222:        { "tunnel", oTunnel },
                    223:        { "tunneldevice", oTunnelDevice },
                    224:        { "localcommand", oLocalCommand },
                    225:        { "permitlocalcommand", oPermitLocalCommand },
1.92      stevesk   226:        { NULL, oBadOption }
1.13      markus    227: };
                    228:
1.19      markus    229: /*
                    230:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    231:  * error.
                    232:  */
1.1       deraadt   233:
1.26      markus    234: void
1.135     djm       235: add_local_forward(Options *options, const Forward *newfwd)
1.1       deraadt   236: {
1.17      markus    237:        Forward *fwd;
                    238:        extern uid_t original_real_uid;
1.135     djm       239:        if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
1.64      millert   240:                fatal("Privileged ports can only be forwarded by root.");
1.17      markus    241:        if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    242:                fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
                    243:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       244:
                    245:        fwd->listen_host = (newfwd->listen_host == NULL) ?
                    246:            NULL : xstrdup(newfwd->listen_host);
                    247:        fwd->listen_port = newfwd->listen_port;
                    248:        fwd->connect_host = xstrdup(newfwd->connect_host);
                    249:        fwd->connect_port = newfwd->connect_port;
1.1       deraadt   250: }
                    251:
1.19      markus    252: /*
                    253:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    254:  * an error.
                    255:  */
1.1       deraadt   256:
1.26      markus    257: void
1.135     djm       258: add_remote_forward(Options *options, const Forward *newfwd)
1.1       deraadt   259: {
1.17      markus    260:        Forward *fwd;
                    261:        if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    262:                fatal("Too many remote forwards (max %d).",
1.93      deraadt   263:                    SSH_MAX_FORWARDS_PER_DIRECTION);
1.17      markus    264:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       265:
                    266:        fwd->listen_host = (newfwd->listen_host == NULL) ?
                    267:            NULL : xstrdup(newfwd->listen_host);
                    268:        fwd->listen_port = newfwd->listen_port;
                    269:        fwd->connect_host = xstrdup(newfwd->connect_host);
                    270:        fwd->connect_port = newfwd->connect_port;
1.1       deraadt   271: }
                    272:
1.90      stevesk   273: static void
                    274: clear_forwardings(Options *options)
                    275: {
                    276:        int i;
                    277:
1.135     djm       278:        for (i = 0; i < options->num_local_forwards; i++) {
1.138     dtucker   279:                if (options->local_forwards[i].listen_host != NULL)
                    280:                        xfree(options->local_forwards[i].listen_host);
1.135     djm       281:                xfree(options->local_forwards[i].connect_host);
                    282:        }
1.90      stevesk   283:        options->num_local_forwards = 0;
1.135     djm       284:        for (i = 0; i < options->num_remote_forwards; i++) {
1.138     dtucker   285:                if (options->remote_forwards[i].listen_host != NULL)
                    286:                        xfree(options->remote_forwards[i].listen_host);
1.135     djm       287:                xfree(options->remote_forwards[i].connect_host);
                    288:        }
1.90      stevesk   289:        options->num_remote_forwards = 0;
1.145     reyk      290:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   291: }
                    292:
1.19      markus    293: /*
1.70      stevesk   294:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    295:  */
1.1       deraadt   296:
1.26      markus    297: static OpCodes
1.17      markus    298: parse_token(const char *cp, const char *filename, int linenum)
1.1       deraadt   299: {
1.51      markus    300:        u_int i;
1.1       deraadt   301:
1.17      markus    302:        for (i = 0; keywords[i].name; i++)
1.20      markus    303:                if (strcasecmp(cp, keywords[i].name) == 0)
1.17      markus    304:                        return keywords[i].opcode;
                    305:
1.75      stevesk   306:        error("%s: line %d: Bad configuration option: %s",
                    307:            filename, linenum, cp);
1.17      markus    308:        return oBadOption;
1.1       deraadt   309: }
                    310:
1.19      markus    311: /*
                    312:  * Processes a single option line as used in the configuration files. This
                    313:  * only sets those values that have not already been set.
                    314:  */
1.102     markus    315: #define WHITESPACE " \t\r\n"
1.1       deraadt   316:
1.14      markus    317: int
                    318: process_config_line(Options *options, const char *host,
1.17      markus    319:                    char *line, const char *filename, int linenum,
                    320:                    int *activep)
1.1       deraadt   321: {
1.135     djm       322:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
1.146     djm       323:        int opcode, *intptr, value, value2, scale;
1.164     dtucker   324:        LogLevel *log_level_ptr;
1.146     djm       325:        long long orig, val64;
1.102     markus    326:        size_t len;
1.135     djm       327:        Forward fwd;
1.106     djm       328:
                    329:        /* Strip trailing whitespace */
1.139     deraadt   330:        for (len = strlen(line) - 1; len > 0; len--) {
1.106     djm       331:                if (strchr(WHITESPACE, line[len]) == NULL)
                    332:                        break;
                    333:                line[len] = '\0';
                    334:        }
1.1       deraadt   335:
1.42      provos    336:        s = line;
                    337:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       338:        if ((keyword = strdelim(&s)) == NULL)
                    339:                return 0;
1.42      provos    340:        /* Ignore leading whitespace. */
                    341:        if (*keyword == '\0')
1.43      markus    342:                keyword = strdelim(&s);
1.56      deraadt   343:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    344:                return 0;
                    345:
1.38      provos    346:        opcode = parse_token(keyword, filename, linenum);
1.17      markus    347:
                    348:        switch (opcode) {
                    349:        case oBadOption:
1.19      markus    350:                /* don't panic, but count bad options */
                    351:                return -1;
1.17      markus    352:                /* NOTREACHED */
1.111     djm       353:        case oConnectTimeout:
                    354:                intptr = &options->connection_timeout;
1.127     markus    355: parse_time:
1.111     djm       356:                arg = strdelim(&s);
                    357:                if (!arg || *arg == '\0')
                    358:                        fatal("%s line %d: missing time value.",
                    359:                            filename, linenum);
                    360:                if ((value = convtime(arg)) == -1)
                    361:                        fatal("%s line %d: invalid time value.",
                    362:                            filename, linenum);
1.160     dtucker   363:                if (*activep && *intptr == -1)
1.111     djm       364:                        *intptr = value;
                    365:                break;
                    366:
1.17      markus    367:        case oForwardAgent:
                    368:                intptr = &options->forward_agent;
                    369: parse_flag:
1.42      provos    370:                arg = strdelim(&s);
1.40      ho        371:                if (!arg || *arg == '\0')
1.17      markus    372:                        fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
                    373:                value = 0;      /* To avoid compiler warning... */
1.38      provos    374:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    375:                        value = 1;
1.38      provos    376:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    377:                        value = 0;
                    378:                else
                    379:                        fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
                    380:                if (*activep && *intptr == -1)
                    381:                        *intptr = value;
                    382:                break;
                    383:
                    384:        case oForwardX11:
                    385:                intptr = &options->forward_x11;
                    386:                goto parse_flag;
                    387:
1.123     markus    388:        case oForwardX11Trusted:
                    389:                intptr = &options->forward_x11_trusted;
                    390:                goto parse_flag;
                    391:
1.17      markus    392:        case oGatewayPorts:
                    393:                intptr = &options->gateway_ports;
                    394:                goto parse_flag;
                    395:
1.153     markus    396:        case oExitOnForwardFailure:
                    397:                intptr = &options->exit_on_forward_failure;
                    398:                goto parse_flag;
                    399:
1.17      markus    400:        case oUsePrivilegedPort:
                    401:                intptr = &options->use_privileged_port;
                    402:                goto parse_flag;
                    403:
                    404:        case oPasswordAuthentication:
                    405:                intptr = &options->password_authentication;
                    406:                goto parse_flag;
                    407:
1.48      markus    408:        case oKbdInteractiveAuthentication:
                    409:                intptr = &options->kbd_interactive_authentication;
                    410:                goto parse_flag;
                    411:
                    412:        case oKbdInteractiveDevices:
                    413:                charptr = &options->kbd_interactive_devices;
                    414:                goto parse_string;
                    415:
1.50      markus    416:        case oPubkeyAuthentication:
                    417:                intptr = &options->pubkey_authentication;
1.30      markus    418:                goto parse_flag;
                    419:
1.17      markus    420:        case oRSAAuthentication:
                    421:                intptr = &options->rsa_authentication;
                    422:                goto parse_flag;
                    423:
                    424:        case oRhostsRSAAuthentication:
                    425:                intptr = &options->rhosts_rsa_authentication;
                    426:                goto parse_flag;
                    427:
1.72      markus    428:        case oHostbasedAuthentication:
                    429:                intptr = &options->hostbased_authentication;
                    430:                goto parse_flag;
                    431:
1.59      markus    432:        case oChallengeResponseAuthentication:
1.78      markus    433:                intptr = &options->challenge_response_authentication;
1.17      markus    434:                goto parse_flag;
1.108     jakob     435:
1.118     markus    436:        case oGssAuthentication:
                    437:                intptr = &options->gss_authentication;
                    438:                goto parse_flag;
                    439:
                    440:        case oGssDelegateCreds:
                    441:                intptr = &options->gss_deleg_creds;
                    442:                goto parse_flag;
                    443:
1.17      markus    444:        case oBatchMode:
                    445:                intptr = &options->batch_mode;
                    446:                goto parse_flag;
                    447:
                    448:        case oCheckHostIP:
                    449:                intptr = &options->check_host_ip;
1.166   ! grunk     450:                arg = strdelim(&s);
        !           451:                if (!arg || *arg == '\0')
        !           452:                        fatal("%.200s line %d: Missing CheckHostIP argument.",
        !           453:                            filename, linenum);
        !           454:                value = 0;      /* To avoid compiler warning... */
        !           455:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
        !           456:                        value = SSHCTL_CHECKHOSTIP_YES;
        !           457:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
        !           458:                        value = SSHCTL_CHECKHOSTIP_NO;
        !           459:                else if (strcmp(arg, "fingerprint") == 0)
        !           460:                        value = SSHCTL_CHECKHOSTIP_FPR;
        !           461:                else
        !           462:                        fatal("%.200s line %d: Bad CheckHostIP argument.",
        !           463:                            filename, linenum);
        !           464:                if (*activep && *intptr == -1)
        !           465:                        *intptr = value;
        !           466:                break;
1.17      markus    467:
1.107     jakob     468:        case oVerifyHostKeyDNS:
                    469:                intptr = &options->verify_host_key_dns;
1.125     jakob     470:                goto parse_yesnoask;
1.107     jakob     471:
1.17      markus    472:        case oStrictHostKeyChecking:
                    473:                intptr = &options->strict_host_key_checking;
1.125     jakob     474: parse_yesnoask:
1.42      provos    475:                arg = strdelim(&s);
1.40      ho        476:                if (!arg || *arg == '\0')
1.60      stevesk   477:                        fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93      deraadt   478:                            filename, linenum);
1.17      markus    479:                value = 0;      /* To avoid compiler warning... */
1.38      provos    480:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17      markus    481:                        value = 1;
1.38      provos    482:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17      markus    483:                        value = 0;
1.38      provos    484:                else if (strcmp(arg, "ask") == 0)
1.17      markus    485:                        value = 2;
                    486:                else
                    487:                        fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
                    488:                if (*activep && *intptr == -1)
                    489:                        *intptr = value;
                    490:                break;
                    491:
                    492:        case oCompression:
                    493:                intptr = &options->compression;
                    494:                goto parse_flag;
                    495:
1.126     markus    496:        case oTCPKeepAlive:
                    497:                intptr = &options->tcp_keep_alive;
1.17      markus    498:                goto parse_flag;
                    499:
1.91      markus    500:        case oNoHostAuthenticationForLocalhost:
                    501:                intptr = &options->no_host_authentication_for_localhost;
                    502:                goto parse_flag;
                    503:
1.17      markus    504:        case oNumberOfPasswordPrompts:
                    505:                intptr = &options->number_of_password_prompts;
                    506:                goto parse_int;
                    507:
                    508:        case oCompressionLevel:
                    509:                intptr = &options->compression_level;
                    510:                goto parse_int;
                    511:
1.105     markus    512:        case oRekeyLimit:
                    513:                arg = strdelim(&s);
                    514:                if (!arg || *arg == '\0')
                    515:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    516:                if (arg[0] < '0' || arg[0] > '9')
                    517:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.146     djm       518:                orig = val64 = strtoll(arg, &endofnumber, 10);
1.105     markus    519:                if (arg == endofnumber)
                    520:                        fatal("%.200s line %d: Bad number.", filename, linenum);
                    521:                switch (toupper(*endofnumber)) {
1.146     djm       522:                case '\0':
                    523:                        scale = 1;
                    524:                        break;
1.105     markus    525:                case 'K':
1.146     djm       526:                        scale = 1<<10;
1.105     markus    527:                        break;
                    528:                case 'M':
1.146     djm       529:                        scale = 1<<20;
1.105     markus    530:                        break;
                    531:                case 'G':
1.146     djm       532:                        scale = 1<<30;
1.105     markus    533:                        break;
1.146     djm       534:                default:
                    535:                        fatal("%.200s line %d: Invalid RekeyLimit suffix",
                    536:                            filename, linenum);
1.105     markus    537:                }
1.146     djm       538:                val64 *= scale;
                    539:                /* detect integer wrap and too-large limits */
1.165     djm       540:                if ((val64 / scale) != orig || val64 > UINT_MAX)
1.146     djm       541:                        fatal("%.200s line %d: RekeyLimit too large",
                    542:                            filename, linenum);
                    543:                if (val64 < 16)
                    544:                        fatal("%.200s line %d: RekeyLimit too small",
                    545:                            filename, linenum);
1.165     djm       546:                if (*activep && options->rekey_limit == -1)
                    547:                        options->rekey_limit = (u_int32_t)val64;
1.105     markus    548:                break;
                    549:
1.17      markus    550:        case oIdentityFile:
1.42      provos    551:                arg = strdelim(&s);
1.40      ho        552:                if (!arg || *arg == '\0')
1.17      markus    553:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    554:                if (*activep) {
1.50      markus    555:                        intptr = &options->num_identity_files;
1.27      markus    556:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    557:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt   558:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.161     stevesk   559:                        charptr = &options->identity_files[*intptr];
1.38      provos    560:                        *charptr = xstrdup(arg);
1.27      markus    561:                        *intptr = *intptr + 1;
1.17      markus    562:                }
                    563:                break;
                    564:
1.34      markus    565:        case oXAuthLocation:
                    566:                charptr=&options->xauth_location;
                    567:                goto parse_string;
                    568:
1.17      markus    569:        case oUser:
                    570:                charptr = &options->user;
                    571: parse_string:
1.42      provos    572:                arg = strdelim(&s);
1.40      ho        573:                if (!arg || *arg == '\0')
1.17      markus    574:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    575:                if (*activep && *charptr == NULL)
1.38      provos    576:                        *charptr = xstrdup(arg);
1.17      markus    577:                break;
                    578:
                    579:        case oGlobalKnownHostsFile:
                    580:                charptr = &options->system_hostfile;
                    581:                goto parse_string;
                    582:
                    583:        case oUserKnownHostsFile:
                    584:                charptr = &options->user_hostfile;
                    585:                goto parse_string;
                    586:
1.27      markus    587:        case oGlobalKnownHostsFile2:
                    588:                charptr = &options->system_hostfile2;
                    589:                goto parse_string;
                    590:
                    591:        case oUserKnownHostsFile2:
                    592:                charptr = &options->user_hostfile2;
                    593:                goto parse_string;
                    594:
1.17      markus    595:        case oHostName:
                    596:                charptr = &options->hostname;
                    597:                goto parse_string;
                    598:
1.52      markus    599:        case oHostKeyAlias:
                    600:                charptr = &options->host_key_alias;
                    601:                goto parse_string;
                    602:
1.67      markus    603:        case oPreferredAuthentications:
                    604:                charptr = &options->preferred_authentications;
                    605:                goto parse_string;
                    606:
1.77      markus    607:        case oBindAddress:
                    608:                charptr = &options->bind_address;
                    609:                goto parse_string;
                    610:
1.85      jakob     611:        case oSmartcardDevice:
1.86      markus    612:                charptr = &options->smartcard_device;
                    613:                goto parse_string;
1.85      jakob     614:
1.17      markus    615:        case oProxyCommand:
1.144     reyk      616:                charptr = &options->proxy_command;
                    617: parse_command:
1.113     markus    618:                if (s == NULL)
                    619:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus    620:                len = strspn(s, WHITESPACE "=");
1.17      markus    621:                if (*activep && *charptr == NULL)
1.102     markus    622:                        *charptr = xstrdup(s + len);
1.17      markus    623:                return 0;
                    624:
                    625:        case oPort:
                    626:                intptr = &options->port;
                    627: parse_int:
1.42      provos    628:                arg = strdelim(&s);
1.40      ho        629:                if (!arg || *arg == '\0')
1.17      markus    630:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    631:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus    632:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus    633:
                    634:                /* Octal, decimal, or hex format? */
1.38      provos    635:                value = strtol(arg, &endofnumber, 0);
                    636:                if (arg == endofnumber)
1.21      markus    637:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus    638:                if (*activep && *intptr == -1)
                    639:                        *intptr = value;
                    640:                break;
                    641:
                    642:        case oConnectionAttempts:
                    643:                intptr = &options->connection_attempts;
                    644:                goto parse_int;
                    645:
                    646:        case oCipher:
                    647:                intptr = &options->cipher;
1.42      provos    648:                arg = strdelim(&s);
1.40      ho        649:                if (!arg || *arg == '\0')
1.32      markus    650:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    651:                value = cipher_number(arg);
1.17      markus    652:                if (value == -1)
                    653:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.93      deraadt   654:                            filename, linenum, arg ? arg : "<NONE>");
1.17      markus    655:                if (*activep && *intptr == -1)
                    656:                        *intptr = value;
                    657:                break;
                    658:
1.25      markus    659:        case oCiphers:
1.42      provos    660:                arg = strdelim(&s);
1.40      ho        661:                if (!arg || *arg == '\0')
1.32      markus    662:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    663:                if (!ciphers_valid(arg))
1.31      markus    664:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt   665:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    666:                if (*activep && options->ciphers == NULL)
1.38      provos    667:                        options->ciphers = xstrdup(arg);
1.25      markus    668:                break;
                    669:
1.62      markus    670:        case oMacs:
                    671:                arg = strdelim(&s);
                    672:                if (!arg || *arg == '\0')
                    673:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    674:                if (!mac_valid(arg))
                    675:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt   676:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus    677:                if (*activep && options->macs == NULL)
                    678:                        options->macs = xstrdup(arg);
                    679:                break;
                    680:
1.76      markus    681:        case oHostKeyAlgorithms:
                    682:                arg = strdelim(&s);
                    683:                if (!arg || *arg == '\0')
                    684:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    685:                if (!key_names_valid2(arg))
                    686:                        fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93      deraadt   687:                            filename, linenum, arg ? arg : "<NONE>");
1.76      markus    688:                if (*activep && options->hostkeyalgorithms == NULL)
                    689:                        options->hostkeyalgorithms = xstrdup(arg);
                    690:                break;
                    691:
1.25      markus    692:        case oProtocol:
                    693:                intptr = &options->protocol;
1.42      provos    694:                arg = strdelim(&s);
1.40      ho        695:                if (!arg || *arg == '\0')
1.32      markus    696:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    697:                value = proto_spec(arg);
1.25      markus    698:                if (value == SSH_PROTO_UNKNOWN)
                    699:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93      deraadt   700:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus    701:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                    702:                        *intptr = value;
                    703:                break;
                    704:
1.17      markus    705:        case oLogLevel:
1.164     dtucker   706:                log_level_ptr = &options->log_level;
1.42      provos    707:                arg = strdelim(&s);
1.38      provos    708:                value = log_level_number(arg);
1.95      markus    709:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert   710:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt   711:                            filename, linenum, arg ? arg : "<NONE>");
1.164     dtucker   712:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                    713:                        *log_level_ptr = (LogLevel) value;
1.17      markus    714:                break;
                    715:
1.88      stevesk   716:        case oLocalForward:
1.17      markus    717:        case oRemoteForward:
1.42      provos    718:                arg = strdelim(&s);
1.135     djm       719:                if (arg == NULL || *arg == '\0')
1.88      stevesk   720:                        fatal("%.200s line %d: Missing port argument.",
                    721:                            filename, linenum);
1.135     djm       722:                arg2 = strdelim(&s);
                    723:                if (arg2 == NULL || *arg2 == '\0')
                    724:                        fatal("%.200s line %d: Missing target argument.",
1.88      stevesk   725:                            filename, linenum);
1.135     djm       726:
                    727:                /* construct a string for parse_forward */
                    728:                snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
                    729:
                    730:                if (parse_forward(&fwd, fwdarg) == 0)
1.88      stevesk   731:                        fatal("%.200s line %d: Bad forwarding specification.",
                    732:                            filename, linenum);
1.135     djm       733:
1.88      stevesk   734:                if (*activep) {
                    735:                        if (opcode == oLocalForward)
1.135     djm       736:                                add_local_forward(options, &fwd);
1.88      stevesk   737:                        else if (opcode == oRemoteForward)
1.135     djm       738:                                add_remote_forward(options, &fwd);
1.88      stevesk   739:                }
1.17      markus    740:                break;
1.71      markus    741:
                    742:        case oDynamicForward:
                    743:                arg = strdelim(&s);
                    744:                if (!arg || *arg == '\0')
                    745:                        fatal("%.200s line %d: Missing port argument.",
                    746:                            filename, linenum);
1.135     djm       747:                memset(&fwd, '\0', sizeof(fwd));
                    748:                fwd.connect_host = "socks";
                    749:                fwd.listen_host = hpdelim(&arg);
                    750:                if (fwd.listen_host == NULL ||
                    751:                    strlen(fwd.listen_host) >= NI_MAXHOST)
                    752:                        fatal("%.200s line %d: Bad forwarding specification.",
                    753:                            filename, linenum);
                    754:                if (arg) {
                    755:                        fwd.listen_port = a2port(arg);
                    756:                        fwd.listen_host = cleanhostname(fwd.listen_host);
                    757:                } else {
                    758:                        fwd.listen_port = a2port(fwd.listen_host);
1.143     djm       759:                        fwd.listen_host = NULL;
1.135     djm       760:                }
                    761:                if (fwd.listen_port == 0)
1.71      markus    762:                        fatal("%.200s line %d: Badly formatted port number.",
                    763:                            filename, linenum);
1.87      markus    764:                if (*activep)
1.135     djm       765:                        add_local_forward(options, &fwd);
1.72      markus    766:                break;
1.17      markus    767:
1.90      stevesk   768:        case oClearAllForwardings:
                    769:                intptr = &options->clear_forwardings;
                    770:                goto parse_flag;
                    771:
1.17      markus    772:        case oHost:
                    773:                *activep = 0;
1.42      provos    774:                while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38      provos    775:                        if (match_pattern(host, arg)) {
                    776:                                debug("Applying options for %.100s", arg);
1.17      markus    777:                                *activep = 1;
                    778:                                break;
                    779:                        }
1.42      provos    780:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus    781:                return 0;
                    782:
                    783:        case oEscapeChar:
                    784:                intptr = &options->escape_char;
1.42      provos    785:                arg = strdelim(&s);
1.40      ho        786:                if (!arg || *arg == '\0')
1.17      markus    787:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    788:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus    789:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                    790:                        value = (u_char) arg[1] & 31;
1.38      provos    791:                else if (strlen(arg) == 1)
1.51      markus    792:                        value = (u_char) arg[0];
1.38      provos    793:                else if (strcmp(arg, "none") == 0)
1.79      stevesk   794:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus    795:                else {
                    796:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt   797:                            filename, linenum);
1.17      markus    798:                        /* NOTREACHED */
                    799:                        value = 0;      /* Avoid compiler warning. */
                    800:                }
                    801:                if (*activep && *intptr == -1)
                    802:                        *intptr = value;
1.112     djm       803:                break;
                    804:
                    805:        case oAddressFamily:
                    806:                arg = strdelim(&s);
1.140     markus    807:                if (!arg || *arg == '\0')
                    808:                        fatal("%s line %d: missing address family.",
                    809:                            filename, linenum);
1.114     djm       810:                intptr = &options->address_family;
1.112     djm       811:                if (strcasecmp(arg, "inet") == 0)
1.114     djm       812:                        value = AF_INET;
1.112     djm       813:                else if (strcasecmp(arg, "inet6") == 0)
1.114     djm       814:                        value = AF_INET6;
1.112     djm       815:                else if (strcasecmp(arg, "any") == 0)
1.114     djm       816:                        value = AF_UNSPEC;
1.112     djm       817:                else
                    818:                        fatal("Unsupported AddressFamily \"%s\"", arg);
1.114     djm       819:                if (*activep && *intptr == -1)
                    820:                        *intptr = value;
1.17      markus    821:                break;
                    822:
1.101     markus    823:        case oEnableSSHKeysign:
                    824:                intptr = &options->enable_ssh_keysign;
                    825:                goto parse_flag;
                    826:
1.128     markus    827:        case oIdentitiesOnly:
                    828:                intptr = &options->identities_only;
                    829:                goto parse_flag;
                    830:
1.127     markus    831:        case oServerAliveInterval:
                    832:                intptr = &options->server_alive_interval;
                    833:                goto parse_time;
                    834:
                    835:        case oServerAliveCountMax:
                    836:                intptr = &options->server_alive_count_max;
                    837:                goto parse_int;
                    838:
1.130     djm       839:        case oSendEnv:
                    840:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                    841:                        if (strchr(arg, '=') != NULL)
                    842:                                fatal("%s line %d: Invalid environment name.",
                    843:                                    filename, linenum);
1.137     djm       844:                        if (!*activep)
                    845:                                continue;
1.130     djm       846:                        if (options->num_send_env >= MAX_SEND_ENV)
                    847:                                fatal("%s line %d: too many send env.",
                    848:                                    filename, linenum);
                    849:                        options->send_env[options->num_send_env++] =
                    850:                            xstrdup(arg);
                    851:                }
                    852:                break;
                    853:
1.132     djm       854:        case oControlPath:
                    855:                charptr = &options->control_path;
                    856:                goto parse_string;
                    857:
                    858:        case oControlMaster:
                    859:                intptr = &options->control_master;
1.141     djm       860:                arg = strdelim(&s);
                    861:                if (!arg || *arg == '\0')
                    862:                        fatal("%.200s line %d: Missing ControlMaster argument.",
                    863:                            filename, linenum);
                    864:                value = 0;      /* To avoid compiler warning... */
                    865:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                    866:                        value = SSHCTL_MASTER_YES;
                    867:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                    868:                        value = SSHCTL_MASTER_NO;
                    869:                else if (strcmp(arg, "auto") == 0)
                    870:                        value = SSHCTL_MASTER_AUTO;
                    871:                else if (strcmp(arg, "ask") == 0)
                    872:                        value = SSHCTL_MASTER_ASK;
                    873:                else if (strcmp(arg, "autoask") == 0)
                    874:                        value = SSHCTL_MASTER_AUTO_ASK;
                    875:                else
                    876:                        fatal("%.200s line %d: Bad ControlMaster argument.",
                    877:                            filename, linenum);
                    878:                if (*activep && *intptr == -1)
                    879:                        *intptr = value;
                    880:                break;
1.132     djm       881:
1.136     djm       882:        case oHashKnownHosts:
                    883:                intptr = &options->hash_known_hosts;
                    884:                goto parse_flag;
                    885:
1.144     reyk      886:        case oTunnel:
                    887:                intptr = &options->tun_open;
1.145     reyk      888:                arg = strdelim(&s);
                    889:                if (!arg || *arg == '\0')
                    890:                        fatal("%s line %d: Missing yes/point-to-point/"
                    891:                            "ethernet/no argument.", filename, linenum);
                    892:                value = 0;      /* silence compiler */
                    893:                if (strcasecmp(arg, "ethernet") == 0)
                    894:                        value = SSH_TUNMODE_ETHERNET;
                    895:                else if (strcasecmp(arg, "point-to-point") == 0)
                    896:                        value = SSH_TUNMODE_POINTOPOINT;
                    897:                else if (strcasecmp(arg, "yes") == 0)
                    898:                        value = SSH_TUNMODE_DEFAULT;
                    899:                else if (strcasecmp(arg, "no") == 0)
                    900:                        value = SSH_TUNMODE_NO;
                    901:                else
                    902:                        fatal("%s line %d: Bad yes/point-to-point/ethernet/"
                    903:                            "no argument: %s", filename, linenum, arg);
                    904:                if (*activep)
                    905:                        *intptr = value;
                    906:                break;
1.144     reyk      907:
                    908:        case oTunnelDevice:
                    909:                arg = strdelim(&s);
                    910:                if (!arg || *arg == '\0')
                    911:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    912:                value = a2tun(arg, &value2);
1.145     reyk      913:                if (value == SSH_TUNID_ERR)
1.144     reyk      914:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                    915:                if (*activep) {
                    916:                        options->tun_local = value;
                    917:                        options->tun_remote = value2;
                    918:                }
                    919:                break;
                    920:
                    921:        case oLocalCommand:
                    922:                charptr = &options->local_command;
                    923:                goto parse_command;
                    924:
                    925:        case oPermitLocalCommand:
                    926:                intptr = &options->permit_local_command;
                    927:                goto parse_flag;
                    928:
1.96      markus    929:        case oDeprecated:
1.98      markus    930:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus    931:                    filename, linenum, keyword);
1.98      markus    932:                return 0;
1.96      markus    933:
1.110     jakob     934:        case oUnsupported:
                    935:                error("%s line %d: Unsupported option \"%s\"",
                    936:                    filename, linenum, keyword);
                    937:                return 0;
                    938:
1.17      markus    939:        default:
                    940:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                    941:        }
                    942:
                    943:        /* Check that there is no garbage at end of line. */
1.57      djm       944:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho        945:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm       946:                    filename, linenum, arg);
1.39      ho        947:        }
1.17      markus    948:        return 0;
1.1       deraadt   949: }
                    950:
                    951:
1.19      markus    952: /*
                    953:  * Reads the config file and modifies the options accordingly.  Options
                    954:  * should already be initialized before this call.  This never returns if
1.89      stevesk   955:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus    956:  */
1.1       deraadt   957:
1.89      stevesk   958: int
1.134     deraadt   959: read_config_file(const char *filename, const char *host, Options *options,
1.129     djm       960:     int checkperm)
1.1       deraadt   961: {
1.17      markus    962:        FILE *f;
                    963:        char line[1024];
                    964:        int active, linenum;
                    965:        int bad_options = 0;
                    966:
                    967:        /* Open the file. */
1.129     djm       968:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk   969:                return 0;
1.129     djm       970:
                    971:        if (checkperm) {
                    972:                struct stat sb;
1.134     deraadt   973:
1.131     dtucker   974:                if (fstat(fileno(f), &sb) == -1)
1.129     djm       975:                        fatal("fstat %s: %s", filename, strerror(errno));
                    976:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker   977:                    (sb.st_mode & 022) != 0))
1.129     djm       978:                        fatal("Bad owner or permissions on %s", filename);
                    979:        }
1.17      markus    980:
                    981:        debug("Reading configuration data %.200s", filename);
                    982:
1.19      markus    983:        /*
                    984:         * Mark that we are now processing the options.  This flag is turned
                    985:         * on/off by Host specifications.
                    986:         */
1.17      markus    987:        active = 1;
                    988:        linenum = 0;
                    989:        while (fgets(line, sizeof(line), f)) {
                    990:                /* Update line number counter. */
                    991:                linenum++;
                    992:                if (process_config_line(options, host, line, filename, linenum, &active) != 0)
                    993:                        bad_options++;
                    994:        }
                    995:        fclose(f);
                    996:        if (bad_options > 0)
1.64      millert   997:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt   998:                    filename, bad_options);
1.89      stevesk   999:        return 1;
1.1       deraadt  1000: }
                   1001:
1.19      markus   1002: /*
                   1003:  * Initializes options to special values that indicate that they have not yet
                   1004:  * been set.  Read_config_file will only set options with this value. Options
                   1005:  * are processed in the following order: command line, user config file,
                   1006:  * system config file.  Last, fill_default_options is called.
                   1007:  */
1.1       deraadt  1008:
1.26      markus   1009: void
1.17      markus   1010: initialize_options(Options * options)
1.1       deraadt  1011: {
1.17      markus   1012:        memset(options, 'X', sizeof(*options));
                   1013:        options->forward_agent = -1;
                   1014:        options->forward_x11 = -1;
1.123     markus   1015:        options->forward_x11_trusted = -1;
1.153     markus   1016:        options->exit_on_forward_failure = -1;
1.34      markus   1017:        options->xauth_location = NULL;
1.17      markus   1018:        options->gateway_ports = -1;
                   1019:        options->use_privileged_port = -1;
                   1020:        options->rsa_authentication = -1;
1.50      markus   1021:        options->pubkey_authentication = -1;
1.78      markus   1022:        options->challenge_response_authentication = -1;
1.118     markus   1023:        options->gss_authentication = -1;
                   1024:        options->gss_deleg_creds = -1;
1.17      markus   1025:        options->password_authentication = -1;
1.48      markus   1026:        options->kbd_interactive_authentication = -1;
                   1027:        options->kbd_interactive_devices = NULL;
1.17      markus   1028:        options->rhosts_rsa_authentication = -1;
1.72      markus   1029:        options->hostbased_authentication = -1;
1.17      markus   1030:        options->batch_mode = -1;
                   1031:        options->check_host_ip = -1;
                   1032:        options->strict_host_key_checking = -1;
                   1033:        options->compression = -1;
1.126     markus   1034:        options->tcp_keep_alive = -1;
1.17      markus   1035:        options->compression_level = -1;
                   1036:        options->port = -1;
1.114     djm      1037:        options->address_family = -1;
1.17      markus   1038:        options->connection_attempts = -1;
1.111     djm      1039:        options->connection_timeout = -1;
1.17      markus   1040:        options->number_of_password_prompts = -1;
                   1041:        options->cipher = -1;
1.25      markus   1042:        options->ciphers = NULL;
1.62      markus   1043:        options->macs = NULL;
1.76      markus   1044:        options->hostkeyalgorithms = NULL;
1.25      markus   1045:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus   1046:        options->num_identity_files = 0;
                   1047:        options->hostname = NULL;
1.52      markus   1048:        options->host_key_alias = NULL;
1.17      markus   1049:        options->proxy_command = NULL;
                   1050:        options->user = NULL;
                   1051:        options->escape_char = -1;
                   1052:        options->system_hostfile = NULL;
                   1053:        options->user_hostfile = NULL;
1.27      markus   1054:        options->system_hostfile2 = NULL;
                   1055:        options->user_hostfile2 = NULL;
1.17      markus   1056:        options->num_local_forwards = 0;
                   1057:        options->num_remote_forwards = 0;
1.90      stevesk  1058:        options->clear_forwardings = -1;
1.95      markus   1059:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1060:        options->preferred_authentications = NULL;
1.77      markus   1061:        options->bind_address = NULL;
1.86      markus   1062:        options->smartcard_device = NULL;
1.101     markus   1063:        options->enable_ssh_keysign = - 1;
1.91      markus   1064:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1065:        options->identities_only = - 1;
1.105     markus   1066:        options->rekey_limit = - 1;
1.107     jakob    1067:        options->verify_host_key_dns = -1;
1.127     markus   1068:        options->server_alive_interval = -1;
                   1069:        options->server_alive_count_max = -1;
1.130     djm      1070:        options->num_send_env = 0;
1.132     djm      1071:        options->control_path = NULL;
                   1072:        options->control_master = -1;
1.136     djm      1073:        options->hash_known_hosts = -1;
1.144     reyk     1074:        options->tun_open = -1;
                   1075:        options->tun_local = -1;
                   1076:        options->tun_remote = -1;
                   1077:        options->local_command = NULL;
                   1078:        options->permit_local_command = -1;
1.1       deraadt  1079: }
                   1080:
1.19      markus   1081: /*
                   1082:  * Called after processing other sources of option data, this fills those
                   1083:  * options for which no value has been specified with their default values.
                   1084:  */
1.1       deraadt  1085:
1.26      markus   1086: void
1.17      markus   1087: fill_default_options(Options * options)
1.1       deraadt  1088: {
1.61      deraadt  1089:        int len;
                   1090:
1.17      markus   1091:        if (options->forward_agent == -1)
1.33      markus   1092:                options->forward_agent = 0;
1.17      markus   1093:        if (options->forward_x11 == -1)
1.23      markus   1094:                options->forward_x11 = 0;
1.123     markus   1095:        if (options->forward_x11_trusted == -1)
                   1096:                options->forward_x11_trusted = 0;
1.153     markus   1097:        if (options->exit_on_forward_failure == -1)
                   1098:                options->exit_on_forward_failure = 0;
1.34      markus   1099:        if (options->xauth_location == NULL)
1.80      markus   1100:                options->xauth_location = _PATH_XAUTH;
1.17      markus   1101:        if (options->gateway_ports == -1)
                   1102:                options->gateway_ports = 0;
                   1103:        if (options->use_privileged_port == -1)
1.65      markus   1104:                options->use_privileged_port = 0;
1.17      markus   1105:        if (options->rsa_authentication == -1)
                   1106:                options->rsa_authentication = 1;
1.50      markus   1107:        if (options->pubkey_authentication == -1)
                   1108:                options->pubkey_authentication = 1;
1.78      markus   1109:        if (options->challenge_response_authentication == -1)
1.83      markus   1110:                options->challenge_response_authentication = 1;
1.118     markus   1111:        if (options->gss_authentication == -1)
1.122     markus   1112:                options->gss_authentication = 0;
1.118     markus   1113:        if (options->gss_deleg_creds == -1)
                   1114:                options->gss_deleg_creds = 0;
1.17      markus   1115:        if (options->password_authentication == -1)
                   1116:                options->password_authentication = 1;
1.48      markus   1117:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1118:                options->kbd_interactive_authentication = 1;
1.17      markus   1119:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk  1120:                options->rhosts_rsa_authentication = 0;
1.72      markus   1121:        if (options->hostbased_authentication == -1)
                   1122:                options->hostbased_authentication = 0;
1.17      markus   1123:        if (options->batch_mode == -1)
                   1124:                options->batch_mode = 0;
                   1125:        if (options->check_host_ip == -1)
                   1126:                options->check_host_ip = 1;
                   1127:        if (options->strict_host_key_checking == -1)
                   1128:                options->strict_host_key_checking = 2;  /* 2 is default */
                   1129:        if (options->compression == -1)
                   1130:                options->compression = 0;
1.126     markus   1131:        if (options->tcp_keep_alive == -1)
                   1132:                options->tcp_keep_alive = 1;
1.17      markus   1133:        if (options->compression_level == -1)
                   1134:                options->compression_level = 6;
                   1135:        if (options->port == -1)
                   1136:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1137:        if (options->address_family == -1)
                   1138:                options->address_family = AF_UNSPEC;
1.17      markus   1139:        if (options->connection_attempts == -1)
1.84      markus   1140:                options->connection_attempts = 1;
1.17      markus   1141:        if (options->number_of_password_prompts == -1)
                   1142:                options->number_of_password_prompts = 3;
                   1143:        /* Selected in ssh_login(). */
                   1144:        if (options->cipher == -1)
                   1145:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus   1146:        /* options->ciphers, default set in myproposals.h */
1.62      markus   1147:        /* options->macs, default set in myproposals.h */
1.76      markus   1148:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus   1149:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.69      markus   1150:                options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17      markus   1151:        if (options->num_identity_files == 0) {
1.50      markus   1152:                if (options->protocol & SSH_PROTO_1) {
1.61      deraadt  1153:                        len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50      markus   1154:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1155:                            xmalloc(len);
                   1156:                        snprintf(options->identity_files[options->num_identity_files++],
                   1157:                            len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50      markus   1158:                }
                   1159:                if (options->protocol & SSH_PROTO_2) {
1.63      deraadt  1160:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
                   1161:                        options->identity_files[options->num_identity_files] =
                   1162:                            xmalloc(len);
                   1163:                        snprintf(options->identity_files[options->num_identity_files++],
                   1164:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
                   1165:
1.61      deraadt  1166:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50      markus   1167:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1168:                            xmalloc(len);
                   1169:                        snprintf(options->identity_files[options->num_identity_files++],
                   1170:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50      markus   1171:                }
1.27      markus   1172:        }
1.17      markus   1173:        if (options->escape_char == -1)
                   1174:                options->escape_char = '~';
                   1175:        if (options->system_hostfile == NULL)
1.55      markus   1176:                options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17      markus   1177:        if (options->user_hostfile == NULL)
1.55      markus   1178:                options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27      markus   1179:        if (options->system_hostfile2 == NULL)
1.55      markus   1180:                options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27      markus   1181:        if (options->user_hostfile2 == NULL)
1.55      markus   1182:                options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95      markus   1183:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1184:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk  1185:        if (options->clear_forwardings == 1)
                   1186:                clear_forwardings(options);
1.91      markus   1187:        if (options->no_host_authentication_for_localhost == - 1)
                   1188:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1189:        if (options->identities_only == -1)
                   1190:                options->identities_only = 0;
1.101     markus   1191:        if (options->enable_ssh_keysign == -1)
                   1192:                options->enable_ssh_keysign = 0;
1.105     markus   1193:        if (options->rekey_limit == -1)
                   1194:                options->rekey_limit = 0;
1.107     jakob    1195:        if (options->verify_host_key_dns == -1)
                   1196:                options->verify_host_key_dns = 0;
1.127     markus   1197:        if (options->server_alive_interval == -1)
                   1198:                options->server_alive_interval = 0;
                   1199:        if (options->server_alive_count_max == -1)
                   1200:                options->server_alive_count_max = 3;
1.132     djm      1201:        if (options->control_master == -1)
                   1202:                options->control_master = 0;
1.136     djm      1203:        if (options->hash_known_hosts == -1)
                   1204:                options->hash_known_hosts = 0;
1.144     reyk     1205:        if (options->tun_open == -1)
1.145     reyk     1206:                options->tun_open = SSH_TUNMODE_NO;
                   1207:        if (options->tun_local == -1)
                   1208:                options->tun_local = SSH_TUNID_ANY;
                   1209:        if (options->tun_remote == -1)
                   1210:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1211:        if (options->permit_local_command == -1)
                   1212:                options->permit_local_command = 0;
                   1213:        /* options->local_command should not be set by default */
1.17      markus   1214:        /* options->proxy_command should not be set by default */
                   1215:        /* options->user will be set in the main program if appropriate */
                   1216:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   1217:        /* options->host_key_alias should not be set by default */
1.67      markus   1218:        /* options->preferred_authentications will be set in ssh */
1.135     djm      1219: }
                   1220:
                   1221: /*
                   1222:  * parse_forward
                   1223:  * parses a string containing a port forwarding specification of the form:
                   1224:  *     [listenhost:]listenport:connecthost:connectport
                   1225:  * returns number of arguments parsed or zero on error
                   1226:  */
                   1227: int
                   1228: parse_forward(Forward *fwd, const char *fwdspec)
                   1229: {
                   1230:        int i;
                   1231:        char *p, *cp, *fwdarg[4];
                   1232:
                   1233:        memset(fwd, '\0', sizeof(*fwd));
                   1234:
                   1235:        cp = p = xstrdup(fwdspec);
                   1236:
                   1237:        /* skip leading spaces */
1.162     tedu     1238:        while (isspace(*cp))
1.135     djm      1239:                cp++;
                   1240:
                   1241:        for (i = 0; i < 4; ++i)
                   1242:                if ((fwdarg[i] = hpdelim(&cp)) == NULL)
                   1243:                        break;
                   1244:
                   1245:        /* Check for trailing garbage in 4-arg case*/
                   1246:        if (cp != NULL)
                   1247:                i = 0;  /* failure */
                   1248:
                   1249:        switch (i) {
                   1250:        case 3:
                   1251:                fwd->listen_host = NULL;
                   1252:                fwd->listen_port = a2port(fwdarg[0]);
                   1253:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
                   1254:                fwd->connect_port = a2port(fwdarg[2]);
                   1255:                break;
                   1256:
                   1257:        case 4:
                   1258:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1259:                fwd->listen_port = a2port(fwdarg[1]);
                   1260:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
                   1261:                fwd->connect_port = a2port(fwdarg[3]);
                   1262:                break;
                   1263:        default:
                   1264:                i = 0; /* failure */
                   1265:        }
                   1266:
                   1267:        xfree(p);
                   1268:
1.163     markus   1269:        if (fwd->listen_port == 0 || fwd->connect_port == 0)
1.135     djm      1270:                goto fail_free;
                   1271:
                   1272:        if (fwd->connect_host != NULL &&
                   1273:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   1274:                goto fail_free;
                   1275:
                   1276:        return (i);
                   1277:
                   1278:  fail_free:
                   1279:        if (fwd->connect_host != NULL)
                   1280:                xfree(fwd->connect_host);
                   1281:        if (fwd->listen_host != NULL)
                   1282:                xfree(fwd->listen_host);
                   1283:        return (0);
1.1       deraadt  1284: }