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

1.220   ! millert     1: /* $OpenBSD: readconf.c,v 1.219 2014/04/23 12:42:34 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>
1.206     djm        18: #include <sys/wait.h>
1.220   ! millert    19: #include <sys/un.h>
1.152     stevesk    20:
                     21: #include <netinet/in.h>
1.190     djm        22: #include <netinet/in_systm.h>
                     23: #include <netinet/ip.h>
1.148     stevesk    24:
                     25: #include <ctype.h>
1.154     stevesk    26: #include <errno.h>
1.206     djm        27: #include <fcntl.h>
1.155     stevesk    28: #include <netdb.h>
1.206     djm        29: #include <paths.h>
                     30: #include <pwd.h>
1.159     deraadt    31: #include <signal.h>
1.158     stevesk    32: #include <stdio.h>
1.157     stevesk    33: #include <string.h>
1.156     stevesk    34: #include <unistd.h>
1.200     dtucker    35: #include <util.h>
1.1       deraadt    36:
1.159     deraadt    37: #include "xmalloc.h"
1.1       deraadt    38: #include "ssh.h"
1.25      markus     39: #include "compat.h"
1.58      markus     40: #include "cipher.h"
1.55      markus     41: #include "pathnames.h"
1.58      markus     42: #include "log.h"
1.159     deraadt    43: #include "key.h"
1.220   ! millert    44: #include "misc.h"
1.58      markus     45: #include "readconf.h"
                     46: #include "match.h"
1.159     deraadt    47: #include "buffer.h"
1.62      markus     48: #include "kex.h"
                     49: #include "mac.h"
1.206     djm        50: #include "uidswap.h"
1.1       deraadt    51:
                     52: /* Format of the configuration file:
                     53:
                     54:    # Configuration data is parsed as follows:
                     55:    #  1. command line options
                     56:    #  2. user-specific file
                     57:    #  3. system-wide file
                     58:    # Any configuration value is only changed the first time it is set.
                     59:    # Thus, host-specific definitions should be at the beginning of the
                     60:    # configuration file, and defaults at the end.
                     61:
                     62:    # Host-specific declarations.  These may override anything above.  A single
                     63:    # host may match multiple declarations; these are processed in the order
                     64:    # that they are given in.
                     65:
                     66:    Host *.ngs.fi ngs.fi
1.96      markus     67:      User foo
1.1       deraadt    68:
                     69:    Host fake.com
                     70:      HostName another.host.name.real.org
                     71:      User blaah
                     72:      Port 34289
                     73:      ForwardX11 no
                     74:      ForwardAgent no
                     75:
                     76:    Host books.com
                     77:      RemoteForward 9999 shadows.cs.hut.fi:9999
                     78:      Cipher 3des
                     79:
                     80:    Host fascist.blob.com
                     81:      Port 23123
                     82:      User tylonen
                     83:      PasswordAuthentication no
                     84:
                     85:    Host puukko.hut.fi
                     86:      User t35124p
                     87:      ProxyCommand ssh-proxy %h %p
                     88:
                     89:    Host *.fr
1.96      markus     90:      PublicKeyAuthentication no
1.1       deraadt    91:
                     92:    Host *.su
                     93:      Cipher none
                     94:      PasswordAuthentication no
                     95:
1.144     reyk       96:    Host vpn.fake.com
                     97:      Tunnel yes
                     98:      TunnelDevice 3
                     99:
1.1       deraadt   100:    # Defaults for various options
                    101:    Host *
                    102:      ForwardAgent no
1.50      markus    103:      ForwardX11 no
1.1       deraadt   104:      PasswordAuthentication yes
                    105:      RSAAuthentication yes
                    106:      RhostsRSAAuthentication yes
                    107:      StrictHostKeyChecking yes
1.126     markus    108:      TcpKeepAlive no
1.1       deraadt   109:      IdentityFile ~/.ssh/identity
                    110:      Port 22
                    111:      EscapeChar ~
                    112:
                    113: */
                    114:
                    115: /* Keyword tokens. */
                    116:
1.17      markus    117: typedef enum {
                    118:        oBadOption,
1.206     djm       119:        oHost, oMatch,
1.186     djm       120:        oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
                    121:        oGatewayPorts, oExitOnForwardFailure,
1.100     deraadt   122:        oPasswordAuthentication, oRSAAuthentication,
1.59      markus    123:        oChallengeResponseAuthentication, oXAuthLocation,
1.17      markus    124:        oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
1.206     djm       125:        oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
1.17      markus    126:        oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
                    127:        oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126     markus    128:        oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62      markus    129:        oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50      markus    130:        oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67      markus    131:        oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76      markus    132:        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.183     markus    133:        oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
1.96      markus    134:        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111     djm       135:        oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118     markus    136:        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128     markus    137:        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.187     djm       138:        oSendEnv, oControlPath, oControlMaster, oControlPersist,
                    139:        oHashKnownHosts,
1.144     reyk      140:        oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
1.216     djm       141:        oVisualHostKey, oUseRoaming,
1.205     djm       142:        oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
1.209     djm       143:        oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
                    144:        oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
1.220   ! millert   145:        oStreamLocalBindMask, oStreamLocalBindUnlink,
1.199     djm       146:        oIgnoredUnknownOption, oDeprecated, oUnsupported
1.1       deraadt   147: } OpCodes;
                    148:
                    149: /* Textual representations of the tokens. */
                    150:
