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

1.191   ! djm         1: /* $OpenBSD: readconf.c,v 1.190 2010/11/13 23:27:50 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.191   ! djm       353:        int negated, 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.191   ! djm       792:                arg2 = NULL;
        !           793:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
        !           794:                        negated = *arg == '!';
        !           795:                        if (negated)
        !           796:                                arg++;
1.38      provos    797:                        if (match_pattern(host, arg)) {
1.191   ! djm       798:                                if (negated) {
        !           799:                                        debug("%.200s line %d: Skipping Host "
        !           800:                                            "block because of negated match "
        !           801:                                            "for %.100s", filename, linenum,
        !           802:                                            arg);
        !           803:                                        *activep = 0;
        !           804:                                        break;
        !           805:                                }
        !           806:                                if (!*activep)
        !           807:                                        arg2 = arg; /* logged below */
1.17      markus    808:                                *activep = 1;
                    809:                        }
1.191   ! djm       810:                }
        !           811:                if (*activep)
        !           812:                        debug("%.200s line %d: Applying options for %.100s",
        !           813:                            filename, linenum, arg2);
1.42      provos    814:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus    815:                return 0;
                    816:
                    817:        case oEscapeChar:
                    818:                intptr = &options->escape_char;
1.42      provos    819:                arg = strdelim(&s);
1.40      ho        820:                if (!arg || *arg == '\0')
1.17      markus    821:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos    822:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus    823:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                    824:                        value = (u_char) arg[1] & 31;
1.38      provos    825:                else if (strlen(arg) == 1)
1.51      markus    826:                        value = (u_char) arg[0];
1.38      provos    827:                else if (strcmp(arg, "none") == 0)
1.79      stevesk   828:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus    829:                else {
                    830:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt   831:                            filename, linenum);
1.17      markus    832:                        /* NOTREACHED */
                    833:                        value = 0;      /* Avoid compiler warning. */
                    834:                }
                    835:                if (*activep && *intptr == -1)
                    836:                        *intptr = value;
1.112     djm       837:                break;
                    838:
                    839:        case oAddressFamily:
                    840:                arg = strdelim(&s);
1.140     markus    841:                if (!arg || *arg == '\0')
                    842:                        fatal("%s line %d: missing address family.",
                    843:                            filename, linenum);
1.114     djm       844:                intptr = &options->address_family;
1.112     djm       845:                if (strcasecmp(arg, "inet") == 0)
1.114     djm       846:                        value = AF_INET;
1.112     djm       847:                else if (strcasecmp(arg, "inet6") == 0)
1.114     djm       848:                        value = AF_INET6;
1.112     djm       849:                else if (strcasecmp(arg, "any") == 0)
1.114     djm       850:                        value = AF_UNSPEC;
1.112     djm       851:                else
                    852:                        fatal("Unsupported AddressFamily \"%s\"", arg);
1.114     djm       853:                if (*activep && *intptr == -1)
                    854:                        *intptr = value;
1.17      markus    855:                break;
                    856:
1.101     markus    857:        case oEnableSSHKeysign:
                    858:                intptr = &options->enable_ssh_keysign;
                    859:                goto parse_flag;
                    860:
1.128     markus    861:        case oIdentitiesOnly:
                    862:                intptr = &options->identities_only;
                    863:                goto parse_flag;
                    864:
1.127     markus    865:        case oServerAliveInterval:
                    866:                intptr = &options->server_alive_interval;
                    867:                goto parse_time;
                    868:
                    869:        case oServerAliveCountMax:
                    870:                intptr = &options->server_alive_count_max;
                    871:                goto parse_int;
                    872:
1.130     djm       873:        case oSendEnv:
                    874:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                    875:                        if (strchr(arg, '=') != NULL)
                    876:                                fatal("%s line %d: Invalid environment name.",
                    877:                                    filename, linenum);
1.137     djm       878:                        if (!*activep)
                    879:                                continue;
1.130     djm       880:                        if (options->num_send_env >= MAX_SEND_ENV)
                    881:                                fatal("%s line %d: too many send env.",
                    882:                                    filename, linenum);
                    883:                        options->send_env[options->num_send_env++] =
                    884:                            xstrdup(arg);
                    885:                }
                    886:                break;
                    887:
