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