1.17      markus    151: static struct {
                    152:        const char *name;
                    153:        OpCodes opcode;
                    154: } keywords[] = {
                    155:        { "forwardagent", oForwardAgent },
                    156:        { "forwardx11", oForwardX11 },
1.123     markus    157:        { "forwardx11trusted", oForwardX11Trusted },
1.186     djm       158:        { "forwardx11timeout", oForwardX11Timeout },
1.153     markus    159:        { "exitonforwardfailure", oExitOnForwardFailure },
1.34      markus    160:        { "xauthlocation", oXAuthLocation },
1.17      markus    161:        { "gatewayports", oGatewayPorts },
                    162:        { "useprivilegedport", oUsePrivilegedPort },
1.116     markus    163:        { "rhostsauthentication", oDeprecated },
1.17      markus    164:        { "passwordauthentication", oPasswordAuthentication },
1.48      markus    165:        { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
                    166:        { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17      markus    167:        { "rsaauthentication", oRSAAuthentication },
1.50      markus    168:        { "pubkeyauthentication", oPubkeyAuthentication },
1.59      markus    169:        { "dsaauthentication", oPubkeyAuthentication },             /* alias */
1.72      markus    170:        { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73      markus    171:        { "hostbasedauthentication", oHostbasedAuthentication },
1.59      markus    172:        { "challengeresponseauthentication", oChallengeResponseAuthentication },
                    173:        { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
                    174:        { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
1.110     jakob     175:        { "kerberosauthentication", oUnsupported },
                    176:        { "kerberostgtpassing", oUnsupported },
                    177:        { "afstokenpassing", oUnsupported },
1.118     markus    178: #if defined(GSSAPI)
                    179:        { "gssapiauthentication", oGssAuthentication },
                    180:        { "gssapidelegatecredentials", oGssDelegateCreds },
                    181: #else
                    182:        { "gssapiauthentication", oUnsupported },
                    183:        { "gssapidelegatecredentials", oUnsupported },
                    184: #endif
1.96      markus    185:        { "fallbacktorsh", oDeprecated },
                    186:        { "usersh", oDeprecated },
1.17      markus    187:        { "identityfile", oIdentityFile },
1.174     stevesk   188:        { "identityfile2", oIdentityFile },                     /* obsolete */
1.128     markus    189:        { "identitiesonly", oIdentitiesOnly },
1.17      markus    190:        { "hostname", oHostName },
1.52      markus    191:        { "hostkeyalias", oHostKeyAlias },
1.17      markus    192:        { "proxycommand", oProxyCommand },
                    193:        { "port", oPort },
                    194:        { "cipher", oCipher },
1.25      markus    195:        { "ciphers", oCiphers },
1.62      markus    196:        { "macs", oMacs },
1.25      markus    197:        { "protocol", oProtocol },
1.17      markus    198:        { "remoteforward", oRemoteForward },
                    199:        { "localforward", oLocalForward },
                    200:        { "user", oUser },
                    201:        { "host", oHost },
1.206     djm       202:        { "match", oMatch },
1.17      markus    203:        { "escapechar", oEscapeChar },
                    204:        { "globalknownhostsfile", oGlobalKnownHostsFile },
1.193     djm       205:        { "globalknownhostsfile2", oDeprecated },
1.174     stevesk   206:        { "userknownhostsfile", oUserKnownHostsFile },
1.193     djm       207:        { "userknownhostsfile2", oDeprecated },
1.17      markus    208:        { "connectionattempts", oConnectionAttempts },
                    209:        { "batchmode", oBatchMode },
                    210:        { "checkhostip", oCheckHostIP },
                    211:        { "stricthostkeychecking", oStrictHostKeyChecking },
                    212:        { "compression", oCompression },
                    213:        { "compressionlevel", oCompressionLevel },
1.126     markus    214:        { "tcpkeepalive", oTCPKeepAlive },
                    215:        { "keepalive", oTCPKeepAlive },                         /* obsolete */
1.17      markus    216:        { "numberofpasswordprompts", oNumberOfPasswordPrompts },
                    217:        { "loglevel", oLogLevel },
1.71      markus    218:        { "dynamicforward", oDynamicForward },
1.67      markus    219:        { "preferredauthentications", oPreferredAuthentications },
1.76      markus    220:        { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77      markus    221:        { "bindaddress", oBindAddress },
1.183     markus    222: #ifdef ENABLE_PKCS11
                    223:        { "smartcarddevice", oPKCS11Provider },
                    224:        { "pkcs11provider", oPKCS11Provider },
1.110     jakob     225: #else
                    226:        { "smartcarddevice", oUnsupported },
1.183     markus    227:        { "pkcs11provider", oUnsupported },
1.110     jakob     228: #endif
1.93      deraadt   229:        { "clearallforwardings", oClearAllForwardings },
1.101     markus    230:        { "enablesshkeysign", oEnableSSHKeysign },
1.107     jakob     231:        { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93      deraadt   232:        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105     markus    233:        { "rekeylimit", oRekeyLimit },
1.111     djm       234:        { "connecttimeout", oConnectTimeout },
1.112     djm       235:        { "addressfamily", oAddressFamily },
1.127     markus    236:        { "serveraliveinterval", oServerAliveInterval },
                    237:        { "serveralivecountmax", oServerAliveCountMax },
1.130     djm       238:        { "sendenv", oSendEnv },
1.132     djm       239:        { "controlpath", oControlPath },
                    240:        { "controlmaster", oControlMaster },
1.187     djm       241:        { "controlpersist", oControlPersist },
1.136     djm       242:        { "hashknownhosts", oHashKnownHosts },
1.144     reyk      243:        { "tunnel", oTunnel },
                    244:        { "tunneldevice", oTunnelDevice },
                    245:        { "localcommand", oLocalCommand },
                    246:        { "permitlocalcommand", oPermitLocalCommand },
1.167     grunk     247:        { "visualhostkey", oVisualHostKey },
1.177     andreas   248:        { "useroaming", oUseRoaming },
1.189     djm       249:        { "kexalgorithms", oKexAlgorithms },
1.190     djm       250:        { "ipqos", oIPQoS },
1.192     djm       251:        { "requesttty", oRequestTTY },
1.205     djm       252:        { "proxyusefdpass", oProxyUseFdpass },
1.208     djm       253:        { "canonicaldomains", oCanonicalDomains },
1.209     djm       254:        { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
                    255:        { "canonicalizehostname", oCanonicalizeHostname },
                    256:        { "canonicalizemaxdots", oCanonicalizeMaxDots },
                    257:        { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
1.220   ! millert   258:        { "streamlocalbindmask", oStreamLocalBindMask },
        !           259:        { "streamlocalbindunlink", oStreamLocalBindUnlink },
1.199     djm       260:        { "ignoreunknown", oIgnoreUnknown },
1.171     djm       261:
1.92      stevesk   262:        { NULL, oBadOption }
1.13      markus    263: };
                    264:
1.19      markus    265: /*
                    266:  * Adds a local TCP/IP port forward to options.  Never returns if there is an
                    267:  * error.
                    268:  */
1.1       deraadt   269:
1.26      markus    270: void
1.220   ! millert   271: add_local_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   272: {
1.220   ! millert   273:        struct Forward *fwd;
1.17      markus    274:        extern uid_t original_real_uid;
1.185     djm       275:
1.220   ! millert   276:        if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
        !           277:            newfwd->listen_path == NULL)
1.64      millert   278:                fatal("Privileged ports can only be forwarded by root.");
1.185     djm       279:        options->local_forwards = xrealloc(options->local_forwards,
                    280:            options->num_local_forwards + 1,
                    281:            sizeof(*options->local_forwards));
1.17      markus    282:        fwd = &options->local_forwards[options->num_local_forwards++];
1.135     djm       283:
1.172     stevesk   284:        fwd->listen_host = newfwd->listen_host;
1.135     djm       285:        fwd->listen_port = newfwd->listen_port;
1.220   ! millert   286:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   287:        fwd->connect_host = newfwd->connect_host;
1.135     djm       288:        fwd->connect_port = newfwd->connect_port;
1.220   ! millert   289:        fwd->connect_path = newfwd->connect_path;
1.1       deraadt   290: }
                    291:
1.19      markus    292: /*
                    293:  * Adds a remote TCP/IP port forward to options.  Never returns if there is
                    294:  * an error.
                    295:  */
1.1       deraadt   296:
1.26      markus    297: void
1.220   ! millert   298: add_remote_forward(Options *options, const struct Forward *newfwd)
1.1       deraadt   299: {
1.220   ! millert   300:        struct Forward *fwd;
1.185     djm       301:
                    302:        options->remote_forwards = xrealloc(options->remote_forwards,
                    303:            options->num_remote_forwards + 1,
                    304:            sizeof(*options->remote_forwards));
1.17      markus    305:        fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135     djm       306:
1.172     stevesk   307:        fwd->listen_host = newfwd->listen_host;
1.135     djm       308:        fwd->listen_port = newfwd->listen_port;
1.220   ! millert   309:        fwd->listen_path = newfwd->listen_path;
1.172     stevesk   310:        fwd->connect_host = newfwd->connect_host;
1.135     djm       311:        fwd->connect_port = newfwd->connect_port;
1.220   ! millert   312:        fwd->connect_path = newfwd->connect_path;
1.194     markus    313:        fwd->handle = newfwd->handle;
1.184     markus    314:        fwd->allocated_port = 0;
1.1       deraadt   315: }
                    316:
1.90      stevesk   317: static void
                    318: clear_forwardings(Options *options)
                    319: {
                    320:        int i;
                    321:
1.135     djm       322:        for (i = 0; i < options->num_local_forwards; i++) {
1.202     djm       323:                free(options->local_forwards[i].listen_host);
1.220   ! millert   324:                free(options->local_forwards[i].listen_path);
1.202     djm       325:                free(options->local_forwards[i].connect_host);
1.220   ! millert   326:                free(options->local_forwards[i].connect_path);
1.135     djm       327:        }
1.185     djm       328:        if (options->num_local_forwards > 0) {
1.202     djm       329:                free(options->local_forwards);
1.185     djm       330:                options->local_forwards = NULL;
                    331:        }
1.90      stevesk   332:        options->num_local_forwards = 0;
1.135     djm       333:        for (i = 0; i < options->num_remote_forwards; i++) {
1.202     djm       334:                free(options->remote_forwards[i].listen_host);
1.220   ! millert   335:                free(options->remote_forwards[i].listen_path);
1.202     djm       336:                free(options->remote_forwards[i].connect_host);
1.220   ! millert   337:                free(options->remote_forwards[i].connect_path);
1.135     djm       338:        }
1.185     djm       339:        if (options->num_remote_forwards > 0) {
1.202     djm       340:                free(options->remote_forwards);
1.185     djm       341:                options->remote_forwards = NULL;
                    342:        }
1.90      stevesk   343:        options->num_remote_forwards = 0;
1.145     reyk      344:        options->tun_open = SSH_TUNMODE_NO;
1.90      stevesk   345: }
                    346:
1.195     dtucker   347: void
                    348: add_identity_file(Options *options, const char *dir, const char *filename,
                    349:     int userprovided)
                    350: {
                    351:        char *path;
1.219     djm       352:        int i;
1.195     dtucker   353:
                    354:        if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
                    355:                fatal("Too many identity files specified (max %d)",
                    356:                    SSH_MAX_IDENTITY_FILES);
                    357:
                    358:        if (dir == NULL) /* no dir, filename is absolute */
                    359:                path = xstrdup(filename);
                    360:        else
                    361:                (void)xasprintf(&path, "%.100s%.100s", dir, filename);
1.219     djm       362:
                    363:        /* Avoid registering duplicates */
                    364:        for (i = 0; i < options->num_identity_files; i++) {
                    365:                if (options->identity_file_userprovided[i] == userprovided &&
                    366:                    strcmp(options->identity_files[i], path) == 0) {
                    367:                        debug2("%s: ignoring duplicate key %s", __func__, path);
                    368:                        free(path);
                    369:                        return;
                    370:                }
                    371:        }
1.195     dtucker   372:
                    373:        options->identity_file_userprovided[options->num_identity_files] =
                    374:            userprovided;
                    375:        options->identity_files[options->num_identity_files++] = path;
                    376: }
                    377:
1.206     djm       378: int
                    379: default_ssh_port(void)
                    380: {
                    381:        static int port;
                    382:        struct servent *sp;
                    383:
                    384:        if (port == 0) {
                    385:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    386:                port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
                    387:        }
                    388:        return port;
                    389: }
                    390:
                    391: /*
                    392:  * Execute a command in a shell.
                    393:  * Return its exit status or -1 on abnormal exit.
                    394:  */
                    395: static int
                    396: execute_in_shell(const char *cmd)
                    397: {
                    398:        char *shell, *command_string;
                    399:        pid_t pid;
                    400:        int devnull, status;
                    401:        extern uid_t original_real_uid;
                    402:
                    403:        if ((shell = getenv("SHELL")) == NULL)
                    404:                shell = _PATH_BSHELL;
                    405:
                    406:        /*
                    407:         * Use "exec" to avoid "sh -c" processes on some platforms
                    408:         * (e.g. Solaris)
                    409:         */
                    410:        xasprintf(&command_string, "exec %s", cmd);
                    411:
                    412:        /* Need this to redirect subprocess stdin/out */
                    413:        if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
                    414:                fatal("open(/dev/null): %s", strerror(errno));
                    415:
                    416:        debug("Executing command: '%.500s'", cmd);
                    417:
                    418:        /* Fork and execute the command. */
                    419:        if ((pid = fork()) == 0) {
                    420:                char *argv[4];
                    421:
                    422:                /* Child.  Permanently give up superuser privileges. */
                    423:                permanently_drop_suid(original_real_uid);
                    424:
                    425:                /* Redirect child stdin and stdout. Leave stderr */
                    426:                if (dup2(devnull, STDIN_FILENO) == -1)
                    427:                        fatal("dup2: %s", strerror(errno));
                    428:                if (dup2(devnull, STDOUT_FILENO) == -1)
                    429:                        fatal("dup2: %s", strerror(errno));
                    430:                if (devnull > STDERR_FILENO)
                    431:                        close(devnull);
                    432:                closefrom(STDERR_FILENO + 1);
                    433:
                    434:                argv[0] = shell;
                    435:                argv[1] = "-c";
                    436:                argv[2] = command_string;
                    437:                argv[3] = NULL;
                    438:
                    439:                execv(argv[0], argv);
                    440:                error("Unable to execute '%.100s': %s", cmd, strerror(errno));
                    441:                /* Die with signal to make this error apparent to parent. */
                    442:                signal(SIGTERM, SIG_DFL);
                    443:                kill(getpid(), SIGTERM);
                    444:                _exit(1);
                    445:        }
                    446:        /* Parent. */
                    447:        if (pid < 0)
                    448:                fatal("%s: fork: %.100s", __func__, strerror(errno));
                    449:
                    450:        close(devnull);
                    451:        free(command_string);
                    452:
                    453:        while (waitpid(pid, &status, 0) == -1) {
                    454:                if (errno != EINTR && errno != EAGAIN)
                    455:                        fatal("%s: waitpid: %s", __func__, strerror(errno));
                    456:        }
                    457:        if (!WIFEXITED(status)) {
                    458:                error("command '%.100s' exited abnormally", cmd);
                    459:                return -1;
                    460:        }
                    461:        debug3("command returned status %d", WEXITSTATUS(status));
                    462:        return WEXITSTATUS(status);
                    463: }
                    464:
                    465: /*
                    466:  * Parse and execute a Match directive.
                    467:  */
                    468: static int
                    469: match_cfg_line(Options *options, char **condition, struct passwd *pw,
                    470:     const char *host_arg, const char *filename, int linenum)
                    471: {
1.211     djm       472:        char *arg, *attrib, *cmd, *cp = *condition, *host;
                    473:        const char *ruser;
1.213     dtucker   474:        int r, port, result = 1, attributes = 0;
1.206     djm       475:        size_t len;
                    476:        char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
                    477:
                    478:        /*
                    479:         * Configuration is likely to be incomplete at this point so we
                    480:         * must be prepared to use default values.
                    481:         */
                    482:        port = options->port <= 0 ? default_ssh_port() : options->port;
                    483:        ruser = options->user == NULL ? pw->pw_name : options->user;
1.211     djm       484:        if (options->hostname != NULL) {
1.212     djm       485:                /* NB. Please keep in sync with ssh.c:main() */
1.211     djm       486:                host = percent_expand(options->hostname,
                    487:                    "h", host_arg, (char *)NULL);
                    488:        } else
                    489:                host = xstrdup(host_arg);
1.206     djm       490:
                    491:        debug3("checking match for '%s' host %s", cp, host);
                    492:        while ((attrib = strdelim(&cp)) && *attrib != '\0') {
1.213     dtucker   493:                attributes++;
                    494:                if (strcasecmp(attrib, "all") == 0) {
                    495:                        if (attributes != 1 ||
                    496:                            ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
                    497:                                error("'all' cannot be combined with other "
                    498:                                    "Match attributes");
                    499:                                result = -1;
                    500:                                goto out;
                    501:                        }
                    502:                        *condition = cp;
                    503:                        result = 1;
                    504:                        goto out;
                    505:                }
1.206     djm       506:                if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
                    507:                        error("Missing Match criteria for %s", attrib);
1.211     djm       508:                        result = -1;
                    509:                        goto out;
1.206     djm       510:                }
                    511:                len = strlen(arg);
                    512:                if (strcasecmp(attrib, "host") == 0) {
                    513:                        if (match_hostname(host, arg, len) != 1)
                    514:                                result = 0;
                    515:                        else
                    516:                                debug("%.200s line %d: matched 'Host %.100s' ",
                    517:                                    filename, linenum, host);
                    518:                } else if (strcasecmp(attrib, "originalhost") == 0) {
                    519:                        if (match_hostname(host_arg, arg, len) != 1)
                    520:                                result = 0;
                    521:                        else
                    522:                                debug("%.200s line %d: matched "
                    523:                                    "'OriginalHost %.100s' ",
                    524:                                    filename, linenum, host_arg);
                    525:                } else if (strcasecmp(attrib, "user") == 0) {
                    526:                        if (match_pattern_list(ruser, arg, len, 0) != 1)
                    527:                                result = 0;
                    528:                        else
                    529:                                debug("%.200s line %d: matched 'User %.100s' ",
                    530:                                    filename, linenum, ruser);
                    531:                } else if (strcasecmp(attrib, "localuser") == 0) {
                    532:                        if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
                    533:                                result = 0;
                    534:                        else
                    535:                                debug("%.200s line %d: matched "
                    536:                                    "'LocalUser %.100s' ",
                    537:                                    filename, linenum, pw->pw_name);
1.210     djm       538:                } else if (strcasecmp(attrib, "exec") == 0) {
1.206     djm       539:                        if (gethostname(thishost, sizeof(thishost)) == -1)
                    540:                                fatal("gethostname: %s", strerror(errno));
                    541:                        strlcpy(shorthost, thishost, sizeof(shorthost));
                    542:                        shorthost[strcspn(thishost, ".")] = '\0';
                    543:                        snprintf(portstr, sizeof(portstr), "%d", port);
                    544:
                    545:                        cmd = percent_expand(arg,
                    546:                            "L", shorthost,
                    547:                            "d", pw->pw_dir,
                    548:                            "h", host,
                    549:                            "l", thishost,
                    550:                            "n", host_arg,
                    551:                            "p", portstr,
                    552:                            "r", ruser,
                    553:                            "u", pw->pw_name,
                    554:                            (char *)NULL);
1.217     djm       555:                        if (result != 1) {
                    556:                                /* skip execution if prior predicate failed */
                    557:                                debug("%.200s line %d: skipped exec \"%.100s\"",
1.206     djm       558:                                    filename, linenum, cmd);
1.217     djm       559:                        } else {
                    560:                                r = execute_in_shell(cmd);
                    561:                                if (r == -1) {
                    562:                                        fatal("%.200s line %d: match exec "
                    563:                                            "'%.100s' error", filename,
                    564:                                            linenum, cmd);
                    565:                                } else if (r == 0) {
                    566:                                        debug("%.200s line %d: matched "
                    567:                                            "'exec \"%.100s\"'", filename,
                    568:                                            linenum, cmd);
                    569:                                } else {
                    570:                                        debug("%.200s line %d: no match "
                    571:                                            "'exec \"%.100s\"'", filename,
                    572:                                            linenum, cmd);
                    573:                                        result = 0;
                    574:                                }
                    575:                        }
1.206     djm       576:                        free(cmd);
                    577:                } else {
                    578:                        error("Unsupported Match attribute %s", attrib);
1.211     djm       579:                        result = -1;
                    580:                        goto out;
1.206     djm       581:                }
1.213     dtucker   582:        }
                    583:        if (attributes == 0) {
                    584:                error("One or more attributes required for Match");
                    585:                result = -1;
                    586:                goto out;
1.206     djm       587:        }
                    588:        debug3("match %sfound", result ? "" : "not ");
                    589:        *condition = cp;
1.211     djm       590:  out:
                    591:        free(host);
1.206     djm       592:        return result;
                    593: }
                    594:
1.208     djm       595: /* Check and prepare a domain name: removes trailing '.' and lowercases */
                    596: static void
                    597: valid_domain(char *name, const char *filename, int linenum)
                    598: {
                    599:        size_t i, l = strlen(name);
                    600:        u_char c, last = '\0';
                    601:
                    602:        if (l == 0)
                    603:                fatal("%s line %d: empty hostname suffix", filename, linenum);
                    604:        if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
                    605:                fatal("%s line %d: hostname suffix \"%.100s\" "
                    606:                    "starts with invalid character", filename, linenum, name);
                    607:        for (i = 0; i < l; i++) {
                    608:                c = tolower((u_char)name[i]);
                    609:                name[i] = (char)c;
                    610:                if (last == '.' && c == '.')
                    611:                        fatal("%s line %d: hostname suffix \"%.100s\" contains "
                    612:                            "consecutive separators", filename, linenum, name);
                    613:                if (c != '.' && c != '-' && !isalnum(c) &&
                    614:                    c != '_') /* technically invalid, but common */
                    615:                        fatal("%s line %d: hostname suffix \"%.100s\" contains "
                    616:                            "invalid characters", filename, linenum, name);
                    617:                last = c;
                    618:        }
                    619:        if (name[l - 1] == '.')
                    620:                name[l - 1] = '\0';
                    621: }
                    622:
1.19      markus    623: /*
1.70      stevesk   624:  * Returns the number of the token pointed to by cp or oBadOption.
1.19      markus    625:  */
1.26      markus    626: static OpCodes
1.199     djm       627: parse_token(const char *cp, const char *filename, int linenum,
                    628:     const char *ignored_unknown)
1.1       deraadt   629: {
1.199     djm       630:        int i;
1.1       deraadt   631:
1.17      markus    632:        for (i = 0; keywords[i].name; i++)
1.199     djm       633:                if (strcmp(cp, keywords[i].name) == 0)
1.17      markus    634:                        return keywords[i].opcode;
1.199     djm       635:        if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
                    636:            strlen(ignored_unknown), 1) == 1)
                    637:                return oIgnoredUnknownOption;
1.75      stevesk   638:        error("%s: line %d: Bad configuration option: %s",
                    639:            filename, linenum, cp);
1.17      markus    640:        return oBadOption;
1.1       deraadt   641: }
                    642:
1.207     djm       643: /* Multistate option parsing */
                    644: struct multistate {
                    645:        char *key;
                    646:        int value;
                    647: };
                    648: static const struct multistate multistate_flag[] = {
                    649:        { "true",                       1 },
                    650:        { "false",                      0 },
                    651:        { "yes",                        1 },
                    652:        { "no",                         0 },
                    653:        { NULL, -1 }
                    654: };
                    655: static const struct multistate multistate_yesnoask[] = {
                    656:        { "true",                       1 },
                    657:        { "false",                      0 },
                    658:        { "yes",                        1 },
                    659:        { "no",                         0 },
                    660:        { "ask",                        2 },
                    661:        { NULL, -1 }
                    662: };
                    663: static const struct multistate multistate_addressfamily[] = {
                    664:        { "inet",                       AF_INET },
                    665:        { "inet6",                      AF_INET6 },
                    666:        { "any",                        AF_UNSPEC },
                    667:        { NULL, -1 }
                    668: };
                    669: static const struct multistate multistate_controlmaster[] = {
                    670:        { "true",                       SSHCTL_MASTER_YES },
                    671:        { "yes",                        SSHCTL_MASTER_YES },
                    672:        { "false",                      SSHCTL_MASTER_NO },
                    673:        { "no",                         SSHCTL_MASTER_NO },
                    674:        { "auto",                       SSHCTL_MASTER_AUTO },
                    675:        { "ask",                        SSHCTL_MASTER_ASK },
                    676:        { "autoask",                    SSHCTL_MASTER_AUTO_ASK },
                    677:        { NULL, -1 }
                    678: };
                    679: static const struct multistate multistate_tunnel[] = {
                    680:        { "ethernet",                   SSH_TUNMODE_ETHERNET },
                    681:        { "point-to-point",             SSH_TUNMODE_POINTOPOINT },
                    682:        { "true",                       SSH_TUNMODE_DEFAULT },
                    683:        { "yes",                        SSH_TUNMODE_DEFAULT },
                    684:        { "false",                      SSH_TUNMODE_NO },
                    685:        { "no",                         SSH_TUNMODE_NO },
                    686:        { NULL, -1 }
                    687: };
                    688: static const struct multistate multistate_requesttty[] = {
                    689:        { "true",                       REQUEST_TTY_YES },
                    690:        { "yes",                        REQUEST_TTY_YES },
                    691:        { "false",                      REQUEST_TTY_NO },
                    692:        { "no",                         REQUEST_TTY_NO },
                    693:        { "force",                      REQUEST_TTY_FORCE },
                    694:        { "auto",                       REQUEST_TTY_AUTO },
                    695:        { NULL, -1 }
                    696: };
1.209     djm       697: static const struct multistate multistate_canonicalizehostname[] = {
1.208     djm       698:        { "true",                       SSH_CANONICALISE_YES },
                    699:        { "false",                      SSH_CANONICALISE_NO },
                    700:        { "yes",                        SSH_CANONICALISE_YES },
                    701:        { "no",                         SSH_CANONICALISE_NO },
                    702:        { "always",                     SSH_CANONICALISE_ALWAYS },
                    703:        { NULL, -1 }
                    704: };
1.207     djm       705:
1.19      markus    706: /*
                    707:  * Processes a single option line as used in the configuration files. This
                    708:  * only sets those values that have not already been set.
                    709:  */
1.102     markus    710: #define WHITESPACE " \t\r\n"
1.14      markus    711: int
1.206     djm       712: process_config_line(Options *options, struct passwd *pw, const char *host,
                    713:     char *line, const char *filename, int linenum, int *activep, int userconfig)
1.1       deraadt   714: {
1.193     djm       715:        char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
                    716:        char **cpptr, fwdarg[256];
1.199     djm       717:        u_int i, *uintptr, max_entries = 0;
1.206     djm       718:        int negated, opcode, *intptr, value, value2, cmdline = 0;
1.164     dtucker   719:        LogLevel *log_level_ptr;
1.201     dtucker   720:        long long val64;
1.102     markus    721:        size_t len;
1.220   ! millert   722:        struct Forward fwd;
1.207     djm       723:        const struct multistate *multistate_ptr;
1.208     djm       724:        struct allowed_cname *cname;
1.106     djm       725:
1.206     djm       726:        if (activep == NULL) { /* We are processing a command line directive */
                    727:                cmdline = 1;
                    728:                activep = &cmdline;
                    729:        }
                    730:
1.106     djm       731:        /* Strip trailing whitespace */
1.139     deraadt   732:        for (len = strlen(line) - 1; len > 0; len--) {
1.106     djm       733:                if (strchr(WHITESPACE, line[len]) == NULL)
                    734:                        break;
                    735:                line[len] = '\0';
                    736:        }
1.1       deraadt   737:
1.42      provos    738:        s = line;
                    739:        /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149     djm       740:        if ((keyword = strdelim(&s)) == NULL)
                    741:                return 0;
1.42      provos    742:        /* Ignore leading whitespace. */
                    743:        if (*keyword == '\0')
1.43      markus    744:                keyword = strdelim(&s);
1.56      deraadt   745:        if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17      markus    746:                return 0;
1.199     djm       747:        /* Match lowercase keyword */
1.207     djm       748:        lowercase(keyword);
1.17      markus    749:
1.199     djm       750:        opcode = parse_token(keyword, filename, linenum,
                    751:            options->ignored_unknown);
1.17      markus    752:
                    753:        switch (opcode) {
                    754:        case oBadOption:
1.19      markus    755:                /* don't panic, but count bad options */
                    756:                return -1;
1.17      markus    757:                /* NOTREACHED */
1.199     djm       758:        case oIgnoredUnknownOption:
                    759:                debug("%s line %d: Ignored unknown option \"%s\"",
                    760:                    filename, linenum, keyword);
                    761:                return 0;
1.111     djm       762:        case oConnectTimeout:
                    763:                intptr = &options->connection_timeout;
1.127     markus    764: parse_time:
1.111     djm       765:                arg = strdelim(&s);
                    766:                if (!arg || *arg == '\0')
                    767:                        fatal("%s line %d: missing time value.",
                    768:                            filename, linenum);
                    769:                if ((value = convtime(arg)) == -1)
                    770:                        fatal("%s line %d: invalid time value.",
                    771:                            filename, linenum);
1.160     dtucker   772:                if (*activep && *intptr == -1)
1.111     djm       773:                        *intptr = value;
                    774:                break;
                    775:
1.17      markus    776:        case oForwardAgent:
                    777:                intptr = &options->forward_agent;
1.207     djm       778:  parse_flag:
                    779:                multistate_ptr = multistate_flag;
                    780:  parse_multistate:
1.42      provos    781:                arg = strdelim(&s);
1.40      ho        782:                if (!arg || *arg == '\0')
1.207     djm       783:                        fatal("%s line %d: missing argument.",
                    784:                            filename, linenum);
                    785:                value = -1;
                    786:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                    787:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                    788:                                value = multistate_ptr[i].value;
                    789:                                break;
                    790:                        }
                    791:                }
                    792:                if (value == -1)
                    793:                        fatal("%s line %d: unsupported option \"%s\".",
                    794:                            filename, linenum, arg);
1.17      markus    795:                if (*activep && *intptr == -1)
                    796:                        *intptr = value;
                    797:                break;
                    798:
                    799:        case oForwardX11:
                    800:                intptr = &options->forward_x11;
                    801:                goto parse_flag;
                    802:
1.123     markus    803:        case oForwardX11Trusted:
                    804:                intptr = &options->forward_x11_trusted;
                    805:                goto parse_flag;
1.186     djm       806:
                    807:        case oForwardX11Timeout:
                    808:                intptr = &options->forward_x11_timeout;
                    809:                goto parse_time;
1.123     markus    810:
1.17      markus    811:        case oGatewayPorts:
1.220   ! millert   812:                intptr = &options->fwd_opts.gateway_ports;
1.17      markus    813:                goto parse_flag;
                    814:
1.153     markus    815:        case oExitOnForwardFailure:
                    816:                intptr = &options->exit_on_forward_failure;
                    817:                goto parse_flag;
                    818:
1.17      markus    819:        case oUsePrivilegedPort:
                    820:                intptr = &options->use_privileged_port;
                    821:                goto parse_flag;
                    822:
                    823:        case oPasswordAuthentication:
                    824:                intptr = &options->password_authentication;
                    825:                goto parse_flag;
                    826:
1.48      markus    827:        case oKbdInteractiveAuthentication:
                    828:                intptr = &options->kbd_interactive_authentication;
                    829:                goto parse_flag;
                    830:
                    831:        case oKbdInteractiveDevices:
                    832:                charptr = &options->kbd_interactive_devices;
                    833:                goto parse_string;
                    834:
1.50      markus    835:        case oPubkeyAuthentication:
                    836:                intptr = &options->pubkey_authentication;
1.30      markus    837:                goto parse_flag;
                    838:
1.17      markus    839:        case oRSAAuthentication:
                    840:                intptr = &options->rsa_authentication;
                    841:                goto parse_flag;
                    842:
                    843:        case oRhostsRSAAuthentication:
                    844:                intptr = &options->rhosts_rsa_authentication;
                    845:                goto parse_flag;
                    846:
1.72      markus    847:        case oHostbasedAuthentication:
                    848:                intptr = &options->hostbased_authentication;
                    849:                goto parse_flag;
                    850:
1.59      markus    851:        case oChallengeResponseAuthentication:
1.78      markus    852:                intptr = &options->challenge_response_authentication;
1.17      markus    853:                goto parse_flag;
1.108     jakob     854:
1.118     markus    855:        case oGssAuthentication:
                    856:                intptr = &options->gss_authentication;
                    857:                goto parse_flag;
                    858:
                    859:        case oGssDelegateCreds:
                    860:                intptr = &options->gss_deleg_creds;
                    861:                goto parse_flag;
                    862:
1.17      markus    863:        case oBatchMode:
                    864:                intptr = &options->batch_mode;
                    865:                goto parse_flag;
                    866:
                    867:        case oCheckHostIP:
                    868:                intptr = &options->check_host_ip;
1.167     grunk     869:                goto parse_flag;
1.17      markus    870:
1.107     jakob     871:        case oVerifyHostKeyDNS:
                    872:                intptr = &options->verify_host_key_dns;
1.207     djm       873:                multistate_ptr = multistate_yesnoask;
                    874:                goto parse_multistate;
1.107     jakob     875:
1.17      markus    876:        case oStrictHostKeyChecking:
                    877:                intptr = &options->strict_host_key_checking;
1.207     djm       878:                multistate_ptr = multistate_yesnoask;
                    879:                goto parse_multistate;
1.17      markus    880:
                    881:        case oCompression:
                    882:                intptr = &options->compression;
                    883:                goto parse_flag;
                    884:
1.126     markus    885:        case oTCPKeepAlive:
                    886:                intptr = &options->tcp_keep_alive;
1.17      markus    887:                goto parse_flag;
                    888:
1.91      markus    889:        case oNoHostAuthenticationForLocalhost:
                    890:                intptr = &options->no_host_authentication_for_localhost;
                    891:                goto parse_flag;
                    892:
1.17      markus    893:        case oNumberOfPasswordPrompts:
                    894:                intptr = &options->number_of_password_prompts;
                    895:                goto parse_int;
                    896:
                    897:        case oCompressionLevel:
                    898:                intptr = &options->compression_level;
                    899:                goto parse_int;
                    900:
1.105     markus    901:        case oRekeyLimit:
                    902:                arg = strdelim(&s);
                    903:                if (!arg || *arg == '\0')
1.198     dtucker   904:                        fatal("%.200s line %d: Missing argument.", filename,
                    905:                            linenum);
                    906:                if (strcmp(arg, "default") == 0) {
                    907:                        val64 = 0;
                    908:                } else {
1.200     dtucker   909:                        if (scan_scaled(arg, &val64) == -1)
                    910:                                fatal("%.200s line %d: Bad number '%s': %s",
                    911:                                    filename, linenum, arg, strerror(errno));
                    912:                        /* check for too-large or too-small limits */
                    913:                        if (val64 > UINT_MAX)
1.198     dtucker   914:                                fatal("%.200s line %d: RekeyLimit too large",
                    915:                                    filename, linenum);
                    916:                        if (val64 != 0 && val64 < 16)
                    917:                                fatal("%.200s line %d: RekeyLimit too small",
                    918:                                    filename, linenum);
1.105     markus    919:                }
1.165     djm       920:                if (*activep && options->rekey_limit == -1)
                    921:                        options->rekey_limit = (u_int32_t)val64;
1.198     dtucker   922:                if (s != NULL) { /* optional rekey interval present */
                    923:                        if (strcmp(s, "none") == 0) {
                    924:                                (void)strdelim(&s);     /* discard */
                    925:                                break;
                    926:                        }
                    927:                        intptr = &options->rekey_interval;
                    928:                        goto parse_time;
                    929:                }
1.105     markus    930:                break;
                    931:
1.17      markus    932:        case oIdentityFile:
1.42      provos    933:                arg = strdelim(&s);
1.40      ho        934:                if (!arg || *arg == '\0')
1.17      markus    935:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                    936:                if (*activep) {
1.50      markus    937:                        intptr = &options->num_identity_files;
1.27      markus    938:                        if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17      markus    939:                                fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93      deraadt   940:                                    filename, linenum, SSH_MAX_IDENTITY_FILES);
1.196     dtucker   941:                        add_identity_file(options, NULL, arg, userconfig);
1.17      markus    942:                }
                    943:                break;
                    944:
1.34      markus    945:        case oXAuthLocation:
                    946:                charptr=&options->xauth_location;
                    947:                goto parse_string;
                    948:
1.17      markus    949:        case oUser:
                    950:                charptr = &options->user;
                    951: parse_string:
1.42      provos    952:                arg = strdelim(&s);
1.40      ho        953:                if (!arg || *arg == '\0')
1.193     djm       954:                        fatal("%.200s line %d: Missing argument.",
                    955:                            filename, linenum);
1.17      markus    956:                if (*activep && *charptr == NULL)
1.38      provos    957:                        *charptr = xstrdup(arg);
1.17      markus    958:                break;
                    959:
                    960:        case oGlobalKnownHostsFile:
1.193     djm       961:                cpptr = (char **)&options->system_hostfiles;
                    962:                uintptr = &options->num_system_hostfiles;
                    963:                max_entries = SSH_MAX_HOSTS_FILES;
                    964: parse_char_array:
                    965:                if (*activep && *uintptr == 0) {
                    966:                        while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                    967:                                if ((*uintptr) >= max_entries)
                    968:                                        fatal("%s line %d: "
                    969:                                            "too many authorized keys files.",
                    970:                                            filename, linenum);
                    971:                                cpptr[(*uintptr)++] = xstrdup(arg);
                    972:                        }
                    973:                }
                    974:                return 0;
1.17      markus    975:
                    976:        case oUserKnownHostsFile:
1.193     djm       977:                cpptr = (char **)&options->user_hostfiles;
                    978:                uintptr = &options->num_user_hostfiles;
                    979:                max_entries = SSH_MAX_HOSTS_FILES;
                    980:                goto parse_char_array;
1.27      markus    981:
1.17      markus    982:        case oHostName:
                    983:                charptr = &options->hostname;
                    984:                goto parse_string;
                    985:
1.52      markus    986:        case oHostKeyAlias:
                    987:                charptr = &options->host_key_alias;
                    988:                goto parse_string;
                    989:
1.67      markus    990:        case oPreferredAuthentications:
                    991:                charptr = &options->preferred_authentications;
                    992:                goto parse_string;
                    993:
1.77      markus    994:        case oBindAddress:
                    995:                charptr = &options->bind_address;
                    996:                goto parse_string;
                    997:
1.183     markus    998:        case oPKCS11Provider:
                    999:                charptr = &options->pkcs11_provider;
1.86      markus   1000:                goto parse_string;
1.85      jakob    1001:
1.17      markus   1002:        case oProxyCommand:
1.144     reyk     1003:                charptr = &options->proxy_command;
                   1004: parse_command:
1.113     markus   1005:                if (s == NULL)
                   1006:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102     markus   1007:                len = strspn(s, WHITESPACE "=");
1.17      markus   1008:                if (*activep && *charptr == NULL)
1.102     markus   1009:                        *charptr = xstrdup(s + len);
1.17      markus   1010:                return 0;
                   1011:
                   1012:        case oPort:
                   1013:                intptr = &options->port;
                   1014: parse_int:
1.42      provos   1015:                arg = strdelim(&s);
1.40      ho       1016:                if (!arg || *arg == '\0')
1.17      markus   1017:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1018:                if (arg[0] < '0' || arg[0] > '9')
1.17      markus   1019:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.21      markus   1020:
                   1021:                /* Octal, decimal, or hex format? */
1.38      provos   1022:                value = strtol(arg, &endofnumber, 0);
                   1023:                if (arg == endofnumber)
1.21      markus   1024:                        fatal("%.200s line %d: Bad number.", filename, linenum);
1.17      markus   1025:                if (*activep && *intptr == -1)
                   1026:                        *intptr = value;
                   1027:                break;
                   1028:
                   1029:        case oConnectionAttempts:
                   1030:                intptr = &options->connection_attempts;
                   1031:                goto parse_int;
                   1032:
                   1033:        case oCipher:
                   1034:                intptr = &options->cipher;
1.42      provos   1035:                arg = strdelim(&s);
1.40      ho       1036:                if (!arg || *arg == '\0')
1.32      markus   1037:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1038:                value = cipher_number(arg);
1.17      markus   1039:                if (value == -1)
                   1040:                        fatal("%.200s line %d: Bad cipher '%s'.",
1.93      deraadt  1041:                            filename, linenum, arg ? arg : "<NONE>");
1.17      markus   1042:                if (*activep && *intptr == -1)
                   1043:                        *intptr = value;
                   1044:                break;
                   1045:
1.25      markus   1046:        case oCiphers:
1.42      provos   1047:                arg = strdelim(&s);
1.40      ho       1048:                if (!arg || *arg == '\0')
1.32      markus   1049:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1050:                if (!ciphers_valid(arg))
1.31      markus   1051:                        fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93      deraadt  1052:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1053:                if (*activep && options->ciphers == NULL)
1.38      provos   1054:                        options->ciphers = xstrdup(arg);
1.25      markus   1055:                break;
                   1056:
1.62      markus   1057:        case oMacs:
                   1058:                arg = strdelim(&s);
                   1059:                if (!arg || *arg == '\0')
                   1060:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1061:                if (!mac_valid(arg))
                   1062:                        fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93      deraadt  1063:                            filename, linenum, arg ? arg : "<NONE>");
1.62      markus   1064:                if (*activep && options->macs == NULL)
                   1065:                        options->macs = xstrdup(arg);
                   1066:                break;
                   1067:
1.189     djm      1068:        case oKexAlgorithms:
                   1069:                arg = strdelim(&s);
                   1070:                if (!arg || *arg == '\0')
                   1071:                        fatal("%.200s line %d: Missing argument.",
                   1072:                            filename, linenum);
                   1073:                if (!kex_names_valid(arg))
                   1074:                        fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
                   1075:                            filename, linenum, arg ? arg : "<NONE>");
                   1076:                if (*activep && options->kex_algorithms == NULL)
                   1077:                        options->kex_algorithms = xstrdup(arg);
                   1078:                break;
                   1079:
1.76      markus   1080:        case oHostKeyAlgorithms:
                   1081:                arg = strdelim(&s);
                   1082:                if (!arg || *arg == '\0')
                   1083:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1084:                if (!key_names_valid2(arg))
                   1085:                        fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93      deraadt  1086:                            filename, linenum, arg ? arg : "<NONE>");
