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