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