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