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

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