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

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