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