1.76      markus   1087:                if (*activep && options->hostkeyalgorithms == NULL)
                   1088:                        options->hostkeyalgorithms = xstrdup(arg);
                   1089:                break;
                   1090:
1.25      markus   1091:        case oProtocol:
                   1092:                intptr = &options->protocol;
1.42      provos   1093:                arg = strdelim(&s);
1.40      ho       1094:                if (!arg || *arg == '\0')
1.32      markus   1095:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1096:                value = proto_spec(arg);
1.25      markus   1097:                if (value == SSH_PROTO_UNKNOWN)
                   1098:                        fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93      deraadt  1099:                            filename, linenum, arg ? arg : "<NONE>");
1.25      markus   1100:                if (*activep && *intptr == SSH_PROTO_UNKNOWN)
                   1101:                        *intptr = value;
                   1102:                break;
                   1103:
1.17      markus   1104:        case oLogLevel:
1.164     dtucker  1105:                log_level_ptr = &options->log_level;
1.42      provos   1106:                arg = strdelim(&s);
1.38      provos   1107:                value = log_level_number(arg);
1.95      markus   1108:                if (value == SYSLOG_LEVEL_NOT_SET)
1.64      millert  1109:                        fatal("%.200s line %d: unsupported log level '%s'",
1.93      deraadt  1110:                            filename, linenum, arg ? arg : "<NONE>");
1.164     dtucker  1111:                if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
                   1112:                        *log_level_ptr = (LogLevel) value;
