Annotation of src/usr.bin/ssh/readconf.c, Revision 1.345
1.345 ! djm 1: /* $OpenBSD: readconf.c,v 1.344 2020/12/17 23:10:27 djm Exp $ */
1.1 deraadt 2: /*
1.18 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Functions for reading the configuration files.
1.26 markus 7: *
1.46 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.18 deraadt 13: */
1.1 deraadt 14:
1.147 stevesk 15: #include <sys/types.h>
16: #include <sys/stat.h>
1.152 stevesk 17: #include <sys/socket.h>
1.206 djm 18: #include <sys/wait.h>
1.220 millert 19: #include <sys/un.h>
1.152 stevesk 20:
21: #include <netinet/in.h>
1.190 djm 22: #include <netinet/ip.h>
1.148 stevesk 23:
24: #include <ctype.h>
1.154 stevesk 25: #include <errno.h>
1.206 djm 26: #include <fcntl.h>
1.252 djm 27: #include <glob.h>
1.155 stevesk 28: #include <netdb.h>
1.206 djm 29: #include <paths.h>
30: #include <pwd.h>
1.159 deraadt 31: #include <signal.h>
1.158 stevesk 32: #include <stdio.h>
1.157 stevesk 33: #include <string.h>
1.312 deraadt 34: #include <stdarg.h>
1.156 stevesk 35: #include <unistd.h>
1.228 deraadt 36: #include <limits.h>
1.200 dtucker 37: #include <util.h>
1.221 djm 38: #include <vis.h>
1.1 deraadt 39:
1.159 deraadt 40: #include "xmalloc.h"
1.1 deraadt 41: #include "ssh.h"
1.297 djm 42: #include "ssherr.h"
1.25 markus 43: #include "compat.h"
1.58 markus 44: #include "cipher.h"
1.55 markus 45: #include "pathnames.h"
1.58 markus 46: #include "log.h"
1.227 djm 47: #include "sshkey.h"
1.220 millert 48: #include "misc.h"
1.58 markus 49: #include "readconf.h"
50: #include "match.h"
1.62 markus 51: #include "kex.h"
52: #include "mac.h"
1.206 djm 53: #include "uidswap.h"
1.221 djm 54: #include "myproposal.h"
1.224 djm 55: #include "digest.h"
1.1 deraadt 56:
57: /* Format of the configuration file:
58:
59: # Configuration data is parsed as follows:
60: # 1. command line options
61: # 2. user-specific file
62: # 3. system-wide file
63: # Any configuration value is only changed the first time it is set.
64: # Thus, host-specific definitions should be at the beginning of the
65: # configuration file, and defaults at the end.
66:
67: # Host-specific declarations. These may override anything above. A single
68: # host may match multiple declarations; these are processed in the order
69: # that they are given in.
70:
71: Host *.ngs.fi ngs.fi
1.96 markus 72: User foo
1.1 deraadt 73:
74: Host fake.com
1.306 jmc 75: Hostname another.host.name.real.org
1.1 deraadt 76: User blaah
77: Port 34289
78: ForwardX11 no
79: ForwardAgent no
80:
81: Host books.com
82: RemoteForward 9999 shadows.cs.hut.fi:9999
1.266 djm 83: Ciphers 3des-cbc
1.1 deraadt 84:
85: Host fascist.blob.com
86: Port 23123
87: User tylonen
88: PasswordAuthentication no
89:
90: Host puukko.hut.fi
91: User t35124p
92: ProxyCommand ssh-proxy %h %p
93:
94: Host *.fr
1.96 markus 95: PublicKeyAuthentication no
1.1 deraadt 96:
97: Host *.su
1.266 djm 98: Ciphers aes128-ctr
1.1 deraadt 99: PasswordAuthentication no
100:
1.144 reyk 101: Host vpn.fake.com
102: Tunnel yes
103: TunnelDevice 3
104:
1.1 deraadt 105: # Defaults for various options
106: Host *
107: ForwardAgent no
1.50 markus 108: ForwardX11 no
1.1 deraadt 109: PasswordAuthentication 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,
1.302 djm 120: int flags, int *activep, int *want_final_pass, int depth);
1.252 djm 121: static int process_config_line_depth(Options *options, struct passwd *pw,
122: const char *host, const char *original_host, char *line,
1.302 djm 123: const char *filename, int linenum, int *activep, int flags,
124: int *want_final_pass, int depth);
1.252 djm 125:
1.1 deraadt 126: /* Keyword tokens. */
127:
1.17 markus 128: typedef enum {
129: oBadOption,
1.252 djm 130: oHost, oMatch, oInclude,
1.186 djm 131: oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
132: oGatewayPorts, oExitOnForwardFailure,
1.317 dtucker 133: oPasswordAuthentication,
1.59 markus 134: oChallengeResponseAuthentication, oXAuthLocation,
1.317 dtucker 135: oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
1.253 markus 136: oCertificateFile, oAddKeysToAgent, oIdentityAgent,
1.317 dtucker 137: oUser, oEscapeChar, oProxyCommand,
1.17 markus 138: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
139: oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.317 dtucker 140: oTCPKeepAlive, oNumberOfPasswordPrompts,
1.339 djm 141: oLogFacility, oLogLevel, oLogVerbose, oCiphers, oMacs,
1.221 djm 142: oPubkeyAuthentication,
1.67 markus 143: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76 markus 144: oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.282 djm 145: oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
1.96 markus 146: oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111 djm 147: oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118 markus 148: oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128 markus 149: oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.290 djm 150: oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
1.187 djm 151: oHashKnownHosts,
1.277 bluhm 152: oTunnel, oTunnelDevice,
153: oLocalCommand, oPermitLocalCommand, oRemoteCommand,
1.248 markus 154: oVisualHostKey,
1.205 djm 155: oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
1.209 djm 156: oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
157: oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
1.223 djm 158: oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
1.230 djm 159: oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
1.298 djm 160: oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
1.310 djm 161: oSecurityKeyProvider,
1.273 djm 162: oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
1.1 deraadt 163: } OpCodes;
164:
165: /* Textual representations of the tokens. */
166:
1.17 markus 167: static struct {
168: const char *name;
169: OpCodes opcode;
170: } keywords[] = {
1.266 djm 171: /* Deprecated options */
1.273 djm 172: { "protocol", oIgnore }, /* NB. silently ignored */
1.274 djm 173: { "cipher", oDeprecated },
1.266 djm 174: { "fallbacktorsh", oDeprecated },
175: { "globalknownhostsfile2", oDeprecated },
176: { "rhostsauthentication", oDeprecated },
177: { "userknownhostsfile2", oDeprecated },
178: { "useroaming", oDeprecated },
179: { "usersh", oDeprecated },
1.294 dtucker 180: { "useprivilegedport", oDeprecated },
1.266 djm 181:
182: /* Unsupported options */
183: { "afstokenpassing", oUnsupported },
184: { "kerberosauthentication", oUnsupported },
185: { "kerberostgtpassing", oUnsupported },
1.318 dtucker 186: { "rsaauthentication", oUnsupported },
187: { "rhostsrsaauthentication", oUnsupported },
188: { "compressionlevel", oUnsupported },
1.266 djm 189:
190: /* Sometimes-unsupported options */
191: #if defined(GSSAPI)
192: { "gssapiauthentication", oGssAuthentication },
193: { "gssapidelegatecredentials", oGssDelegateCreds },
194: # else
195: { "gssapiauthentication", oUnsupported },
196: { "gssapidelegatecredentials", oUnsupported },
197: #endif
198: #ifdef ENABLE_PKCS11
1.304 djm 199: { "pkcs11provider", oPKCS11Provider },
1.266 djm 200: { "smartcarddevice", oPKCS11Provider },
201: # else
202: { "smartcarddevice", oUnsupported },
203: { "pkcs11provider", oUnsupported },
204: #endif
205:
1.17 markus 206: { "forwardagent", oForwardAgent },
207: { "forwardx11", oForwardX11 },
1.123 markus 208: { "forwardx11trusted", oForwardX11Trusted },
1.186 djm 209: { "forwardx11timeout", oForwardX11Timeout },
1.153 markus 210: { "exitonforwardfailure", oExitOnForwardFailure },
1.34 markus 211: { "xauthlocation", oXAuthLocation },
1.17 markus 212: { "gatewayports", oGatewayPorts },
213: { "passwordauthentication", oPasswordAuthentication },
1.48 markus 214: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
215: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.50 markus 216: { "pubkeyauthentication", oPubkeyAuthentication },
1.59 markus 217: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
1.73 markus 218: { "hostbasedauthentication", oHostbasedAuthentication },
1.59 markus 219: { "challengeresponseauthentication", oChallengeResponseAuthentication },
220: { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
221: { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
1.17 markus 222: { "identityfile", oIdentityFile },
1.174 stevesk 223: { "identityfile2", oIdentityFile }, /* obsolete */
1.128 markus 224: { "identitiesonly", oIdentitiesOnly },
1.241 djm 225: { "certificatefile", oCertificateFile },
1.246 jcs 226: { "addkeystoagent", oAddKeysToAgent },
1.253 markus 227: { "identityagent", oIdentityAgent },
1.306 jmc 228: { "hostname", oHostname },
1.52 markus 229: { "hostkeyalias", oHostKeyAlias },
1.17 markus 230: { "proxycommand", oProxyCommand },
231: { "port", oPort },
1.25 markus 232: { "ciphers", oCiphers },
1.62 markus 233: { "macs", oMacs },
1.17 markus 234: { "remoteforward", oRemoteForward },
235: { "localforward", oLocalForward },
236: { "user", oUser },
237: { "host", oHost },
1.206 djm 238: { "match", oMatch },
1.17 markus 239: { "escapechar", oEscapeChar },
240: { "globalknownhostsfile", oGlobalKnownHostsFile },
1.174 stevesk 241: { "userknownhostsfile", oUserKnownHostsFile },
1.17 markus 242: { "connectionattempts", oConnectionAttempts },
243: { "batchmode", oBatchMode },
244: { "checkhostip", oCheckHostIP },
245: { "stricthostkeychecking", oStrictHostKeyChecking },
246: { "compression", oCompression },
1.126 markus 247: { "tcpkeepalive", oTCPKeepAlive },
248: { "keepalive", oTCPKeepAlive }, /* obsolete */
1.17 markus 249: { "numberofpasswordprompts", oNumberOfPasswordPrompts },
1.271 dtucker 250: { "syslogfacility", oLogFacility },
1.17 markus 251: { "loglevel", oLogLevel },
1.339 djm 252: { "logverbose", oLogVerbose },
1.71 markus 253: { "dynamicforward", oDynamicForward },
1.67 markus 254: { "preferredauthentications", oPreferredAuthentications },
1.76 markus 255: { "hostkeyalgorithms", oHostKeyAlgorithms },
1.298 djm 256: { "casignaturealgorithms", oCASignatureAlgorithms },
1.77 markus 257: { "bindaddress", oBindAddress },
1.282 djm 258: { "bindinterface", oBindInterface },
1.93 deraadt 259: { "clearallforwardings", oClearAllForwardings },
1.101 markus 260: { "enablesshkeysign", oEnableSSHKeysign },
1.107 jakob 261: { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93 deraadt 262: { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105 markus 263: { "rekeylimit", oRekeyLimit },
1.111 djm 264: { "connecttimeout", oConnectTimeout },
1.112 djm 265: { "addressfamily", oAddressFamily },
1.127 markus 266: { "serveraliveinterval", oServerAliveInterval },
267: { "serveralivecountmax", oServerAliveCountMax },
1.130 djm 268: { "sendenv", oSendEnv },
1.290 djm 269: { "setenv", oSetEnv },
1.132 djm 270: { "controlpath", oControlPath },
271: { "controlmaster", oControlMaster },
1.187 djm 272: { "controlpersist", oControlPersist },
1.136 djm 273: { "hashknownhosts", oHashKnownHosts },
1.252 djm 274: { "include", oInclude },
1.144 reyk 275: { "tunnel", oTunnel },
276: { "tunneldevice", oTunnelDevice },
277: { "localcommand", oLocalCommand },
278: { "permitlocalcommand", oPermitLocalCommand },
1.277 bluhm 279: { "remotecommand", oRemoteCommand },
1.167 grunk 280: { "visualhostkey", oVisualHostKey },
1.189 djm 281: { "kexalgorithms", oKexAlgorithms },
1.190 djm 282: { "ipqos", oIPQoS },
1.192 djm 283: { "requesttty", oRequestTTY },
1.205 djm 284: { "proxyusefdpass", oProxyUseFdpass },
1.208 djm 285: { "canonicaldomains", oCanonicalDomains },
1.209 djm 286: { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
287: { "canonicalizehostname", oCanonicalizeHostname },
288: { "canonicalizemaxdots", oCanonicalizeMaxDots },
289: { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
1.220 millert 290: { "streamlocalbindmask", oStreamLocalBindMask },
291: { "streamlocalbindunlink", oStreamLocalBindUnlink },
1.223 djm 292: { "revokedhostkeys", oRevokedHostKeys },
1.224 djm 293: { "fingerprinthash", oFingerprintHash },
1.229 djm 294: { "updatehostkeys", oUpdateHostkeys },
1.230 djm 295: { "hostbasedkeytypes", oHostbasedKeyTypes },
1.238 markus 296: { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
1.199 djm 297: { "ignoreunknown", oIgnoreUnknown },
1.257 djm 298: { "proxyjump", oProxyJump },
1.318 dtucker 299: { "securitykeyprovider", oSecurityKeyProvider },
1.171 djm 300:
1.92 stevesk 301: { NULL, oBadOption }
1.13 markus 302: };
303:
1.320 dtucker 304:
305: const char *
306: kex_default_pk_alg(void)
307: {
1.344 djm 308: static char *pkalgs;
309:
310: if (pkalgs == NULL) {
311: char *all_key;
312:
313: all_key = sshkey_alg_list(0, 0, 1, ',');
314: pkalgs = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
315: free(all_key);
316: }
317: return pkalgs;
1.320 dtucker 318: }
319:
1.327 dtucker 320: char *
321: ssh_connection_hash(const char *thishost, const char *host, const char *portstr,
322: const char *user)
323: {
324: struct ssh_digest_ctx *md;
325: u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
326:
327: if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
328: ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
329: ssh_digest_update(md, host, strlen(host)) < 0 ||
330: ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
331: ssh_digest_update(md, user, strlen(user)) < 0 ||
332: ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
1.340 djm 333: fatal_f("mux digest failed");
1.327 dtucker 334: ssh_digest_free(md);
335: return tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
336: }
337:
1.19 markus 338: /*
339: * Adds a local TCP/IP port forward to options. Never returns if there is an
340: * error.
341: */
1.1 deraadt 342:
1.26 markus 343: void
1.220 millert 344: add_local_forward(Options *options, const struct Forward *newfwd)
1.1 deraadt 345: {
1.220 millert 346: struct Forward *fwd;
1.251 djm 347: int i;
1.185 djm 348:
1.251 djm 349: /* Don't add duplicates */
350: for (i = 0; i < options->num_local_forwards; i++) {
351: if (forward_equals(newfwd, options->local_forwards + i))
352: return;
353: }
1.234 deraadt 354: options->local_forwards = xreallocarray(options->local_forwards,
1.185 djm 355: options->num_local_forwards + 1,
356: sizeof(*options->local_forwards));
1.17 markus 357: fwd = &options->local_forwards[options->num_local_forwards++];
1.135 djm 358:
1.172 stevesk 359: fwd->listen_host = newfwd->listen_host;
1.135 djm 360: fwd->listen_port = newfwd->listen_port;
1.220 millert 361: fwd->listen_path = newfwd->listen_path;
1.172 stevesk 362: fwd->connect_host = newfwd->connect_host;
1.135 djm 363: fwd->connect_port = newfwd->connect_port;
1.220 millert 364: fwd->connect_path = newfwd->connect_path;
1.1 deraadt 365: }
366:
1.19 markus 367: /*
368: * Adds a remote TCP/IP port forward to options. Never returns if there is
369: * an error.
370: */
1.1 deraadt 371:
1.26 markus 372: void
1.220 millert 373: add_remote_forward(Options *options, const struct Forward *newfwd)
1.1 deraadt 374: {
1.220 millert 375: struct Forward *fwd;
1.251 djm 376: int i;
1.185 djm 377:
1.251 djm 378: /* Don't add duplicates */
379: for (i = 0; i < options->num_remote_forwards; i++) {
380: if (forward_equals(newfwd, options->remote_forwards + i))
381: return;
382: }
1.234 deraadt 383: options->remote_forwards = xreallocarray(options->remote_forwards,
1.185 djm 384: options->num_remote_forwards + 1,
385: sizeof(*options->remote_forwards));
1.17 markus 386: fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135 djm 387:
1.172 stevesk 388: fwd->listen_host = newfwd->listen_host;
1.135 djm 389: fwd->listen_port = newfwd->listen_port;
1.220 millert 390: fwd->listen_path = newfwd->listen_path;
1.172 stevesk 391: fwd->connect_host = newfwd->connect_host;
1.135 djm 392: fwd->connect_port = newfwd->connect_port;
1.220 millert 393: fwd->connect_path = newfwd->connect_path;
1.194 markus 394: fwd->handle = newfwd->handle;
1.184 markus 395: fwd->allocated_port = 0;
1.1 deraadt 396: }
397:
1.90 stevesk 398: static void
399: clear_forwardings(Options *options)
400: {
401: int i;
402:
1.135 djm 403: for (i = 0; i < options->num_local_forwards; i++) {
1.202 djm 404: free(options->local_forwards[i].listen_host);
1.220 millert 405: free(options->local_forwards[i].listen_path);
1.202 djm 406: free(options->local_forwards[i].connect_host);
1.220 millert 407: free(options->local_forwards[i].connect_path);
1.135 djm 408: }
1.185 djm 409: if (options->num_local_forwards > 0) {
1.202 djm 410: free(options->local_forwards);
1.185 djm 411: options->local_forwards = NULL;
412: }
1.90 stevesk 413: options->num_local_forwards = 0;
1.135 djm 414: for (i = 0; i < options->num_remote_forwards; i++) {
1.202 djm 415: free(options->remote_forwards[i].listen_host);
1.220 millert 416: free(options->remote_forwards[i].listen_path);
1.202 djm 417: free(options->remote_forwards[i].connect_host);
1.220 millert 418: free(options->remote_forwards[i].connect_path);
1.135 djm 419: }
1.185 djm 420: if (options->num_remote_forwards > 0) {
1.202 djm 421: free(options->remote_forwards);
1.185 djm 422: options->remote_forwards = NULL;
423: }
1.90 stevesk 424: options->num_remote_forwards = 0;
1.145 reyk 425: options->tun_open = SSH_TUNMODE_NO;
1.90 stevesk 426: }
427:
1.195 dtucker 428: void
1.241 djm 429: add_certificate_file(Options *options, const char *path, int userprovided)
430: {
431: int i;
432:
433: if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
434: fatal("Too many certificate files specified (max %d)",
435: SSH_MAX_CERTIFICATE_FILES);
436:
437: /* Avoid registering duplicates */
438: for (i = 0; i < options->num_certificate_files; i++) {
439: if (options->certificate_file_userprovided[i] == userprovided &&
440: strcmp(options->certificate_files[i], path) == 0) {
1.340 djm 441: debug2_f("ignoring duplicate key %s", path);
1.241 djm 442: return;
443: }
444: }
445:
446: options->certificate_file_userprovided[options->num_certificate_files] =
447: userprovided;
448: options->certificate_files[options->num_certificate_files++] =
449: xstrdup(path);
450: }
451:
452: void
1.195 dtucker 453: add_identity_file(Options *options, const char *dir, const char *filename,
454: int userprovided)
455: {
456: char *path;
1.219 djm 457: int i;
1.195 dtucker 458:
459: if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
460: fatal("Too many identity files specified (max %d)",
461: SSH_MAX_IDENTITY_FILES);
462:
463: if (dir == NULL) /* no dir, filename is absolute */
464: path = xstrdup(filename);
1.276 djm 465: else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
466: fatal("Identity file path %s too long", path);
1.219 djm 467:
468: /* Avoid registering duplicates */
469: for (i = 0; i < options->num_identity_files; i++) {
470: if (options->identity_file_userprovided[i] == userprovided &&
471: strcmp(options->identity_files[i], path) == 0) {
1.340 djm 472: debug2_f("ignoring duplicate key %s", path);
1.219 djm 473: free(path);
474: return;
475: }
476: }
1.195 dtucker 477:
478: options->identity_file_userprovided[options->num_identity_files] =
479: userprovided;
480: options->identity_files[options->num_identity_files++] = path;
481: }
482:
1.206 djm 483: int
484: default_ssh_port(void)
485: {
486: static int port;
487: struct servent *sp;
488:
489: if (port == 0) {
490: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
491: port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
492: }
493: return port;
494: }
495:
496: /*
497: * Execute a command in a shell.
498: * Return its exit status or -1 on abnormal exit.
499: */
500: static int
501: execute_in_shell(const char *cmd)
502: {
1.243 dtucker 503: char *shell;
1.206 djm 504: pid_t pid;
1.337 djm 505: int status;
1.206 djm 506:
507: if ((shell = getenv("SHELL")) == NULL)
508: shell = _PATH_BSHELL;
1.308 djm 509:
510: if (access(shell, X_OK) == -1) {
511: fatal("Shell \"%s\" is not executable: %s",
512: shell, strerror(errno));
513: }
1.206 djm 514:
515: debug("Executing command: '%.500s'", cmd);
516:
517: /* Fork and execute the command. */
518: if ((pid = fork()) == 0) {
1.245 djm 519: char *argv[4];
1.206 djm 520:
1.337 djm 521: if (stdfd_devnull(1, 1, 0) == -1)
1.340 djm 522: fatal_f("stdfd_devnull failed");
1.206 djm 523: closefrom(STDERR_FILENO + 1);
1.245 djm 524:
525: argv[0] = shell;
526: argv[1] = "-c";
527: argv[2] = xstrdup(cmd);
528: argv[3] = NULL;
1.206 djm 529:
530: execv(argv[0], argv);
531: error("Unable to execute '%.100s': %s", cmd, strerror(errno));
532: /* Die with signal to make this error apparent to parent. */
1.321 dtucker 533: ssh_signal(SIGTERM, SIG_DFL);
1.206 djm 534: kill(getpid(), SIGTERM);
535: _exit(1);
536: }
537: /* Parent. */
1.307 deraadt 538: if (pid == -1)
1.340 djm 539: fatal_f("fork: %.100s", strerror(errno));
1.206 djm 540:
541: while (waitpid(pid, &status, 0) == -1) {
542: if (errno != EINTR && errno != EAGAIN)
1.340 djm 543: fatal_f("waitpid: %s", strerror(errno));
1.206 djm 544: }
545: if (!WIFEXITED(status)) {
546: error("command '%.100s' exited abnormally", cmd);
547: return -1;
1.221 djm 548: }
1.206 djm 549: debug3("command returned status %d", WEXITSTATUS(status));
550: return WEXITSTATUS(status);
551: }
552:
553: /*
554: * Parse and execute a Match directive.
555: */
556: static int
557: match_cfg_line(Options *options, char **condition, struct passwd *pw,
1.302 djm 558: const char *host_arg, const char *original_host, int final_pass,
559: int *want_final_pass, const char *filename, int linenum)
1.206 djm 560: {
1.221 djm 561: char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
1.211 djm 562: const char *ruser;
1.221 djm 563: int r, port, this_result, result = 1, attributes = 0, negate;
1.206 djm 564: char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
1.288 djm 565: char uidstr[32];
1.206 djm 566:
567: /*
568: * Configuration is likely to be incomplete at this point so we
569: * must be prepared to use default values.
570: */
571: port = options->port <= 0 ? default_ssh_port() : options->port;
572: ruser = options->user == NULL ? pw->pw_name : options->user;
1.302 djm 573: if (final_pass) {
1.250 djm 574: host = xstrdup(options->hostname);
575: } else if (options->hostname != NULL) {
1.212 djm 576: /* NB. Please keep in sync with ssh.c:main() */
1.211 djm 577: host = percent_expand(options->hostname,
578: "h", host_arg, (char *)NULL);
1.250 djm 579: } else {
1.211 djm 580: host = xstrdup(host_arg);
1.250 djm 581: }
1.206 djm 582:
1.221 djm 583: debug2("checking match for '%s' host %s originally %s",
584: cp, host, original_host);
585: while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
586: criteria = NULL;
587: this_result = 1;
588: if ((negate = attrib[0] == '!'))
589: attrib++;
590: /* criteria "all" and "canonical" have no argument */
1.213 dtucker 591: if (strcasecmp(attrib, "all") == 0) {
1.221 djm 592: if (attributes > 1 ||
1.213 dtucker 593: ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
1.221 djm 594: error("%.200s line %d: '%s' cannot be combined "
595: "with other Match attributes",
596: filename, linenum, oattrib);
1.213 dtucker 597: result = -1;
598: goto out;
599: }
1.221 djm 600: if (result)
601: result = negate ? 0 : 1;
1.213 dtucker 602: goto out;
603: }
1.221 djm 604: attributes++;
1.302 djm 605: if (strcasecmp(attrib, "canonical") == 0 ||
606: strcasecmp(attrib, "final") == 0) {
607: /*
608: * If the config requests "Match final" then remember
609: * this so we can perform a second pass later.
610: */
611: if (strcasecmp(attrib, "final") == 0 &&
612: want_final_pass != NULL)
613: *want_final_pass = 1;
614: r = !!final_pass; /* force bitmask member to boolean */
1.221 djm 615: if (r == (negate ? 1 : 0))
616: this_result = result = 0;
617: debug3("%.200s line %d: %smatched '%s'",
618: filename, linenum,
619: this_result ? "" : "not ", oattrib);
620: continue;
621: }
622: /* All other criteria require an argument */
1.206 djm 623: if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
624: error("Missing Match criteria for %s", attrib);
1.211 djm 625: result = -1;
626: goto out;
1.206 djm 627: }
628: if (strcasecmp(attrib, "host") == 0) {
1.221 djm 629: criteria = xstrdup(host);
1.235 djm 630: r = match_hostname(host, arg) == 1;
1.221 djm 631: if (r == (negate ? 1 : 0))
632: this_result = result = 0;
1.206 djm 633: } else if (strcasecmp(attrib, "originalhost") == 0) {
1.221 djm 634: criteria = xstrdup(original_host);
1.235 djm 635: r = match_hostname(original_host, arg) == 1;
1.221 djm 636: if (r == (negate ? 1 : 0))
637: this_result = result = 0;
1.206 djm 638: } else if (strcasecmp(attrib, "user") == 0) {
1.221 djm 639: criteria = xstrdup(ruser);
1.235 djm 640: r = match_pattern_list(ruser, arg, 0) == 1;
1.221 djm 641: if (r == (negate ? 1 : 0))
642: this_result = result = 0;
1.206 djm 643: } else if (strcasecmp(attrib, "localuser") == 0) {
1.221 djm 644: criteria = xstrdup(pw->pw_name);
1.235 djm 645: r = match_pattern_list(pw->pw_name, arg, 0) == 1;
1.221 djm 646: if (r == (negate ? 1 : 0))
647: this_result = result = 0;
1.210 djm 648: } else if (strcasecmp(attrib, "exec") == 0) {
1.333 dtucker 649: char *conn_hash_hex, *keyalias;
1.327 dtucker 650:
1.206 djm 651: if (gethostname(thishost, sizeof(thishost)) == -1)
652: fatal("gethostname: %s", strerror(errno));
653: strlcpy(shorthost, thishost, sizeof(shorthost));
654: shorthost[strcspn(thishost, ".")] = '\0';
655: snprintf(portstr, sizeof(portstr), "%d", port);
1.288 djm 656: snprintf(uidstr, sizeof(uidstr), "%llu",
657: (unsigned long long)pw->pw_uid);
1.327 dtucker 658: conn_hash_hex = ssh_connection_hash(thishost, host,
1.328 dtucker 659: portstr, ruser);
1.333 dtucker 660: keyalias = options->host_key_alias ?
661: options->host_key_alias : host;
1.206 djm 662:
663: cmd = percent_expand(arg,
1.327 dtucker 664: "C", conn_hash_hex,
1.206 djm 665: "L", shorthost,
666: "d", pw->pw_dir,
667: "h", host,
1.333 dtucker 668: "k", keyalias,
1.206 djm 669: "l", thishost,
1.221 djm 670: "n", original_host,
1.206 djm 671: "p", portstr,
672: "r", ruser,
673: "u", pw->pw_name,
1.288 djm 674: "i", uidstr,
1.206 djm 675: (char *)NULL);
1.327 dtucker 676: free(conn_hash_hex);
1.217 djm 677: if (result != 1) {
678: /* skip execution if prior predicate failed */
1.221 djm 679: debug3("%.200s line %d: skipped exec "
680: "\"%.100s\"", filename, linenum, cmd);
681: free(cmd);
682: continue;
683: }
684: r = execute_in_shell(cmd);
685: if (r == -1) {
686: fatal("%.200s line %d: match exec "
687: "'%.100s' error", filename,
688: linenum, cmd);
1.217 djm 689: }
1.221 djm 690: criteria = xstrdup(cmd);
1.206 djm 691: free(cmd);
1.221 djm 692: /* Force exit status to boolean */
693: r = r == 0;
694: if (r == (negate ? 1 : 0))
695: this_result = result = 0;
1.206 djm 696: } else {
697: error("Unsupported Match attribute %s", attrib);
1.211 djm 698: result = -1;
699: goto out;
1.206 djm 700: }
1.221 djm 701: debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
702: filename, linenum, this_result ? "": "not ",
703: oattrib, criteria);
704: free(criteria);
1.213 dtucker 705: }
706: if (attributes == 0) {
707: error("One or more attributes required for Match");
708: result = -1;
709: goto out;
1.206 djm 710: }
1.221 djm 711: out:
712: if (result != -1)
713: debug2("match %sfound", result ? "" : "not ");
1.206 djm 714: *condition = cp;
1.211 djm 715: free(host);
1.206 djm 716: return result;
717: }
718:
1.286 djm 719: /* Remove environment variable by pattern */
720: static void
721: rm_env(Options *options, const char *arg, const char *filename, int linenum)
722: {
1.330 djm 723: int i, j, onum_send_env = options->num_send_env;
1.286 djm 724: char *cp;
725:
726: /* Remove an environment variable */
727: for (i = 0; i < options->num_send_env; ) {
728: cp = xstrdup(options->send_env[i]);
729: if (!match_pattern(cp, arg + 1)) {
730: free(cp);
731: i++;
732: continue;
733: }
734: debug3("%s line %d: removing environment %s",
735: filename, linenum, cp);
736: free(cp);
737: free(options->send_env[i]);
738: options->send_env[i] = NULL;
739: for (j = i; j < options->num_send_env - 1; j++) {
740: options->send_env[j] = options->send_env[j + 1];
741: options->send_env[j + 1] = NULL;
742: }
743: options->num_send_env--;
744: /* NB. don't increment i */
1.330 djm 745: }
746: if (onum_send_env != options->num_send_env) {
747: options->send_env = xrecallocarray(options->send_env,
748: onum_send_env, options->num_send_env,
749: sizeof(*options->send_env));
1.286 djm 750: }
751: }
752:
1.19 markus 753: /*
1.70 stevesk 754: * Returns the number of the token pointed to by cp or oBadOption.
1.19 markus 755: */
1.26 markus 756: static OpCodes
1.199 djm 757: parse_token(const char *cp, const char *filename, int linenum,
758: const char *ignored_unknown)
1.1 deraadt 759: {
1.199 djm 760: int i;
1.1 deraadt 761:
1.17 markus 762: for (i = 0; keywords[i].name; i++)
1.199 djm 763: if (strcmp(cp, keywords[i].name) == 0)
1.17 markus 764: return keywords[i].opcode;
1.235 djm 765: if (ignored_unknown != NULL &&
766: match_pattern_list(cp, ignored_unknown, 1) == 1)
1.199 djm 767: return oIgnoredUnknownOption;
1.75 stevesk 768: error("%s: line %d: Bad configuration option: %s",
769: filename, linenum, cp);
1.17 markus 770: return oBadOption;
1.1 deraadt 771: }
772:
1.207 djm 773: /* Multistate option parsing */
774: struct multistate {
775: char *key;
776: int value;
777: };
778: static const struct multistate multistate_flag[] = {
779: { "true", 1 },
780: { "false", 0 },
781: { "yes", 1 },
782: { "no", 0 },
783: { NULL, -1 }
784: };
785: static const struct multistate multistate_yesnoask[] = {
786: { "true", 1 },
787: { "false", 0 },
788: { "yes", 1 },
789: { "no", 0 },
790: { "ask", 2 },
791: { NULL, -1 }
792: };
1.278 djm 793: static const struct multistate multistate_strict_hostkey[] = {
794: { "true", SSH_STRICT_HOSTKEY_YES },
795: { "false", SSH_STRICT_HOSTKEY_OFF },
796: { "yes", SSH_STRICT_HOSTKEY_YES },
797: { "no", SSH_STRICT_HOSTKEY_OFF },
798: { "ask", SSH_STRICT_HOSTKEY_ASK },
799: { "off", SSH_STRICT_HOSTKEY_OFF },
800: { "accept-new", SSH_STRICT_HOSTKEY_NEW },
801: { NULL, -1 }
802: };
1.246 jcs 803: static const struct multistate multistate_yesnoaskconfirm[] = {
804: { "true", 1 },
805: { "false", 0 },
806: { "yes", 1 },
807: { "no", 0 },
808: { "ask", 2 },
809: { "confirm", 3 },
810: { NULL, -1 }
811: };
1.207 djm 812: static const struct multistate multistate_addressfamily[] = {
813: { "inet", AF_INET },
814: { "inet6", AF_INET6 },
815: { "any", AF_UNSPEC },
816: { NULL, -1 }
817: };
818: static const struct multistate multistate_controlmaster[] = {
819: { "true", SSHCTL_MASTER_YES },
820: { "yes", SSHCTL_MASTER_YES },
821: { "false", SSHCTL_MASTER_NO },
822: { "no", SSHCTL_MASTER_NO },
823: { "auto", SSHCTL_MASTER_AUTO },
824: { "ask", SSHCTL_MASTER_ASK },
825: { "autoask", SSHCTL_MASTER_AUTO_ASK },
826: { NULL, -1 }
827: };
828: static const struct multistate multistate_tunnel[] = {
829: { "ethernet", SSH_TUNMODE_ETHERNET },
830: { "point-to-point", SSH_TUNMODE_POINTOPOINT },
831: { "true", SSH_TUNMODE_DEFAULT },
832: { "yes", SSH_TUNMODE_DEFAULT },
833: { "false", SSH_TUNMODE_NO },
834: { "no", SSH_TUNMODE_NO },
835: { NULL, -1 }
836: };
837: static const struct multistate multistate_requesttty[] = {
838: { "true", REQUEST_TTY_YES },
839: { "yes", REQUEST_TTY_YES },
840: { "false", REQUEST_TTY_NO },
841: { "no", REQUEST_TTY_NO },
842: { "force", REQUEST_TTY_FORCE },
843: { "auto", REQUEST_TTY_AUTO },
844: { NULL, -1 }
845: };
1.209 djm 846: static const struct multistate multistate_canonicalizehostname[] = {
1.208 djm 847: { "true", SSH_CANONICALISE_YES },
848: { "false", SSH_CANONICALISE_NO },
849: { "yes", SSH_CANONICALISE_YES },
850: { "no", SSH_CANONICALISE_NO },
851: { "always", SSH_CANONICALISE_ALWAYS },
852: { NULL, -1 }
853: };
1.322 dtucker 854: static const struct multistate multistate_compression[] = {
855: #ifdef WITH_ZLIB
856: { "yes", COMP_ZLIB },
857: #endif
858: { "no", COMP_NONE },
859: { NULL, -1 }
860: };
1.207 djm 861:
1.334 djm 862: static int
863: parse_multistate_value(const char *arg, const char *filename, int linenum,
864: const struct multistate *multistate_ptr)
865: {
866: int i;
867:
1.344 djm 868: if (!arg || *arg == '\0') {
869: error("%s line %d: missing argument.", filename, linenum);
870: return -1;
871: }
1.334 djm 872: for (i = 0; multistate_ptr[i].key != NULL; i++) {
873: if (strcasecmp(arg, multistate_ptr[i].key) == 0)
874: return multistate_ptr[i].value;
875: }
876: return -1;
877: }
878:
1.19 markus 879: /*
880: * Processes a single option line as used in the configuration files. This
881: * only sets those values that have not already been set.
882: */
1.14 markus 883: int
1.206 djm 884: process_config_line(Options *options, struct passwd *pw, const char *host,
1.221 djm 885: const char *original_host, char *line, const char *filename,
886: int linenum, int *activep, int flags)
1.1 deraadt 887: {
1.252 djm 888: return process_config_line_depth(options, pw, host, original_host,
1.302 djm 889: line, filename, linenum, activep, flags, NULL, 0);
1.252 djm 890: }
891:
892: #define WHITESPACE " \t\r\n"
893: static int
894: process_config_line_depth(Options *options, struct passwd *pw, const char *host,
895: const char *original_host, char *line, const char *filename,
1.302 djm 896: int linenum, int *activep, int flags, int *want_final_pass, int depth)
1.252 djm 897: {
1.193 djm 898: char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
1.339 djm 899: char **cpptr, ***cppptr, fwdarg[256];
1.199 djm 900: u_int i, *uintptr, max_entries = 0;
1.252 djm 901: int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
1.279 markus 902: int remotefwd, dynamicfwd;
1.164 dtucker 903: LogLevel *log_level_ptr;
1.271 dtucker 904: SyslogFacility *log_facility_ptr;
1.201 dtucker 905: long long val64;
1.102 markus 906: size_t len;
1.220 millert 907: struct Forward fwd;
1.207 djm 908: const struct multistate *multistate_ptr;
1.208 djm 909: struct allowed_cname *cname;
1.252 djm 910: glob_t gl;
1.281 dtucker 911: const char *errstr;
1.106 djm 912:
1.206 djm 913: if (activep == NULL) { /* We are processing a command line directive */
914: cmdline = 1;
915: activep = &cmdline;
916: }
917:
1.267 djm 918: /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1.233 djm 919: if ((len = strlen(line)) == 0)
920: return 0;
921: for (len--; len > 0; len--) {
1.267 djm 922: if (strchr(WHITESPACE "\f", line[len]) == NULL)
1.106 djm 923: break;
924: line[len] = '\0';
925: }
1.1 deraadt 926:
1.42 provos 927: s = line;
928: /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149 djm 929: if ((keyword = strdelim(&s)) == NULL)
930: return 0;
1.42 provos 931: /* Ignore leading whitespace. */
932: if (*keyword == '\0')
1.43 markus 933: keyword = strdelim(&s);
1.56 deraadt 934: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 935: return 0;
1.199 djm 936: /* Match lowercase keyword */
1.207 djm 937: lowercase(keyword);
1.17 markus 938:
1.199 djm 939: opcode = parse_token(keyword, filename, linenum,
940: options->ignored_unknown);
1.17 markus 941:
942: switch (opcode) {
943: case oBadOption:
1.19 markus 944: /* don't panic, but count bad options */
945: return -1;
1.273 djm 946: case oIgnore:
947: return 0;
1.199 djm 948: case oIgnoredUnknownOption:
949: debug("%s line %d: Ignored unknown option \"%s\"",
950: filename, linenum, keyword);
951: return 0;
1.111 djm 952: case oConnectTimeout:
953: intptr = &options->connection_timeout;
1.127 markus 954: parse_time:
1.111 djm 955: arg = strdelim(&s);
1.344 djm 956: if (!arg || *arg == '\0') {
957: error("%s line %d: missing time value.",
1.111 djm 958: filename, linenum);
1.344 djm 959: return -1;
960: }
1.221 djm 961: if (strcmp(arg, "none") == 0)
962: value = -1;
1.344 djm 963: else if ((value = convtime(arg)) == -1) {
964: error("%s line %d: invalid time value.",
1.111 djm 965: filename, linenum);
1.344 djm 966: return -1;
967: }
1.160 dtucker 968: if (*activep && *intptr == -1)
1.111 djm 969: *intptr = value;
970: break;
971:
1.17 markus 972: case oForwardAgent:
973: intptr = &options->forward_agent;
1.319 djm 974:
975: arg = strdelim(&s);
1.344 djm 976: if (!arg || *arg == '\0') {
977: error("%s line %d: missing argument.",
1.319 djm 978: filename, linenum);
1.344 djm 979: return -1;
980: }
1.319 djm 981:
982: value = -1;
983: multistate_ptr = multistate_flag;
984: for (i = 0; multistate_ptr[i].key != NULL; i++) {
985: if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
986: value = multistate_ptr[i].value;
987: break;
988: }
989: }
990: if (value != -1) {
991: if (*activep && *intptr == -1)
992: *intptr = value;
993: break;
994: }
995: /* ForwardAgent wasn't 'yes' or 'no', assume a path */
996: if (*activep && *intptr == -1)
997: *intptr = 1;
998:
999: charptr = &options->forward_agent_sock_path;
1000: goto parse_agent_path;
1001:
1002: case oForwardX11:
1003: intptr = &options->forward_x11;
1.207 djm 1004: parse_flag:
1005: multistate_ptr = multistate_flag;
1006: parse_multistate:
1.42 provos 1007: arg = strdelim(&s);
1.334 djm 1008: if ((value = parse_multistate_value(arg, filename, linenum,
1009: multistate_ptr)) == -1) {
1.344 djm 1010: error("%s line %d: unsupported option \"%s\".",
1.207 djm 1011: filename, linenum, arg);
1.344 djm 1012: return -1;
1.334 djm 1013: }
1.17 markus 1014: if (*activep && *intptr == -1)
1015: *intptr = value;
1016: break;
1017:
1.123 markus 1018: case oForwardX11Trusted:
1019: intptr = &options->forward_x11_trusted;
1020: goto parse_flag;
1.221 djm 1021:
1.186 djm 1022: case oForwardX11Timeout:
1023: intptr = &options->forward_x11_timeout;
1024: goto parse_time;
1.123 markus 1025:
1.17 markus 1026: case oGatewayPorts:
1.220 millert 1027: intptr = &options->fwd_opts.gateway_ports;
1.17 markus 1028: goto parse_flag;
1029:
1.153 markus 1030: case oExitOnForwardFailure:
1031: intptr = &options->exit_on_forward_failure;
1032: goto parse_flag;
1033:
1.17 markus 1034: case oPasswordAuthentication:
1035: intptr = &options->password_authentication;
1036: goto parse_flag;
1037:
1.48 markus 1038: case oKbdInteractiveAuthentication:
1039: intptr = &options->kbd_interactive_authentication;
1040: goto parse_flag;
1041:
1042: case oKbdInteractiveDevices:
1043: charptr = &options->kbd_interactive_devices;
1044: goto parse_string;
1045:
1.50 markus 1046: case oPubkeyAuthentication:
1047: intptr = &options->pubkey_authentication;
1.30 markus 1048: goto parse_flag;
1049:
1.72 markus 1050: case oHostbasedAuthentication:
1051: intptr = &options->hostbased_authentication;
1052: goto parse_flag;
1053:
1.59 markus 1054: case oChallengeResponseAuthentication:
1.78 markus 1055: intptr = &options->challenge_response_authentication;
1.17 markus 1056: goto parse_flag;
1.108 jakob 1057:
1.118 markus 1058: case oGssAuthentication:
1059: intptr = &options->gss_authentication;
1060: goto parse_flag;
1061:
1062: case oGssDelegateCreds:
1063: intptr = &options->gss_deleg_creds;
1064: goto parse_flag;
1065:
1.17 markus 1066: case oBatchMode:
1067: intptr = &options->batch_mode;
1068: goto parse_flag;
1069:
1070: case oCheckHostIP:
1071: intptr = &options->check_host_ip;
1.167 grunk 1072: goto parse_flag;
1.17 markus 1073:
1.107 jakob 1074: case oVerifyHostKeyDNS:
1075: intptr = &options->verify_host_key_dns;
1.207 djm 1076: multistate_ptr = multistate_yesnoask;
1077: goto parse_multistate;
1.107 jakob 1078:
1.17 markus 1079: case oStrictHostKeyChecking:
1080: intptr = &options->strict_host_key_checking;
1.278 djm 1081: multistate_ptr = multistate_strict_hostkey;
1.207 djm 1082: goto parse_multistate;
1.17 markus 1083:
1084: case oCompression:
1085: intptr = &options->compression;
1.322 dtucker 1086: multistate_ptr = multistate_compression;
1087: goto parse_multistate;
1.17 markus 1088:
1.126 markus 1089: case oTCPKeepAlive:
1090: intptr = &options->tcp_keep_alive;
1.17 markus 1091: goto parse_flag;
1092:
1.91 markus 1093: case oNoHostAuthenticationForLocalhost:
1094: intptr = &options->no_host_authentication_for_localhost;
1095: goto parse_flag;
1096:
1.17 markus 1097: case oNumberOfPasswordPrompts:
1098: intptr = &options->number_of_password_prompts;
1099: goto parse_int;
1100:
1.105 markus 1101: case oRekeyLimit:
1102: arg = strdelim(&s);
1.344 djm 1103: if (!arg || *arg == '\0') {
1104: error("%.200s line %d: Missing argument.", filename,
1.198 dtucker 1105: linenum);
1.344 djm 1106: return -1;
1107: }
1.198 dtucker 1108: if (strcmp(arg, "default") == 0) {
1109: val64 = 0;
1110: } else {
1.344 djm 1111: if (scan_scaled(arg, &val64) == -1) {
1112: error("%.200s line %d: Bad number '%s': %s",
1.200 dtucker 1113: filename, linenum, arg, strerror(errno));
1.344 djm 1114: return -1;
1115: }
1116: if (val64 != 0 && val64 < 16) {
1117: error("%.200s line %d: RekeyLimit too small",
1.198 dtucker 1118: filename, linenum);
1.344 djm 1119: return -1;
1120: }
1.105 markus 1121: }
1.165 djm 1122: if (*activep && options->rekey_limit == -1)
1.249 dtucker 1123: options->rekey_limit = val64;
1.198 dtucker 1124: if (s != NULL) { /* optional rekey interval present */
1125: if (strcmp(s, "none") == 0) {
1126: (void)strdelim(&s); /* discard */
1127: break;
1128: }
1129: intptr = &options->rekey_interval;
1130: goto parse_time;
1131: }
1.105 markus 1132: break;
1133:
1.17 markus 1134: case oIdentityFile:
1.42 provos 1135: arg = strdelim(&s);
1.344 djm 1136: if (!arg || *arg == '\0') {
1137: error("%.200s line %d: Missing argument.",
1138: filename, linenum);
1139: return -1;
1140: }
1.17 markus 1141: if (*activep) {
1.50 markus 1142: intptr = &options->num_identity_files;
1.344 djm 1143: if (*intptr >= SSH_MAX_IDENTITY_FILES) {
1144: error("%.200s line %d: Too many identity files "
1145: "specified (max %d).", filename, linenum,
1146: SSH_MAX_IDENTITY_FILES);
1147: return -1;
1148: }
1.221 djm 1149: add_identity_file(options, NULL,
1150: arg, flags & SSHCONF_USERCONF);
1.17 markus 1151: }
1152: break;
1153:
1.241 djm 1154: case oCertificateFile:
1155: arg = strdelim(&s);
1.344 djm 1156: if (!arg || *arg == '\0') {
1157: error("%.200s line %d: Missing argument.",
1.241 djm 1158: filename, linenum);
1.344 djm 1159: return -1;
1160: }
1.241 djm 1161: if (*activep) {
1162: intptr = &options->num_certificate_files;
1163: if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1.344 djm 1164: error("%.200s line %d: Too many certificate "
1.241 djm 1165: "files specified (max %d).",
1166: filename, linenum,
1167: SSH_MAX_CERTIFICATE_FILES);
1.344 djm 1168: return -1;
1.241 djm 1169: }
1170: add_certificate_file(options, arg,
1171: flags & SSHCONF_USERCONF);
1172: }
1173: break;
1174:
1.34 markus 1175: case oXAuthLocation:
1176: charptr=&options->xauth_location;
1177: goto parse_string;
1178:
1.17 markus 1179: case oUser:
1180: charptr = &options->user;
1181: parse_string:
1.42 provos 1182: arg = strdelim(&s);
1.344 djm 1183: if (!arg || *arg == '\0') {
1184: error("%.200s line %d: Missing argument.",
1.193 djm 1185: filename, linenum);
1.344 djm 1186: return -1;
1187: }
1.17 markus 1188: if (*activep && *charptr == NULL)
1.38 provos 1189: *charptr = xstrdup(arg);
1.17 markus 1190: break;
1191:
1192: case oGlobalKnownHostsFile:
1.193 djm 1193: cpptr = (char **)&options->system_hostfiles;
1194: uintptr = &options->num_system_hostfiles;
1195: max_entries = SSH_MAX_HOSTS_FILES;
1196: parse_char_array:
1197: if (*activep && *uintptr == 0) {
1198: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.344 djm 1199: if ((*uintptr) >= max_entries) {
1200: error("%s line %d: too many known "
1201: "hosts files.", filename, linenum);
1202: return -1;
1203: }
1.193 djm 1204: cpptr[(*uintptr)++] = xstrdup(arg);
1205: }
1206: }
1207: return 0;
1.17 markus 1208:
1209: case oUserKnownHostsFile:
1.193 djm 1210: cpptr = (char **)&options->user_hostfiles;
1211: uintptr = &options->num_user_hostfiles;
1212: max_entries = SSH_MAX_HOSTS_FILES;
1213: goto parse_char_array;
1.27 markus 1214:
1.306 jmc 1215: case oHostname:
1.17 markus 1216: charptr = &options->hostname;
1217: goto parse_string;
1218:
1.52 markus 1219: case oHostKeyAlias:
1220: charptr = &options->host_key_alias;
1221: goto parse_string;
1222:
1.67 markus 1223: case oPreferredAuthentications:
1224: charptr = &options->preferred_authentications;
1225: goto parse_string;
1226:
1.77 markus 1227: case oBindAddress:
1228: charptr = &options->bind_address;
1229: goto parse_string;
1230:
1.282 djm 1231: case oBindInterface:
1232: charptr = &options->bind_interface;
1233: goto parse_string;
1234:
1.183 markus 1235: case oPKCS11Provider:
1236: charptr = &options->pkcs11_provider;
1.86 markus 1237: goto parse_string;
1.85 jakob 1238:
1.310 djm 1239: case oSecurityKeyProvider:
1240: charptr = &options->sk_provider;
1241: goto parse_string;
1242:
1.17 markus 1243: case oProxyCommand:
1.144 reyk 1244: charptr = &options->proxy_command;
1.257 djm 1245: /* Ignore ProxyCommand if ProxyJump already specified */
1246: if (options->jump_host != NULL)
1247: charptr = &options->jump_host; /* Skip below */
1.144 reyk 1248: parse_command:
1.344 djm 1249: if (s == NULL) {
1250: error("%.200s line %d: Missing argument.",
1251: filename, linenum);
1252: return -1;
1253: }
1.102 markus 1254: len = strspn(s, WHITESPACE "=");
1.17 markus 1255: if (*activep && *charptr == NULL)
1.102 markus 1256: *charptr = xstrdup(s + len);
1.17 markus 1257: return 0;
1258:
1.257 djm 1259: case oProxyJump:
1260: if (s == NULL) {
1.344 djm 1261: error("%.200s line %d: Missing argument.",
1.257 djm 1262: filename, linenum);
1.344 djm 1263: return -1;
1.257 djm 1264: }
1265: len = strspn(s, WHITESPACE "=");
1266: if (parse_jump(s + len, options, *activep) == -1) {
1.344 djm 1267: error("%.200s line %d: Invalid ProxyJump \"%s\"",
1.257 djm 1268: filename, linenum, s + len);
1.344 djm 1269: return -1;
1.257 djm 1270: }
1271: return 0;
1272:
1.17 markus 1273: case oPort:
1.300 naddy 1274: arg = strdelim(&s);
1.344 djm 1275: if (!arg || *arg == '\0') {
1276: error("%.200s line %d: Missing argument.",
1.300 naddy 1277: filename, linenum);
1.344 djm 1278: return -1;
1279: }
1.300 naddy 1280: value = a2port(arg);
1.344 djm 1281: if (value <= 0) {
1282: error("%.200s line %d: Bad port '%s'.",
1.300 naddy 1283: filename, linenum, arg);
1.344 djm 1284: return -1;
1285: }
1.300 naddy 1286: if (*activep && options->port == -1)
1287: options->port = value;
1288: break;
1289:
1290: case oConnectionAttempts:
1291: intptr = &options->connection_attempts;
1.17 markus 1292: parse_int:
1.42 provos 1293: arg = strdelim(&s);
1.344 djm 1294: if ((errstr = atoi_err(arg, &value)) != NULL) {
1295: error("%s line %d: integer value %s.",
1.281 dtucker 1296: filename, linenum, errstr);
1.344 djm 1297: return -1;
1298: }
1.17 markus 1299: if (*activep && *intptr == -1)
1300: *intptr = value;
1301: break;
1302:
1.25 markus 1303: case oCiphers:
1.42 provos 1304: arg = strdelim(&s);
1.344 djm 1305: if (!arg || *arg == '\0') {
1306: error("%.200s line %d: Missing argument.",
1307: filename, linenum);
1308: return -1;
1309: }
1.309 naddy 1310: if (*arg != '-' &&
1.344 djm 1311: !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)){
1312: error("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93 deraadt 1313: filename, linenum, arg ? arg : "<NONE>");
1.344 djm 1314: return -1;
1315: }
1.25 markus 1316: if (*activep && options->ciphers == NULL)
1.38 provos 1317: options->ciphers = xstrdup(arg);
1.25 markus 1318: break;
1319:
1.62 markus 1320: case oMacs:
1321: arg = strdelim(&s);
1.344 djm 1322: if (!arg || *arg == '\0') {
1323: error("%.200s line %d: Missing argument.",
1324: filename, linenum);
1325: return -1;
1326: }
1.309 naddy 1327: if (*arg != '-' &&
1.344 djm 1328: !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)) {
1329: error("%.200s line %d: Bad SSH2 MAC spec '%s'.",
1.93 deraadt 1330: filename, linenum, arg ? arg : "<NONE>");
1.344 djm 1331: return -1;
1332: }
1.62 markus 1333: if (*activep && options->macs == NULL)
1334: options->macs = xstrdup(arg);
1335: break;
1336:
1.189 djm 1337: case oKexAlgorithms:
1338: arg = strdelim(&s);
1.344 djm 1339: if (!arg || *arg == '\0') {
1340: error("%.200s line %d: Missing argument.",
1.189 djm 1341: filename, linenum);
1.344 djm 1342: return -1;
1343: }
1.268 djm 1344: if (*arg != '-' &&
1.309 naddy 1345: !kex_names_valid(*arg == '+' || *arg == '^' ?
1.344 djm 1346: arg + 1 : arg)) {
1347: error("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1.189 djm 1348: filename, linenum, arg ? arg : "<NONE>");
1.344 djm 1349: return -1;
1350: }
1.189 djm 1351: if (*activep && options->kex_algorithms == NULL)
1352: options->kex_algorithms = xstrdup(arg);
1353: break;
1354:
1.76 markus 1355: case oHostKeyAlgorithms:
1.238 markus 1356: charptr = &options->hostkeyalgorithms;
1357: parse_keytypes:
1.76 markus 1358: arg = strdelim(&s);
1.344 djm 1359: if (!arg || *arg == '\0') {
1360: error("%.200s line %d: Missing argument.",
1.238 markus 1361: filename, linenum);
1.344 djm 1362: return -1;
1363: }
1.268 djm 1364: if (*arg != '-' &&
1.309 naddy 1365: !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1.344 djm 1366: arg + 1 : arg, 1)) {
1367: error("%s line %d: Bad key types '%s'.",
1368: filename, linenum, arg ? arg : "<NONE>");
1369: return -1;
1370: }
1.238 markus 1371: if (*activep && *charptr == NULL)
1372: *charptr = xstrdup(arg);
1.76 markus 1373: break;
1374:
1.298 djm 1375: case oCASignatureAlgorithms:
1376: charptr = &options->ca_sign_algorithms;
1377: goto parse_keytypes;
1378:
1.17 markus 1379: case oLogLevel:
1.164 dtucker 1380: log_level_ptr = &options->log_level;
1.42 provos 1381: arg = strdelim(&s);
1.38 provos 1382: value = log_level_number(arg);
1.344 djm 1383: if (value == SYSLOG_LEVEL_NOT_SET) {
1384: error("%.200s line %d: unsupported log level '%s'",
1.93 deraadt 1385: filename, linenum, arg ? arg : "<NONE>");
1.344 djm 1386: return -1;
1387: }
1.164 dtucker 1388: if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1389: *log_level_ptr = (LogLevel) value;
1.17 markus 1390: break;
1391:
1.271 dtucker 1392: case oLogFacility:
1393: log_facility_ptr = &options->log_facility;
1394: arg = strdelim(&s);
1395: value = log_facility_number(arg);
1.344 djm 1396: if (value == SYSLOG_FACILITY_NOT_SET) {
1397: error("%.200s line %d: unsupported log facility '%s'",
1.271 dtucker 1398: filename, linenum, arg ? arg : "<NONE>");
1.344 djm 1399: return -1;
1400: }
1.271 dtucker 1401: if (*log_facility_ptr == -1)
1402: *log_facility_ptr = (SyslogFacility) value;
1403: break;
1404:
1.339 djm 1405: case oLogVerbose:
1406: cppptr = &options->log_verbose;
1407: uintptr = &options->num_log_verbose;
1408: if (*activep && *uintptr == 0) {
1409: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1410: *cppptr = xrecallocarray(*cppptr, *uintptr,
1411: *uintptr + 1, sizeof(**cppptr));
1412: (*cppptr)[(*uintptr)++] = xstrdup(arg);
1413: }
1414: }
1415: return 0;
1416:
1.88 stevesk 1417: case oLocalForward:
1.17 markus 1418: case oRemoteForward:
1.168 stevesk 1419: case oDynamicForward:
1.42 provos 1420: arg = strdelim(&s);
1.344 djm 1421: if (!arg || *arg == '\0') {
1422: error("%.200s line %d: Missing argument.",
1.88 stevesk 1423: filename, linenum);
1.344 djm 1424: return -1;
1425: }
1.135 djm 1426:
1.279 markus 1427: remotefwd = (opcode == oRemoteForward);
1428: dynamicfwd = (opcode == oDynamicForward);
1429:
1430: if (!dynamicfwd) {
1.168 stevesk 1431: arg2 = strdelim(&s);
1.279 markus 1432: if (arg2 == NULL || *arg2 == '\0') {
1433: if (remotefwd)
1434: dynamicfwd = 1;
1.344 djm 1435: else {
1436: error("%.200s line %d: Missing target "
1.279 markus 1437: "argument.", filename, linenum);
1.344 djm 1438: return -1;
1439: }
1.279 markus 1440: } else {
1441: /* construct a string for parse_forward */
1442: snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1443: arg2);
1444: }
1445: }
1446: if (dynamicfwd)
1.168 stevesk 1447: strlcpy(fwdarg, arg, sizeof(fwdarg));
1448:
1.344 djm 1449: if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0) {
1450: error("%.200s line %d: Bad forwarding specification.",
1.88 stevesk 1451: filename, linenum);
1.344 djm 1452: return -1;
1453: }
1.135 djm 1454:
1.88 stevesk 1455: if (*activep) {
1.279 markus 1456: if (remotefwd) {
1457: add_remote_forward(options, &fwd);
1458: } else {
1.135 djm 1459: add_local_forward(options, &fwd);
1.279 markus 1460: }
1.88 stevesk 1461: }
1.17 markus 1462: break;
1.71 markus 1463:
1.90 stevesk 1464: case oClearAllForwardings:
1465: intptr = &options->clear_forwardings;
1466: goto parse_flag;
1467:
1.17 markus 1468: case oHost:
1.344 djm 1469: if (cmdline) {
1470: error("Host directive not supported as a command-line "
1.206 djm 1471: "option");
1.344 djm 1472: return -1;
1473: }
1.17 markus 1474: *activep = 0;
1.191 djm 1475: arg2 = NULL;
1476: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.252 djm 1477: if ((flags & SSHCONF_NEVERMATCH) != 0)
1478: break;
1.191 djm 1479: negated = *arg == '!';
1480: if (negated)
1481: arg++;
1.38 provos 1482: if (match_pattern(host, arg)) {
1.191 djm 1483: if (negated) {
1484: debug("%.200s line %d: Skipping Host "
1485: "block because of negated match "
1486: "for %.100s", filename, linenum,
1487: arg);
1488: *activep = 0;
1489: break;
1490: }
1491: if (!*activep)
1492: arg2 = arg; /* logged below */
1.17 markus 1493: *activep = 1;
1494: }
1.191 djm 1495: }
1496: if (*activep)
1497: debug("%.200s line %d: Applying options for %.100s",
1498: filename, linenum, arg2);
1.42 provos 1499: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 1500: return 0;
1501:
1.206 djm 1502: case oMatch:
1.344 djm 1503: if (cmdline) {
1504: error("Host directive not supported as a command-line "
1.206 djm 1505: "option");
1.344 djm 1506: return -1;
1507: }
1.221 djm 1508: value = match_cfg_line(options, &s, pw, host, original_host,
1.302 djm 1509: flags & SSHCONF_FINAL, want_final_pass,
1510: filename, linenum);
1.344 djm 1511: if (value < 0) {
1512: error("%.200s line %d: Bad Match condition", filename,
1.206 djm 1513: linenum);
1.344 djm 1514: return -1;
1515: }
1.252 djm 1516: *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
1.206 djm 1517: break;
1518:
1.17 markus 1519: case oEscapeChar:
1520: intptr = &options->escape_char;
1.42 provos 1521: arg = strdelim(&s);
1.344 djm 1522: if (!arg || *arg == '\0') {
1523: error("%.200s line %d: Missing argument.",
1524: filename, linenum);
1525: return -1;
1526: }
1.236 djm 1527: if (strcmp(arg, "none") == 0)
1528: value = SSH_ESCAPECHAR_NONE;
1529: else if (arg[1] == '\0')
1530: value = (u_char) arg[0];
1531: else if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 1532: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1533: value = (u_char) arg[1] & 31;
1.17 markus 1534: else {
1.344 djm 1535: error("%.200s line %d: Bad escape character.",
1.93 deraadt 1536: filename, linenum);
1.344 djm 1537: return -1;
1.17 markus 1538: }
1539: if (*activep && *intptr == -1)
1540: *intptr = value;
1.112 djm 1541: break;
1542:
1543: case oAddressFamily:
1.114 djm 1544: intptr = &options->address_family;
1.207 djm 1545: multistate_ptr = multistate_addressfamily;
1546: goto parse_multistate;
1.17 markus 1547:
1.101 markus 1548: case oEnableSSHKeysign:
1549: intptr = &options->enable_ssh_keysign;
1550: goto parse_flag;
1551:
1.128 markus 1552: case oIdentitiesOnly:
1553: intptr = &options->identities_only;
1554: goto parse_flag;
1555:
1.127 markus 1556: case oServerAliveInterval:
1557: intptr = &options->server_alive_interval;
1558: goto parse_time;
1559:
1560: case oServerAliveCountMax:
1561: intptr = &options->server_alive_count_max;
1562: goto parse_int;
1563:
1.130 djm 1564: case oSendEnv:
1565: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.344 djm 1566: if (strchr(arg, '=') != NULL) {
1567: error("%s line %d: Invalid environment name.",
1.130 djm 1568: filename, linenum);
1.344 djm 1569: return -1;
1570: }
1.137 djm 1571: if (!*activep)
1572: continue;
1.286 djm 1573: if (*arg == '-') {
1574: /* Removing an env var */
1575: rm_env(options, arg, filename, linenum);
1576: continue;
1577: } else {
1578: /* Adding an env var */
1.344 djm 1579: if (options->num_send_env >= INT_MAX) {
1580: error("%s line %d: too many send env.",
1.286 djm 1581: filename, linenum);
1.344 djm 1582: return -1;
1583: }
1.290 djm 1584: options->send_env = xrecallocarray(
1585: options->send_env, options->num_send_env,
1.291 djm 1586: options->num_send_env + 1,
1.290 djm 1587: sizeof(*options->send_env));
1.286 djm 1588: options->send_env[options->num_send_env++] =
1589: xstrdup(arg);
1590: }
1.130 djm 1591: }
1592: break;
1593:
1.290 djm 1594: case oSetEnv:
1595: value = options->num_setenv;
1596: while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1.344 djm 1597: if (strchr(arg, '=') == NULL) {
1598: error("%s line %d: Invalid SetEnv.",
1.290 djm 1599: filename, linenum);
1.344 djm 1600: return -1;
1601: }
1.290 djm 1602: if (!*activep || value != 0)
1603: continue;
1604: /* Adding a setenv var */
1.344 djm 1605: if (options->num_setenv >= INT_MAX) {
1606: error("%s line %d: too many SetEnv.",
1.290 djm 1607: filename, linenum);
1.344 djm 1608: return -1;
1609: }
1.290 djm 1610: options->setenv = xrecallocarray(
1611: options->setenv, options->num_setenv,
1612: options->num_setenv + 1, sizeof(*options->setenv));
1613: options->setenv[options->num_setenv++] = xstrdup(arg);
1614: }
1615: break;
1616:
1.132 djm 1617: case oControlPath:
1618: charptr = &options->control_path;
1619: goto parse_string;
1620:
1621: case oControlMaster:
1622: intptr = &options->control_master;
1.207 djm 1623: multistate_ptr = multistate_controlmaster;
1624: goto parse_multistate;
1.132 djm 1625:
1.187 djm 1626: case oControlPersist:
1627: /* no/false/yes/true, or a time spec */
1628: intptr = &options->control_persist;
1629: arg = strdelim(&s);
1.344 djm 1630: if (!arg || *arg == '\0') {
1631: error("%.200s line %d: Missing ControlPersist"
1.187 djm 1632: " argument.", filename, linenum);
1.344 djm 1633: return -1;
1634: }
1.187 djm 1635: value = 0;
1636: value2 = 0; /* timeout */
1637: if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1638: value = 0;
1639: else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1640: value = 1;
1641: else if ((value2 = convtime(arg)) >= 0)
1642: value = 1;
1.344 djm 1643: else {
1644: error("%.200s line %d: Bad ControlPersist argument.",
1.187 djm 1645: filename, linenum);
1.344 djm 1646: return -1;
1647: }
1.187 djm 1648: if (*activep && *intptr == -1) {
1649: *intptr = value;
1650: options->control_persist_timeout = value2;
1651: }
1652: break;
1653:
1.136 djm 1654: case oHashKnownHosts:
1655: intptr = &options->hash_known_hosts;
1656: goto parse_flag;
1657:
1.144 reyk 1658: case oTunnel:
1659: intptr = &options->tun_open;
1.207 djm 1660: multistate_ptr = multistate_tunnel;
1661: goto parse_multistate;
1.144 reyk 1662:
1663: case oTunnelDevice:
1664: arg = strdelim(&s);
1.344 djm 1665: if (!arg || *arg == '\0') {
1666: error("%.200s line %d: Missing argument.",
1667: filename, linenum);
1668: return -1;
1669: }
1.144 reyk 1670: value = a2tun(arg, &value2);
1.344 djm 1671: if (value == SSH_TUNID_ERR) {
1672: error("%.200s line %d: Bad tun device.",
1673: filename, linenum);
1674: return -1;
1675: }
1.144 reyk 1676: if (*activep) {
1677: options->tun_local = value;
1678: options->tun_remote = value2;
1679: }
1680: break;
1681:
1682: case oLocalCommand:
1683: charptr = &options->local_command;
1684: goto parse_command;
1685:
1686: case oPermitLocalCommand:
1687: intptr = &options->permit_local_command;
1688: goto parse_flag;
1689:
1.277 bluhm 1690: case oRemoteCommand:
1691: charptr = &options->remote_command;
1692: goto parse_command;
1693:
1.167 grunk 1694: case oVisualHostKey:
1695: intptr = &options->visual_host_key;
1696: goto parse_flag;
1697:
1.252 djm 1698: case oInclude:
1.344 djm 1699: if (cmdline) {
1700: error("Include directive not supported as a "
1.252 djm 1701: "command-line option");
1.344 djm 1702: return -1;
1703: }
1.252 djm 1704: value = 0;
1705: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1706: /*
1707: * Ensure all paths are anchored. User configuration
1708: * files may begin with '~/' but system configurations
1709: * must not. If the path is relative, then treat it
1710: * as living in ~/.ssh for user configurations or
1711: * /etc/ssh for system ones.
1712: */
1.344 djm 1713: if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0) {
1714: error("%.200s line %d: bad include path %s.",
1.252 djm 1715: filename, linenum, arg);
1.344 djm 1716: return -1;
1717: }
1.301 djm 1718: if (!path_absolute(arg) && *arg != '~') {
1.252 djm 1719: xasprintf(&arg2, "%s/%s",
1720: (flags & SSHCONF_USERCONF) ?
1721: "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1722: } else
1723: arg2 = xstrdup(arg);
1724: memset(&gl, 0, sizeof(gl));
1725: r = glob(arg2, GLOB_TILDE, NULL, &gl);
1726: if (r == GLOB_NOMATCH) {
1727: debug("%.200s line %d: include %s matched no "
1728: "files",filename, linenum, arg2);
1.269 dtucker 1729: free(arg2);
1.252 djm 1730: continue;
1.344 djm 1731: } else if (r != 0) {
1732: error("%.200s line %d: glob failed for %s.",
1.252 djm 1733: filename, linenum, arg2);
1.344 djm 1734: return -1;
1735: }
1.252 djm 1736: free(arg2);
1737: oactive = *activep;
1.313 deraadt 1738: for (i = 0; i < gl.gl_pathc; i++) {
1.252 djm 1739: debug3("%.200s line %d: Including file %s "
1740: "depth %d%s", filename, linenum,
1741: gl.gl_pathv[i], depth,
1742: oactive ? "" : " (parse only)");
1743: r = read_config_file_depth(gl.gl_pathv[i],
1744: pw, host, original_host, options,
1745: flags | SSHCONF_CHECKPERM |
1746: (oactive ? 0 : SSHCONF_NEVERMATCH),
1.302 djm 1747: activep, want_final_pass, depth + 1);
1.264 djm 1748: if (r != 1 && errno != ENOENT) {
1.344 djm 1749: error("Can't open user config file "
1.263 djm 1750: "%.100s: %.100s", gl.gl_pathv[i],
1751: strerror(errno));
1.344 djm 1752: globfree(&gl);
1753: return -1;
1.263 djm 1754: }
1.252 djm 1755: /*
1756: * don't let Match in includes clobber the
1757: * containing file's Match state.
1758: */
1759: *activep = oactive;
1760: if (r != 1)
1761: value = -1;
1762: }
1763: globfree(&gl);
1764: }
1765: if (value != 0)
1766: return value;
1767: break;
1768:
1.190 djm 1769: case oIPQoS:
1770: arg = strdelim(&s);
1.344 djm 1771: if ((value = parse_ipqos(arg)) == -1) {
1772: error("%s line %d: Bad IPQoS value: %s",
1.190 djm 1773: filename, linenum, arg);
1.344 djm 1774: return -1;
1775: }
1.190 djm 1776: arg = strdelim(&s);
1777: if (arg == NULL)
1778: value2 = value;
1.344 djm 1779: else if ((value2 = parse_ipqos(arg)) == -1) {
1780: error("%s line %d: Bad IPQoS value: %s",
1.190 djm 1781: filename, linenum, arg);
1.344 djm 1782: return -1;
1783: }
1.190 djm 1784: if (*activep) {
1785: options->ip_qos_interactive = value;
1786: options->ip_qos_bulk = value2;
1787: }
1788: break;
1789:
1.192 djm 1790: case oRequestTTY:
1791: intptr = &options->request_tty;
1.207 djm 1792: multistate_ptr = multistate_requesttty;
1793: goto parse_multistate;
1.192 djm 1794:
1.199 djm 1795: case oIgnoreUnknown:
1796: charptr = &options->ignored_unknown;
1797: goto parse_string;
1798:
1.205 djm 1799: case oProxyUseFdpass:
1800: intptr = &options->proxy_use_fdpass;
1801: goto parse_flag;
1802:
1.208 djm 1803: case oCanonicalDomains:
1804: value = options->num_canonical_domains != 0;
1805: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.280 millert 1806: if (!valid_domain(arg, 1, &errstr)) {
1.344 djm 1807: error("%s line %d: %s", filename, linenum,
1.280 millert 1808: errstr);
1.344 djm 1809: return -1;
1.280 millert 1810: }
1.208 djm 1811: if (!*activep || value)
1812: continue;
1.344 djm 1813: if (options->num_canonical_domains >=
1814: MAX_CANON_DOMAINS) {
1815: error("%s line %d: too many hostname suffixes.",
1.208 djm 1816: filename, linenum);
1.344 djm 1817: return -1;
1818: }
1.208 djm 1819: options->canonical_domains[
1820: options->num_canonical_domains++] = xstrdup(arg);
1821: }
1822: break;
1823:
1.209 djm 1824: case oCanonicalizePermittedCNAMEs:
1.208 djm 1825: value = options->num_permitted_cnames != 0;
1826: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1827: /* Either '*' for everything or 'list:list' */
1828: if (strcmp(arg, "*") == 0)
1829: arg2 = arg;
1830: else {
1831: lowercase(arg);
1832: if ((arg2 = strchr(arg, ':')) == NULL ||
1833: arg2[1] == '\0') {
1.344 djm 1834: error("%s line %d: "
1.208 djm 1835: "Invalid permitted CNAME \"%s\"",
1836: filename, linenum, arg);
1.344 djm 1837: return -1;
1.208 djm 1838: }
1839: *arg2 = '\0';
1840: arg2++;
1841: }
1842: if (!*activep || value)
1843: continue;
1.344 djm 1844: if (options->num_permitted_cnames >=
1845: MAX_CANON_DOMAINS) {
1846: error("%s line %d: too many permitted CNAMEs.",
1.208 djm 1847: filename, linenum);
1.344 djm 1848: return -1;
1849: }
1.208 djm 1850: cname = options->permitted_cnames +
1851: options->num_permitted_cnames++;
1852: cname->source_list = xstrdup(arg);
1853: cname->target_list = xstrdup(arg2);
1854: }
1855: break;
1856:
1.209 djm 1857: case oCanonicalizeHostname:
1858: intptr = &options->canonicalize_hostname;
1859: multistate_ptr = multistate_canonicalizehostname;
1.208 djm 1860: goto parse_multistate;
1861:
1.209 djm 1862: case oCanonicalizeMaxDots:
1863: intptr = &options->canonicalize_max_dots;
1.208 djm 1864: goto parse_int;
1865:
1.209 djm 1866: case oCanonicalizeFallbackLocal:
1867: intptr = &options->canonicalize_fallback_local;
1.208 djm 1868: goto parse_flag;
1869:
1.220 millert 1870: case oStreamLocalBindMask:
1871: arg = strdelim(&s);
1.344 djm 1872: if (!arg || *arg == '\0') {
1873: error("%.200s line %d: Missing StreamLocalBindMask "
1874: "argument.", filename, linenum);
1875: return -1;
1876: }
1.220 millert 1877: /* Parse mode in octal format */
1878: value = strtol(arg, &endofnumber, 8);
1.344 djm 1879: if (arg == endofnumber || value < 0 || value > 0777) {
1880: error("%.200s line %d: Bad mask.", filename, linenum);
1881: return -1;
1882: }
1.220 millert 1883: options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1884: break;
1885:
1886: case oStreamLocalBindUnlink:
1887: intptr = &options->fwd_opts.streamlocal_bind_unlink;
1888: goto parse_flag;
1889:
1.223 djm 1890: case oRevokedHostKeys:
1891: charptr = &options->revoked_host_keys;
1892: goto parse_string;
1893:
1.224 djm 1894: case oFingerprintHash:
1.225 djm 1895: intptr = &options->fingerprint_hash;
1.224 djm 1896: arg = strdelim(&s);
1.344 djm 1897: if (!arg || *arg == '\0') {
1898: error("%.200s line %d: Missing argument.",
1.224 djm 1899: filename, linenum);
1.344 djm 1900: return -1;
1901: }
1902: if ((value = ssh_digest_alg_by_name(arg)) == -1) {
1903: error("%.200s line %d: Invalid hash algorithm \"%s\".",
1.224 djm 1904: filename, linenum, arg);
1.344 djm 1905: return -1;
1906: }
1.225 djm 1907: if (*activep && *intptr == -1)
1908: *intptr = value;
1.224 djm 1909: break;
1910:
1.229 djm 1911: case oUpdateHostkeys:
1912: intptr = &options->update_hostkeys;
1.232 djm 1913: multistate_ptr = multistate_yesnoask;
1914: goto parse_multistate;
1.229 djm 1915:
1.230 djm 1916: case oHostbasedKeyTypes:
1917: charptr = &options->hostbased_key_types;
1.238 markus 1918: goto parse_keytypes;
1919:
1920: case oPubkeyAcceptedKeyTypes:
1921: charptr = &options->pubkey_key_types;
1922: goto parse_keytypes;
1.230 djm 1923:
1.246 jcs 1924: case oAddKeysToAgent:
1.334 djm 1925: arg = strdelim(&s);
1926: arg2 = strdelim(&s);
1927: value = parse_multistate_value(arg, filename, linenum,
1928: multistate_yesnoaskconfirm);
1929: value2 = 0; /* unlimited lifespan by default */
1930: if (value == 3 && arg2 != NULL) {
1931: /* allow "AddKeysToAgent confirm 5m" */
1.344 djm 1932: if ((value2 = convtime(arg2)) == -1 ||
1933: value2 > INT_MAX) {
1934: error("%s line %d: invalid time value.",
1.334 djm 1935: filename, linenum);
1.344 djm 1936: return -1;
1937: }
1.334 djm 1938: } else if (value == -1 && arg2 == NULL) {
1.344 djm 1939: if ((value2 = convtime(arg)) == -1 ||
1940: value2 > INT_MAX) {
1941: error("%s line %d: unsupported option",
1.334 djm 1942: filename, linenum);
1.344 djm 1943: return -1;
1944: }
1.334 djm 1945: value = 1; /* yes */
1946: } else if (value == -1 || arg2 != NULL) {
1.344 djm 1947: error("%s line %d: unsupported option",
1.334 djm 1948: filename, linenum);
1.344 djm 1949: return -1;
1.334 djm 1950: }
1951: if (*activep && options->add_keys_to_agent == -1) {
1952: options->add_keys_to_agent = value;
1953: options->add_keys_to_agent_lifespan = value2;
1954: }
1955: break;
1.246 jcs 1956:
1.253 markus 1957: case oIdentityAgent:
1958: charptr = &options->identity_agent;
1.299 djm 1959: arg = strdelim(&s);
1.344 djm 1960: if (!arg || *arg == '\0') {
1961: error("%.200s line %d: Missing argument.",
1.299 djm 1962: filename, linenum);
1.344 djm 1963: return -1;
1964: }
1.319 djm 1965: parse_agent_path:
1.299 djm 1966: /* Extra validation if the string represents an env var. */
1.344 djm 1967: if ((arg2 = dollar_expand(&r, arg)) == NULL || r) {
1968: error("%.200s line %d: Invalid environment expansion "
1.331 dtucker 1969: "%s.", filename, linenum, arg);
1.344 djm 1970: return -1;
1971: }
1.331 dtucker 1972: free(arg2);
1973: /* check for legacy environment format */
1.344 djm 1974: if (arg[0] == '$' && arg[1] != '{' &&
1975: !valid_env_name(arg + 1)) {
1976: error("%.200s line %d: Invalid environment name %s.",
1.299 djm 1977: filename, linenum, arg);
1.344 djm 1978: return -1;
1.299 djm 1979: }
1980: if (*activep && *charptr == NULL)
1981: *charptr = xstrdup(arg);
1982: break;
1.253 markus 1983:
1.96 markus 1984: case oDeprecated:
1.98 markus 1985: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 1986: filename, linenum, keyword);
1.98 markus 1987: return 0;
1.96 markus 1988:
1.110 jakob 1989: case oUnsupported:
1990: error("%s line %d: Unsupported option \"%s\"",
1991: filename, linenum, keyword);
1992: return 0;
1993:
1.17 markus 1994: default:
1.344 djm 1995: error("%s line %d: Unimplemented opcode %d",
1996: filename, linenum, opcode);
1.17 markus 1997: }
1998:
1999: /* Check that there is no garbage at end of line. */
1.57 djm 2000: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.344 djm 2001: error("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142 djm 2002: filename, linenum, arg);
1.344 djm 2003: return -1;
1.39 ho 2004: }
1.17 markus 2005: return 0;
1.1 deraadt 2006: }
2007:
1.19 markus 2008: /*
2009: * Reads the config file and modifies the options accordingly. Options
2010: * should already be initialized before this call. This never returns if
1.89 stevesk 2011: * there is an error. If the file does not exist, this returns 0.
1.19 markus 2012: */
1.89 stevesk 2013: int
1.206 djm 2014: read_config_file(const char *filename, struct passwd *pw, const char *host,
1.302 djm 2015: const char *original_host, Options *options, int flags,
2016: int *want_final_pass)
1.1 deraadt 2017: {
1.252 djm 2018: int active = 1;
2019:
2020: return read_config_file_depth(filename, pw, host, original_host,
1.302 djm 2021: options, flags, &active, want_final_pass, 0);
1.252 djm 2022: }
2023:
2024: #define READCONF_MAX_DEPTH 16
2025: static int
2026: read_config_file_depth(const char *filename, struct passwd *pw,
2027: const char *host, const char *original_host, Options *options,
1.302 djm 2028: int flags, int *activep, int *want_final_pass, int depth)
1.252 djm 2029: {
1.17 markus 2030: FILE *f;
1.343 dtucker 2031: char *cp, *line = NULL;
1.289 markus 2032: size_t linesize = 0;
1.252 djm 2033: int linenum;
1.17 markus 2034: int bad_options = 0;
2035:
1.252 djm 2036: if (depth < 0 || depth > READCONF_MAX_DEPTH)
2037: fatal("Too many recursive configuration includes");
2038:
1.129 djm 2039: if ((f = fopen(filename, "r")) == NULL)
1.89 stevesk 2040: return 0;
1.129 djm 2041:
1.196 dtucker 2042: if (flags & SSHCONF_CHECKPERM) {
1.129 djm 2043: struct stat sb;
1.134 deraadt 2044:
1.131 dtucker 2045: if (fstat(fileno(f), &sb) == -1)
1.129 djm 2046: fatal("fstat %s: %s", filename, strerror(errno));
2047: if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131 dtucker 2048: (sb.st_mode & 022) != 0))
1.129 djm 2049: fatal("Bad owner or permissions on %s", filename);
2050: }
1.17 markus 2051:
2052: debug("Reading configuration data %.200s", filename);
2053:
1.19 markus 2054: /*
2055: * Mark that we are now processing the options. This flag is turned
2056: * on/off by Host specifications.
2057: */
1.17 markus 2058: linenum = 0;
1.289 markus 2059: while (getline(&line, &linesize, f) != -1) {
1.17 markus 2060: /* Update line number counter. */
2061: linenum++;
1.343 dtucker 2062: /*
2063: * Trim out comments and strip whitespace.
2064: * NB - preserve newlines, they are needed to reproduce
2065: * line numbers later for error messages.
2066: */
2067: if ((cp = strchr(line, '#')) != NULL)
2068: *cp = '\0';
1.252 djm 2069: if (process_config_line_depth(options, pw, host, original_host,
1.302 djm 2070: line, filename, linenum, activep, flags, want_final_pass,
2071: depth) != 0)
1.17 markus 2072: bad_options++;
2073: }
1.289 markus 2074: free(line);
1.17 markus 2075: fclose(f);
2076: if (bad_options > 0)
1.64 millert 2077: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 2078: filename, bad_options);
1.89 stevesk 2079: return 1;
1.1 deraadt 2080: }
2081:
1.218 djm 2082: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
2083: int
2084: option_clear_or_none(const char *o)
2085: {
2086: return o == NULL || strcasecmp(o, "none") == 0;
2087: }
2088:
1.19 markus 2089: /*
2090: * Initializes options to special values that indicate that they have not yet
2091: * been set. Read_config_file will only set options with this value. Options
2092: * are processed in the following order: command line, user config file,
2093: * system config file. Last, fill_default_options is called.
2094: */
1.1 deraadt 2095:
1.26 markus 2096: void
1.17 markus 2097: initialize_options(Options * options)
1.1 deraadt 2098: {
1.17 markus 2099: memset(options, 'X', sizeof(*options));
2100: options->forward_agent = -1;
1.319 djm 2101: options->forward_agent_sock_path = NULL;
1.17 markus 2102: options->forward_x11 = -1;
1.123 markus 2103: options->forward_x11_trusted = -1;
1.186 djm 2104: options->forward_x11_timeout = -1;
1.255 dtucker 2105: options->stdio_forward_host = NULL;
2106: options->stdio_forward_port = 0;
1.256 dtucker 2107: options->clear_forwardings = -1;
1.153 markus 2108: options->exit_on_forward_failure = -1;
1.34 markus 2109: options->xauth_location = NULL;
1.220 millert 2110: options->fwd_opts.gateway_ports = -1;
2111: options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
2112: options->fwd_opts.streamlocal_bind_unlink = -1;
1.50 markus 2113: options->pubkey_authentication = -1;
1.78 markus 2114: options->challenge_response_authentication = -1;
1.118 markus 2115: options->gss_authentication = -1;
2116: options->gss_deleg_creds = -1;
1.17 markus 2117: options->password_authentication = -1;
1.48 markus 2118: options->kbd_interactive_authentication = -1;
2119: options->kbd_interactive_devices = NULL;
1.72 markus 2120: options->hostbased_authentication = -1;
1.17 markus 2121: options->batch_mode = -1;
2122: options->check_host_ip = -1;
2123: options->strict_host_key_checking = -1;
2124: options->compression = -1;
1.126 markus 2125: options->tcp_keep_alive = -1;
1.17 markus 2126: options->port = -1;
1.114 djm 2127: options->address_family = -1;
1.17 markus 2128: options->connection_attempts = -1;
1.111 djm 2129: options->connection_timeout = -1;
1.17 markus 2130: options->number_of_password_prompts = -1;
1.25 markus 2131: options->ciphers = NULL;
1.62 markus 2132: options->macs = NULL;
1.189 djm 2133: options->kex_algorithms = NULL;
1.76 markus 2134: options->hostkeyalgorithms = NULL;
1.298 djm 2135: options->ca_sign_algorithms = NULL;
1.17 markus 2136: options->num_identity_files = 0;
1.344 djm 2137: memset(options->identity_keys, 0, sizeof(options->identity_keys));
1.241 djm 2138: options->num_certificate_files = 0;
1.344 djm 2139: memset(options->certificates, 0, sizeof(options->certificates));
1.17 markus 2140: options->hostname = NULL;
1.52 markus 2141: options->host_key_alias = NULL;
1.17 markus 2142: options->proxy_command = NULL;
1.257 djm 2143: options->jump_user = NULL;
2144: options->jump_host = NULL;
2145: options->jump_port = -1;
2146: options->jump_extra = NULL;
1.17 markus 2147: options->user = NULL;
2148: options->escape_char = -1;
1.193 djm 2149: options->num_system_hostfiles = 0;
2150: options->num_user_hostfiles = 0;
1.185 djm 2151: options->local_forwards = NULL;
1.17 markus 2152: options->num_local_forwards = 0;
1.185 djm 2153: options->remote_forwards = NULL;
1.17 markus 2154: options->num_remote_forwards = 0;
1.271 dtucker 2155: options->log_facility = SYSLOG_FACILITY_NOT_SET;
1.95 markus 2156: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.339 djm 2157: options->num_log_verbose = 0;
2158: options->log_verbose = NULL;
1.67 markus 2159: options->preferred_authentications = NULL;
1.77 markus 2160: options->bind_address = NULL;
1.282 djm 2161: options->bind_interface = NULL;
1.183 markus 2162: options->pkcs11_provider = NULL;
1.310 djm 2163: options->sk_provider = NULL;
1.101 markus 2164: options->enable_ssh_keysign = - 1;
1.91 markus 2165: options->no_host_authentication_for_localhost = - 1;
1.128 markus 2166: options->identities_only = - 1;
1.105 markus 2167: options->rekey_limit = - 1;
1.198 dtucker 2168: options->rekey_interval = -1;
1.107 jakob 2169: options->verify_host_key_dns = -1;
1.127 markus 2170: options->server_alive_interval = -1;
2171: options->server_alive_count_max = -1;
1.290 djm 2172: options->send_env = NULL;
1.130 djm 2173: options->num_send_env = 0;
1.290 djm 2174: options->setenv = NULL;
2175: options->num_setenv = 0;
1.132 djm 2176: options->control_path = NULL;
2177: options->control_master = -1;
1.187 djm 2178: options->control_persist = -1;
2179: options->control_persist_timeout = 0;
1.136 djm 2180: options->hash_known_hosts = -1;
1.144 reyk 2181: options->tun_open = -1;
2182: options->tun_local = -1;
2183: options->tun_remote = -1;
2184: options->local_command = NULL;
2185: options->permit_local_command = -1;
1.277 bluhm 2186: options->remote_command = NULL;
1.246 jcs 2187: options->add_keys_to_agent = -1;
1.334 djm 2188: options->add_keys_to_agent_lifespan = -1;
1.253 markus 2189: options->identity_agent = NULL;
1.167 grunk 2190: options->visual_host_key = -1;
1.190 djm 2191: options->ip_qos_interactive = -1;
2192: options->ip_qos_bulk = -1;
1.192 djm 2193: options->request_tty = -1;
1.205 djm 2194: options->proxy_use_fdpass = -1;
1.199 djm 2195: options->ignored_unknown = NULL;
1.208 djm 2196: options->num_canonical_domains = 0;
2197: options->num_permitted_cnames = 0;
1.209 djm 2198: options->canonicalize_max_dots = -1;
2199: options->canonicalize_fallback_local = -1;
2200: options->canonicalize_hostname = -1;
1.223 djm 2201: options->revoked_host_keys = NULL;
1.224 djm 2202: options->fingerprint_hash = -1;
1.229 djm 2203: options->update_hostkeys = -1;
1.230 djm 2204: options->hostbased_key_types = NULL;
1.238 markus 2205: options->pubkey_key_types = NULL;
1.1 deraadt 2206: }
2207:
1.19 markus 2208: /*
1.218 djm 2209: * A petite version of fill_default_options() that just fills the options
2210: * needed for hostname canonicalization to proceed.
2211: */
2212: void
2213: fill_default_options_for_canonicalization(Options *options)
2214: {
2215: if (options->canonicalize_max_dots == -1)
2216: options->canonicalize_max_dots = 1;
2217: if (options->canonicalize_fallback_local == -1)
2218: options->canonicalize_fallback_local = 1;
2219: if (options->canonicalize_hostname == -1)
2220: options->canonicalize_hostname = SSH_CANONICALISE_NO;
2221: }
2222:
2223: /*
1.19 markus 2224: * Called after processing other sources of option data, this fills those
2225: * options for which no value has been specified with their default values.
2226: */
1.344 djm 2227: int
1.17 markus 2228: fill_default_options(Options * options)
1.1 deraadt 2229: {
1.298 djm 2230: char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
1.320 dtucker 2231: char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
1.344 djm 2232: int ret = 0, r;
1.292 djm 2233:
1.17 markus 2234: if (options->forward_agent == -1)
1.33 markus 2235: options->forward_agent = 0;
1.17 markus 2236: if (options->forward_x11 == -1)
1.23 markus 2237: options->forward_x11 = 0;
1.123 markus 2238: if (options->forward_x11_trusted == -1)
2239: options->forward_x11_trusted = 0;
1.186 djm 2240: if (options->forward_x11_timeout == -1)
2241: options->forward_x11_timeout = 1200;
1.256 dtucker 2242: /*
2243: * stdio forwarding (-W) changes the default for these but we defer
2244: * setting the values so they can be overridden.
2245: */
1.153 markus 2246: if (options->exit_on_forward_failure == -1)
1.256 dtucker 2247: options->exit_on_forward_failure =
2248: options->stdio_forward_host != NULL ? 1 : 0;
2249: if (options->clear_forwardings == -1)
2250: options->clear_forwardings =
2251: options->stdio_forward_host != NULL ? 1 : 0;
2252: if (options->clear_forwardings == 1)
2253: clear_forwardings(options);
2254:
1.34 markus 2255: if (options->xauth_location == NULL)
1.344 djm 2256: options->xauth_location = xstrdup(_PATH_XAUTH);
1.220 millert 2257: if (options->fwd_opts.gateway_ports == -1)
2258: options->fwd_opts.gateway_ports = 0;
2259: if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2260: options->fwd_opts.streamlocal_bind_mask = 0177;
2261: if (options->fwd_opts.streamlocal_bind_unlink == -1)
2262: options->fwd_opts.streamlocal_bind_unlink = 0;
1.50 markus 2263: if (options->pubkey_authentication == -1)
2264: options->pubkey_authentication = 1;
1.78 markus 2265: if (options->challenge_response_authentication == -1)
1.83 markus 2266: options->challenge_response_authentication = 1;
1.118 markus 2267: if (options->gss_authentication == -1)
1.122 markus 2268: options->gss_authentication = 0;
1.118 markus 2269: if (options->gss_deleg_creds == -1)
2270: options->gss_deleg_creds = 0;
1.17 markus 2271: if (options->password_authentication == -1)
2272: options->password_authentication = 1;
1.48 markus 2273: if (options->kbd_interactive_authentication == -1)
1.59 markus 2274: options->kbd_interactive_authentication = 1;
1.72 markus 2275: if (options->hostbased_authentication == -1)
2276: options->hostbased_authentication = 0;
1.17 markus 2277: if (options->batch_mode == -1)
2278: options->batch_mode = 0;
2279: if (options->check_host_ip == -1)
2280: options->check_host_ip = 1;
2281: if (options->strict_host_key_checking == -1)
1.278 djm 2282: options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
1.17 markus 2283: if (options->compression == -1)
2284: options->compression = 0;
1.126 markus 2285: if (options->tcp_keep_alive == -1)
2286: options->tcp_keep_alive = 1;
1.17 markus 2287: if (options->port == -1)
2288: options->port = 0; /* Filled in ssh_connect. */
1.114 djm 2289: if (options->address_family == -1)
2290: options->address_family = AF_UNSPEC;
1.17 markus 2291: if (options->connection_attempts == -1)
1.84 markus 2292: options->connection_attempts = 1;
1.17 markus 2293: if (options->number_of_password_prompts == -1)
2294: options->number_of_password_prompts = 3;
1.76 markus 2295: /* options->hostkeyalgorithms, default set in myproposals.h */
1.334 djm 2296: if (options->add_keys_to_agent == -1) {
1.246 jcs 2297: options->add_keys_to_agent = 0;
1.334 djm 2298: options->add_keys_to_agent_lifespan = 0;
2299: }
1.17 markus 2300: if (options->num_identity_files == 0) {
1.273 djm 2301: add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2302: add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
2303: add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
2304: add_identity_file(options, "~/",
1.310 djm 2305: _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
2306: add_identity_file(options, "~/",
1.273 djm 2307: _PATH_SSH_CLIENT_ID_ED25519, 0);
1.311 markus 2308: add_identity_file(options, "~/",
2309: _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
1.283 markus 2310: add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
1.27 markus 2311: }
1.17 markus 2312: if (options->escape_char == -1)
2313: options->escape_char = '~';
1.193 djm 2314: if (options->num_system_hostfiles == 0) {
2315: options->system_hostfiles[options->num_system_hostfiles++] =
2316: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2317: options->system_hostfiles[options->num_system_hostfiles++] =
2318: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2319: }
1.336 djm 2320: if (options->update_hostkeys == -1) {
1.338 djm 2321: if (options->verify_host_key_dns <= 0 &&
2322: (options->num_user_hostfiles == 0 ||
1.336 djm 2323: (options->num_user_hostfiles == 1 && strcmp(options->
1.338 djm 2324: user_hostfiles[0], _PATH_SSH_USER_HOSTFILE) == 0)))
1.336 djm 2325: options->update_hostkeys = SSH_UPDATE_HOSTKEYS_YES;
2326: else
1.325 djm 2327: options->update_hostkeys = SSH_UPDATE_HOSTKEYS_NO;
1.336 djm 2328: }
1.193 djm 2329: if (options->num_user_hostfiles == 0) {
2330: options->user_hostfiles[options->num_user_hostfiles++] =
2331: xstrdup(_PATH_SSH_USER_HOSTFILE);
2332: options->user_hostfiles[options->num_user_hostfiles++] =
2333: xstrdup(_PATH_SSH_USER_HOSTFILE2);
2334: }
1.95 markus 2335: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 2336: options->log_level = SYSLOG_LEVEL_INFO;
1.271 dtucker 2337: if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2338: options->log_facility = SYSLOG_FACILITY_USER;
1.91 markus 2339: if (options->no_host_authentication_for_localhost == - 1)
2340: options->no_host_authentication_for_localhost = 0;
1.128 markus 2341: if (options->identities_only == -1)
2342: options->identities_only = 0;
1.101 markus 2343: if (options->enable_ssh_keysign == -1)
2344: options->enable_ssh_keysign = 0;
1.105 markus 2345: if (options->rekey_limit == -1)
2346: options->rekey_limit = 0;
1.198 dtucker 2347: if (options->rekey_interval == -1)
2348: options->rekey_interval = 0;
1.107 jakob 2349: if (options->verify_host_key_dns == -1)
2350: options->verify_host_key_dns = 0;
1.127 markus 2351: if (options->server_alive_interval == -1)
2352: options->server_alive_interval = 0;
2353: if (options->server_alive_count_max == -1)
2354: options->server_alive_count_max = 3;
1.132 djm 2355: if (options->control_master == -1)
2356: options->control_master = 0;
1.187 djm 2357: if (options->control_persist == -1) {
2358: options->control_persist = 0;
2359: options->control_persist_timeout = 0;
2360: }
1.136 djm 2361: if (options->hash_known_hosts == -1)
2362: options->hash_known_hosts = 0;
1.144 reyk 2363: if (options->tun_open == -1)
1.145 reyk 2364: options->tun_open = SSH_TUNMODE_NO;
2365: if (options->tun_local == -1)
2366: options->tun_local = SSH_TUNID_ANY;
2367: if (options->tun_remote == -1)
2368: options->tun_remote = SSH_TUNID_ANY;
1.144 reyk 2369: if (options->permit_local_command == -1)
2370: options->permit_local_command = 0;
1.167 grunk 2371: if (options->visual_host_key == -1)
2372: options->visual_host_key = 0;
1.190 djm 2373: if (options->ip_qos_interactive == -1)
1.284 job 2374: options->ip_qos_interactive = IPTOS_DSCP_AF21;
1.190 djm 2375: if (options->ip_qos_bulk == -1)
1.284 job 2376: options->ip_qos_bulk = IPTOS_DSCP_CS1;
1.192 djm 2377: if (options->request_tty == -1)
2378: options->request_tty = REQUEST_TTY_AUTO;
1.205 djm 2379: if (options->proxy_use_fdpass == -1)
2380: options->proxy_use_fdpass = 0;
1.209 djm 2381: if (options->canonicalize_max_dots == -1)
2382: options->canonicalize_max_dots = 1;
2383: if (options->canonicalize_fallback_local == -1)
2384: options->canonicalize_fallback_local = 1;
2385: if (options->canonicalize_hostname == -1)
2386: options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.224 djm 2387: if (options->fingerprint_hash == -1)
2388: options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.310 djm 2389: if (options->sk_provider == NULL)
1.314 djm 2390: options->sk_provider = xstrdup("internal");
1.292 djm 2391:
2392: /* Expand KEX name lists */
2393: all_cipher = cipher_alg_list(',', 0);
2394: all_mac = mac_alg_list(',');
2395: all_kex = kex_alg_list(',');
2396: all_key = sshkey_alg_list(0, 0, 1, ',');
1.298 djm 2397: all_sig = sshkey_alg_list(0, 1, 1, ',');
1.320 dtucker 2398: /* remove unsupported algos from default lists */
1.332 djm 2399: def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
2400: def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
2401: def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
2402: def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
2403: def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
1.297 djm 2404: #define ASSEMBLE(what, defaults, all) \
2405: do { \
2406: if ((r = kex_assemble_names(&options->what, \
1.344 djm 2407: defaults, all)) != 0) { \
2408: error_fr(r, "%s", #what); \
2409: goto fail; \
2410: } \
1.297 djm 2411: } while (0)
1.320 dtucker 2412: ASSEMBLE(ciphers, def_cipher, all_cipher);
2413: ASSEMBLE(macs, def_mac, all_mac);
2414: ASSEMBLE(kex_algorithms, def_kex, all_kex);
2415: ASSEMBLE(hostbased_key_types, def_key, all_key);
2416: ASSEMBLE(pubkey_key_types, def_key, all_key);
2417: ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
1.297 djm 2418: #undef ASSEMBLE
1.224 djm 2419:
1.207 djm 2420: #define CLEAR_ON_NONE(v) \
2421: do { \
1.218 djm 2422: if (option_clear_or_none(v)) { \
1.207 djm 2423: free(v); \
2424: v = NULL; \
2425: } \
2426: } while(0)
2427: CLEAR_ON_NONE(options->local_command);
1.277 bluhm 2428: CLEAR_ON_NONE(options->remote_command);
1.207 djm 2429: CLEAR_ON_NONE(options->proxy_command);
2430: CLEAR_ON_NONE(options->control_path);
1.223 djm 2431: CLEAR_ON_NONE(options->revoked_host_keys);
1.304 djm 2432: CLEAR_ON_NONE(options->pkcs11_provider);
1.310 djm 2433: CLEAR_ON_NONE(options->sk_provider);
1.287 djm 2434: if (options->jump_host != NULL &&
2435: strcmp(options->jump_host, "none") == 0 &&
2436: options->jump_port == 0 && options->jump_user == NULL) {
2437: free(options->jump_host);
2438: options->jump_host = NULL;
2439: }
1.254 markus 2440: /* options->identity_agent distinguishes NULL from 'none' */
1.17 markus 2441: /* options->user will be set in the main program if appropriate */
2442: /* options->hostname will be set in the main program if appropriate */
1.52 markus 2443: /* options->host_key_alias should not be set by default */
1.67 markus 2444: /* options->preferred_authentications will be set in ssh */
1.344 djm 2445:
2446: /* success */
2447: ret = 0;
2448: fail:
2449: free(all_cipher);
2450: free(all_mac);
2451: free(all_kex);
2452: free(all_key);
2453: free(all_sig);
2454: free(def_cipher);
2455: free(def_mac);
2456: free(def_kex);
2457: free(def_key);
2458: free(def_sig);
2459: return ret;
2460: }
2461:
2462: void
2463: free_options(Options *o)
2464: {
2465: int i;
2466:
2467: if (o == NULL)
2468: return;
2469:
2470: #define FREE_ARRAY(type, n, a) \
2471: do { \
2472: type _i; \
2473: for (_i = 0; _i < (n); _i++) \
2474: free((a)[_i]); \
2475: } while (0)
2476:
2477: free(o->forward_agent_sock_path);
2478: free(o->xauth_location);
2479: FREE_ARRAY(u_int, o->num_log_verbose, o->log_verbose);
2480: free(o->log_verbose);
2481: free(o->ciphers);
2482: free(o->macs);
2483: free(o->hostkeyalgorithms);
2484: free(o->kex_algorithms);
2485: free(o->ca_sign_algorithms);
2486: free(o->hostname);
2487: free(o->host_key_alias);
2488: free(o->proxy_command);
2489: free(o->user);
2490: FREE_ARRAY(u_int, o->num_system_hostfiles, o->system_hostfiles);
2491: FREE_ARRAY(u_int, o->num_user_hostfiles, o->user_hostfiles);
2492: free(o->preferred_authentications);
2493: free(o->bind_address);
2494: free(o->bind_interface);
2495: free(o->pkcs11_provider);
2496: free(o->sk_provider);
2497: for (i = 0; i < o->num_identity_files; i++) {
2498: free(o->identity_files[i]);
2499: sshkey_free(o->identity_keys[i]);
2500: }
2501: for (i = 0; i < o->num_certificate_files; i++) {
2502: free(o->certificate_files[i]);
2503: sshkey_free(o->certificates[i]);
2504: }
2505: free(o->identity_agent);
2506: for (i = 0; i < o->num_local_forwards; i++) {
2507: free(o->local_forwards[i].listen_host);
2508: free(o->local_forwards[i].listen_path);
2509: free(o->local_forwards[i].connect_host);
2510: free(o->local_forwards[i].connect_path);
2511: }
2512: free(o->local_forwards);
2513: for (i = 0; i < o->num_remote_forwards; i++) {
2514: free(o->remote_forwards[i].listen_host);
2515: free(o->remote_forwards[i].listen_path);
2516: free(o->remote_forwards[i].connect_host);
2517: free(o->remote_forwards[i].connect_path);
2518: }
2519: free(o->remote_forwards);
2520: free(o->stdio_forward_host);
2521: FREE_ARRAY(int, o->num_send_env, o->send_env);
2522: free(o->send_env);
2523: FREE_ARRAY(int, o->num_setenv, o->setenv);
2524: free(o->setenv);
2525: free(o->control_path);
2526: free(o->local_command);
2527: free(o->remote_command);
2528: FREE_ARRAY(int, o->num_canonical_domains, o->canonical_domains);
2529: for (i = 0; i < o->num_permitted_cnames; i++) {
2530: free(o->permitted_cnames[i].source_list);
2531: free(o->permitted_cnames[i].target_list);
2532: }
2533: free(o->revoked_host_keys);
2534: free(o->hostbased_key_types);
2535: free(o->pubkey_key_types);
2536: free(o->jump_user);
2537: free(o->jump_host);
2538: free(o->jump_extra);
2539: free(o->ignored_unknown);
2540: explicit_bzero(o, sizeof(*o));
2541: #undef FREE_ARRAY
1.135 djm 2542: }
2543:
1.220 millert 2544: struct fwdarg {
2545: char *arg;
2546: int ispath;
2547: };
2548:
2549: /*
2550: * parse_fwd_field
2551: * parses the next field in a port forwarding specification.
2552: * sets fwd to the parsed field and advances p past the colon
2553: * or sets it to NULL at end of string.
2554: * returns 0 on success, else non-zero.
2555: */
2556: static int
2557: parse_fwd_field(char **p, struct fwdarg *fwd)
2558: {
2559: char *ep, *cp = *p;
2560: int ispath = 0;
2561:
2562: if (*cp == '\0') {
2563: *p = NULL;
2564: return -1; /* end of string */
2565: }
2566:
2567: /*
2568: * A field escaped with square brackets is used literally.
2569: * XXX - allow ']' to be escaped via backslash?
2570: */
2571: if (*cp == '[') {
2572: /* find matching ']' */
2573: for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2574: if (*ep == '/')
2575: ispath = 1;
2576: }
2577: /* no matching ']' or not at end of field. */
2578: if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2579: return -1;
2580: /* NUL terminate the field and advance p past the colon */
2581: *ep++ = '\0';
2582: if (*ep != '\0')
2583: *ep++ = '\0';
2584: fwd->arg = cp + 1;
2585: fwd->ispath = ispath;
2586: *p = ep;
2587: return 0;
2588: }
2589:
2590: for (cp = *p; *cp != '\0'; cp++) {
2591: switch (*cp) {
2592: case '\\':
2593: memmove(cp, cp + 1, strlen(cp + 1) + 1);
1.237 djm 2594: if (*cp == '\0')
2595: return -1;
1.220 millert 2596: break;
2597: case '/':
2598: ispath = 1;
2599: break;
2600: case ':':
2601: *cp++ = '\0';
2602: goto done;
2603: }
2604: }
2605: done:
2606: fwd->arg = *p;
2607: fwd->ispath = ispath;
2608: *p = cp;
2609: return 0;
2610: }
2611:
1.135 djm 2612: /*
2613: * parse_forward
2614: * parses a string containing a port forwarding specification of the form:
1.168 stevesk 2615: * dynamicfwd == 0
1.220 millert 2616: * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2617: * listenpath:connectpath
1.168 stevesk 2618: * dynamicfwd == 1
2619: * [listenhost:]listenport
1.135 djm 2620: * returns number of arguments parsed or zero on error
2621: */
2622: int
1.220 millert 2623: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135 djm 2624: {
1.220 millert 2625: struct fwdarg fwdargs[4];
2626: char *p, *cp;
1.331 dtucker 2627: int i, err;
1.135 djm 2628:
1.220 millert 2629: memset(fwd, 0, sizeof(*fwd));
2630: memset(fwdargs, 0, sizeof(fwdargs));
1.135 djm 2631:
1.331 dtucker 2632: /*
2633: * We expand environment variables before checking if we think they're
2634: * paths so that if ${VAR} expands to a fully qualified path it is
2635: * treated as a path.
2636: */
2637: cp = p = dollar_expand(&err, fwdspec);
2638: if (p == NULL || err)
2639: return 0;
1.135 djm 2640:
2641: /* skip leading spaces */
1.214 deraadt 2642: while (isspace((u_char)*cp))
1.135 djm 2643: cp++;
2644:
1.220 millert 2645: for (i = 0; i < 4; ++i) {
2646: if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135 djm 2647: break;
1.220 millert 2648: }
1.135 djm 2649:
1.170 stevesk 2650: /* Check for trailing garbage */
1.220 millert 2651: if (cp != NULL && *cp != '\0') {
1.135 djm 2652: i = 0; /* failure */
1.220 millert 2653: }
1.135 djm 2654:
2655: switch (i) {
1.168 stevesk 2656: case 1:
1.220 millert 2657: if (fwdargs[0].ispath) {
2658: fwd->listen_path = xstrdup(fwdargs[0].arg);
2659: fwd->listen_port = PORT_STREAMLOCAL;
2660: } else {
2661: fwd->listen_host = NULL;
2662: fwd->listen_port = a2port(fwdargs[0].arg);
2663: }
1.168 stevesk 2664: fwd->connect_host = xstrdup("socks");
2665: break;
2666:
2667: case 2:
1.220 millert 2668: if (fwdargs[0].ispath && fwdargs[1].ispath) {
2669: fwd->listen_path = xstrdup(fwdargs[0].arg);
2670: fwd->listen_port = PORT_STREAMLOCAL;
2671: fwd->connect_path = xstrdup(fwdargs[1].arg);
2672: fwd->connect_port = PORT_STREAMLOCAL;
2673: } else if (fwdargs[1].ispath) {
2674: fwd->listen_host = NULL;
2675: fwd->listen_port = a2port(fwdargs[0].arg);
2676: fwd->connect_path = xstrdup(fwdargs[1].arg);
2677: fwd->connect_port = PORT_STREAMLOCAL;
2678: } else {
2679: fwd->listen_host = xstrdup(fwdargs[0].arg);
2680: fwd->listen_port = a2port(fwdargs[1].arg);
2681: fwd->connect_host = xstrdup("socks");
2682: }
1.168 stevesk 2683: break;
2684:
1.135 djm 2685: case 3:
1.220 millert 2686: if (fwdargs[0].ispath) {
2687: fwd->listen_path = xstrdup(fwdargs[0].arg);
2688: fwd->listen_port = PORT_STREAMLOCAL;
2689: fwd->connect_host = xstrdup(fwdargs[1].arg);
2690: fwd->connect_port = a2port(fwdargs[2].arg);
2691: } else if (fwdargs[2].ispath) {
2692: fwd->listen_host = xstrdup(fwdargs[0].arg);
2693: fwd->listen_port = a2port(fwdargs[1].arg);
2694: fwd->connect_path = xstrdup(fwdargs[2].arg);
2695: fwd->connect_port = PORT_STREAMLOCAL;
2696: } else {
2697: fwd->listen_host = NULL;
2698: fwd->listen_port = a2port(fwdargs[0].arg);
2699: fwd->connect_host = xstrdup(fwdargs[1].arg);
2700: fwd->connect_port = a2port(fwdargs[2].arg);
2701: }
1.135 djm 2702: break;
2703:
2704: case 4:
1.220 millert 2705: fwd->listen_host = xstrdup(fwdargs[0].arg);
2706: fwd->listen_port = a2port(fwdargs[1].arg);
2707: fwd->connect_host = xstrdup(fwdargs[2].arg);
2708: fwd->connect_port = a2port(fwdargs[3].arg);
1.135 djm 2709: break;
2710: default:
2711: i = 0; /* failure */
2712: }
2713:
1.202 djm 2714: free(p);
1.135 djm 2715:
1.168 stevesk 2716: if (dynamicfwd) {
2717: if (!(i == 1 || i == 2))
2718: goto fail_free;
2719: } else {
1.220 millert 2720: if (!(i == 3 || i == 4)) {
2721: if (fwd->connect_path == NULL &&
2722: fwd->listen_path == NULL)
2723: goto fail_free;
2724: }
2725: if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168 stevesk 2726: goto fail_free;
2727: }
2728:
1.220 millert 2729: if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2730: (!remotefwd && fwd->listen_port == 0))
1.135 djm 2731: goto fail_free;
2732: if (fwd->connect_host != NULL &&
2733: strlen(fwd->connect_host) >= NI_MAXHOST)
2734: goto fail_free;
1.220 millert 2735: /* XXX - if connecting to a remote socket, max sun len may not match this host */
2736: if (fwd->connect_path != NULL &&
2737: strlen(fwd->connect_path) >= PATH_MAX_SUN)
2738: goto fail_free;
1.176 djm 2739: if (fwd->listen_host != NULL &&
2740: strlen(fwd->listen_host) >= NI_MAXHOST)
2741: goto fail_free;
1.220 millert 2742: if (fwd->listen_path != NULL &&
2743: strlen(fwd->listen_path) >= PATH_MAX_SUN)
2744: goto fail_free;
1.135 djm 2745:
2746: return (i);
2747:
2748: fail_free:
1.202 djm 2749: free(fwd->connect_host);
2750: fwd->connect_host = NULL;
1.220 millert 2751: free(fwd->connect_path);
2752: fwd->connect_path = NULL;
1.202 djm 2753: free(fwd->listen_host);
2754: fwd->listen_host = NULL;
1.220 millert 2755: free(fwd->listen_path);
2756: fwd->listen_path = NULL;
1.135 djm 2757: return (0);
1.221 djm 2758: }
2759:
1.257 djm 2760: int
2761: parse_jump(const char *s, Options *o, int active)
2762: {
2763: char *orig, *sdup, *cp;
2764: char *host = NULL, *user = NULL;
1.345 ! djm 2765: int r, ret = -1, port = -1, first;
1.257 djm 2766:
2767: active &= o->proxy_command == NULL && o->jump_host == NULL;
2768:
2769: orig = sdup = xstrdup(s);
1.258 naddy 2770: first = active;
1.259 djm 2771: do {
1.287 djm 2772: if (strcasecmp(s, "none") == 0)
2773: break;
1.259 djm 2774: if ((cp = strrchr(sdup, ',')) == NULL)
2775: cp = sdup; /* last */
2776: else
2777: *cp++ = '\0';
2778:
1.258 naddy 2779: if (first) {
1.257 djm 2780: /* First argument and configuration is active */
1.345 ! djm 2781: r = parse_ssh_uri(cp, &user, &host, &port);
! 2782: if (r == -1 || (r == 1 &&
! 2783: parse_user_host_port(cp, &user, &host, &port) != 0))
1.257 djm 2784: goto out;
2785: } else {
2786: /* Subsequent argument or inactive configuration */
1.344 djm 2787: if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 &&
1.280 millert 2788: parse_user_host_port(cp, NULL, NULL, NULL) != 0)
1.345 ! djm 2789: goto out;
! 2790: r = parse_ssh_uri(cp, NULL, NULL, NULL);
! 2791: if (r == -1 || (r == 1 &&
! 2792: parse_user_host_port(cp, NULL, NULL, NULL) != 0))
1.257 djm 2793: goto out;
2794: }
1.258 naddy 2795: first = 0; /* only check syntax for subsequent hosts */
1.259 djm 2796: } while (cp != sdup);
1.257 djm 2797: /* success */
1.258 naddy 2798: if (active) {
1.287 djm 2799: if (strcasecmp(s, "none") == 0) {
2800: o->jump_host = xstrdup("none");
2801: o->jump_port = 0;
2802: } else {
2803: o->jump_user = user;
2804: o->jump_host = host;
2805: o->jump_port = port;
2806: o->proxy_command = xstrdup("none");
2807: user = host = NULL;
2808: if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2809: o->jump_extra = xstrdup(s);
2810: o->jump_extra[cp - s] = '\0';
2811: }
1.259 djm 2812: }
1.258 naddy 2813: }
1.257 djm 2814: ret = 0;
2815: out:
1.258 naddy 2816: free(orig);
1.257 djm 2817: free(user);
2818: free(host);
2819: return ret;
1.280 millert 2820: }
2821:
2822: int
2823: parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2824: {
1.344 djm 2825: char *user = NULL, *host = NULL, *path = NULL;
2826: int r, port;
1.280 millert 2827:
1.344 djm 2828: r = parse_uri("ssh", uri, &user, &host, &port, &path);
1.280 millert 2829: if (r == 0 && path != NULL)
2830: r = -1; /* path not allowed */
1.344 djm 2831: if (r == 0) {
2832: if (userp != NULL) {
2833: *userp = user;
2834: user = NULL;
2835: }
2836: if (hostp != NULL) {
2837: *hostp = host;
2838: host = NULL;
2839: }
2840: if (portp != NULL)
2841: *portp = port;
2842: }
2843: free(user);
2844: free(host);
2845: free(path);
1.280 millert 2846: return r;
1.257 djm 2847: }
2848:
1.221 djm 2849: /* XXX the following is a near-vebatim copy from servconf.c; refactor */
2850: static const char *
2851: fmt_multistate_int(int val, const struct multistate *m)
2852: {
2853: u_int i;
2854:
2855: for (i = 0; m[i].key != NULL; i++) {
2856: if (m[i].value == val)
2857: return m[i].key;
2858: }
2859: return "UNKNOWN";
2860: }
2861:
2862: static const char *
2863: fmt_intarg(OpCodes code, int val)
2864: {
2865: if (val == -1)
2866: return "unset";
2867: switch (code) {
2868: case oAddressFamily:
2869: return fmt_multistate_int(val, multistate_addressfamily);
2870: case oVerifyHostKeyDNS:
1.232 djm 2871: case oUpdateHostkeys:
1.221 djm 2872: return fmt_multistate_int(val, multistate_yesnoask);
1.278 djm 2873: case oStrictHostKeyChecking:
2874: return fmt_multistate_int(val, multistate_strict_hostkey);
1.221 djm 2875: case oControlMaster:
2876: return fmt_multistate_int(val, multistate_controlmaster);
2877: case oTunnel:
2878: return fmt_multistate_int(val, multistate_tunnel);
2879: case oRequestTTY:
2880: return fmt_multistate_int(val, multistate_requesttty);
2881: case oCanonicalizeHostname:
2882: return fmt_multistate_int(val, multistate_canonicalizehostname);
1.285 djm 2883: case oAddKeysToAgent:
2884: return fmt_multistate_int(val, multistate_yesnoaskconfirm);
1.224 djm 2885: case oFingerprintHash:
2886: return ssh_digest_alg_name(val);
1.221 djm 2887: default:
2888: switch (val) {
2889: case 0:
2890: return "no";
2891: case 1:
2892: return "yes";
2893: default:
2894: return "UNKNOWN";
2895: }
2896: }
2897: }
2898:
2899: static const char *
2900: lookup_opcode_name(OpCodes code)
2901: {
2902: u_int i;
2903:
2904: for (i = 0; keywords[i].name != NULL; i++)
2905: if (keywords[i].opcode == code)
2906: return(keywords[i].name);
2907: return "UNKNOWN";
2908: }
2909:
2910: static void
2911: dump_cfg_int(OpCodes code, int val)
2912: {
2913: printf("%s %d\n", lookup_opcode_name(code), val);
2914: }
2915:
2916: static void
2917: dump_cfg_fmtint(OpCodes code, int val)
2918: {
2919: printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2920: }
2921:
2922: static void
2923: dump_cfg_string(OpCodes code, const char *val)
2924: {
2925: if (val == NULL)
2926: return;
2927: printf("%s %s\n", lookup_opcode_name(code), val);
2928: }
2929:
2930: static void
2931: dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2932: {
2933: u_int i;
2934:
2935: for (i = 0; i < count; i++)
2936: printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2937: }
2938:
2939: static void
2940: dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2941: {
2942: u_int i;
2943:
2944: printf("%s", lookup_opcode_name(code));
2945: for (i = 0; i < count; i++)
2946: printf(" %s", vals[i]);
2947: printf("\n");
2948: }
2949:
2950: static void
2951: dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2952: {
2953: const struct Forward *fwd;
2954: u_int i;
2955:
2956: /* oDynamicForward */
2957: for (i = 0; i < count; i++) {
2958: fwd = &fwds[i];
1.265 djm 2959: if (code == oDynamicForward && fwd->connect_host != NULL &&
1.221 djm 2960: strcmp(fwd->connect_host, "socks") != 0)
2961: continue;
1.265 djm 2962: if (code == oLocalForward && fwd->connect_host != NULL &&
1.221 djm 2963: strcmp(fwd->connect_host, "socks") == 0)
2964: continue;
2965: printf("%s", lookup_opcode_name(code));
2966: if (fwd->listen_port == PORT_STREAMLOCAL)
2967: printf(" %s", fwd->listen_path);
2968: else if (fwd->listen_host == NULL)
2969: printf(" %d", fwd->listen_port);
2970: else {
2971: printf(" [%s]:%d",
2972: fwd->listen_host, fwd->listen_port);
2973: }
2974: if (code != oDynamicForward) {
2975: if (fwd->connect_port == PORT_STREAMLOCAL)
2976: printf(" %s", fwd->connect_path);
2977: else if (fwd->connect_host == NULL)
2978: printf(" %d", fwd->connect_port);
2979: else {
2980: printf(" [%s]:%d",
2981: fwd->connect_host, fwd->connect_port);
2982: }
2983: }
2984: printf("\n");
2985: }
2986: }
2987:
2988: void
2989: dump_client_config(Options *o, const char *host)
2990: {
1.326 djm 2991: int i, r;
2992: char buf[8], *all_key;
2993:
2994: /*
2995: * Expand HostKeyAlgorithms name lists. This isn't handled in
2996: * fill_default_options() like the other algorithm lists because
2997: * the host key algorithms are by default dynamically chosen based
2998: * on the host's keys found in known_hosts.
2999: */
3000: all_key = sshkey_alg_list(0, 0, 1, ',');
3001: if ((r = kex_assemble_names(&o->hostkeyalgorithms, kex_default_pk_alg(),
3002: all_key)) != 0)
1.340 djm 3003: fatal_fr(r, "expand HostKeyAlgorithms");
1.326 djm 3004: free(all_key);
1.240 djm 3005:
1.221 djm 3006: /* Most interesting options first: user, host, port */
3007: dump_cfg_string(oUser, o->user);
1.306 jmc 3008: dump_cfg_string(oHostname, host);
1.221 djm 3009: dump_cfg_int(oPort, o->port);
3010:
3011: /* Flag options */
3012: dump_cfg_fmtint(oAddressFamily, o->address_family);
3013: dump_cfg_fmtint(oBatchMode, o->batch_mode);
3014: dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
3015: dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
3016: dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
3017: dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
3018: dump_cfg_fmtint(oCompression, o->compression);
3019: dump_cfg_fmtint(oControlMaster, o->control_master);
3020: dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
1.256 dtucker 3021: dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
1.221 djm 3022: dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
1.224 djm 3023: dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
1.221 djm 3024: dump_cfg_fmtint(oForwardX11, o->forward_x11);
3025: dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
3026: dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
3027: #ifdef GSSAPI
3028: dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
3029: dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
3030: #endif /* GSSAPI */
3031: dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
3032: dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
3033: dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
3034: dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
3035: dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
3036: dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
3037: dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
3038: dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
3039: dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
3040: dump_cfg_fmtint(oRequestTTY, o->request_tty);
3041: dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
3042: dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
3043: dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
3044: dump_cfg_fmtint(oTunnel, o->tun_open);
3045: dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
3046: dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
1.229 djm 3047: dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
1.221 djm 3048:
3049: /* Integer options */
3050: dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
3051: dump_cfg_int(oConnectionAttempts, o->connection_attempts);
3052: dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
3053: dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
3054: dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
3055: dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
3056:
3057: /* String options */
3058: dump_cfg_string(oBindAddress, o->bind_address);
1.282 djm 3059: dump_cfg_string(oBindInterface, o->bind_interface);
1.320 dtucker 3060: dump_cfg_string(oCiphers, o->ciphers);
1.221 djm 3061: dump_cfg_string(oControlPath, o->control_path);
1.240 djm 3062: dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
1.221 djm 3063: dump_cfg_string(oHostKeyAlias, o->host_key_alias);
1.230 djm 3064: dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
1.253 markus 3065: dump_cfg_string(oIdentityAgent, o->identity_agent);
1.285 djm 3066: dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
1.221 djm 3067: dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
1.320 dtucker 3068: dump_cfg_string(oKexAlgorithms, o->kex_algorithms);
3069: dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms);
1.221 djm 3070: dump_cfg_string(oLocalCommand, o->local_command);
1.277 bluhm 3071: dump_cfg_string(oRemoteCommand, o->remote_command);
1.221 djm 3072: dump_cfg_string(oLogLevel, log_level_name(o->log_level));
1.320 dtucker 3073: dump_cfg_string(oMacs, o->macs);
1.266 djm 3074: #ifdef ENABLE_PKCS11
1.221 djm 3075: dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
1.266 djm 3076: #endif
1.310 djm 3077: dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
1.221 djm 3078: dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
1.242 djm 3079: dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
1.230 djm 3080: dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
1.221 djm 3081: dump_cfg_string(oXAuthLocation, o->xauth_location);
3082:
1.230 djm 3083: /* Forwards */
1.221 djm 3084: dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
3085: dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
3086: dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
3087:
3088: /* String array options */
3089: dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
3090: dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
1.285 djm 3091: dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
1.221 djm 3092: dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
3093: dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
3094: dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
1.290 djm 3095: dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
1.339 djm 3096: dump_cfg_strarray_oneline(oLogVerbose,
3097: o->num_log_verbose, o->log_verbose);
1.221 djm 3098:
3099: /* Special cases */
1.334 djm 3100:
3101: /* AddKeysToAgent */
3102: if (o->add_keys_to_agent_lifespan <= 0)
3103: dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
3104: else {
3105: printf("addkeystoagent%s %d\n",
3106: o->add_keys_to_agent == 3 ? " confirm" : "",
3107: o->add_keys_to_agent_lifespan);
3108: }
1.319 djm 3109:
3110: /* oForwardAgent */
3111: if (o->forward_agent_sock_path == NULL)
3112: dump_cfg_fmtint(oForwardAgent, o->forward_agent);
3113: else
3114: dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
1.221 djm 3115:
3116: /* oConnectTimeout */
3117: if (o->connection_timeout == -1)
3118: printf("connecttimeout none\n");
3119: else
3120: dump_cfg_int(oConnectTimeout, o->connection_timeout);
3121:
3122: /* oTunnelDevice */
3123: printf("tunneldevice");
3124: if (o->tun_local == SSH_TUNID_ANY)
3125: printf(" any");
3126: else
3127: printf(" %d", o->tun_local);
3128: if (o->tun_remote == SSH_TUNID_ANY)
3129: printf(":any");
3130: else
3131: printf(":%d", o->tun_remote);
3132: printf("\n");
3133:
3134: /* oCanonicalizePermittedCNAMEs */
3135: if ( o->num_permitted_cnames > 0) {
3136: printf("canonicalizePermittedcnames");
3137: for (i = 0; i < o->num_permitted_cnames; i++) {
3138: printf(" %s:%s", o->permitted_cnames[i].source_list,
3139: o->permitted_cnames[i].target_list);
3140: }
3141: printf("\n");
3142: }
3143:
3144: /* oControlPersist */
3145: if (o->control_persist == 0 || o->control_persist_timeout == 0)
3146: dump_cfg_fmtint(oControlPersist, o->control_persist);
3147: else
3148: dump_cfg_int(oControlPersist, o->control_persist_timeout);
3149:
3150: /* oEscapeChar */
3151: if (o->escape_char == SSH_ESCAPECHAR_NONE)
3152: printf("escapechar none\n");
3153: else {
1.257 djm 3154: vis(buf, o->escape_char, VIS_WHITE, 0);
3155: printf("escapechar %s\n", buf);
1.221 djm 3156: }
3157:
3158: /* oIPQoS */
3159: printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
3160: printf("%s\n", iptos2str(o->ip_qos_bulk));
3161:
3162: /* oRekeyLimit */
1.249 dtucker 3163: printf("rekeylimit %llu %d\n",
3164: (unsigned long long)o->rekey_limit, o->rekey_interval);
1.221 djm 3165:
3166: /* oStreamLocalBindMask */
3167: printf("streamlocalbindmask 0%o\n",
3168: o->fwd_opts.streamlocal_bind_mask);
1.285 djm 3169:
3170: /* oLogFacility */
3171: printf("syslogfacility %s\n", log_facility_name(o->log_facility));
1.257 djm 3172:
3173: /* oProxyCommand / oProxyJump */
3174: if (o->jump_host == NULL)
3175: dump_cfg_string(oProxyCommand, o->proxy_command);
3176: else {
3177: /* Check for numeric addresses */
3178: i = strchr(o->jump_host, ':') != NULL ||
3179: strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
3180: snprintf(buf, sizeof(buf), "%d", o->jump_port);
3181: printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
1.259 djm 3182: /* optional additional jump spec */
3183: o->jump_extra == NULL ? "" : o->jump_extra,
3184: o->jump_extra == NULL ? "" : ",",
1.257 djm 3185: /* optional user */
3186: o->jump_user == NULL ? "" : o->jump_user,
3187: o->jump_user == NULL ? "" : "@",
3188: /* opening [ if hostname is numeric */
3189: i ? "[" : "",
3190: /* mandatory hostname */
3191: o->jump_host,
3192: /* closing ] if hostname is numeric */
3193: i ? "]" : "",
3194: /* optional port number */
3195: o->jump_port <= 0 ? "" : ":",
1.259 djm 3196: o->jump_port <= 0 ? "" : buf);
1.257 djm 3197: }
1.1 deraadt 3198: }