1.132     djm       888:        case oControlPath:
                    889:                charptr = &options->control_path;
                    890:                goto parse_string;
                    891:
                    892:        case oControlMaster:
                    893:                intptr = &options->control_master;
1.141     djm       894:                arg = strdelim(&s);
                    895:                if (!arg || *arg == '\0')
                    896:                        fatal("%.200s line %d: Missing ControlMaster argument.",
                    897:                            filename, linenum);
                    898:                value = 0;      /* To avoid compiler warning... */
                    899:                if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                    900:                        value = SSHCTL_MASTER_YES;
                    901:                else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                    902:                        value = SSHCTL_MASTER_NO;
                    903:                else if (strcmp(arg, "auto") == 0)
                    904:                        value = SSHCTL_MASTER_AUTO;
                    905:                else if (strcmp(arg, "ask") == 0)
                    906:                        value = SSHCTL_MASTER_ASK;
                    907:                else if (strcmp(arg, "autoask") == 0)
                    908:                        value = SSHCTL_MASTER_AUTO_ASK;
                    909:                else
                    910:                        fatal("%.200s line %d: Bad ControlMaster argument.",
                    911:                            filename, linenum);
                    912:                if (*activep && *intptr == -1)
                    913:                        *intptr = value;
                    914:                break;
1.132     djm       915:
1.187     djm       916:        case oControlPersist:
                    917:                /* no/false/yes/true, or a time spec */
                    918:                intptr = &options->control_persist;
                    919:                arg = strdelim(&s);
                    920:                if (!arg || *arg == '\0')
                    921:                        fatal("%.200s line %d: Missing ControlPersist"
                    922:                            " argument.", filename, linenum);
                    923:                value = 0;
                    924:                value2 = 0;     /* timeout */
                    925:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                    926:                        value = 0;
                    927:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                    928:                        value = 1;
                    929:                else if ((value2 = convtime(arg)) >= 0)
                    930:                        value = 1;
                    931:                else
                    932:                        fatal("%.200s line %d: Bad ControlPersist argument.",
                    933:                            filename, linenum);
                    934:                if (*activep && *intptr == -1) {
                    935:                        *intptr = value;
                    936:                        options->control_persist_timeout = value2;
                    937:                }
                    938:                break;
                    939:
1.136     djm       940:        case oHashKnownHosts:
                    941:                intptr = &options->hash_known_hosts;
                    942:                goto parse_flag;
                    943:
1.144     reyk      944:        case oTunnel:
                    945:                intptr = &options->tun_open;
1.145     reyk      946:                arg = strdelim(&s);
                    947:                if (!arg || *arg == '\0')
                    948:                        fatal("%s line %d: Missing yes/point-to-point/"
                    949:                            "ethernet/no argument.", filename, linenum);
                    950:                value = 0;      /* silence compiler */
                    951:                if (strcasecmp(arg, "ethernet") == 0)
                    952:                        value = SSH_TUNMODE_ETHERNET;
                    953:                else if (strcasecmp(arg, "point-to-point") == 0)
                    954:                        value = SSH_TUNMODE_POINTOPOINT;
                    955:                else if (strcasecmp(arg, "yes") == 0)
                    956:                        value = SSH_TUNMODE_DEFAULT;
                    957:                else if (strcasecmp(arg, "no") == 0)
                    958:                        value = SSH_TUNMODE_NO;
                    959:                else
                    960:                        fatal("%s line %d: Bad yes/point-to-point/ethernet/"
                    961:                            "no argument: %s", filename, linenum, arg);
                    962:                if (*activep)
                    963:                        *intptr = value;
                    964:                break;
1.144     reyk      965:
                    966:        case oTunnelDevice:
                    967:                arg = strdelim(&s);
                    968:                if (!arg || *arg == '\0')
                    969:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    970:                value = a2tun(arg, &value2);
1.145     reyk      971:                if (value == SSH_TUNID_ERR)
1.144     reyk      972:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                    973:                if (*activep) {
                    974:                        options->tun_local = value;
                    975:                        options->tun_remote = value2;
                    976:                }
                    977:                break;
                    978:
                    979:        case oLocalCommand:
                    980:                charptr = &options->local_command;
                    981:                goto parse_command;
                    982:
                    983:        case oPermitLocalCommand:
                    984:                intptr = &options->permit_local_command;
                    985:                goto parse_flag;
                    986:
1.167     grunk     987:        case oVisualHostKey:
                    988:                intptr = &options->visual_host_key;
                    989:                goto parse_flag;
                    990:
1.190     djm       991:        case oIPQoS:
                    992:                arg = strdelim(&s);
                    993:                if ((value = parse_ipqos(arg)) == -1)
                    994:                        fatal("%s line %d: Bad IPQoS value: %s",
                    995:                            filename, linenum, arg);
                    996:                arg = strdelim(&s);
                    997:                if (arg == NULL)
                    998:                        value2 = value;
                    999:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1000:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1001:                            filename, linenum, arg);
                   1002:                if (*activep) {
                   1003:                        options->ip_qos_interactive = value;
                   1004:                        options->ip_qos_bulk = value2;
                   1005:                }
                   1006:                break;
                   1007:
1.177     andreas  1008:        case oUseRoaming:
                   1009:                intptr = &options->use_roaming;
                   1010:                goto parse_flag;
                   1011:
1.96      markus   1012:        case oDeprecated:
1.98      markus   1013:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   1014:                    filename, linenum, keyword);
1.98      markus   1015:                return 0;
1.96      markus   1016:
1.110     jakob    1017:        case oUnsupported:
                   1018:                error("%s line %d: Unsupported option \"%s\"",
                   1019:                    filename, linenum, keyword);
                   1020:                return 0;
                   1021:
1.17      markus   1022:        default:
                   1023:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                   1024:        }
                   1025:
                   1026:        /* Check that there is no garbage at end of line. */
1.57      djm      1027:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho       1028:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm      1029:                    filename, linenum, arg);
1.39      ho       1030:        }
1.17      markus   1031:        return 0;
1.1       deraadt  1032: }
                   1033:
                   1034:
1.19      markus   1035: /*
                   1036:  * Reads the config file and modifies the options accordingly.  Options
                   1037:  * should already be initialized before this call.  This never returns if
1.89      stevesk  1038:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   1039:  */
1.1       deraadt  1040:
1.89      stevesk  1041: int
1.134     deraadt  1042: read_config_file(const char *filename, const char *host, Options *options,
1.129     djm      1043:     int checkperm)
1.1       deraadt  1044: {
1.17      markus   1045:        FILE *f;
                   1046:        char line[1024];
                   1047:        int active, linenum;
                   1048:        int bad_options = 0;
                   1049:
1.129     djm      1050:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  1051:                return 0;
1.129     djm      1052:
                   1053:        if (checkperm) {
                   1054:                struct stat sb;
1.134     deraadt  1055:
1.131     dtucker  1056:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      1057:                        fatal("fstat %s: %s", filename, strerror(errno));
                   1058:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  1059:                    (sb.st_mode & 022) != 0))
1.129     djm      1060:                        fatal("Bad owner or permissions on %s", filename);
                   1061:        }
1.17      markus   1062:
                   1063:        debug("Reading configuration data %.200s", filename);
                   1064:
1.19      markus   1065:        /*
                   1066:         * Mark that we are now processing the options.  This flag is turned
                   1067:         * on/off by Host specifications.
                   1068:         */
1.17      markus   1069:        active = 1;
                   1070:        linenum = 0;
                   1071:        while (fgets(line, sizeof(line), f)) {
                   1072:                /* Update line number counter. */
                   1073:                linenum++;
                   1074:                if (process_config_line(options, host, line, filename, linenum, &active) != 0)
                   1075:                        bad_options++;
                   1076:        }
                   1077:        fclose(f);
                   1078:        if (bad_options > 0)
1.64      millert  1079:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  1080:                    filename, bad_options);
1.89      stevesk  1081:        return 1;
1.1       deraadt  1082: }
                   1083:
1.19      markus   1084: /*
                   1085:  * Initializes options to special values that indicate that they have not yet
                   1086:  * been set.  Read_config_file will only set options with this value. Options
                   1087:  * are processed in the following order: command line, user config file,
                   1088:  * system config file.  Last, fill_default_options is called.
                   1089:  */
