Annotation of src/usr.bin/ssh/readconf.c, Revision 1.268
1.268 ! djm 1: /* $OpenBSD: readconf.c,v 1.267 2017/02/03 05:05:56 djm Exp $ */
1.1 deraadt 2: /*
1.18 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Functions for reading the configuration files.
1.26 markus 7: *
1.46 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.18 deraadt 13: */
1.1 deraadt 14:
1.147 stevesk 15: #include <sys/types.h>
16: #include <sys/stat.h>
1.152 stevesk 17: #include <sys/socket.h>
1.206 djm 18: #include <sys/wait.h>
1.220 millert 19: #include <sys/un.h>
1.152 stevesk 20:
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);
1488: continue;
1489: } else if (r != 0 || gl.gl_pathc < 0)
1490: fatal("%.200s line %d: glob failed for %s.",
1491: filename, linenum, arg2);
1492: free(arg2);
1493: oactive = *activep;
1494: for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1495: debug3("%.200s line %d: Including file %s "
1496: "depth %d%s", filename, linenum,
1497: gl.gl_pathv[i], depth,
1498: oactive ? "" : " (parse only)");
1499: r = read_config_file_depth(gl.gl_pathv[i],
1500: pw, host, original_host, options,
1501: flags | SSHCONF_CHECKPERM |
1502: (oactive ? 0 : SSHCONF_NEVERMATCH),
1503: activep, depth + 1);
1.264 djm 1504: if (r != 1 && errno != ENOENT) {
1.263 djm 1505: fatal("Can't open user config file "
1506: "%.100s: %.100s", gl.gl_pathv[i],
1507: strerror(errno));
1508: }
1.252 djm 1509: /*
1510: * don't let Match in includes clobber the
1511: * containing file's Match state.
1512: */
1513: *activep = oactive;
1514: if (r != 1)
1515: value = -1;
1516: }
1517: globfree(&gl);
1518: }
1519: if (value != 0)
1520: return value;
1521: break;
1522:
1.190 djm 1523: case oIPQoS:
1524: arg = strdelim(&s);
1525: if ((value = parse_ipqos(arg)) == -1)
1526: fatal("%s line %d: Bad IPQoS value: %s",
1527: filename, linenum, arg);
1528: arg = strdelim(&s);
1529: if (arg == NULL)
1530: value2 = value;
1531: else if ((value2 = parse_ipqos(arg)) == -1)
1532: fatal("%s line %d: Bad IPQoS value: %s",
1533: filename, linenum, arg);
1534: if (*activep) {
1535: options->ip_qos_interactive = value;
1536: options->ip_qos_bulk = value2;
1537: }
1538: break;
1539:
1.192 djm 1540: case oRequestTTY:
1541: intptr = &options->request_tty;
1.207 djm 1542: multistate_ptr = multistate_requesttty;
1543: goto parse_multistate;
1.192 djm 1544:
1.199 djm 1545: case oIgnoreUnknown:
1546: charptr = &options->ignored_unknown;
1547: goto parse_string;
1548:
1.205 djm 1549: case oProxyUseFdpass:
1550: intptr = &options->proxy_use_fdpass;
1551: goto parse_flag;
1552:
1.208 djm 1553: case oCanonicalDomains:
1554: value = options->num_canonical_domains != 0;
1555: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1556: valid_domain(arg, filename, linenum);
1557: if (!*activep || value)
1558: continue;
1559: if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1560: fatal("%s line %d: too many hostname suffixes.",
1561: filename, linenum);
1562: options->canonical_domains[
1563: options->num_canonical_domains++] = xstrdup(arg);
1564: }
1565: break;
1566:
1.209 djm 1567: case oCanonicalizePermittedCNAMEs:
1.208 djm 1568: value = options->num_permitted_cnames != 0;
1569: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1570: /* Either '*' for everything or 'list:list' */
1571: if (strcmp(arg, "*") == 0)
1572: arg2 = arg;
1573: else {
1574: lowercase(arg);
1575: if ((arg2 = strchr(arg, ':')) == NULL ||
1576: arg2[1] == '\0') {
1577: fatal("%s line %d: "
1578: "Invalid permitted CNAME \"%s\"",
1579: filename, linenum, arg);
1580: }
1581: *arg2 = '\0';
1582: arg2++;
1583: }
1584: if (!*activep || value)
1585: continue;
1586: if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1587: fatal("%s line %d: too many permitted CNAMEs.",
1588: filename, linenum);
1589: cname = options->permitted_cnames +
1590: options->num_permitted_cnames++;
1591: cname->source_list = xstrdup(arg);
1592: cname->target_list = xstrdup(arg2);
1593: }
1594: break;
1595:
1.209 djm 1596: case oCanonicalizeHostname:
1597: intptr = &options->canonicalize_hostname;
1598: multistate_ptr = multistate_canonicalizehostname;
1.208 djm 1599: goto parse_multistate;
1600:
1.209 djm 1601: case oCanonicalizeMaxDots:
1602: intptr = &options->canonicalize_max_dots;
1.208 djm 1603: goto parse_int;
1604:
1.209 djm 1605: case oCanonicalizeFallbackLocal:
1606: intptr = &options->canonicalize_fallback_local;
1.208 djm 1607: goto parse_flag;
1608:
1.220 millert 1609: case oStreamLocalBindMask:
1610: arg = strdelim(&s);
1611: if (!arg || *arg == '\0')
1612: fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1613: /* Parse mode in octal format */
1614: value = strtol(arg, &endofnumber, 8);
1615: if (arg == endofnumber || value < 0 || value > 0777)
1616: fatal("%.200s line %d: Bad mask.", filename, linenum);
1617: options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1618: break;
1619:
1620: case oStreamLocalBindUnlink:
1621: intptr = &options->fwd_opts.streamlocal_bind_unlink;
1622: goto parse_flag;
1623:
1.223 djm 1624: case oRevokedHostKeys:
1625: charptr = &options->revoked_host_keys;
1626: goto parse_string;
1627:
1.224 djm 1628: case oFingerprintHash:
1.225 djm 1629: intptr = &options->fingerprint_hash;
1.224 djm 1630: arg = strdelim(&s);
1631: if (!arg || *arg == '\0')
1632: fatal("%.200s line %d: Missing argument.",
1633: filename, linenum);
1634: if ((value = ssh_digest_alg_by_name(arg)) == -1)
1635: fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1636: filename, linenum, arg);
1.225 djm 1637: if (*activep && *intptr == -1)
1638: *intptr = value;
1.224 djm 1639: break;
1640:
1.229 djm 1641: case oUpdateHostkeys:
1642: intptr = &options->update_hostkeys;
1.232 djm 1643: multistate_ptr = multistate_yesnoask;
1644: goto parse_multistate;
1.229 djm 1645:
1.230 djm 1646: case oHostbasedKeyTypes:
1647: charptr = &options->hostbased_key_types;
1.238 markus 1648: goto parse_keytypes;
1649:
1650: case oPubkeyAcceptedKeyTypes:
1651: charptr = &options->pubkey_key_types;
1652: goto parse_keytypes;
1.230 djm 1653:
1.246 jcs 1654: case oAddKeysToAgent:
1655: intptr = &options->add_keys_to_agent;
1656: multistate_ptr = multistate_yesnoaskconfirm;
1657: goto parse_multistate;
1658:
1.253 markus 1659: case oIdentityAgent:
1660: charptr = &options->identity_agent;
1661: goto parse_string;
1662:
1.96 markus 1663: case oDeprecated:
1.98 markus 1664: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 1665: filename, linenum, keyword);
1.98 markus 1666: return 0;
1.96 markus 1667:
1.110 jakob 1668: case oUnsupported:
1669: error("%s line %d: Unsupported option \"%s\"",
1670: filename, linenum, keyword);
1671: return 0;
1672:
1.17 markus 1673: default:
1.221 djm 1674: fatal("%s: Unimplemented opcode %d", __func__, opcode);
1.17 markus 1675: }
1676:
1677: /* Check that there is no garbage at end of line. */
1.57 djm 1678: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 1679: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142 djm 1680: filename, linenum, arg);
1.39 ho 1681: }
1.17 markus 1682: return 0;
1.1 deraadt 1683: }
1684:
1.19 markus 1685: /*
1686: * Reads the config file and modifies the options accordingly. Options
1687: * should already be initialized before this call. This never returns if
1.89 stevesk 1688: * there is an error. If the file does not exist, this returns 0.
1.19 markus 1689: */
1.89 stevesk 1690: int
1.206 djm 1691: read_config_file(const char *filename, struct passwd *pw, const char *host,
1.221 djm 1692: const char *original_host, Options *options, int flags)
1.1 deraadt 1693: {
1.252 djm 1694: int active = 1;
1695:
1696: return read_config_file_depth(filename, pw, host, original_host,
1697: options, flags, &active, 0);
1698: }
1699:
1700: #define READCONF_MAX_DEPTH 16
1701: static int
1702: read_config_file_depth(const char *filename, struct passwd *pw,
1703: const char *host, const char *original_host, Options *options,
1704: int flags, int *activep, int depth)
1705: {
1.17 markus 1706: FILE *f;
1707: char line[1024];
1.252 djm 1708: int linenum;
1.17 markus 1709: int bad_options = 0;
1710:
1.252 djm 1711: if (depth < 0 || depth > READCONF_MAX_DEPTH)
1712: fatal("Too many recursive configuration includes");
1713:
1.129 djm 1714: if ((f = fopen(filename, "r")) == NULL)
1.89 stevesk 1715: return 0;
1.129 djm 1716:
1.196 dtucker 1717: if (flags & SSHCONF_CHECKPERM) {
1.129 djm 1718: struct stat sb;
1.134 deraadt 1719:
1.131 dtucker 1720: if (fstat(fileno(f), &sb) == -1)
1.129 djm 1721: fatal("fstat %s: %s", filename, strerror(errno));
1722: if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131 dtucker 1723: (sb.st_mode & 022) != 0))
1.129 djm 1724: fatal("Bad owner or permissions on %s", filename);
1725: }
1.17 markus 1726:
1727: debug("Reading configuration data %.200s", filename);
1728:
1.19 markus 1729: /*
1730: * Mark that we are now processing the options. This flag is turned
1731: * on/off by Host specifications.
1732: */
1.17 markus 1733: linenum = 0;
1734: while (fgets(line, sizeof(line), f)) {
1735: /* Update line number counter. */
1736: linenum++;
1.252 djm 1737: if (process_config_line_depth(options, pw, host, original_host,
1738: line, filename, linenum, activep, flags, depth) != 0)
1.17 markus 1739: bad_options++;
1740: }
1741: fclose(f);
1742: if (bad_options > 0)
1.64 millert 1743: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 1744: filename, bad_options);
1.89 stevesk 1745: return 1;
1.1 deraadt 1746: }
1747:
1.218 djm 1748: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1749: int
1750: option_clear_or_none(const char *o)
1751: {
1752: return o == NULL || strcasecmp(o, "none") == 0;
1753: }
1754:
1.19 markus 1755: /*
1756: * Initializes options to special values that indicate that they have not yet
1757: * been set. Read_config_file will only set options with this value. Options
1758: * are processed in the following order: command line, user config file,
1759: * system config file. Last, fill_default_options is called.
1760: */
1.1 deraadt 1761:
1.26 markus 1762: void
1.17 markus 1763: initialize_options(Options * options)
1.1 deraadt 1764: {
1.17 markus 1765: memset(options, 'X', sizeof(*options));
1766: options->forward_agent = -1;
1767: options->forward_x11 = -1;
1.123 markus 1768: options->forward_x11_trusted = -1;
1.186 djm 1769: options->forward_x11_timeout = -1;
1.255 dtucker 1770: options->stdio_forward_host = NULL;
1771: options->stdio_forward_port = 0;
1.256 dtucker 1772: options->clear_forwardings = -1;
1.153 markus 1773: options->exit_on_forward_failure = -1;
1.34 markus 1774: options->xauth_location = NULL;
1.220 millert 1775: options->fwd_opts.gateway_ports = -1;
1776: options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1777: options->fwd_opts.streamlocal_bind_unlink = -1;
1.17 markus 1778: options->use_privileged_port = -1;
1779: options->rsa_authentication = -1;
1.50 markus 1780: options->pubkey_authentication = -1;
1.78 markus 1781: options->challenge_response_authentication = -1;
1.118 markus 1782: options->gss_authentication = -1;
1783: options->gss_deleg_creds = -1;
1.17 markus 1784: options->password_authentication = -1;
1.48 markus 1785: options->kbd_interactive_authentication = -1;
1786: options->kbd_interactive_devices = NULL;
1.17 markus 1787: options->rhosts_rsa_authentication = -1;
1.72 markus 1788: options->hostbased_authentication = -1;
1.17 markus 1789: options->batch_mode = -1;
1790: options->check_host_ip = -1;
1791: options->strict_host_key_checking = -1;
1792: options->compression = -1;
1.126 markus 1793: options->tcp_keep_alive = -1;
1.17 markus 1794: options->compression_level = -1;
1795: options->port = -1;
1.114 djm 1796: options->address_family = -1;
1.17 markus 1797: options->connection_attempts = -1;
1.111 djm 1798: options->connection_timeout = -1;
1.17 markus 1799: options->number_of_password_prompts = -1;
1800: options->cipher = -1;
1.25 markus 1801: options->ciphers = NULL;
1.62 markus 1802: options->macs = NULL;
1.189 djm 1803: options->kex_algorithms = NULL;
1.76 markus 1804: options->hostkeyalgorithms = NULL;
1.25 markus 1805: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 1806: options->num_identity_files = 0;
1.241 djm 1807: options->num_certificate_files = 0;
1.17 markus 1808: options->hostname = NULL;
1.52 markus 1809: options->host_key_alias = NULL;
1.17 markus 1810: options->proxy_command = NULL;
1.257 djm 1811: options->jump_user = NULL;
1812: options->jump_host = NULL;
1813: options->jump_port = -1;
1814: options->jump_extra = NULL;
1.17 markus 1815: options->user = NULL;
1816: options->escape_char = -1;
1.193 djm 1817: options->num_system_hostfiles = 0;
1818: options->num_user_hostfiles = 0;
1.185 djm 1819: options->local_forwards = NULL;
1.17 markus 1820: options->num_local_forwards = 0;
1.185 djm 1821: options->remote_forwards = NULL;
1.17 markus 1822: options->num_remote_forwards = 0;
1.95 markus 1823: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67 markus 1824: options->preferred_authentications = NULL;
1.77 markus 1825: options->bind_address = NULL;
1.183 markus 1826: options->pkcs11_provider = NULL;
1.101 markus 1827: options->enable_ssh_keysign = - 1;
1.91 markus 1828: options->no_host_authentication_for_localhost = - 1;
1.128 markus 1829: options->identities_only = - 1;
1.105 markus 1830: options->rekey_limit = - 1;
1.198 dtucker 1831: options->rekey_interval = -1;
1.107 jakob 1832: options->verify_host_key_dns = -1;
1.127 markus 1833: options->server_alive_interval = -1;
1834: options->server_alive_count_max = -1;
1.130 djm 1835: options->num_send_env = 0;
1.132 djm 1836: options->control_path = NULL;
1837: options->control_master = -1;
1.187 djm 1838: options->control_persist = -1;
1839: options->control_persist_timeout = 0;
1.136 djm 1840: options->hash_known_hosts = -1;
1.144 reyk 1841: options->tun_open = -1;
1842: options->tun_local = -1;
1843: options->tun_remote = -1;
1844: options->local_command = NULL;
1845: options->permit_local_command = -1;
1.246 jcs 1846: options->add_keys_to_agent = -1;
1.253 markus 1847: options->identity_agent = NULL;
1.167 grunk 1848: options->visual_host_key = -1;
1.190 djm 1849: options->ip_qos_interactive = -1;
1850: options->ip_qos_bulk = -1;
1.192 djm 1851: options->request_tty = -1;
1.205 djm 1852: options->proxy_use_fdpass = -1;
1.199 djm 1853: options->ignored_unknown = NULL;
1.208 djm 1854: options->num_canonical_domains = 0;
1855: options->num_permitted_cnames = 0;
1.209 djm 1856: options->canonicalize_max_dots = -1;
1857: options->canonicalize_fallback_local = -1;
1858: options->canonicalize_hostname = -1;
1.223 djm 1859: options->revoked_host_keys = NULL;
1.224 djm 1860: options->fingerprint_hash = -1;
1.229 djm 1861: options->update_hostkeys = -1;
1.230 djm 1862: options->hostbased_key_types = NULL;
1.238 markus 1863: options->pubkey_key_types = NULL;
1.1 deraadt 1864: }
1865:
1.19 markus 1866: /*
1.218 djm 1867: * A petite version of fill_default_options() that just fills the options
1868: * needed for hostname canonicalization to proceed.
1869: */
1870: void
1871: fill_default_options_for_canonicalization(Options *options)
1872: {
1873: if (options->canonicalize_max_dots == -1)
1874: options->canonicalize_max_dots = 1;
1875: if (options->canonicalize_fallback_local == -1)
1876: options->canonicalize_fallback_local = 1;
1877: if (options->canonicalize_hostname == -1)
1878: options->canonicalize_hostname = SSH_CANONICALISE_NO;
1879: }
1880:
1881: /*
1.19 markus 1882: * Called after processing other sources of option data, this fills those
1883: * options for which no value has been specified with their default values.
1884: */
1.26 markus 1885: void
1.17 markus 1886: fill_default_options(Options * options)
1.1 deraadt 1887: {
1.17 markus 1888: if (options->forward_agent == -1)
1.33 markus 1889: options->forward_agent = 0;
1.17 markus 1890: if (options->forward_x11 == -1)
1.23 markus 1891: options->forward_x11 = 0;
1.123 markus 1892: if (options->forward_x11_trusted == -1)
1893: options->forward_x11_trusted = 0;
1.186 djm 1894: if (options->forward_x11_timeout == -1)
1895: options->forward_x11_timeout = 1200;
1.256 dtucker 1896: /*
1897: * stdio forwarding (-W) changes the default for these but we defer
1898: * setting the values so they can be overridden.
1899: */
1.153 markus 1900: if (options->exit_on_forward_failure == -1)
1.256 dtucker 1901: options->exit_on_forward_failure =
1902: options->stdio_forward_host != NULL ? 1 : 0;
1903: if (options->clear_forwardings == -1)
1904: options->clear_forwardings =
1905: options->stdio_forward_host != NULL ? 1 : 0;
1906: if (options->clear_forwardings == 1)
1907: clear_forwardings(options);
1908:
1.34 markus 1909: if (options->xauth_location == NULL)
1.80 markus 1910: options->xauth_location = _PATH_XAUTH;
1.220 millert 1911: if (options->fwd_opts.gateway_ports == -1)
1912: options->fwd_opts.gateway_ports = 0;
1913: if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1914: options->fwd_opts.streamlocal_bind_mask = 0177;
1915: if (options->fwd_opts.streamlocal_bind_unlink == -1)
1916: options->fwd_opts.streamlocal_bind_unlink = 0;
1.17 markus 1917: if (options->use_privileged_port == -1)
1.65 markus 1918: options->use_privileged_port = 0;
1.17 markus 1919: if (options->rsa_authentication == -1)
1920: options->rsa_authentication = 1;
1.50 markus 1921: if (options->pubkey_authentication == -1)
1922: options->pubkey_authentication = 1;
1.78 markus 1923: if (options->challenge_response_authentication == -1)
1.83 markus 1924: options->challenge_response_authentication = 1;
1.118 markus 1925: if (options->gss_authentication == -1)
1.122 markus 1926: options->gss_authentication = 0;
1.118 markus 1927: if (options->gss_deleg_creds == -1)
1928: options->gss_deleg_creds = 0;
1.17 markus 1929: if (options->password_authentication == -1)
1930: options->password_authentication = 1;
1.48 markus 1931: if (options->kbd_interactive_authentication == -1)
1.59 markus 1932: options->kbd_interactive_authentication = 1;
1.17 markus 1933: if (options->rhosts_rsa_authentication == -1)
1.99 stevesk 1934: options->rhosts_rsa_authentication = 0;
1.72 markus 1935: if (options->hostbased_authentication == -1)
1936: options->hostbased_authentication = 0;
1.17 markus 1937: if (options->batch_mode == -1)
1938: options->batch_mode = 0;
1939: if (options->check_host_ip == -1)
1940: options->check_host_ip = 1;
1941: if (options->strict_host_key_checking == -1)
1942: options->strict_host_key_checking = 2; /* 2 is default */
1943: if (options->compression == -1)
1944: options->compression = 0;
1.126 markus 1945: if (options->tcp_keep_alive == -1)
1946: options->tcp_keep_alive = 1;
1.17 markus 1947: if (options->compression_level == -1)
1948: options->compression_level = 6;
1949: if (options->port == -1)
1950: options->port = 0; /* Filled in ssh_connect. */
1.114 djm 1951: if (options->address_family == -1)
1952: options->address_family = AF_UNSPEC;
1.17 markus 1953: if (options->connection_attempts == -1)
1.84 markus 1954: options->connection_attempts = 1;
1.17 markus 1955: if (options->number_of_password_prompts == -1)
1956: options->number_of_password_prompts = 3;
1957: /* Selected in ssh_login(). */
1958: if (options->cipher == -1)
1959: options->cipher = SSH_CIPHER_NOT_SET;
1.76 markus 1960: /* options->hostkeyalgorithms, default set in myproposals.h */
1.25 markus 1961: if (options->protocol == SSH_PROTO_UNKNOWN)
1.178 markus 1962: options->protocol = SSH_PROTO_2;
1.246 jcs 1963: if (options->add_keys_to_agent == -1)
1964: options->add_keys_to_agent = 0;
1.17 markus 1965: if (options->num_identity_files == 0) {
1.50 markus 1966: if (options->protocol & SSH_PROTO_1) {
1.195 dtucker 1967: add_identity_file(options, "~/",
1968: _PATH_SSH_CLIENT_IDENTITY, 0);
1.50 markus 1969: }
1970: if (options->protocol & SSH_PROTO_2) {
1.195 dtucker 1971: add_identity_file(options, "~/",
1972: _PATH_SSH_CLIENT_ID_RSA, 0);
1973: add_identity_file(options, "~/",
1974: _PATH_SSH_CLIENT_ID_DSA, 0);
1975: add_identity_file(options, "~/",
1976: _PATH_SSH_CLIENT_ID_ECDSA, 0);
1.215 markus 1977: add_identity_file(options, "~/",
1978: _PATH_SSH_CLIENT_ID_ED25519, 0);
1.50 markus 1979: }
1.27 markus 1980: }
1.17 markus 1981: if (options->escape_char == -1)
1982: options->escape_char = '~';
1.193 djm 1983: if (options->num_system_hostfiles == 0) {
1984: options->system_hostfiles[options->num_system_hostfiles++] =
1985: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1986: options->system_hostfiles[options->num_system_hostfiles++] =
1987: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1988: }
1989: if (options->num_user_hostfiles == 0) {
1990: options->user_hostfiles[options->num_user_hostfiles++] =
1991: xstrdup(_PATH_SSH_USER_HOSTFILE);
1992: options->user_hostfiles[options->num_user_hostfiles++] =
1993: xstrdup(_PATH_SSH_USER_HOSTFILE2);
1994: }
1.95 markus 1995: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 1996: options->log_level = SYSLOG_LEVEL_INFO;
1.91 markus 1997: if (options->no_host_authentication_for_localhost == - 1)
1998: options->no_host_authentication_for_localhost = 0;
1.128 markus 1999: if (options->identities_only == -1)
2000: options->identities_only = 0;
1.101 markus 2001: if (options->enable_ssh_keysign == -1)
2002: options->enable_ssh_keysign = 0;
1.105 markus 2003: if (options->rekey_limit == -1)
2004: options->rekey_limit = 0;
1.198 dtucker 2005: if (options->rekey_interval == -1)
2006: options->rekey_interval = 0;
1.107 jakob 2007: if (options->verify_host_key_dns == -1)
2008: options->verify_host_key_dns = 0;
1.127 markus 2009: if (options->server_alive_interval == -1)
2010: options->server_alive_interval = 0;
2011: if (options->server_alive_count_max == -1)
2012: options->server_alive_count_max = 3;
1.132 djm 2013: if (options->control_master == -1)
2014: options->control_master = 0;
1.187 djm 2015: if (options->control_persist == -1) {
2016: options->control_persist = 0;
2017: options->control_persist_timeout = 0;
2018: }
1.136 djm 2019: if (options->hash_known_hosts == -1)
2020: options->hash_known_hosts = 0;
1.144 reyk 2021: if (options->tun_open == -1)
1.145 reyk 2022: options->tun_open = SSH_TUNMODE_NO;
2023: if (options->tun_local == -1)
2024: options->tun_local = SSH_TUNID_ANY;
2025: if (options->tun_remote == -1)
2026: options->tun_remote = SSH_TUNID_ANY;
1.144 reyk 2027: if (options->permit_local_command == -1)
2028: options->permit_local_command = 0;
1.167 grunk 2029: if (options->visual_host_key == -1)
2030: options->visual_host_key = 0;
1.190 djm 2031: if (options->ip_qos_interactive == -1)
2032: options->ip_qos_interactive = IPTOS_LOWDELAY;
2033: if (options->ip_qos_bulk == -1)
2034: options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.192 djm 2035: if (options->request_tty == -1)
2036: options->request_tty = REQUEST_TTY_AUTO;
1.205 djm 2037: if (options->proxy_use_fdpass == -1)
2038: options->proxy_use_fdpass = 0;
1.209 djm 2039: if (options->canonicalize_max_dots == -1)
2040: options->canonicalize_max_dots = 1;
2041: if (options->canonicalize_fallback_local == -1)
2042: options->canonicalize_fallback_local = 1;
2043: if (options->canonicalize_hostname == -1)
2044: options->canonicalize_hostname = SSH_CANONICALISE_NO;
1.224 djm 2045: if (options->fingerprint_hash == -1)
2046: options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.229 djm 2047: if (options->update_hostkeys == -1)
1.231 djm 2048: options->update_hostkeys = 0;
1.239 djm 2049: if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2050: kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2051: kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2052: kex_assemble_names(KEX_DEFAULT_PK_ALG,
2053: &options->hostbased_key_types) != 0 ||
2054: kex_assemble_names(KEX_DEFAULT_PK_ALG,
2055: &options->pubkey_key_types) != 0)
2056: fatal("%s: kex_assemble_names failed", __func__);
1.224 djm 2057:
1.207 djm 2058: #define CLEAR_ON_NONE(v) \
2059: do { \
1.218 djm 2060: if (option_clear_or_none(v)) { \
1.207 djm 2061: free(v); \
2062: v = NULL; \
2063: } \
2064: } while(0)
2065: CLEAR_ON_NONE(options->local_command);
2066: CLEAR_ON_NONE(options->proxy_command);
2067: CLEAR_ON_NONE(options->control_path);
1.223 djm 2068: CLEAR_ON_NONE(options->revoked_host_keys);
1.254 markus 2069: /* options->identity_agent distinguishes NULL from 'none' */
1.17 markus 2070: /* options->user will be set in the main program if appropriate */
2071: /* options->hostname will be set in the main program if appropriate */
1.52 markus 2072: /* options->host_key_alias should not be set by default */
1.67 markus 2073: /* options->preferred_authentications will be set in ssh */
1.135 djm 2074: }
2075:
1.220 millert 2076: struct fwdarg {
2077: char *arg;
2078: int ispath;
2079: };
2080:
2081: /*
2082: * parse_fwd_field
2083: * parses the next field in a port forwarding specification.
2084: * sets fwd to the parsed field and advances p past the colon
2085: * or sets it to NULL at end of string.
2086: * returns 0 on success, else non-zero.
2087: */
2088: static int
2089: parse_fwd_field(char **p, struct fwdarg *fwd)
2090: {
2091: char *ep, *cp = *p;
2092: int ispath = 0;
2093:
2094: if (*cp == '\0') {
2095: *p = NULL;
2096: return -1; /* end of string */
2097: }
2098:
2099: /*
2100: * A field escaped with square brackets is used literally.
2101: * XXX - allow ']' to be escaped via backslash?
2102: */
2103: if (*cp == '[') {
2104: /* find matching ']' */
2105: for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2106: if (*ep == '/')
2107: ispath = 1;
2108: }
2109: /* no matching ']' or not at end of field. */
2110: if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2111: return -1;
2112: /* NUL terminate the field and advance p past the colon */
2113: *ep++ = '\0';
2114: if (*ep != '\0')
2115: *ep++ = '\0';
2116: fwd->arg = cp + 1;
2117: fwd->ispath = ispath;
2118: *p = ep;
2119: return 0;
2120: }
2121:
2122: for (cp = *p; *cp != '\0'; cp++) {
2123: switch (*cp) {
2124: case '\\':
2125: memmove(cp, cp + 1, strlen(cp + 1) + 1);
1.237 djm 2126: if (*cp == '\0')
2127: return -1;
1.220 millert 2128: break;
2129: case '/':
2130: ispath = 1;
2131: break;
2132: case ':':
2133: *cp++ = '\0';
2134: goto done;
2135: }
2136: }
2137: done:
2138: fwd->arg = *p;
2139: fwd->ispath = ispath;
2140: *p = cp;
2141: return 0;
2142: }
2143:
1.135 djm 2144: /*
2145: * parse_forward
2146: * parses a string containing a port forwarding specification of the form:
1.168 stevesk 2147: * dynamicfwd == 0
1.220 millert 2148: * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2149: * listenpath:connectpath
1.168 stevesk 2150: * dynamicfwd == 1
2151: * [listenhost:]listenport
1.135 djm 2152: * returns number of arguments parsed or zero on error
2153: */
2154: int
1.220 millert 2155: parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135 djm 2156: {
1.220 millert 2157: struct fwdarg fwdargs[4];
2158: char *p, *cp;
1.135 djm 2159: int i;
2160:
1.220 millert 2161: memset(fwd, 0, sizeof(*fwd));
2162: memset(fwdargs, 0, sizeof(fwdargs));
1.135 djm 2163:
2164: cp = p = xstrdup(fwdspec);
2165:
2166: /* skip leading spaces */
1.214 deraadt 2167: while (isspace((u_char)*cp))
1.135 djm 2168: cp++;
2169:
1.220 millert 2170: for (i = 0; i < 4; ++i) {
2171: if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
1.135 djm 2172: break;
1.220 millert 2173: }
1.135 djm 2174:
1.170 stevesk 2175: /* Check for trailing garbage */
1.220 millert 2176: if (cp != NULL && *cp != '\0') {
1.135 djm 2177: i = 0; /* failure */
1.220 millert 2178: }
1.135 djm 2179:
2180: switch (i) {
1.168 stevesk 2181: case 1:
1.220 millert 2182: if (fwdargs[0].ispath) {
2183: fwd->listen_path = xstrdup(fwdargs[0].arg);
2184: fwd->listen_port = PORT_STREAMLOCAL;
2185: } else {
2186: fwd->listen_host = NULL;
2187: fwd->listen_port = a2port(fwdargs[0].arg);
2188: }
1.168 stevesk 2189: fwd->connect_host = xstrdup("socks");
2190: break;
2191:
2192: case 2:
1.220 millert 2193: if (fwdargs[0].ispath && fwdargs[1].ispath) {
2194: fwd->listen_path = xstrdup(fwdargs[0].arg);
2195: fwd->listen_port = PORT_STREAMLOCAL;
2196: fwd->connect_path = xstrdup(fwdargs[1].arg);
2197: fwd->connect_port = PORT_STREAMLOCAL;
2198: } else if (fwdargs[1].ispath) {
2199: fwd->listen_host = NULL;
2200: fwd->listen_port = a2port(fwdargs[0].arg);
2201: fwd->connect_path = xstrdup(fwdargs[1].arg);
2202: fwd->connect_port = PORT_STREAMLOCAL;
2203: } else {
2204: fwd->listen_host = xstrdup(fwdargs[0].arg);
2205: fwd->listen_port = a2port(fwdargs[1].arg);
2206: fwd->connect_host = xstrdup("socks");
2207: }
1.168 stevesk 2208: break;
2209:
1.135 djm 2210: case 3:
1.220 millert 2211: if (fwdargs[0].ispath) {
2212: fwd->listen_path = xstrdup(fwdargs[0].arg);
2213: fwd->listen_port = PORT_STREAMLOCAL;
2214: fwd->connect_host = xstrdup(fwdargs[1].arg);
2215: fwd->connect_port = a2port(fwdargs[2].arg);
2216: } else if (fwdargs[2].ispath) {
2217: fwd->listen_host = xstrdup(fwdargs[0].arg);
2218: fwd->listen_port = a2port(fwdargs[1].arg);
2219: fwd->connect_path = xstrdup(fwdargs[2].arg);
2220: fwd->connect_port = PORT_STREAMLOCAL;
2221: } else {
2222: fwd->listen_host = NULL;
2223: fwd->listen_port = a2port(fwdargs[0].arg);
2224: fwd->connect_host = xstrdup(fwdargs[1].arg);
2225: fwd->connect_port = a2port(fwdargs[2].arg);
2226: }
1.135 djm 2227: break;
2228:
2229: case 4:
1.220 millert 2230: fwd->listen_host = xstrdup(fwdargs[0].arg);
2231: fwd->listen_port = a2port(fwdargs[1].arg);
2232: fwd->connect_host = xstrdup(fwdargs[2].arg);
2233: fwd->connect_port = a2port(fwdargs[3].arg);
1.135 djm 2234: break;
2235: default:
2236: i = 0; /* failure */
2237: }
2238:
1.202 djm 2239: free(p);
1.135 djm 2240:
1.168 stevesk 2241: if (dynamicfwd) {
2242: if (!(i == 1 || i == 2))
2243: goto fail_free;
2244: } else {
1.220 millert 2245: if (!(i == 3 || i == 4)) {
2246: if (fwd->connect_path == NULL &&
2247: fwd->listen_path == NULL)
2248: goto fail_free;
2249: }
2250: if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
1.168 stevesk 2251: goto fail_free;
2252: }
2253:
1.220 millert 2254: if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2255: (!remotefwd && fwd->listen_port == 0))
1.135 djm 2256: goto fail_free;
2257: if (fwd->connect_host != NULL &&
2258: strlen(fwd->connect_host) >= NI_MAXHOST)
2259: goto fail_free;
1.220 millert 2260: /* XXX - if connecting to a remote socket, max sun len may not match this host */
2261: if (fwd->connect_path != NULL &&
2262: strlen(fwd->connect_path) >= PATH_MAX_SUN)
2263: goto fail_free;
1.176 djm 2264: if (fwd->listen_host != NULL &&
2265: strlen(fwd->listen_host) >= NI_MAXHOST)
2266: goto fail_free;
1.220 millert 2267: if (fwd->listen_path != NULL &&
2268: strlen(fwd->listen_path) >= PATH_MAX_SUN)
2269: goto fail_free;
1.135 djm 2270:
2271: return (i);
2272:
2273: fail_free:
1.202 djm 2274: free(fwd->connect_host);
2275: fwd->connect_host = NULL;
1.220 millert 2276: free(fwd->connect_path);
2277: fwd->connect_path = NULL;
1.202 djm 2278: free(fwd->listen_host);
2279: fwd->listen_host = NULL;
1.220 millert 2280: free(fwd->listen_path);
2281: fwd->listen_path = NULL;
1.135 djm 2282: return (0);
1.221 djm 2283: }
2284:
1.257 djm 2285: int
2286: parse_jump(const char *s, Options *o, int active)
2287: {
2288: char *orig, *sdup, *cp;
2289: char *host = NULL, *user = NULL;
1.258 naddy 2290: int ret = -1, port = -1, first;
1.257 djm 2291:
2292: active &= o->proxy_command == NULL && o->jump_host == NULL;
2293:
2294: orig = sdup = xstrdup(s);
1.258 naddy 2295: first = active;
1.259 djm 2296: do {
2297: if ((cp = strrchr(sdup, ',')) == NULL)
2298: cp = sdup; /* last */
2299: else
2300: *cp++ = '\0';
2301:
1.258 naddy 2302: if (first) {
1.257 djm 2303: /* First argument and configuration is active */
2304: if (parse_user_host_port(cp, &user, &host, &port) != 0)
2305: goto out;
2306: } else {
2307: /* Subsequent argument or inactive configuration */
2308: if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2309: goto out;
2310: }
1.258 naddy 2311: first = 0; /* only check syntax for subsequent hosts */
1.259 djm 2312: } while (cp != sdup);
1.257 djm 2313: /* success */
1.258 naddy 2314: if (active) {
2315: o->jump_user = user;
2316: o->jump_host = host;
2317: o->jump_port = port;
2318: o->proxy_command = xstrdup("none");
2319: user = host = NULL;
1.259 djm 2320: if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2321: o->jump_extra = xstrdup(s);
2322: o->jump_extra[cp - s] = '\0';
2323: }
1.258 naddy 2324: }
1.257 djm 2325: ret = 0;
2326: out:
1.258 naddy 2327: free(orig);
1.257 djm 2328: free(user);
2329: free(host);
2330: return ret;
2331: }
2332:
1.221 djm 2333: /* XXX the following is a near-vebatim copy from servconf.c; refactor */
2334: static const char *
2335: fmt_multistate_int(int val, const struct multistate *m)
2336: {
2337: u_int i;
2338:
2339: for (i = 0; m[i].key != NULL; i++) {
2340: if (m[i].value == val)
2341: return m[i].key;
2342: }
2343: return "UNKNOWN";
2344: }
2345:
2346: static const char *
2347: fmt_intarg(OpCodes code, int val)
2348: {
2349: if (val == -1)
2350: return "unset";
2351: switch (code) {
2352: case oAddressFamily:
2353: return fmt_multistate_int(val, multistate_addressfamily);
2354: case oVerifyHostKeyDNS:
2355: case oStrictHostKeyChecking:
1.232 djm 2356: case oUpdateHostkeys:
1.221 djm 2357: return fmt_multistate_int(val, multistate_yesnoask);
2358: case oControlMaster:
2359: return fmt_multistate_int(val, multistate_controlmaster);
2360: case oTunnel:
2361: return fmt_multistate_int(val, multistate_tunnel);
2362: case oRequestTTY:
2363: return fmt_multistate_int(val, multistate_requesttty);
2364: case oCanonicalizeHostname:
2365: return fmt_multistate_int(val, multistate_canonicalizehostname);
1.224 djm 2366: case oFingerprintHash:
2367: return ssh_digest_alg_name(val);
1.221 djm 2368: case oProtocol:
2369: switch (val) {
2370: case SSH_PROTO_1:
2371: return "1";
2372: case SSH_PROTO_2:
2373: return "2";
2374: case (SSH_PROTO_1|SSH_PROTO_2):
2375: return "2,1";
2376: default:
2377: return "UNKNOWN";
2378: }
2379: default:
2380: switch (val) {
2381: case 0:
2382: return "no";
2383: case 1:
2384: return "yes";
2385: default:
2386: return "UNKNOWN";
2387: }
2388: }
2389: }
2390:
2391: static const char *
2392: lookup_opcode_name(OpCodes code)
2393: {
2394: u_int i;
2395:
2396: for (i = 0; keywords[i].name != NULL; i++)
2397: if (keywords[i].opcode == code)
2398: return(keywords[i].name);
2399: return "UNKNOWN";
2400: }
2401:
2402: static void
2403: dump_cfg_int(OpCodes code, int val)
2404: {
2405: printf("%s %d\n", lookup_opcode_name(code), val);
2406: }
2407:
2408: static void
2409: dump_cfg_fmtint(OpCodes code, int val)
2410: {
2411: printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2412: }
2413:
2414: static void
2415: dump_cfg_string(OpCodes code, const char *val)
2416: {
2417: if (val == NULL)
2418: return;
2419: printf("%s %s\n", lookup_opcode_name(code), val);
2420: }
2421:
2422: static void
2423: dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2424: {
2425: u_int i;
2426:
2427: for (i = 0; i < count; i++)
2428: printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2429: }
2430:
2431: static void
2432: dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2433: {
2434: u_int i;
2435:
2436: printf("%s", lookup_opcode_name(code));
2437: for (i = 0; i < count; i++)
2438: printf(" %s", vals[i]);
2439: printf("\n");
2440: }
2441:
2442: static void
2443: dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2444: {
2445: const struct Forward *fwd;
2446: u_int i;
2447:
2448: /* oDynamicForward */
2449: for (i = 0; i < count; i++) {
2450: fwd = &fwds[i];
1.265 djm 2451: if (code == oDynamicForward && fwd->connect_host != NULL &&
1.221 djm 2452: strcmp(fwd->connect_host, "socks") != 0)
2453: continue;
1.265 djm 2454: if (code == oLocalForward && fwd->connect_host != NULL &&
1.221 djm 2455: strcmp(fwd->connect_host, "socks") == 0)
2456: continue;
2457: printf("%s", lookup_opcode_name(code));
2458: if (fwd->listen_port == PORT_STREAMLOCAL)
2459: printf(" %s", fwd->listen_path);
2460: else if (fwd->listen_host == NULL)
2461: printf(" %d", fwd->listen_port);
2462: else {
2463: printf(" [%s]:%d",
2464: fwd->listen_host, fwd->listen_port);
2465: }
2466: if (code != oDynamicForward) {
2467: if (fwd->connect_port == PORT_STREAMLOCAL)
2468: printf(" %s", fwd->connect_path);
2469: else if (fwd->connect_host == NULL)
2470: printf(" %d", fwd->connect_port);
2471: else {
2472: printf(" [%s]:%d",
2473: fwd->connect_host, fwd->connect_port);
2474: }
2475: }
2476: printf("\n");
2477: }
2478: }
2479:
2480: void
2481: dump_client_config(Options *o, const char *host)
2482: {
2483: int i;
1.257 djm 2484: char buf[8];
1.221 djm 2485:
1.240 djm 2486: /* This is normally prepared in ssh_kex2 */
2487: if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2488: fatal("%s: kex_assemble_names failed", __func__);
2489:
1.221 djm 2490: /* Most interesting options first: user, host, port */
2491: dump_cfg_string(oUser, o->user);
2492: dump_cfg_string(oHostName, host);
2493: dump_cfg_int(oPort, o->port);
2494:
2495: /* Flag options */
2496: dump_cfg_fmtint(oAddressFamily, o->address_family);
2497: dump_cfg_fmtint(oBatchMode, o->batch_mode);
2498: dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2499: dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2500: dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2501: dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2502: dump_cfg_fmtint(oCompression, o->compression);
2503: dump_cfg_fmtint(oControlMaster, o->control_master);
2504: dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
1.256 dtucker 2505: dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
1.221 djm 2506: dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
1.224 djm 2507: dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
1.221 djm 2508: dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2509: dump_cfg_fmtint(oForwardX11, o->forward_x11);
2510: dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2511: dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2512: #ifdef GSSAPI
2513: dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2514: dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2515: #endif /* GSSAPI */
2516: dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2517: dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2518: dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2519: dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2520: dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2521: dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2522: dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2523: dump_cfg_fmtint(oProtocol, o->protocol);
2524: dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2525: dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2526: dump_cfg_fmtint(oRequestTTY, o->request_tty);
1.266 djm 2527: #ifdef WITH_RSA1
1.221 djm 2528: dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2529: dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
1.266 djm 2530: #endif
1.221 djm 2531: dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2532: dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2533: dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2534: dump_cfg_fmtint(oTunnel, o->tun_open);
2535: dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2536: dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2537: dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
1.229 djm 2538: dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
1.221 djm 2539:
2540: /* Integer options */
2541: dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
1.266 djm 2542: #ifdef WITH_SSH1
1.221 djm 2543: dump_cfg_int(oCompressionLevel, o->compression_level);
1.266 djm 2544: #endif
1.221 djm 2545: dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2546: dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2547: dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2548: dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2549: dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2550:
2551: /* String options */
2552: dump_cfg_string(oBindAddress, o->bind_address);
2553: dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2554: dump_cfg_string(oControlPath, o->control_path);
1.240 djm 2555: dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
1.221 djm 2556: dump_cfg_string(oHostKeyAlias, o->host_key_alias);
1.230 djm 2557: dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
1.253 markus 2558: dump_cfg_string(oIdentityAgent, o->identity_agent);
1.221 djm 2559: dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2560: dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2561: dump_cfg_string(oLocalCommand, o->local_command);
2562: dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2563: dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
1.266 djm 2564: #ifdef ENABLE_PKCS11
1.221 djm 2565: dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
1.266 djm 2566: #endif
1.221 djm 2567: dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
1.242 djm 2568: dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
1.230 djm 2569: dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
1.221 djm 2570: dump_cfg_string(oXAuthLocation, o->xauth_location);
2571:
1.230 djm 2572: /* Forwards */
1.221 djm 2573: dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2574: dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2575: dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2576:
2577: /* String array options */
2578: dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2579: dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2580: dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2581: dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2582: dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2583:
2584: /* Special cases */
2585:
2586: /* oConnectTimeout */
2587: if (o->connection_timeout == -1)
2588: printf("connecttimeout none\n");
2589: else
2590: dump_cfg_int(oConnectTimeout, o->connection_timeout);
2591:
2592: /* oTunnelDevice */
2593: printf("tunneldevice");
2594: if (o->tun_local == SSH_TUNID_ANY)
2595: printf(" any");
2596: else
2597: printf(" %d", o->tun_local);
2598: if (o->tun_remote == SSH_TUNID_ANY)
2599: printf(":any");
2600: else
2601: printf(":%d", o->tun_remote);
2602: printf("\n");
2603:
2604: /* oCanonicalizePermittedCNAMEs */
2605: if ( o->num_permitted_cnames > 0) {
2606: printf("canonicalizePermittedcnames");
2607: for (i = 0; i < o->num_permitted_cnames; i++) {
2608: printf(" %s:%s", o->permitted_cnames[i].source_list,
2609: o->permitted_cnames[i].target_list);
2610: }
2611: printf("\n");
2612: }
2613:
2614: /* oCipher */
2615: if (o->cipher != SSH_CIPHER_NOT_SET)
2616: printf("Cipher %s\n", cipher_name(o->cipher));
2617:
2618: /* oControlPersist */
2619: if (o->control_persist == 0 || o->control_persist_timeout == 0)
2620: dump_cfg_fmtint(oControlPersist, o->control_persist);
2621: else
2622: dump_cfg_int(oControlPersist, o->control_persist_timeout);
2623:
2624: /* oEscapeChar */
2625: if (o->escape_char == SSH_ESCAPECHAR_NONE)
2626: printf("escapechar none\n");
2627: else {
1.257 djm 2628: vis(buf, o->escape_char, VIS_WHITE, 0);
2629: printf("escapechar %s\n", buf);
1.221 djm 2630: }
2631:
2632: /* oIPQoS */
2633: printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2634: printf("%s\n", iptos2str(o->ip_qos_bulk));
2635:
2636: /* oRekeyLimit */
1.249 dtucker 2637: printf("rekeylimit %llu %d\n",
2638: (unsigned long long)o->rekey_limit, o->rekey_interval);
1.221 djm 2639:
2640: /* oStreamLocalBindMask */
2641: printf("streamlocalbindmask 0%o\n",
2642: o->fwd_opts.streamlocal_bind_mask);
1.257 djm 2643:
2644: /* oProxyCommand / oProxyJump */
2645: if (o->jump_host == NULL)
2646: dump_cfg_string(oProxyCommand, o->proxy_command);
2647: else {
2648: /* Check for numeric addresses */
2649: i = strchr(o->jump_host, ':') != NULL ||
2650: strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2651: snprintf(buf, sizeof(buf), "%d", o->jump_port);
2652: printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
1.259 djm 2653: /* optional additional jump spec */
2654: o->jump_extra == NULL ? "" : o->jump_extra,
2655: o->jump_extra == NULL ? "" : ",",
1.257 djm 2656: /* optional user */
2657: o->jump_user == NULL ? "" : o->jump_user,
2658: o->jump_user == NULL ? "" : "@",
2659: /* opening [ if hostname is numeric */
2660: i ? "[" : "",
2661: /* mandatory hostname */
2662: o->jump_host,
2663: /* closing ] if hostname is numeric */
2664: i ? "]" : "",
2665: /* optional port number */
2666: o->jump_port <= 0 ? "" : ":",
1.259 djm 2667: o->jump_port <= 0 ? "" : buf);
1.257 djm 2668: }
1.1 deraadt 2669: }