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