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