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