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