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