1.17      markus   1113:                break;
                   1114:
1.88      stevesk  1115:        case oLocalForward:
1.17      markus   1116:        case oRemoteForward:
1.168     stevesk  1117:        case oDynamicForward:
1.42      provos   1118:                arg = strdelim(&s);
1.135     djm      1119:                if (arg == NULL || *arg == '\0')
1.88      stevesk  1120:                        fatal("%.200s line %d: Missing port argument.",
                   1121:                            filename, linenum);
1.135     djm      1122:
1.168     stevesk  1123:                if (opcode == oLocalForward ||
                   1124:                    opcode == oRemoteForward) {
                   1125:                        arg2 = strdelim(&s);
                   1126:                        if (arg2 == NULL || *arg2 == '\0')
                   1127:                                fatal("%.200s line %d: Missing target argument.",
                   1128:                                    filename, linenum);
1.135     djm      1129:
1.168     stevesk  1130:                        /* construct a string for parse_forward */
                   1131:                        snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
                   1132:                } else if (opcode == oDynamicForward) {
                   1133:                        strlcpy(fwdarg, arg, sizeof(fwdarg));
                   1134:                }
                   1135:
                   1136:                if (parse_forward(&fwd, fwdarg,
1.176     djm      1137:                    opcode == oDynamicForward ? 1 : 0,
                   1138:                    opcode == oRemoteForward ? 1 : 0) == 0)