1.1       deraadt  1090:
1.26      markus   1091: void
1.17      markus   1092: initialize_options(Options * options)
1.1       deraadt  1093: {
1.17      markus   1094:        memset(options, 'X', sizeof(*options));
                   1095:        options->forward_agent = -1;
                   1096:        options->forward_x11 = -1;
1.123     markus   1097:        options->forward_x11_trusted = -1;
1.186     djm      1098:        options->forward_x11_timeout = -1;
1.153     markus   1099:        options->exit_on_forward_failure = -1;
1.34      markus   1100:        options->xauth_location = NULL;
1.17      markus   1101:        options->gateway_ports = -1;
                   1102:        options->use_privileged_port = -1;
                   1103:        options->rsa_authentication = -1;
1.50      markus   1104:        options->pubkey_authentication = -1;
1.78      markus   1105:        options->challenge_response_authentication = -1;
1.118     markus   1106:        options->gss_authentication = -1;
                   1107:        options->gss_deleg_creds = -1;
1.17      markus   1108:        options->password_authentication = -1;
1.48      markus   1109:        options->kbd_interactive_authentication = -1;
                   1110:        options->kbd_interactive_devices = NULL;
1.17      markus   1111:        options->rhosts_rsa_authentication = -1;
1.72      markus   1112:        options->hostbased_authentication = -1;
1.17      markus   1113:        options->batch_mode = -1;
                   1114:        options->check_host_ip = -1;
                   1115:        options->strict_host_key_checking = -1;
                   1116:        options->compression = -1;
1.126     markus   1117:        options->tcp_keep_alive = -1;
1.17      markus   1118:        options->compression_level = -1;
                   1119:        options->port = -1;
1.114     djm      1120:        options->address_family = -1;
1.17      markus   1121:        options->connection_attempts = -1;
1.111     djm      1122:        options->connection_timeout = -1;
1.17      markus   1123:        options->number_of_password_prompts = -1;
                   1124:        options->cipher = -1;
1.25      markus   1125:        options->ciphers = NULL;
1.62      markus   1126:        options->macs = NULL;
1.189     djm      1127:        options->kex_algorithms = NULL;
1.76      markus   1128:        options->hostkeyalgorithms = NULL;
1.25      markus   1129:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus   1130:        options->num_identity_files = 0;
                   1131:        options->hostname = NULL;
1.52      markus   1132:        options->host_key_alias = NULL;
1.17      markus   1133:        options->proxy_command = NULL;
                   1134:        options->user = NULL;
                   1135:        options->escape_char = -1;
                   1136:        options->system_hostfile = NULL;
                   1137:        options->user_hostfile = NULL;
1.27      markus   1138:        options->system_hostfile2 = NULL;
                   1139:        options->user_hostfile2 = NULL;
1.185     djm      1140:        options->local_forwards = NULL;
1.17      markus   1141:        options->num_local_forwards = 0;
1.185     djm      1142:        options->remote_forwards = NULL;
1.17      markus   1143:        options->num_remote_forwards = 0;
1.90      stevesk  1144:        options->clear_forwardings = -1;
1.95      markus   1145:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1146:        options->preferred_authentications = NULL;
1.77      markus   1147:        options->bind_address = NULL;
1.183     markus   1148:        options->pkcs11_provider = NULL;
1.101     markus   1149:        options->enable_ssh_keysign = - 1;
1.91      markus   1150:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1151:        options->identities_only = - 1;
1.105     markus   1152:        options->rekey_limit = - 1;
1.107     jakob    1153:        options->verify_host_key_dns = -1;
1.127     markus   1154:        options->server_alive_interval = -1;
                   1155:        options->server_alive_count_max = -1;
1.130     djm      1156:        options->num_send_env = 0;
1.132     djm      1157:        options->control_path = NULL;
                   1158:        options->control_master = -1;
1.187     djm      1159:        options->control_persist = -1;
                   1160:        options->control_persist_timeout = 0;
1.136     djm      1161:        options->hash_known_hosts = -1;
1.144     reyk     1162:        options->tun_open = -1;
                   1163:        options->tun_local = -1;
                   1164:        options->tun_remote = -1;
                   1165:        options->local_command = NULL;
                   1166:        options->permit_local_command = -1;
1.177     andreas  1167:        options->use_roaming = -1;
1.167     grunk    1168:        options->visual_host_key = -1;
1.171     djm      1169:        options->zero_knowledge_password_authentication = -1;
1.190     djm      1170:        options->ip_qos_interactive = -1;
                   1171:        options->ip_qos_bulk = -1;
1.1       deraadt  1172: }
                   1173:
1.19      markus   1174: /*
                   1175:  * Called after processing other sources of option data, this fills those
                   1176:  * options for which no value has been specified with their default values.
                   1177:  */
1.1       deraadt  1178:
1.26      markus   1179: void
1.17      markus   1180: fill_default_options(Options * options)
1.1       deraadt  1181: {
1.61      deraadt  1182:        int len;
                   1183:
1.17      markus   1184:        if (options->forward_agent == -1)
1.33      markus   1185:                options->forward_agent = 0;
1.17      markus   1186:        if (options->forward_x11 == -1)
1.23      markus   1187:                options->forward_x11 = 0;
1.123     markus   1188:        if (options->forward_x11_trusted == -1)
                   1189:                options->forward_x11_trusted = 0;
1.186     djm      1190:        if (options->forward_x11_timeout == -1)
                   1191:                options->forward_x11_timeout = 1200;
1.153     markus   1192:        if (options->exit_on_forward_failure == -1)
                   1193:                options->exit_on_forward_failure = 0;
1.34      markus   1194:        if (options->xauth_location == NULL)
1.80      markus   1195:                options->xauth_location = _PATH_XAUTH;
1.17      markus   1196:        if (options->gateway_ports == -1)
                   1197:                options->gateway_ports = 0;
                   1198:        if (options->use_privileged_port == -1)
1.65      markus   1199:                options->use_privileged_port = 0;
1.17      markus   1200:        if (options->rsa_authentication == -1)
                   1201:                options->rsa_authentication = 1;
1.50      markus   1202:        if (options->pubkey_authentication == -1)
                   1203:                options->pubkey_authentication = 1;
1.78      markus   1204:        if (options->challenge_response_authentication == -1)
1.83      markus   1205:                options->challenge_response_authentication = 1;
1.118     markus   1206:        if (options->gss_authentication == -1)
1.122     markus   1207:                options->gss_authentication = 0;
1.118     markus   1208:        if (options->gss_deleg_creds == -1)
                   1209:                options->gss_deleg_creds = 0;
1.17      markus   1210:        if (options->password_authentication == -1)
                   1211:                options->password_authentication = 1;
1.48      markus   1212:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1213:                options->kbd_interactive_authentication = 1;
1.17      markus   1214:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk  1215:                options->rhosts_rsa_authentication = 0;
1.72      markus   1216:        if (options->hostbased_authentication == -1)
                   1217:                options->hostbased_authentication = 0;
1.17      markus   1218:        if (options->batch_mode == -1)
                   1219:                options->batch_mode = 0;
                   1220:        if (options->check_host_ip == -1)
                   1221:                options->check_host_ip = 1;
                   1222:        if (options->strict_host_key_checking == -1)
                   1223:                options->strict_host_key_checking = 2;  /* 2 is default */
                   1224:        if (options->compression == -1)
                   1225:                options->compression = 0;
1.126     markus   1226:        if (options->tcp_keep_alive == -1)
                   1227:                options->tcp_keep_alive = 1;
1.17      markus   1228:        if (options->compression_level == -1)
                   1229:                options->compression_level = 6;
                   1230:        if (options->port == -1)
                   1231:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1232:        if (options->address_family == -1)
                   1233:                options->address_family = AF_UNSPEC;
1.17      markus   1234:        if (options->connection_attempts == -1)
1.84      markus   1235:                options->connection_attempts = 1;
1.17      markus   1236:        if (options->number_of_password_prompts == -1)
                   1237:                options->number_of_password_prompts = 3;
                   1238:        /* Selected in ssh_login(). */
                   1239:        if (options->cipher == -1)
                   1240:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus   1241:        /* options->ciphers, default set in myproposals.h */
1.62      markus   1242:        /* options->macs, default set in myproposals.h */
1.189     djm      1243:        /* options->kex_algorithms, default set in myproposals.h */
1.76      markus   1244:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus   1245:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.178     markus   1246:                options->protocol = SSH_PROTO_2;
1.17      markus   1247:        if (options->num_identity_files == 0) {
1.50      markus   1248:                if (options->protocol & SSH_PROTO_1) {
1.61      deraadt  1249:                        len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50      markus   1250:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1251:                            xmalloc(len);
                   1252:                        snprintf(options->identity_files[options->num_identity_files++],
                   1253:                            len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50      markus   1254:                }
                   1255:                if (options->protocol & SSH_PROTO_2) {
1.63      deraadt  1256:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
                   1257:                        options->identity_files[options->num_identity_files] =
                   1258:                            xmalloc(len);
                   1259:                        snprintf(options->identity_files[options->num_identity_files++],
                   1260:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
                   1261:
1.61      deraadt  1262:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50      markus   1263:                        options->identity_files[options->num_identity_files] =
1.61      deraadt  1264:                            xmalloc(len);
                   1265:                        snprintf(options->identity_files[options->num_identity_files++],
                   1266:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.188     djm      1267:
                   1268:                        len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
                   1269:                        options->identity_files[options->num_identity_files] =
                   1270:                            xmalloc(len);
                   1271:                        snprintf(options->identity_files[options->num_identity_files++],
                   1272:                            len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
1.50      markus   1273:                }
1.27      markus   1274:        }
1.17      markus   1275:        if (options->escape_char == -1)
                   1276:                options->escape_char = '~';
                   1277:        if (options->system_hostfile == NULL)
1.55      markus   1278:                options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17      markus   1279:        if (options->user_hostfile == NULL)
1.55      markus   1280:                options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27      markus   1281:        if (options->system_hostfile2 == NULL)
1.55      markus   1282:                options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27      markus   1283:        if (options->user_hostfile2 == NULL)
1.55      markus   1284:                options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95      markus   1285:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1286:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk  1287:        if (options->clear_forwardings == 1)
                   1288:                clear_forwardings(options);
1.91      markus   1289:        if (options->no_host_authentication_for_localhost == - 1)
                   1290:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1291:        if (options->identities_only == -1)
                   1292:                options->identities_only = 0;
1.101     markus   1293:        if (options->enable_ssh_keysign == -1)
                   1294:                options->enable_ssh_keysign = 0;
1.105     markus   1295:        if (options->rekey_limit == -1)
                   1296:                options->rekey_limit = 0;
1.107     jakob    1297:        if (options->verify_host_key_dns == -1)
                   1298:                options->verify_host_key_dns = 0;
1.127     markus   1299:        if (options->server_alive_interval == -1)
                   1300:                options->server_alive_interval = 0;
                   1301:        if (options->server_alive_count_max == -1)
                   1302:                options->server_alive_count_max = 3;
1.132     djm      1303:        if (options->control_master == -1)
                   1304:                options->control_master = 0;
1.187     djm      1305:        if (options->control_persist == -1) {
                   1306:                options->control_persist = 0;
                   1307:                options->control_persist_timeout = 0;
                   1308:        }
1.136     djm      1309:        if (options->hash_known_hosts == -1)
                   1310:                options->hash_known_hosts = 0;
1.144     reyk     1311:        if (options->tun_open == -1)
1.145     reyk     1312:                options->tun_open = SSH_TUNMODE_NO;
                   1313:        if (options->tun_local == -1)
                   1314:                options->tun_local = SSH_TUNID_ANY;
                   1315:        if (options->tun_remote == -1)
                   1316:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1317:        if (options->permit_local_command == -1)
                   1318:                options->permit_local_command = 0;
1.177     andreas  1319:        if (options->use_roaming == -1)
                   1320:                options->use_roaming = 1;
1.167     grunk    1321:        if (options->visual_host_key == -1)
                   1322:                options->visual_host_key = 0;
1.171     djm      1323:        if (options->zero_knowledge_password_authentication == -1)
                   1324:                options->zero_knowledge_password_authentication = 0;
1.190     djm      1325:        if (options->ip_qos_interactive == -1)
                   1326:                options->ip_qos_interactive = IPTOS_LOWDELAY;
                   1327:        if (options->ip_qos_bulk == -1)
                   1328:                options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.144     reyk     1329:        /* options->local_command should not be set by default */
1.17      markus   1330:        /* options->proxy_command should not be set by default */
                   1331:        /* options->user will be set in the main program if appropriate */
                   1332:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   1333:        /* options->host_key_alias should not be set by default */
1.67      markus   1334:        /* options->preferred_authentications will be set in ssh */
1.135     djm      1335: }
                   1336:
                   1337: /*
                   1338:  * parse_forward
                   1339:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  1340:  *   dynamicfwd == 0
1.135     djm      1341:  *     [listenhost:]listenport:connecthost:connectport
1.168     stevesk  1342:  *   dynamicfwd == 1
                   1343:  *     [listenhost:]listenport
1.135     djm      1344:  * returns number of arguments parsed or zero on error
                   1345:  */
                   1346: int
1.176     djm      1347: parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      1348: {
                   1349:        int i;
                   1350:        char *p, *cp, *fwdarg[4];
                   1351:
                   1352:        memset(fwd, '\0', sizeof(*fwd));
                   1353:
                   1354:        cp = p = xstrdup(fwdspec);
                   1355:
                   1356:        /* skip leading spaces */
1.162     tedu     1357:        while (isspace(*cp))
1.135     djm      1358:                cp++;
                   1359:
                   1360:        for (i = 0; i < 4; ++i)
                   1361:                if ((fwdarg[i] = hpdelim(&cp)) == NULL)
                   1362:                        break;
                   1363:
1.170     stevesk  1364:        /* Check for trailing garbage */
1.135     djm      1365:        if (cp != NULL)
                   1366:                i = 0;  /* failure */
                   1367:
                   1368:        switch (i) {
1.168     stevesk  1369:        case 1:
                   1370:                fwd->listen_host = NULL;
                   1371:                fwd->listen_port = a2port(fwdarg[0]);
                   1372:                fwd->connect_host = xstrdup("socks");
                   1373:                break;
                   1374:
                   1375:        case 2:
                   1376:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1377:                fwd->listen_port = a2port(fwdarg[1]);
                   1378:                fwd->connect_host = xstrdup("socks");
                   1379:                break;
                   1380:
1.135     djm      1381:        case 3:
                   1382:                fwd->listen_host = NULL;
                   1383:                fwd->listen_port = a2port(fwdarg[0]);
                   1384:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
                   1385:                fwd->connect_port = a2port(fwdarg[2]);
                   1386:                break;
                   1387:
                   1388:        case 4:
                   1389:                fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
                   1390:                fwd->listen_port = a2port(fwdarg[1]);
                   1391:                fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
                   1392:                fwd->connect_port = a2port(fwdarg[3]);
                   1393:                break;
                   1394:        default:
                   1395:                i = 0; /* failure */
                   1396:        }
                   1397:
                   1398:        xfree(p);
                   1399:
1.168     stevesk  1400:        if (dynamicfwd) {
                   1401:                if (!(i == 1 || i == 2))
                   1402:                        goto fail_free;
                   1403:        } else {
                   1404:                if (!(i == 3 || i == 4))
                   1405:                        goto fail_free;
1.175     djm      1406:                if (fwd->connect_port <= 0)
1.168     stevesk  1407:                        goto fail_free;
                   1408:        }
                   1409:
1.176     djm      1410:        if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
1.135     djm      1411:                goto fail_free;
                   1412:
                   1413:        if (fwd->connect_host != NULL &&
                   1414:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   1415:                goto fail_free;
1.176     djm      1416:        if (fwd->listen_host != NULL &&
                   1417:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   1418:                goto fail_free;
                   1419:
1.135     djm      1420:
                   1421:        return (i);
                   1422:
                   1423:  fail_free:
1.173     djm      1424:        if (fwd->connect_host != NULL) {
1.135     djm      1425:                xfree(fwd->connect_host);
1.173     djm      1426:                fwd->connect_host = NULL;
                   1427:        }
                   1428:        if (fwd->listen_host != NULL) {
1.135     djm      1429:                xfree(fwd->listen_host);
1.173     djm      1430:                fwd->listen_host = NULL;
                   1431:        }
1.135     djm      1432:        return (0);
1.1       deraadt  1433: }