1.88      stevesk  1139:                        fatal("%.200s line %d: Bad forwarding specification.",
                   1140:                            filename, linenum);
1.135     djm      1141:
1.88      stevesk  1142:                if (*activep) {
1.168     stevesk  1143:                        if (opcode == oLocalForward ||
                   1144:                            opcode == oDynamicForward)
1.135     djm      1145:                                add_local_forward(options, &fwd);
1.88      stevesk  1146:                        else if (opcode == oRemoteForward)
1.135     djm      1147:                                add_remote_forward(options, &fwd);
1.88      stevesk  1148:                }
1.17      markus   1149:                break;
1.71      markus   1150:
1.90      stevesk  1151:        case oClearAllForwardings:
                   1152:                intptr = &options->clear_forwardings;
                   1153:                goto parse_flag;
                   1154:
1.17      markus   1155:        case oHost:
1.206     djm      1156:                if (cmdline)
                   1157:                        fatal("Host directive not supported as a command-line "
                   1158:                            "option");
1.17      markus   1159:                *activep = 0;
1.191     djm      1160:                arg2 = NULL;
                   1161:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1162:                        negated = *arg == '!';
                   1163:                        if (negated)
                   1164:                                arg++;
1.38      provos   1165:                        if (match_pattern(host, arg)) {
1.191     djm      1166:                                if (negated) {
                   1167:                                        debug("%.200s line %d: Skipping Host "
                   1168:                                            "block because of negated match "
                   1169:                                            "for %.100s", filename, linenum,
                   1170:                                            arg);
                   1171:                                        *activep = 0;
                   1172:                                        break;
                   1173:                                }
                   1174:                                if (!*activep)
                   1175:                                        arg2 = arg; /* logged below */
1.17      markus   1176:                                *activep = 1;
                   1177:                        }
1.191     djm      1178:                }
                   1179:                if (*activep)
                   1180:                        debug("%.200s line %d: Applying options for %.100s",
                   1181:                            filename, linenum, arg2);
1.42      provos   1182:                /* Avoid garbage check below, as strdelim is done. */
1.17      markus   1183:                return 0;
                   1184:
1.206     djm      1185:        case oMatch:
                   1186:                if (cmdline)
                   1187:                        fatal("Host directive not supported as a command-line "
                   1188:                            "option");
                   1189:                value = match_cfg_line(options, &s, pw, host,
                   1190:                    filename, linenum);
                   1191:                if (value < 0)
                   1192:                        fatal("%.200s line %d: Bad Match condition", filename,
                   1193:                            linenum);
                   1194:                *activep = value;
                   1195:                break;
                   1196:
1.17      markus   1197:        case oEscapeChar:
                   1198:                intptr = &options->escape_char;
1.42      provos   1199:                arg = strdelim(&s);
1.40      ho       1200:                if (!arg || *arg == '\0')
1.17      markus   1201:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38      provos   1202:                if (arg[0] == '^' && arg[2] == 0 &&
1.51      markus   1203:                    (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
                   1204:                        value = (u_char) arg[1] & 31;
1.38      provos   1205:                else if (strlen(arg) == 1)
1.51      markus   1206:                        value = (u_char) arg[0];
1.38      provos   1207:                else if (strcmp(arg, "none") == 0)
1.79      stevesk  1208:                        value = SSH_ESCAPECHAR_NONE;
1.17      markus   1209:                else {
                   1210:                        fatal("%.200s line %d: Bad escape character.",
1.93      deraadt  1211:                            filename, linenum);
1.17      markus   1212:                        /* NOTREACHED */
                   1213:                        value = 0;      /* Avoid compiler warning. */
                   1214:                }
                   1215:                if (*activep && *intptr == -1)
                   1216:                        *intptr = value;
1.112     djm      1217:                break;
                   1218:
                   1219:        case oAddressFamily:
1.114     djm      1220:                intptr = &options->address_family;
1.207     djm      1221:                multistate_ptr = multistate_addressfamily;
                   1222:                goto parse_multistate;
1.17      markus   1223:
1.101     markus   1224:        case oEnableSSHKeysign:
                   1225:                intptr = &options->enable_ssh_keysign;
                   1226:                goto parse_flag;
                   1227:
1.128     markus   1228:        case oIdentitiesOnly:
                   1229:                intptr = &options->identities_only;
                   1230:                goto parse_flag;
                   1231:
1.127     markus   1232:        case oServerAliveInterval:
                   1233:                intptr = &options->server_alive_interval;
                   1234:                goto parse_time;
                   1235:
                   1236:        case oServerAliveCountMax:
                   1237:                intptr = &options->server_alive_count_max;
                   1238:                goto parse_int;
                   1239:
1.130     djm      1240:        case oSendEnv:
                   1241:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1242:                        if (strchr(arg, '=') != NULL)
                   1243:                                fatal("%s line %d: Invalid environment name.",
                   1244:                                    filename, linenum);
1.137     djm      1245:                        if (!*activep)
                   1246:                                continue;
1.130     djm      1247:                        if (options->num_send_env >= MAX_SEND_ENV)
                   1248:                                fatal("%s line %d: too many send env.",
                   1249:                                    filename, linenum);
                   1250:                        options->send_env[options->num_send_env++] =
                   1251:                            xstrdup(arg);
                   1252:                }
                   1253:                break;
                   1254:
1.132     djm      1255:        case oControlPath:
                   1256:                charptr = &options->control_path;
                   1257:                goto parse_string;
                   1258:
                   1259:        case oControlMaster:
                   1260:                intptr = &options->control_master;
1.207     djm      1261:                multistate_ptr = multistate_controlmaster;
                   1262:                goto parse_multistate;
1.132     djm      1263:
1.187     djm      1264:        case oControlPersist:
                   1265:                /* no/false/yes/true, or a time spec */
                   1266:                intptr = &options->control_persist;
                   1267:                arg = strdelim(&s);
                   1268:                if (!arg || *arg == '\0')
                   1269:                        fatal("%.200s line %d: Missing ControlPersist"
                   1270:                            " argument.", filename, linenum);
                   1271:                value = 0;
                   1272:                value2 = 0;     /* timeout */
                   1273:                if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
                   1274:                        value = 0;
                   1275:                else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
                   1276:                        value = 1;
                   1277:                else if ((value2 = convtime(arg)) >= 0)
                   1278:                        value = 1;
                   1279:                else
                   1280:                        fatal("%.200s line %d: Bad ControlPersist argument.",
                   1281:                            filename, linenum);
                   1282:                if (*activep && *intptr == -1) {
                   1283:                        *intptr = value;
                   1284:                        options->control_persist_timeout = value2;
                   1285:                }
                   1286:                break;
                   1287:
1.136     djm      1288:        case oHashKnownHosts:
                   1289:                intptr = &options->hash_known_hosts;
                   1290:                goto parse_flag;
                   1291:
1.144     reyk     1292:        case oTunnel:
                   1293:                intptr = &options->tun_open;
1.207     djm      1294:                multistate_ptr = multistate_tunnel;
                   1295:                goto parse_multistate;
1.144     reyk     1296:
                   1297:        case oTunnelDevice:
                   1298:                arg = strdelim(&s);
                   1299:                if (!arg || *arg == '\0')
                   1300:                        fatal("%.200s line %d: Missing argument.", filename, linenum);
                   1301:                value = a2tun(arg, &value2);
1.145     reyk     1302:                if (value == SSH_TUNID_ERR)
1.144     reyk     1303:                        fatal("%.200s line %d: Bad tun device.", filename, linenum);
                   1304:                if (*activep) {
                   1305:                        options->tun_local = value;
                   1306:                        options->tun_remote = value2;
                   1307:                }
                   1308:                break;
                   1309:
                   1310:        case oLocalCommand:
                   1311:                charptr = &options->local_command;
                   1312:                goto parse_command;
                   1313:
                   1314:        case oPermitLocalCommand:
                   1315:                intptr = &options->permit_local_command;
                   1316:                goto parse_flag;
                   1317:
1.167     grunk    1318:        case oVisualHostKey:
                   1319:                intptr = &options->visual_host_key;
                   1320:                goto parse_flag;
                   1321:
1.190     djm      1322:        case oIPQoS:
                   1323:                arg = strdelim(&s);
                   1324:                if ((value = parse_ipqos(arg)) == -1)
                   1325:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1326:                            filename, linenum, arg);
                   1327:                arg = strdelim(&s);
                   1328:                if (arg == NULL)
                   1329:                        value2 = value;
                   1330:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1331:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1332:                            filename, linenum, arg);
                   1333:                if (*activep) {
                   1334:                        options->ip_qos_interactive = value;
                   1335:                        options->ip_qos_bulk = value2;
                   1336:                }
                   1337:                break;
                   1338:
1.177     andreas  1339:        case oUseRoaming:
                   1340:                intptr = &options->use_roaming;
                   1341:                goto parse_flag;
                   1342:
1.192     djm      1343:        case oRequestTTY:
                   1344:                intptr = &options->request_tty;
1.207     djm      1345:                multistate_ptr = multistate_requesttty;
                   1346:                goto parse_multistate;
1.192     djm      1347:
1.199     djm      1348:        case oIgnoreUnknown:
                   1349:                charptr = &options->ignored_unknown;
                   1350:                goto parse_string;
                   1351:
1.205     djm      1352:        case oProxyUseFdpass:
                   1353:                intptr = &options->proxy_use_fdpass;
                   1354:                goto parse_flag;
                   1355:
1.208     djm      1356:        case oCanonicalDomains:
                   1357:                value = options->num_canonical_domains != 0;
                   1358:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1359:                        valid_domain(arg, filename, linenum);
                   1360:                        if (!*activep || value)
                   1361:                                continue;
                   1362:                        if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
                   1363:                                fatal("%s line %d: too many hostname suffixes.",
                   1364:                                    filename, linenum);
                   1365:                        options->canonical_domains[
                   1366:                            options->num_canonical_domains++] = xstrdup(arg);
                   1367:                }
                   1368:                break;
                   1369:
1.209     djm      1370:        case oCanonicalizePermittedCNAMEs:
1.208     djm      1371:                value = options->num_permitted_cnames != 0;
                   1372:                while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
                   1373:                        /* Either '*' for everything or 'list:list' */
                   1374:                        if (strcmp(arg, "*") == 0)
                   1375:                                arg2 = arg;
                   1376:                        else {
                   1377:                                lowercase(arg);
                   1378:                                if ((arg2 = strchr(arg, ':')) == NULL ||
                   1379:                                    arg2[1] == '\0') {
                   1380:                                        fatal("%s line %d: "
                   1381:                                            "Invalid permitted CNAME \"%s\"",
                   1382:                                            filename, linenum, arg);
                   1383:                                }
                   1384:                                *arg2 = '\0';
                   1385:                                arg2++;
                   1386:                        }
                   1387:                        if (!*activep || value)
                   1388:                                continue;
                   1389:                        if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
                   1390:                                fatal("%s line %d: too many permitted CNAMEs.",
                   1391:                                    filename, linenum);
                   1392:                        cname = options->permitted_cnames +
                   1393:                            options->num_permitted_cnames++;
                   1394:                        cname->source_list = xstrdup(arg);
                   1395:                        cname->target_list = xstrdup(arg2);
                   1396:                }
                   1397:                break;
                   1398:
1.209     djm      1399:        case oCanonicalizeHostname:
                   1400:                intptr = &options->canonicalize_hostname;
                   1401:                multistate_ptr = multistate_canonicalizehostname;
1.208     djm      1402:                goto parse_multistate;
                   1403:
1.209     djm      1404:        case oCanonicalizeMaxDots:
                   1405:                intptr = &options->canonicalize_max_dots;
1.208     djm      1406:                goto parse_int;
                   1407:
1.209     djm      1408:        case oCanonicalizeFallbackLocal:
                   1409:                intptr = &options->canonicalize_fallback_local;
1.208     djm      1410:                goto parse_flag;
                   1411:
1.220   ! millert  1412:        case oStreamLocalBindMask:
        !          1413:                arg = strdelim(&s);
        !          1414:                if (!arg || *arg == '\0')
        !          1415:                        fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
        !          1416:                /* Parse mode in octal format */
        !          1417:                value = strtol(arg, &endofnumber, 8);
        !          1418:                if (arg == endofnumber || value < 0 || value > 0777)
        !          1419:                        fatal("%.200s line %d: Bad mask.", filename, linenum);
        !          1420:                options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
        !          1421:                break;
        !          1422:
        !          1423:        case oStreamLocalBindUnlink:
        !          1424:                intptr = &options->fwd_opts.streamlocal_bind_unlink;
        !          1425:                goto parse_flag;
        !          1426:
1.96      markus   1427:        case oDeprecated:
1.98      markus   1428:                debug("%s line %d: Deprecated option \"%s\"",
1.96      markus   1429:                    filename, linenum, keyword);
1.98      markus   1430:                return 0;
1.96      markus   1431:
1.110     jakob    1432:        case oUnsupported:
                   1433:                error("%s line %d: Unsupported option \"%s\"",
                   1434:                    filename, linenum, keyword);
                   1435:                return 0;
                   1436:
1.17      markus   1437:        default:
                   1438:                fatal("process_config_line: Unimplemented opcode %d", opcode);
                   1439:        }
                   1440:
                   1441:        /* Check that there is no garbage at end of line. */
1.57      djm      1442:        if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39      ho       1443:                fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142     djm      1444:                    filename, linenum, arg);
1.39      ho       1445:        }
1.17      markus   1446:        return 0;
1.1       deraadt  1447: }
                   1448:
                   1449:
1.19      markus   1450: /*
                   1451:  * Reads the config file and modifies the options accordingly.  Options
                   1452:  * should already be initialized before this call.  This never returns if
1.89      stevesk  1453:  * there is an error.  If the file does not exist, this returns 0.
1.19      markus   1454:  */
1.1       deraadt  1455:
1.89      stevesk  1456: int
1.206     djm      1457: read_config_file(const char *filename, struct passwd *pw, const char *host,
                   1458:     Options *options, int flags)
1.1       deraadt  1459: {
1.17      markus   1460:        FILE *f;
                   1461:        char line[1024];
                   1462:        int active, linenum;
                   1463:        int bad_options = 0;
                   1464:
1.129     djm      1465:        if ((f = fopen(filename, "r")) == NULL)
1.89      stevesk  1466:                return 0;
1.129     djm      1467:
1.196     dtucker  1468:        if (flags & SSHCONF_CHECKPERM) {
1.129     djm      1469:                struct stat sb;
1.134     deraadt  1470:
1.131     dtucker  1471:                if (fstat(fileno(f), &sb) == -1)
1.129     djm      1472:                        fatal("fstat %s: %s", filename, strerror(errno));
                   1473:                if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131     dtucker  1474:                    (sb.st_mode & 022) != 0))
1.129     djm      1475:                        fatal("Bad owner or permissions on %s", filename);
                   1476:        }
1.17      markus   1477:
                   1478:        debug("Reading configuration data %.200s", filename);
                   1479:
1.19      markus   1480:        /*
                   1481:         * Mark that we are now processing the options.  This flag is turned
                   1482:         * on/off by Host specifications.
                   1483:         */
1.17      markus   1484:        active = 1;
                   1485:        linenum = 0;
                   1486:        while (fgets(line, sizeof(line), f)) {
                   1487:                /* Update line number counter. */
                   1488:                linenum++;
1.206     djm      1489:                if (process_config_line(options, pw, host, line, filename,
                   1490:                    linenum, &active, flags & SSHCONF_USERCONF) != 0)
1.17      markus   1491:                        bad_options++;
                   1492:        }
                   1493:        fclose(f);
                   1494:        if (bad_options > 0)
1.64      millert  1495:                fatal("%s: terminating, %d bad configuration options",
1.93      deraadt  1496:                    filename, bad_options);
1.89      stevesk  1497:        return 1;
1.1       deraadt  1498: }
                   1499:
1.218     djm      1500: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                   1501: int
                   1502: option_clear_or_none(const char *o)
                   1503: {
                   1504:        return o == NULL || strcasecmp(o, "none") == 0;
                   1505: }
                   1506:
1.19      markus   1507: /*
                   1508:  * Initializes options to special values that indicate that they have not yet
                   1509:  * been set.  Read_config_file will only set options with this value. Options
                   1510:  * are processed in the following order: command line, user config file,
                   1511:  * system config file.  Last, fill_default_options is called.
                   1512:  */
1.1       deraadt  1513:
1.26      markus   1514: void
1.17      markus   1515: initialize_options(Options * options)
1.1       deraadt  1516: {
1.17      markus   1517:        memset(options, 'X', sizeof(*options));
                   1518:        options->forward_agent = -1;
                   1519:        options->forward_x11 = -1;
1.123     markus   1520:        options->forward_x11_trusted = -1;
1.186     djm      1521:        options->forward_x11_timeout = -1;
1.153     markus   1522:        options->exit_on_forward_failure = -1;
1.34      markus   1523:        options->xauth_location = NULL;
1.220   ! millert  1524:        options->fwd_opts.gateway_ports = -1;
        !          1525:        options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
        !          1526:        options->fwd_opts.streamlocal_bind_unlink = -1;
1.17      markus   1527:        options->use_privileged_port = -1;
                   1528:        options->rsa_authentication = -1;
1.50      markus   1529:        options->pubkey_authentication = -1;
1.78      markus   1530:        options->challenge_response_authentication = -1;
1.118     markus   1531:        options->gss_authentication = -1;
                   1532:        options->gss_deleg_creds = -1;
1.17      markus   1533:        options->password_authentication = -1;
1.48      markus   1534:        options->kbd_interactive_authentication = -1;
                   1535:        options->kbd_interactive_devices = NULL;
1.17      markus   1536:        options->rhosts_rsa_authentication = -1;
1.72      markus   1537:        options->hostbased_authentication = -1;
1.17      markus   1538:        options->batch_mode = -1;
                   1539:        options->check_host_ip = -1;
                   1540:        options->strict_host_key_checking = -1;
                   1541:        options->compression = -1;
1.126     markus   1542:        options->tcp_keep_alive = -1;
1.17      markus   1543:        options->compression_level = -1;
                   1544:        options->port = -1;
1.114     djm      1545:        options->address_family = -1;
1.17      markus   1546:        options->connection_attempts = -1;
1.111     djm      1547:        options->connection_timeout = -1;
1.17      markus   1548:        options->number_of_password_prompts = -1;
                   1549:        options->cipher = -1;
1.25      markus   1550:        options->ciphers = NULL;
1.62      markus   1551:        options->macs = NULL;
1.189     djm      1552:        options->kex_algorithms = NULL;
1.76      markus   1553:        options->hostkeyalgorithms = NULL;
1.25      markus   1554:        options->protocol = SSH_PROTO_UNKNOWN;
1.17      markus   1555:        options->num_identity_files = 0;
                   1556:        options->hostname = NULL;
1.52      markus   1557:        options->host_key_alias = NULL;
1.17      markus   1558:        options->proxy_command = NULL;
                   1559:        options->user = NULL;
                   1560:        options->escape_char = -1;
1.193     djm      1561:        options->num_system_hostfiles = 0;
                   1562:        options->num_user_hostfiles = 0;
1.185     djm      1563:        options->local_forwards = NULL;
1.17      markus   1564:        options->num_local_forwards = 0;
1.185     djm      1565:        options->remote_forwards = NULL;
1.17      markus   1566:        options->num_remote_forwards = 0;
1.90      stevesk  1567:        options->clear_forwardings = -1;
1.95      markus   1568:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67      markus   1569:        options->preferred_authentications = NULL;
1.77      markus   1570:        options->bind_address = NULL;
1.183     markus   1571:        options->pkcs11_provider = NULL;
1.101     markus   1572:        options->enable_ssh_keysign = - 1;
1.91      markus   1573:        options->no_host_authentication_for_localhost = - 1;
1.128     markus   1574:        options->identities_only = - 1;
1.105     markus   1575:        options->rekey_limit = - 1;
1.198     dtucker  1576:        options->rekey_interval = -1;
1.107     jakob    1577:        options->verify_host_key_dns = -1;
1.127     markus   1578:        options->server_alive_interval = -1;
                   1579:        options->server_alive_count_max = -1;
1.130     djm      1580:        options->num_send_env = 0;
1.132     djm      1581:        options->control_path = NULL;
                   1582:        options->control_master = -1;
1.187     djm      1583:        options->control_persist = -1;
                   1584:        options->control_persist_timeout = 0;
1.136     djm      1585:        options->hash_known_hosts = -1;
1.144     reyk     1586:        options->tun_open = -1;
                   1587:        options->tun_local = -1;
                   1588:        options->tun_remote = -1;
                   1589:        options->local_command = NULL;
                   1590:        options->permit_local_command = -1;
1.177     andreas  1591:        options->use_roaming = -1;
1.167     grunk    1592:        options->visual_host_key = -1;
1.190     djm      1593:        options->ip_qos_interactive = -1;
                   1594:        options->ip_qos_bulk = -1;
1.192     djm      1595:        options->request_tty = -1;
1.205     djm      1596:        options->proxy_use_fdpass = -1;
1.199     djm      1597:        options->ignored_unknown = NULL;
1.208     djm      1598:        options->num_canonical_domains = 0;
                   1599:        options->num_permitted_cnames = 0;
1.209     djm      1600:        options->canonicalize_max_dots = -1;
                   1601:        options->canonicalize_fallback_local = -1;
                   1602:        options->canonicalize_hostname = -1;
1.1       deraadt  1603: }
                   1604:
1.19      markus   1605: /*
1.218     djm      1606:  * A petite version of fill_default_options() that just fills the options
                   1607:  * needed for hostname canonicalization to proceed.
                   1608:  */
                   1609: void
                   1610: fill_default_options_for_canonicalization(Options *options)
                   1611: {
                   1612:        if (options->canonicalize_max_dots == -1)
                   1613:                options->canonicalize_max_dots = 1;
                   1614:        if (options->canonicalize_fallback_local == -1)
                   1615:                options->canonicalize_fallback_local = 1;
                   1616:        if (options->canonicalize_hostname == -1)
                   1617:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
                   1618: }
                   1619:
                   1620: /*
1.19      markus   1621:  * Called after processing other sources of option data, this fills those
                   1622:  * options for which no value has been specified with their default values.
                   1623:  */
1.26      markus   1624: void
1.17      markus   1625: fill_default_options(Options * options)
1.1       deraadt  1626: {
1.17      markus   1627:        if (options->forward_agent == -1)
1.33      markus   1628:                options->forward_agent = 0;
1.17      markus   1629:        if (options->forward_x11 == -1)
1.23      markus   1630:                options->forward_x11 = 0;
1.123     markus   1631:        if (options->forward_x11_trusted == -1)
                   1632:                options->forward_x11_trusted = 0;
1.186     djm      1633:        if (options->forward_x11_timeout == -1)
                   1634:                options->forward_x11_timeout = 1200;
1.153     markus   1635:        if (options->exit_on_forward_failure == -1)
                   1636:                options->exit_on_forward_failure = 0;
1.34      markus   1637:        if (options->xauth_location == NULL)
1.80      markus   1638:                options->xauth_location = _PATH_XAUTH;
1.220   ! millert  1639:        if (options->fwd_opts.gateway_ports == -1)
        !          1640:                options->fwd_opts.gateway_ports = 0;
        !          1641:        if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
        !          1642:                options->fwd_opts.streamlocal_bind_mask = 0177;
        !          1643:        if (options->fwd_opts.streamlocal_bind_unlink == -1)
        !          1644:                options->fwd_opts.streamlocal_bind_unlink = 0;
1.17      markus   1645:        if (options->use_privileged_port == -1)
1.65      markus   1646:                options->use_privileged_port = 0;
1.17      markus   1647:        if (options->rsa_authentication == -1)
                   1648:                options->rsa_authentication = 1;
1.50      markus   1649:        if (options->pubkey_authentication == -1)
                   1650:                options->pubkey_authentication = 1;
1.78      markus   1651:        if (options->challenge_response_authentication == -1)
1.83      markus   1652:                options->challenge_response_authentication = 1;
1.118     markus   1653:        if (options->gss_authentication == -1)
1.122     markus   1654:                options->gss_authentication = 0;
1.118     markus   1655:        if (options->gss_deleg_creds == -1)
                   1656:                options->gss_deleg_creds = 0;
1.17      markus   1657:        if (options->password_authentication == -1)
                   1658:                options->password_authentication = 1;
1.48      markus   1659:        if (options->kbd_interactive_authentication == -1)
1.59      markus   1660:                options->kbd_interactive_authentication = 1;
1.17      markus   1661:        if (options->rhosts_rsa_authentication == -1)
1.99      stevesk  1662:                options->rhosts_rsa_authentication = 0;
1.72      markus   1663:        if (options->hostbased_authentication == -1)
                   1664:                options->hostbased_authentication = 0;
1.17      markus   1665:        if (options->batch_mode == -1)
                   1666:                options->batch_mode = 0;
                   1667:        if (options->check_host_ip == -1)
                   1668:                options->check_host_ip = 1;
                   1669:        if (options->strict_host_key_checking == -1)
                   1670:                options->strict_host_key_checking = 2;  /* 2 is default */
                   1671:        if (options->compression == -1)
                   1672:                options->compression = 0;
1.126     markus   1673:        if (options->tcp_keep_alive == -1)
                   1674:                options->tcp_keep_alive = 1;
1.17      markus   1675:        if (options->compression_level == -1)
                   1676:                options->compression_level = 6;
                   1677:        if (options->port == -1)
                   1678:                options->port = 0;      /* Filled in ssh_connect. */
1.114     djm      1679:        if (options->address_family == -1)
                   1680:                options->address_family = AF_UNSPEC;
1.17      markus   1681:        if (options->connection_attempts == -1)
1.84      markus   1682:                options->connection_attempts = 1;
1.17      markus   1683:        if (options->number_of_password_prompts == -1)
                   1684:                options->number_of_password_prompts = 3;
                   1685:        /* Selected in ssh_login(). */
                   1686:        if (options->cipher == -1)
                   1687:                options->cipher = SSH_CIPHER_NOT_SET;
1.31      markus   1688:        /* options->ciphers, default set in myproposals.h */
1.62      markus   1689:        /* options->macs, default set in myproposals.h */
1.189     djm      1690:        /* options->kex_algorithms, default set in myproposals.h */
1.76      markus   1691:        /* options->hostkeyalgorithms, default set in myproposals.h */
1.25      markus   1692:        if (options->protocol == SSH_PROTO_UNKNOWN)
1.178     markus   1693:                options->protocol = SSH_PROTO_2;
1.17      markus   1694:        if (options->num_identity_files == 0) {
1.50      markus   1695:                if (options->protocol & SSH_PROTO_1) {
1.195     dtucker  1696:                        add_identity_file(options, "~/",
                   1697:                            _PATH_SSH_CLIENT_IDENTITY, 0);
1.50      markus   1698:                }
                   1699:                if (options->protocol & SSH_PROTO_2) {
1.195     dtucker  1700:                        add_identity_file(options, "~/",
                   1701:                            _PATH_SSH_CLIENT_ID_RSA, 0);
                   1702:                        add_identity_file(options, "~/",
                   1703:                            _PATH_SSH_CLIENT_ID_DSA, 0);
                   1704:                        add_identity_file(options, "~/",
                   1705:                            _PATH_SSH_CLIENT_ID_ECDSA, 0);
1.215     markus   1706:                        add_identity_file(options, "~/",
                   1707:                            _PATH_SSH_CLIENT_ID_ED25519, 0);
1.50      markus   1708:                }
1.27      markus   1709:        }
1.17      markus   1710:        if (options->escape_char == -1)
                   1711:                options->escape_char = '~';
1.193     djm      1712:        if (options->num_system_hostfiles == 0) {
                   1713:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1714:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
                   1715:                options->system_hostfiles[options->num_system_hostfiles++] =
                   1716:                    xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
                   1717:        }
                   1718:        if (options->num_user_hostfiles == 0) {
                   1719:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1720:                    xstrdup(_PATH_SSH_USER_HOSTFILE);
                   1721:                options->user_hostfiles[options->num_user_hostfiles++] =
                   1722:                    xstrdup(_PATH_SSH_USER_HOSTFILE2);
                   1723:        }
1.95      markus   1724:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54      markus   1725:                options->log_level = SYSLOG_LEVEL_INFO;
1.90      stevesk  1726:        if (options->clear_forwardings == 1)
                   1727:                clear_forwardings(options);
1.91      markus   1728:        if (options->no_host_authentication_for_localhost == - 1)
                   1729:                options->no_host_authentication_for_localhost = 0;
1.128     markus   1730:        if (options->identities_only == -1)
                   1731:                options->identities_only = 0;
1.101     markus   1732:        if (options->enable_ssh_keysign == -1)
                   1733:                options->enable_ssh_keysign = 0;
1.105     markus   1734:        if (options->rekey_limit == -1)
                   1735:                options->rekey_limit = 0;
1.198     dtucker  1736:        if (options->rekey_interval == -1)
                   1737:                options->rekey_interval = 0;
1.107     jakob    1738:        if (options->verify_host_key_dns == -1)
                   1739:                options->verify_host_key_dns = 0;
1.127     markus   1740:        if (options->server_alive_interval == -1)
                   1741:                options->server_alive_interval = 0;
                   1742:        if (options->server_alive_count_max == -1)
                   1743:                options->server_alive_count_max = 3;
1.132     djm      1744:        if (options->control_master == -1)
                   1745:                options->control_master = 0;
1.187     djm      1746:        if (options->control_persist == -1) {
                   1747:                options->control_persist = 0;
                   1748:                options->control_persist_timeout = 0;
                   1749:        }
1.136     djm      1750:        if (options->hash_known_hosts == -1)
                   1751:                options->hash_known_hosts = 0;
1.144     reyk     1752:        if (options->tun_open == -1)
1.145     reyk     1753:                options->tun_open = SSH_TUNMODE_NO;
                   1754:        if (options->tun_local == -1)
                   1755:                options->tun_local = SSH_TUNID_ANY;
                   1756:        if (options->tun_remote == -1)
                   1757:                options->tun_remote = SSH_TUNID_ANY;
1.144     reyk     1758:        if (options->permit_local_command == -1)
                   1759:                options->permit_local_command = 0;
1.177     andreas  1760:        if (options->use_roaming == -1)
                   1761:                options->use_roaming = 1;
1.167     grunk    1762:        if (options->visual_host_key == -1)
                   1763:                options->visual_host_key = 0;
1.190     djm      1764:        if (options->ip_qos_interactive == -1)
                   1765:                options->ip_qos_interactive = IPTOS_LOWDELAY;
                   1766:        if (options->ip_qos_bulk == -1)
                   1767:                options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.192     djm      1768:        if (options->request_tty == -1)
                   1769:                options->request_tty = REQUEST_TTY_AUTO;
1.205     djm      1770:        if (options->proxy_use_fdpass == -1)
                   1771:                options->proxy_use_fdpass = 0;
1.209     djm      1772:        if (options->canonicalize_max_dots == -1)
                   1773:                options->canonicalize_max_dots = 1;
                   1774:        if (options->canonicalize_fallback_local == -1)
                   1775:                options->canonicalize_fallback_local = 1;
                   1776:        if (options->canonicalize_hostname == -1)
                   1777:                options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.207     djm      1778: #define CLEAR_ON_NONE(v) \
                   1779:        do { \
1.218     djm      1780:                if (option_clear_or_none(v)) { \
1.207     djm      1781:                        free(v); \
                   1782:                        v = NULL; \
                   1783:                } \
                   1784:        } while(0)
                   1785:        CLEAR_ON_NONE(options->local_command);
                   1786:        CLEAR_ON_NONE(options->proxy_command);
                   1787:        CLEAR_ON_NONE(options->control_path);
1.17      markus   1788:        /* options->user will be set in the main program if appropriate */
                   1789:        /* options->hostname will be set in the main program if appropriate */
1.52      markus   1790:        /* options->host_key_alias should not be set by default */
1.67      markus   1791:        /* options->preferred_authentications will be set in ssh */
1.135     djm      1792: }
                   1793:
1.220   ! millert  1794: struct fwdarg {
        !          1795:        char *arg;
        !          1796:        int ispath;
        !          1797: };
        !          1798:
        !          1799: /*
        !          1800:  * parse_fwd_field
        !          1801:  * parses the next field in a port forwarding specification.
        !          1802:  * sets fwd to the parsed field and advances p past the colon
        !          1803:  * or sets it to NULL at end of string.
        !          1804:  * returns 0 on success, else non-zero.
        !          1805:  */
        !          1806: static int
        !          1807: parse_fwd_field(char **p, struct fwdarg *fwd)
        !          1808: {
        !          1809:        char *ep, *cp = *p;
        !          1810:        int ispath = 0;
        !          1811:
        !          1812:        if (*cp == '\0') {
        !          1813:                *p = NULL;
        !          1814:                return -1;      /* end of string */
        !          1815:        }
        !          1816:
        !          1817:        /*
        !          1818:         * A field escaped with square brackets is used literally.
        !          1819:         * XXX - allow ']' to be escaped via backslash?
        !          1820:         */
        !          1821:        if (*cp == '[') {
        !          1822:                /* find matching ']' */
        !          1823:                for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
        !          1824:                        if (*ep == '/')
        !          1825:                                ispath = 1;
        !          1826:                }
        !          1827:                /* no matching ']' or not at end of field. */
        !          1828:                if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
        !          1829:                        return -1;
        !          1830:                /* NUL terminate the field and advance p past the colon */
        !          1831:                *ep++ = '\0';
        !          1832:                if (*ep != '\0')
        !          1833:                        *ep++ = '\0';
        !          1834:                fwd->arg = cp + 1;
        !          1835:                fwd->ispath = ispath;
        !          1836:                *p = ep;
        !          1837:                return 0;
        !          1838:        }
        !          1839:
        !          1840:        for (cp = *p; *cp != '\0'; cp++) {
        !          1841:                switch (*cp) {
        !          1842:                case '\\':
        !          1843:                        memmove(cp, cp + 1, strlen(cp + 1) + 1);
        !          1844:                        cp++;
        !          1845:                        break;
        !          1846:                case '/':
        !          1847:                        ispath = 1;
        !          1848:                        break;
        !          1849:                case ':':
        !          1850:                        *cp++ = '\0';
        !          1851:                        goto done;
        !          1852:                }
        !          1853:        }
        !          1854: done:
        !          1855:        fwd->arg = *p;
        !          1856:        fwd->ispath = ispath;
        !          1857:        *p = cp;
        !          1858:        return 0;
        !          1859: }
        !          1860:
1.135     djm      1861: /*
                   1862:  * parse_forward
                   1863:  * parses a string containing a port forwarding specification of the form:
1.168     stevesk  1864:  *   dynamicfwd == 0
1.220   ! millert  1865:  *     [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
        !          1866:  *     listenpath:connectpath
1.168     stevesk  1867:  *   dynamicfwd == 1
                   1868:  *     [listenhost:]listenport
1.135     djm      1869:  * returns number of arguments parsed or zero on error
                   1870:  */
                   1871: int
1.220   ! millert  1872: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135     djm      1873: {
1.220   ! millert  1874:        struct fwdarg fwdargs[4];
        !          1875:        char *p, *cp;
1.135     djm      1876:        int i;
                   1877:
1.220   ! millert  1878:        memset(fwd, 0, sizeof(*fwd));
        !          1879:        memset(fwdargs, 0, sizeof(fwdargs));
1.135     djm      1880:
                   1881:        cp = p = xstrdup(fwdspec);
                   1882:
                   1883:        /* skip leading spaces */
1.214     deraadt  1884:        while (isspace((u_char)*cp))
1.135     djm      1885:                cp++;
                   1886:
1.220   ! millert  1887:        for (i = 0; i < 4; ++i) {
        !          1888:                if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135     djm      1889:                        break;
1.220   ! millert  1890:        }
1.135     djm      1891:
1.170     stevesk  1892:        /* Check for trailing garbage */
1.220   ! millert  1893:        if (cp != NULL && *cp != '\0') {
1.135     djm      1894:                i = 0;  /* failure */
1.220   ! millert  1895:        }
1.135     djm      1896:
                   1897:        switch (i) {
1.168     stevesk  1898:        case 1:
1.220   ! millert  1899:                if (fwdargs[0].ispath) {
        !          1900:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
        !          1901:                        fwd->listen_port = PORT_STREAMLOCAL;
        !          1902:                } else {
        !          1903:                        fwd->listen_host = NULL;
        !          1904:                        fwd->listen_port = a2port(fwdargs[0].arg);
        !          1905:                }
1.168     stevesk  1906:                fwd->connect_host = xstrdup("socks");
                   1907:                break;
                   1908:
                   1909:        case 2:
1.220   ! millert  1910:                if (fwdargs[0].ispath && fwdargs[1].ispath) {
        !          1911:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
        !          1912:                        fwd->listen_port = PORT_STREAMLOCAL;
        !          1913:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
        !          1914:                        fwd->connect_port = PORT_STREAMLOCAL;
        !          1915:                } else if (fwdargs[1].ispath) {
        !          1916:                        fwd->listen_host = NULL;
        !          1917:                        fwd->listen_port = a2port(fwdargs[0].arg);
        !          1918:                        fwd->connect_path = xstrdup(fwdargs[1].arg);
        !          1919:                        fwd->connect_port = PORT_STREAMLOCAL;
        !          1920:                } else {
        !          1921:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
        !          1922:                        fwd->listen_port = a2port(fwdargs[1].arg);
        !          1923:                        fwd->connect_host = xstrdup("socks");
        !          1924:                }
1.168     stevesk  1925:                break;
                   1926:
1.135     djm      1927:        case 3:
1.220   ! millert  1928:                if (fwdargs[0].ispath) {
        !          1929:                        fwd->listen_path = xstrdup(fwdargs[0].arg);
        !          1930:                        fwd->listen_port = PORT_STREAMLOCAL;
        !          1931:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
        !          1932:                        fwd->connect_port = a2port(fwdargs[2].arg);
        !          1933:                } else if (fwdargs[2].ispath) {
        !          1934:                        fwd->listen_host = xstrdup(fwdargs[0].arg);
        !          1935:                        fwd->listen_port = a2port(fwdargs[1].arg);
        !          1936:                        fwd->connect_path = xstrdup(fwdargs[2].arg);
        !          1937:                        fwd->connect_port = PORT_STREAMLOCAL;
        !          1938:                } else {
        !          1939:                        fwd->listen_host = NULL;
        !          1940:                        fwd->listen_port = a2port(fwdargs[0].arg);
        !          1941:                        fwd->connect_host = xstrdup(fwdargs[1].arg);
        !          1942:                        fwd->connect_port = a2port(fwdargs[2].arg);
        !          1943:                }
1.135     djm      1944:                break;
                   1945:
                   1946:        case 4:
1.220   ! millert  1947:                fwd->listen_host = xstrdup(fwdargs[0].arg);
        !          1948:                fwd->listen_port = a2port(fwdargs[1].arg);
        !          1949:                fwd->connect_host = xstrdup(fwdargs[2].arg);
        !          1950:                fwd->connect_port = a2port(fwdargs[3].arg);
1.135     djm      1951:                break;
                   1952:        default:
                   1953:                i = 0; /* failure */
                   1954:        }
                   1955:
1.202     djm      1956:        free(p);
1.135     djm      1957:
1.168     stevesk  1958:        if (dynamicfwd) {
                   1959:                if (!(i == 1 || i == 2))
                   1960:                        goto fail_free;
                   1961:        } else {
1.220   ! millert  1962:                if (!(i == 3 || i == 4)) {
        !          1963:                        if (fwd->connect_path == NULL &&
        !          1964:                            fwd->listen_path == NULL)
        !          1965:                                goto fail_free;
        !          1966:                }
        !          1967:                if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168     stevesk  1968:                        goto fail_free;
                   1969:        }
                   1970:
1.220   ! millert  1971:        if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
        !          1972:            (!remotefwd && fwd->listen_port == 0))
1.135     djm      1973:                goto fail_free;
                   1974:        if (fwd->connect_host != NULL &&
                   1975:            strlen(fwd->connect_host) >= NI_MAXHOST)
                   1976:                goto fail_free;
1.220   ! millert  1977:        /* XXX - if connecting to a remote socket, max sun len may not match this host */
        !          1978:        if (fwd->connect_path != NULL &&
        !          1979:            strlen(fwd->connect_path) >= PATH_MAX_SUN)
        !          1980:                goto fail_free;
1.176     djm      1981:        if (fwd->listen_host != NULL &&
                   1982:            strlen(fwd->listen_host) >= NI_MAXHOST)
                   1983:                goto fail_free;
1.220   ! millert  1984:        if (fwd->listen_path != NULL &&
        !          1985:            strlen(fwd->listen_path) >= PATH_MAX_SUN)
        !          1986:                goto fail_free;
1.135     djm      1987:
                   1988:        return (i);
                   1989:
                   1990:  fail_free:
1.202     djm      1991:        free(fwd->connect_host);
                   1992:        fwd->connect_host = NULL;
1.220   ! millert  1993:        free(fwd->connect_path);
        !          1994:        fwd->connect_path = NULL;
1.202     djm      1995:        free(fwd->listen_host);
                   1996:        fwd->listen_host = NULL;
1.220   ! millert  1997:        free(fwd->listen_path);
        !          1998:        fwd->listen_path = NULL;
1.135     djm      1999:        return (0);
1.1       deraadt  2000: }