blob: f63894f9ca158bd1db59859e89718104a051efde [file] [log] [blame]
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001/* $OpenBSD: readconf.c,v 1.279 2017/09/21 19:16:53 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "cipher.h"
58#include "pathnames.h"
59#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000060#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100061#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "readconf.h"
63#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000064#include "kex.h"
65#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110066#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000067#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000068#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
70/* Format of the configuration file:
71
72 # Configuration data is parsed as follows:
73 # 1. command line options
74 # 2. user-specific file
75 # 3. system-wide file
76 # Any configuration value is only changed the first time it is set.
77 # Thus, host-specific definitions should be at the beginning of the
78 # configuration file, and defaults at the end.
79
80 # Host-specific declarations. These may override anything above. A single
81 # host may match multiple declarations; these are processed in the order
82 # that they are given in.
83
84 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000085 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87 Host fake.com
88 HostName another.host.name.real.org
89 User blaah
90 Port 34289
91 ForwardX11 no
92 ForwardAgent no
93
94 Host books.com
95 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000096 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98 Host fascist.blob.com
99 Port 23123
100 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 PasswordAuthentication no
102
103 Host puukko.hut.fi
104 User t35124p
105 ProxyCommand ssh-proxy %h %p
106
107 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000108 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000111 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 PasswordAuthentication no
113
Damien Millerd27b9472005-12-13 19:29:02 +1100114 Host vpn.fake.com
115 Tunnel yes
116 TunnelDevice 3
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 # Defaults for various options
119 Host *
120 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 PasswordAuthentication yes
123 RSAAuthentication yes
124 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100126 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127 IdentityFile ~/.ssh/identity
128 Port 22
129 EscapeChar ~
130
131*/
132
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000133static int read_config_file_depth(const char *filename, struct passwd *pw,
134 const char *host, const char *original_host, Options *options,
135 int flags, int *activep, int depth);
136static int process_config_line_depth(Options *options, struct passwd *pw,
137 const char *host, const char *original_host, char *line,
138 const char *filename, int linenum, int *activep, int flags, int depth);
139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000147 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100149 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100151 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100154 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000155 oUsePrivilegedPort, oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100159 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000164 oSendEnv, oControlPath, oControlMaster, oControlPersist,
165 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000166 oTunnel, oTunnelDevice,
167 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000168 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000169 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100170 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
171 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000172 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000173 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000174 oPubkeyAcceptedKeyTypes, oProxyJump,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000175 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176} OpCodes;
177
178/* Textual representations of the tokens. */
179
Damien Miller95def091999-11-25 00:26:21 +1100180static struct {
181 const char *name;
182 OpCodes opcode;
183} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000184 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000185 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000186 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000187 { "fallbacktorsh", oDeprecated },
188 { "globalknownhostsfile2", oDeprecated },
189 { "rhostsauthentication", oDeprecated },
190 { "userknownhostsfile2", oDeprecated },
191 { "useroaming", oDeprecated },
192 { "usersh", oDeprecated },
193
194 /* Unsupported options */
195 { "afstokenpassing", oUnsupported },
196 { "kerberosauthentication", oUnsupported },
197 { "kerberostgtpassing", oUnsupported },
198
199 /* Sometimes-unsupported options */
200#if defined(GSSAPI)
201 { "gssapiauthentication", oGssAuthentication },
202 { "gssapidelegatecredentials", oGssDelegateCreds },
203# else
204 { "gssapiauthentication", oUnsupported },
205 { "gssapidelegatecredentials", oUnsupported },
206#endif
207#ifdef ENABLE_PKCS11
208 { "smartcarddevice", oPKCS11Provider },
209 { "pkcs11provider", oPKCS11Provider },
210# else
211 { "smartcarddevice", oUnsupported },
212 { "pkcs11provider", oUnsupported },
213#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000214 { "rsaauthentication", oUnsupported },
215 { "rhostsrsaauthentication", oUnsupported },
216 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000217
Damien Miller95def091999-11-25 00:26:21 +1100218 { "forwardagent", oForwardAgent },
219 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000220 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000221 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000222 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000223 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100224 { "gatewayports", oGatewayPorts },
225 { "useprivilegedport", oUsePrivilegedPort },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100227 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
228 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100229 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000230 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000231 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000232 { "challengeresponseauthentication", oChallengeResponseAuthentication },
233 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
234 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100235 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100236 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100237 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000238 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000239 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000240 { "identityagent", oIdentityAgent },
Damien Miller95def091999-11-25 00:26:21 +1100241 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000242 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100243 { "proxycommand", oProxyCommand },
244 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000245 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000246 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100247 { "remoteforward", oRemoteForward },
248 { "localforward", oLocalForward },
249 { "user", oUser },
250 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100251 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100252 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100253 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100254 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100255 { "connectionattempts", oConnectionAttempts },
256 { "batchmode", oBatchMode },
257 { "checkhostip", oCheckHostIP },
258 { "stricthostkeychecking", oStrictHostKeyChecking },
259 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100260 { "tcpkeepalive", oTCPKeepAlive },
261 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100262 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000263 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100264 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000265 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000266 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000267 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000268 { "bindaddress", oBindAddress },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100269 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000270 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000271 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100272 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000273 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000274 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000275 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100276 { "serveraliveinterval", oServerAliveInterval },
277 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000278 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000279 { "controlpath", oControlPath },
280 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000281 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100282 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000283 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100284 { "tunnel", oTunnel },
285 { "tunneldevice", oTunnelDevice },
286 { "localcommand", oLocalCommand },
287 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000288 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000289 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000290 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100291 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000292 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000293 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100294 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100295 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
296 { "canonicalizehostname", oCanonicalizeHostname },
297 { "canonicalizemaxdots", oCanonicalizeMaxDots },
298 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000299 { "streamlocalbindmask", oStreamLocalBindMask },
300 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000301 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000302 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000303 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000304 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000305 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000306 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000307 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100308
Ben Lindstrom65366a82001-12-06 16:32:47 +0000309 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100310};
311
Damien Miller5428f641999-11-25 11:54:57 +1100312/*
313 * Adds a local TCP/IP port forward to options. Never returns if there is an
314 * error.
315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller4af51302000-04-16 11:18:38 +1000317void
Damien Miller7acefbb2014-07-18 14:11:24 +1000318add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Damien Miller7acefbb2014-07-18 14:11:24 +1000320 struct Forward *fwd;
Damien Miller95def091999-11-25 00:26:21 +1100321 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000322 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000323
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000324 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000325 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000326 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000327 /* Don't add duplicates */
328 for (i = 0; i < options->num_local_forwards; i++) {
329 if (forward_equals(newfwd, options->local_forwards + i))
330 return;
331 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000332 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000333 options->num_local_forwards + 1,
334 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100335 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336
Damien Miller1a0442f2008-11-05 16:30:06 +1100337 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000339 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100340 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
Damien Miller5428f641999-11-25 11:54:57 +1100345/*
346 * Adds a remote TCP/IP port forward to options. Never returns if there is
347 * an error.
348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller4af51302000-04-16 11:18:38 +1000350void
Damien Miller7acefbb2014-07-18 14:11:24 +1000351add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Miller7acefbb2014-07-18 14:11:24 +1000353 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000354 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000355
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000356 /* Don't add duplicates */
357 for (i = 0; i < options->num_remote_forwards; i++) {
358 if (forward_equals(newfwd, options->remote_forwards + i))
359 return;
360 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000361 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000362 options->num_remote_forwards + 1,
363 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100364 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365
Damien Miller1a0442f2008-11-05 16:30:06 +1100366 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000368 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100369 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000371 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100372 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000373 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374}
375
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000376static void
377clear_forwardings(Options *options)
378{
379 int i;
380
Damien Millerf91ee4c2005-03-01 21:24:33 +1100381 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000383 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000385 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100386 }
Damien Miller232cfb12010-06-26 09:50:30 +1000387 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000388 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000389 options->local_forwards = NULL;
390 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000391 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000394 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000396 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100397 }
Damien Miller232cfb12010-06-26 09:50:30 +1000398 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000399 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000400 options->remote_forwards = NULL;
401 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100403 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000404}
405
Darren Tucker19104782013-04-05 11:13:08 +1100406void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000407add_certificate_file(Options *options, const char *path, int userprovided)
408{
409 int i;
410
411 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
412 fatal("Too many certificate files specified (max %d)",
413 SSH_MAX_CERTIFICATE_FILES);
414
415 /* Avoid registering duplicates */
416 for (i = 0; i < options->num_certificate_files; i++) {
417 if (options->certificate_file_userprovided[i] == userprovided &&
418 strcmp(options->certificate_files[i], path) == 0) {
419 debug2("%s: ignoring duplicate key %s", __func__, path);
420 return;
421 }
422 }
423
424 options->certificate_file_userprovided[options->num_certificate_files] =
425 userprovided;
426 options->certificate_files[options->num_certificate_files++] =
427 xstrdup(path);
428}
429
430void
Darren Tucker19104782013-04-05 11:13:08 +1100431add_identity_file(Options *options, const char *dir, const char *filename,
432 int userprovided)
433{
434 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000435 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100436
437 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
438 fatal("Too many identity files specified (max %d)",
439 SSH_MAX_IDENTITY_FILES);
440
441 if (dir == NULL) /* no dir, filename is absolute */
442 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000443 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
444 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100445
Damien Miller15271902014-05-15 13:47:56 +1000446 /* Avoid registering duplicates */
447 for (i = 0; i < options->num_identity_files; i++) {
448 if (options->identity_file_userprovided[i] == userprovided &&
449 strcmp(options->identity_files[i], path) == 0) {
450 debug2("%s: ignoring duplicate key %s", __func__, path);
451 free(path);
452 return;
453 }
454 }
455
Darren Tucker19104782013-04-05 11:13:08 +1100456 options->identity_file_userprovided[options->num_identity_files] =
457 userprovided;
458 options->identity_files[options->num_identity_files++] = path;
459}
460
Damien Miller194fd902013-10-15 12:13:05 +1100461int
462default_ssh_port(void)
463{
464 static int port;
465 struct servent *sp;
466
467 if (port == 0) {
468 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
469 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
470 }
471 return port;
472}
473
474/*
475 * Execute a command in a shell.
476 * Return its exit status or -1 on abnormal exit.
477 */
478static int
479execute_in_shell(const char *cmd)
480{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000481 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100482 pid_t pid;
483 int devnull, status;
484 extern uid_t original_real_uid;
485
486 if ((shell = getenv("SHELL")) == NULL)
487 shell = _PATH_BSHELL;
488
Damien Miller194fd902013-10-15 12:13:05 +1100489 /* Need this to redirect subprocess stdin/out */
490 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
491 fatal("open(/dev/null): %s", strerror(errno));
492
493 debug("Executing command: '%.500s'", cmd);
494
495 /* Fork and execute the command. */
496 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000497 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100498
499 /* Child. Permanently give up superuser privileges. */
500 permanently_drop_suid(original_real_uid);
501
502 /* Redirect child stdin and stdout. Leave stderr */
503 if (dup2(devnull, STDIN_FILENO) == -1)
504 fatal("dup2: %s", strerror(errno));
505 if (dup2(devnull, STDOUT_FILENO) == -1)
506 fatal("dup2: %s", strerror(errno));
507 if (devnull > STDERR_FILENO)
508 close(devnull);
509 closefrom(STDERR_FILENO + 1);
510
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000511 argv[0] = shell;
512 argv[1] = "-c";
513 argv[2] = xstrdup(cmd);
514 argv[3] = NULL;
515
Damien Miller194fd902013-10-15 12:13:05 +1100516 execv(argv[0], argv);
517 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
518 /* Die with signal to make this error apparent to parent. */
519 signal(SIGTERM, SIG_DFL);
520 kill(getpid(), SIGTERM);
521 _exit(1);
522 }
523 /* Parent. */
524 if (pid < 0)
525 fatal("%s: fork: %.100s", __func__, strerror(errno));
526
527 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100528
529 while (waitpid(pid, &status, 0) == -1) {
530 if (errno != EINTR && errno != EAGAIN)
531 fatal("%s: waitpid: %s", __func__, strerror(errno));
532 }
533 if (!WIFEXITED(status)) {
534 error("command '%.100s' exited abnormally", cmd);
535 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000536 }
Damien Miller194fd902013-10-15 12:13:05 +1100537 debug3("command returned status %d", WEXITSTATUS(status));
538 return WEXITSTATUS(status);
539}
540
541/*
542 * Parse and execute a Match directive.
543 */
544static int
545match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000546 const char *host_arg, const char *original_host, int post_canon,
547 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100548{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100550 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000551 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100552 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
553
554 /*
555 * Configuration is likely to be incomplete at this point so we
556 * must be prepared to use default values.
557 */
558 port = options->port <= 0 ? default_ssh_port() : options->port;
559 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000560 if (post_canon) {
561 host = xstrdup(options->hostname);
562 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100563 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100564 host = percent_expand(options->hostname,
565 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000566 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000568 }
Damien Miller194fd902013-10-15 12:13:05 +1100569
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000570 debug2("checking match for '%s' host %s originally %s",
571 cp, host, original_host);
572 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
573 criteria = NULL;
574 this_result = 1;
575 if ((negate = attrib[0] == '!'))
576 attrib++;
577 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100578 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100580 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 error("%.200s line %d: '%s' cannot be combined "
582 "with other Match attributes",
583 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100584 result = -1;
585 goto out;
586 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000587 if (result)
588 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100589 goto out;
590 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 attributes++;
592 if (strcasecmp(attrib, "canonical") == 0) {
593 r = !!post_canon; /* force bitmask member to boolean */
594 if (r == (negate ? 1 : 0))
595 this_result = result = 0;
596 debug3("%.200s line %d: %smatched '%s'",
597 filename, linenum,
598 this_result ? "" : "not ", oattrib);
599 continue;
600 }
601 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100602 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
603 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100604 result = -1;
605 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100606 }
Damien Miller194fd902013-10-15 12:13:05 +1100607 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000609 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 if (r == (negate ? 1 : 0))
611 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100612 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000614 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 if (r == (negate ? 1 : 0))
616 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100617 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000619 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 if (r == (negate ? 1 : 0))
621 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100622 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000624 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000625 if (r == (negate ? 1 : 0))
626 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100627 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100628 if (gethostname(thishost, sizeof(thishost)) == -1)
629 fatal("gethostname: %s", strerror(errno));
630 strlcpy(shorthost, thishost, sizeof(shorthost));
631 shorthost[strcspn(thishost, ".")] = '\0';
632 snprintf(portstr, sizeof(portstr), "%d", port);
633
634 cmd = percent_expand(arg,
635 "L", shorthost,
636 "d", pw->pw_dir,
637 "h", host,
638 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000639 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100640 "p", portstr,
641 "r", ruser,
642 "u", pw->pw_name,
643 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100644 if (result != 1) {
645 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000646 debug3("%.200s line %d: skipped exec "
647 "\"%.100s\"", filename, linenum, cmd);
648 free(cmd);
649 continue;
Damien Miller06287802014-02-24 15:56:45 +1100650 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000651 r = execute_in_shell(cmd);
652 if (r == -1) {
653 fatal("%.200s line %d: match exec "
654 "'%.100s' error", filename,
655 linenum, cmd);
656 }
657 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100658 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000659 /* Force exit status to boolean */
660 r = r == 0;
661 if (r == (negate ? 1 : 0))
662 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100663 } else {
664 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100665 result = -1;
666 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100667 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000668 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
669 filename, linenum, this_result ? "": "not ",
670 oattrib, criteria);
671 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100672 }
Damien Millercf31f382013-10-24 21:02:56 +1100673 if (attributes == 0) {
674 error("One or more attributes required for Match");
675 result = -1;
676 goto out;
677 }
Damien Miller084bcd22013-10-23 16:30:51 +1100678 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000679 if (result != -1)
680 debug2("match %sfound", result ? "" : "not ");
681 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100682 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100683 return result;
684}
685
Damien Miller0faf7472013-10-17 11:47:23 +1100686/* Check and prepare a domain name: removes trailing '.' and lowercases */
687static void
688valid_domain(char *name, const char *filename, int linenum)
689{
690 size_t i, l = strlen(name);
691 u_char c, last = '\0';
692
693 if (l == 0)
694 fatal("%s line %d: empty hostname suffix", filename, linenum);
695 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
696 fatal("%s line %d: hostname suffix \"%.100s\" "
697 "starts with invalid character", filename, linenum, name);
698 for (i = 0; i < l; i++) {
699 c = tolower((u_char)name[i]);
700 name[i] = (char)c;
701 if (last == '.' && c == '.')
702 fatal("%s line %d: hostname suffix \"%.100s\" contains "
703 "consecutive separators", filename, linenum, name);
704 if (c != '.' && c != '-' && !isalnum(c) &&
705 c != '_') /* technically invalid, but common */
706 fatal("%s line %d: hostname suffix \"%.100s\" contains "
707 "invalid characters", filename, linenum, name);
708 last = c;
709 }
710 if (name[l - 1] == '.')
711 name[l - 1] = '\0';
712}
713
Damien Miller5428f641999-11-25 11:54:57 +1100714/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000715 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100716 */
Damien Miller4af51302000-04-16 11:18:38 +1000717static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000718parse_token(const char *cp, const char *filename, int linenum,
719 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720{
Darren Tucker07636982013-05-16 20:30:03 +1000721 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Damien Miller95def091999-11-25 00:26:21 +1100723 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000724 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100725 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000726 if (ignored_unknown != NULL &&
727 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000728 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000729 error("%s: line %d: Bad configuration option: %s",
730 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100731 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732}
733
Damien Millere9fc72e2013-10-15 12:14:12 +1100734/* Multistate option parsing */
735struct multistate {
736 char *key;
737 int value;
738};
739static const struct multistate multistate_flag[] = {
740 { "true", 1 },
741 { "false", 0 },
742 { "yes", 1 },
743 { "no", 0 },
744 { NULL, -1 }
745};
746static const struct multistate multistate_yesnoask[] = {
747 { "true", 1 },
748 { "false", 0 },
749 { "yes", 1 },
750 { "no", 0 },
751 { "ask", 2 },
752 { NULL, -1 }
753};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000754static const struct multistate multistate_strict_hostkey[] = {
755 { "true", SSH_STRICT_HOSTKEY_YES },
756 { "false", SSH_STRICT_HOSTKEY_OFF },
757 { "yes", SSH_STRICT_HOSTKEY_YES },
758 { "no", SSH_STRICT_HOSTKEY_OFF },
759 { "ask", SSH_STRICT_HOSTKEY_ASK },
760 { "off", SSH_STRICT_HOSTKEY_OFF },
761 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
762 { NULL, -1 }
763};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000764static const struct multistate multistate_yesnoaskconfirm[] = {
765 { "true", 1 },
766 { "false", 0 },
767 { "yes", 1 },
768 { "no", 0 },
769 { "ask", 2 },
770 { "confirm", 3 },
771 { NULL, -1 }
772};
Damien Millere9fc72e2013-10-15 12:14:12 +1100773static const struct multistate multistate_addressfamily[] = {
774 { "inet", AF_INET },
775 { "inet6", AF_INET6 },
776 { "any", AF_UNSPEC },
777 { NULL, -1 }
778};
779static const struct multistate multistate_controlmaster[] = {
780 { "true", SSHCTL_MASTER_YES },
781 { "yes", SSHCTL_MASTER_YES },
782 { "false", SSHCTL_MASTER_NO },
783 { "no", SSHCTL_MASTER_NO },
784 { "auto", SSHCTL_MASTER_AUTO },
785 { "ask", SSHCTL_MASTER_ASK },
786 { "autoask", SSHCTL_MASTER_AUTO_ASK },
787 { NULL, -1 }
788};
789static const struct multistate multistate_tunnel[] = {
790 { "ethernet", SSH_TUNMODE_ETHERNET },
791 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
792 { "true", SSH_TUNMODE_DEFAULT },
793 { "yes", SSH_TUNMODE_DEFAULT },
794 { "false", SSH_TUNMODE_NO },
795 { "no", SSH_TUNMODE_NO },
796 { NULL, -1 }
797};
798static const struct multistate multistate_requesttty[] = {
799 { "true", REQUEST_TTY_YES },
800 { "yes", REQUEST_TTY_YES },
801 { "false", REQUEST_TTY_NO },
802 { "no", REQUEST_TTY_NO },
803 { "force", REQUEST_TTY_FORCE },
804 { "auto", REQUEST_TTY_AUTO },
805 { NULL, -1 }
806};
Damien Miller38505592013-10-17 11:48:13 +1100807static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100808 { "true", SSH_CANONICALISE_YES },
809 { "false", SSH_CANONICALISE_NO },
810 { "yes", SSH_CANONICALISE_YES },
811 { "no", SSH_CANONICALISE_NO },
812 { "always", SSH_CANONICALISE_ALWAYS },
813 { NULL, -1 }
814};
Damien Millere9fc72e2013-10-15 12:14:12 +1100815
Damien Miller5428f641999-11-25 11:54:57 +1100816/*
817 * Processes a single option line as used in the configuration files. This
818 * only sets those values that have not already been set.
819 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100820int
Damien Miller194fd902013-10-15 12:13:05 +1100821process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000822 const char *original_host, char *line, const char *filename,
823 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000825 return process_config_line_depth(options, pw, host, original_host,
826 line, filename, linenum, activep, flags, 0);
827}
828
829#define WHITESPACE " \t\r\n"
830static int
831process_config_line_depth(Options *options, struct passwd *pw, const char *host,
832 const char *original_host, char *line, const char *filename,
833 int linenum, int *activep, int flags, int depth)
834{
Damien Miller295ee632011-05-29 21:42:31 +1000835 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
836 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000837 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000838 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000839 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100840 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000841 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000842 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100843 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000844 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100845 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100846 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000847 glob_t gl;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller194fd902013-10-15 12:13:05 +1100849 if (activep == NULL) { /* We are processing a command line directive */
850 cmdline = 1;
851 activep = &cmdline;
852 }
853
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000854 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000855 if ((len = strlen(line)) == 0)
856 return 0;
857 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000858 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000859 break;
860 line[len] = '\0';
861 }
862
Damien Millerbe484b52000-07-15 14:14:16 +1000863 s = line;
864 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100865 if ((keyword = strdelim(&s)) == NULL)
866 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000867 /* Ignore leading whitespace. */
868 if (*keyword == '\0')
869 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000870 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100871 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000872 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100873 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Darren Tucker07636982013-05-16 20:30:03 +1000875 opcode = parse_token(keyword, filename, linenum,
876 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878 switch (opcode) {
879 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100880 /* don't panic, but count bad options */
881 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000882 case oIgnore:
883 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000884 case oIgnoredUnknownOption:
885 debug("%s line %d: Ignored unknown option \"%s\"",
886 filename, linenum, keyword);
887 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000888 case oConnectTimeout:
889 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100890parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000891 arg = strdelim(&s);
892 if (!arg || *arg == '\0')
893 fatal("%s line %d: missing time value.",
894 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000895 if (strcmp(arg, "none") == 0)
896 value = -1;
897 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000898 fatal("%s line %d: invalid time value.",
899 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100900 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000901 *intptr = value;
902 break;
903
Damien Miller95def091999-11-25 00:26:21 +1100904 case oForwardAgent:
905 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100906 parse_flag:
907 multistate_ptr = multistate_flag;
908 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000909 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000910 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100911 fatal("%s line %d: missing argument.",
912 filename, linenum);
913 value = -1;
914 for (i = 0; multistate_ptr[i].key != NULL; i++) {
915 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
916 value = multistate_ptr[i].value;
917 break;
918 }
919 }
920 if (value == -1)
921 fatal("%s line %d: unsupported option \"%s\".",
922 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100923 if (*activep && *intptr == -1)
924 *intptr = value;
925 break;
926
927 case oForwardX11:
928 intptr = &options->forward_x11;
929 goto parse_flag;
930
Darren Tucker0a118da2003-10-15 15:54:32 +1000931 case oForwardX11Trusted:
932 intptr = &options->forward_x11_trusted;
933 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000934
Damien Miller1ab6a512010-06-26 10:02:24 +1000935 case oForwardX11Timeout:
936 intptr = &options->forward_x11_timeout;
937 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000938
Damien Miller95def091999-11-25 00:26:21 +1100939 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000940 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100941 goto parse_flag;
942
Darren Tuckere7d4b192006-07-12 22:17:10 +1000943 case oExitOnForwardFailure:
944 intptr = &options->exit_on_forward_failure;
945 goto parse_flag;
946
Damien Miller95def091999-11-25 00:26:21 +1100947 case oUsePrivilegedPort:
948 intptr = &options->use_privileged_port;
949 goto parse_flag;
950
Damien Miller95def091999-11-25 00:26:21 +1100951 case oPasswordAuthentication:
952 intptr = &options->password_authentication;
953 goto parse_flag;
954
Damien Miller874d77b2000-10-14 16:23:11 +1100955 case oKbdInteractiveAuthentication:
956 intptr = &options->kbd_interactive_authentication;
957 goto parse_flag;
958
959 case oKbdInteractiveDevices:
960 charptr = &options->kbd_interactive_devices;
961 goto parse_string;
962
Damien Miller0bc1bd82000-11-13 22:57:25 +1100963 case oPubkeyAuthentication:
964 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000965 goto parse_flag;
966
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000967 case oHostbasedAuthentication:
968 intptr = &options->hostbased_authentication;
969 goto parse_flag;
970
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000971 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000972 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100973 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000974
Darren Tucker0efd1552003-08-26 11:49:55 +1000975 case oGssAuthentication:
976 intptr = &options->gss_authentication;
977 goto parse_flag;
978
979 case oGssDelegateCreds:
980 intptr = &options->gss_deleg_creds;
981 goto parse_flag;
982
Damien Miller95def091999-11-25 00:26:21 +1100983 case oBatchMode:
984 intptr = &options->batch_mode;
985 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 case oCheckHostIP:
988 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000989 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller37876e92003-05-15 10:19:46 +1000991 case oVerifyHostKeyDNS:
992 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100993 multistate_ptr = multistate_yesnoask;
994 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000995
Damien Miller95def091999-11-25 00:26:21 +1100996 case oStrictHostKeyChecking:
997 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000998 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +1100999 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001 case oCompression:
1002 intptr = &options->compression;
1003 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Damien Miller12c150e2003-12-17 16:31:10 +11001005 case oTCPKeepAlive:
1006 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001007 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001009 case oNoHostAuthenticationForLocalhost:
1010 intptr = &options->no_host_authentication_for_localhost;
1011 goto parse_flag;
1012
Damien Miller95def091999-11-25 00:26:21 +11001013 case oNumberOfPasswordPrompts:
1014 intptr = &options->number_of_password_prompts;
1015 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Damien Millera5539d22003-04-09 20:50:06 +10001017 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001018 arg = strdelim(&s);
1019 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001020 fatal("%.200s line %d: Missing argument.", filename,
1021 linenum);
1022 if (strcmp(arg, "default") == 0) {
1023 val64 = 0;
1024 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001025 if (scan_scaled(arg, &val64) == -1)
1026 fatal("%.200s line %d: Bad number '%s': %s",
1027 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001028 if (val64 != 0 && val64 < 16)
1029 fatal("%.200s line %d: RekeyLimit too small",
1030 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001031 }
Damien Miller3dff1762008-02-10 22:25:52 +11001032 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001033 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001034 if (s != NULL) { /* optional rekey interval present */
1035 if (strcmp(s, "none") == 0) {
1036 (void)strdelim(&s); /* discard */
1037 break;
1038 }
1039 intptr = &options->rekey_interval;
1040 goto parse_time;
1041 }
Damien Millera5539d22003-04-09 20:50:06 +10001042 break;
1043
Damien Miller95def091999-11-25 00:26:21 +11001044 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001045 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001046 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001047 fatal("%.200s line %d: Missing argument.", filename, linenum);
1048 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001049 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001050 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001051 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001052 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001053 add_identity_file(options, NULL,
1054 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001055 }
1056 break;
1057
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001058 case oCertificateFile:
1059 arg = strdelim(&s);
1060 if (!arg || *arg == '\0')
1061 fatal("%.200s line %d: Missing argument.",
1062 filename, linenum);
1063 if (*activep) {
1064 intptr = &options->num_certificate_files;
1065 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1066 fatal("%.200s line %d: Too many certificate "
1067 "files specified (max %d).",
1068 filename, linenum,
1069 SSH_MAX_CERTIFICATE_FILES);
1070 }
1071 add_certificate_file(options, arg,
1072 flags & SSHCONF_USERCONF);
1073 }
1074 break;
1075
Damien Millerd3a18572000-06-07 19:55:44 +10001076 case oXAuthLocation:
1077 charptr=&options->xauth_location;
1078 goto parse_string;
1079
Damien Miller95def091999-11-25 00:26:21 +11001080 case oUser:
1081 charptr = &options->user;
1082parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001083 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001084 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001085 fatal("%.200s line %d: Missing argument.",
1086 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001087 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001088 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001089 break;
1090
1091 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001092 cpptr = (char **)&options->system_hostfiles;
1093 uintptr = &options->num_system_hostfiles;
1094 max_entries = SSH_MAX_HOSTS_FILES;
1095parse_char_array:
1096 if (*activep && *uintptr == 0) {
1097 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1098 if ((*uintptr) >= max_entries)
1099 fatal("%s line %d: "
1100 "too many authorized keys files.",
1101 filename, linenum);
1102 cpptr[(*uintptr)++] = xstrdup(arg);
1103 }
1104 }
1105 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001106
1107 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001108 cpptr = (char **)&options->user_hostfiles;
1109 uintptr = &options->num_user_hostfiles;
1110 max_entries = SSH_MAX_HOSTS_FILES;
1111 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001112
Damien Miller95def091999-11-25 00:26:21 +11001113 case oHostName:
1114 charptr = &options->hostname;
1115 goto parse_string;
1116
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001117 case oHostKeyAlias:
1118 charptr = &options->host_key_alias;
1119 goto parse_string;
1120
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001121 case oPreferredAuthentications:
1122 charptr = &options->preferred_authentications;
1123 goto parse_string;
1124
Ben Lindstrome0f88042001-04-30 13:06:24 +00001125 case oBindAddress:
1126 charptr = &options->bind_address;
1127 goto parse_string;
1128
Damien Miller7ea845e2010-02-12 09:21:02 +11001129 case oPKCS11Provider:
1130 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001131 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001132
Damien Miller95def091999-11-25 00:26:21 +11001133 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001134 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001135 /* Ignore ProxyCommand if ProxyJump already specified */
1136 if (options->jump_host != NULL)
1137 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001138parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001139 if (s == NULL)
1140 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001141 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001142 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001143 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001144 return 0;
1145
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001146 case oProxyJump:
1147 if (s == NULL) {
1148 fatal("%.200s line %d: Missing argument.",
1149 filename, linenum);
1150 }
1151 len = strspn(s, WHITESPACE "=");
1152 if (parse_jump(s + len, options, *activep) == -1) {
1153 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1154 filename, linenum, s + len);
1155 }
1156 return 0;
1157
Damien Miller95def091999-11-25 00:26:21 +11001158 case oPort:
1159 intptr = &options->port;
1160parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001161 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001162 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001163 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001164 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001165 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001166
1167 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001168 value = strtol(arg, &endofnumber, 0);
1169 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001170 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001171 if (*activep && *intptr == -1)
1172 *intptr = value;
1173 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174
Damien Miller95def091999-11-25 00:26:21 +11001175 case oConnectionAttempts:
1176 intptr = &options->connection_attempts;
1177 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001178
Damien Miller78928792000-04-12 20:17:38 +10001179 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001180 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001181 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001182 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001183 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001184 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001186 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001187 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001188 break;
1189
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001190 case oMacs:
1191 arg = strdelim(&s);
1192 if (!arg || *arg == '\0')
1193 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001194 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001195 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001196 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001197 if (*activep && options->macs == NULL)
1198 options->macs = xstrdup(arg);
1199 break;
1200
Damien Millerd5f62bf2010-09-24 22:11:14 +10001201 case oKexAlgorithms:
1202 arg = strdelim(&s);
1203 if (!arg || *arg == '\0')
1204 fatal("%.200s line %d: Missing argument.",
1205 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001206 if (*arg != '-' &&
1207 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001208 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1209 filename, linenum, arg ? arg : "<NONE>");
1210 if (*activep && options->kex_algorithms == NULL)
1211 options->kex_algorithms = xstrdup(arg);
1212 break;
1213
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001214 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001215 charptr = &options->hostkeyalgorithms;
1216parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001217 arg = strdelim(&s);
1218 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001219 fatal("%.200s line %d: Missing argument.",
1220 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001221 if (*arg != '-' &&
1222 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001223 fatal("%s line %d: Bad key types '%s'.",
1224 filename, linenum, arg ? arg : "<NONE>");
1225 if (*activep && *charptr == NULL)
1226 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001227 break;
1228
Damien Miller95def091999-11-25 00:26:21 +11001229 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001230 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001231 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001232 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001233 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001234 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001235 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001236 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1237 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001238 break;
1239
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001240 case oLogFacility:
1241 log_facility_ptr = &options->log_facility;
1242 arg = strdelim(&s);
1243 value = log_facility_number(arg);
1244 if (value == SYSLOG_FACILITY_NOT_SET)
1245 fatal("%.200s line %d: unsupported log facility '%s'",
1246 filename, linenum, arg ? arg : "<NONE>");
1247 if (*log_facility_ptr == -1)
1248 *log_facility_ptr = (SyslogFacility) value;
1249 break;
1250
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001251 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001252 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001253 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001254 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001255 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001256 fatal("%.200s line %d: Missing port argument.",
1257 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001258
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001259 remotefwd = (opcode == oRemoteForward);
1260 dynamicfwd = (opcode == oDynamicForward);
1261
1262 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001263 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001264 if (arg2 == NULL || *arg2 == '\0') {
1265 if (remotefwd)
1266 dynamicfwd = 1;
1267 else
1268 fatal("%.200s line %d: Missing target "
1269 "argument.", filename, linenum);
1270 } else {
1271 /* construct a string for parse_forward */
1272 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1273 arg2);
1274 }
Damien Millera699d952008-11-03 19:27:34 +11001275 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001276 if (dynamicfwd)
1277 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001278
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001279 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001280 fatal("%.200s line %d: Bad forwarding specification.",
1281 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001282
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001283 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001284 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001285 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001286 } else {
1287 add_local_forward(options, &fwd);
1288 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001289 }
Damien Miller95def091999-11-25 00:26:21 +11001290 break;
1291
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001292 case oClearAllForwardings:
1293 intptr = &options->clear_forwardings;
1294 goto parse_flag;
1295
Damien Miller95def091999-11-25 00:26:21 +11001296 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001297 if (cmdline)
1298 fatal("Host directive not supported as a command-line "
1299 "option");
Damien Miller95def091999-11-25 00:26:21 +11001300 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001301 arg2 = NULL;
1302 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001303 if ((flags & SSHCONF_NEVERMATCH) != 0)
1304 break;
Damien Millerfe924212011-05-15 08:44:45 +10001305 negated = *arg == '!';
1306 if (negated)
1307 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001308 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001309 if (negated) {
1310 debug("%.200s line %d: Skipping Host "
1311 "block because of negated match "
1312 "for %.100s", filename, linenum,
1313 arg);
1314 *activep = 0;
1315 break;
1316 }
1317 if (!*activep)
1318 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001319 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001320 }
Damien Millerfe924212011-05-15 08:44:45 +10001321 }
1322 if (*activep)
1323 debug("%.200s line %d: Applying options for %.100s",
1324 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001325 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001326 return 0;
1327
Damien Miller194fd902013-10-15 12:13:05 +11001328 case oMatch:
1329 if (cmdline)
1330 fatal("Host directive not supported as a command-line "
1331 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001332 value = match_cfg_line(options, &s, pw, host, original_host,
1333 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001334 if (value < 0)
1335 fatal("%.200s line %d: Bad Match condition", filename,
1336 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001337 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001338 break;
1339
Damien Miller95def091999-11-25 00:26:21 +11001340 case oEscapeChar:
1341 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001342 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001343 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001344 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001345 if (strcmp(arg, "none") == 0)
1346 value = SSH_ESCAPECHAR_NONE;
1347 else if (arg[1] == '\0')
1348 value = (u_char) arg[0];
1349 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001350 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1351 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001352 else {
1353 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001354 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001355 /* NOTREACHED */
1356 value = 0; /* Avoid compiler warning. */
1357 }
1358 if (*activep && *intptr == -1)
1359 *intptr = value;
1360 break;
1361
Damien Miller20a8f972003-05-18 20:50:30 +10001362 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001363 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001364 multistate_ptr = multistate_addressfamily;
1365 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001366
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001367 case oEnableSSHKeysign:
1368 intptr = &options->enable_ssh_keysign;
1369 goto parse_flag;
1370
Damien Millerbd394c32004-03-08 23:12:36 +11001371 case oIdentitiesOnly:
1372 intptr = &options->identities_only;
1373 goto parse_flag;
1374
Damien Miller509b0102003-12-17 16:33:10 +11001375 case oServerAliveInterval:
1376 intptr = &options->server_alive_interval;
1377 goto parse_time;
1378
1379 case oServerAliveCountMax:
1380 intptr = &options->server_alive_count_max;
1381 goto parse_int;
1382
Darren Tucker46bc0752004-05-02 22:11:30 +10001383 case oSendEnv:
1384 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1385 if (strchr(arg, '=') != NULL)
1386 fatal("%s line %d: Invalid environment name.",
1387 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001388 if (!*activep)
1389 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001390 if (options->num_send_env >= MAX_SEND_ENV)
1391 fatal("%s line %d: too many send env.",
1392 filename, linenum);
1393 options->send_env[options->num_send_env++] =
1394 xstrdup(arg);
1395 }
1396 break;
1397
Damien Miller0e220db2004-06-15 10:34:08 +10001398 case oControlPath:
1399 charptr = &options->control_path;
1400 goto parse_string;
1401
1402 case oControlMaster:
1403 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001404 multistate_ptr = multistate_controlmaster;
1405 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001406
Damien Millere11e1ea2010-08-03 16:04:46 +10001407 case oControlPersist:
1408 /* no/false/yes/true, or a time spec */
1409 intptr = &options->control_persist;
1410 arg = strdelim(&s);
1411 if (!arg || *arg == '\0')
1412 fatal("%.200s line %d: Missing ControlPersist"
1413 " argument.", filename, linenum);
1414 value = 0;
1415 value2 = 0; /* timeout */
1416 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1417 value = 0;
1418 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1419 value = 1;
1420 else if ((value2 = convtime(arg)) >= 0)
1421 value = 1;
1422 else
1423 fatal("%.200s line %d: Bad ControlPersist argument.",
1424 filename, linenum);
1425 if (*activep && *intptr == -1) {
1426 *intptr = value;
1427 options->control_persist_timeout = value2;
1428 }
1429 break;
1430
Damien Millere1776152005-03-01 21:47:37 +11001431 case oHashKnownHosts:
1432 intptr = &options->hash_known_hosts;
1433 goto parse_flag;
1434
Damien Millerd27b9472005-12-13 19:29:02 +11001435 case oTunnel:
1436 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001437 multistate_ptr = multistate_tunnel;
1438 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001439
1440 case oTunnelDevice:
1441 arg = strdelim(&s);
1442 if (!arg || *arg == '\0')
1443 fatal("%.200s line %d: Missing argument.", filename, linenum);
1444 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001445 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001446 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1447 if (*activep) {
1448 options->tun_local = value;
1449 options->tun_remote = value2;
1450 }
1451 break;
1452
1453 case oLocalCommand:
1454 charptr = &options->local_command;
1455 goto parse_command;
1456
1457 case oPermitLocalCommand:
1458 intptr = &options->permit_local_command;
1459 goto parse_flag;
1460
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001461 case oRemoteCommand:
1462 charptr = &options->remote_command;
1463 goto parse_command;
1464
Damien Miller10288242008-06-30 00:04:03 +10001465 case oVisualHostKey:
1466 intptr = &options->visual_host_key;
1467 goto parse_flag;
1468
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001469 case oInclude:
1470 if (cmdline)
1471 fatal("Include directive not supported as a "
1472 "command-line option");
1473 value = 0;
1474 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1475 /*
1476 * Ensure all paths are anchored. User configuration
1477 * files may begin with '~/' but system configurations
1478 * must not. If the path is relative, then treat it
1479 * as living in ~/.ssh for user configurations or
1480 * /etc/ssh for system ones.
1481 */
1482 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1483 fatal("%.200s line %d: bad include path %s.",
1484 filename, linenum, arg);
1485 if (*arg != '/' && *arg != '~') {
1486 xasprintf(&arg2, "%s/%s",
1487 (flags & SSHCONF_USERCONF) ?
1488 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1489 } else
1490 arg2 = xstrdup(arg);
1491 memset(&gl, 0, sizeof(gl));
1492 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1493 if (r == GLOB_NOMATCH) {
1494 debug("%.200s line %d: include %s matched no "
1495 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001496 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001497 continue;
1498 } else if (r != 0 || gl.gl_pathc < 0)
1499 fatal("%.200s line %d: glob failed for %s.",
1500 filename, linenum, arg2);
1501 free(arg2);
1502 oactive = *activep;
1503 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1504 debug3("%.200s line %d: Including file %s "
1505 "depth %d%s", filename, linenum,
1506 gl.gl_pathv[i], depth,
1507 oactive ? "" : " (parse only)");
1508 r = read_config_file_depth(gl.gl_pathv[i],
1509 pw, host, original_host, options,
1510 flags | SSHCONF_CHECKPERM |
1511 (oactive ? 0 : SSHCONF_NEVERMATCH),
1512 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001513 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001514 fatal("Can't open user config file "
1515 "%.100s: %.100s", gl.gl_pathv[i],
1516 strerror(errno));
1517 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001518 /*
1519 * don't let Match in includes clobber the
1520 * containing file's Match state.
1521 */
1522 *activep = oactive;
1523 if (r != 1)
1524 value = -1;
1525 }
1526 globfree(&gl);
1527 }
1528 if (value != 0)
1529 return value;
1530 break;
1531
Damien Miller0dac6fb2010-11-20 15:19:38 +11001532 case oIPQoS:
1533 arg = strdelim(&s);
1534 if ((value = parse_ipqos(arg)) == -1)
1535 fatal("%s line %d: Bad IPQoS value: %s",
1536 filename, linenum, arg);
1537 arg = strdelim(&s);
1538 if (arg == NULL)
1539 value2 = value;
1540 else if ((value2 = parse_ipqos(arg)) == -1)
1541 fatal("%s line %d: Bad IPQoS value: %s",
1542 filename, linenum, arg);
1543 if (*activep) {
1544 options->ip_qos_interactive = value;
1545 options->ip_qos_bulk = value2;
1546 }
1547 break;
1548
Damien Miller21771e22011-05-15 08:45:50 +10001549 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001550 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001551 multistate_ptr = multistate_requesttty;
1552 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001553
Darren Tucker07636982013-05-16 20:30:03 +10001554 case oIgnoreUnknown:
1555 charptr = &options->ignored_unknown;
1556 goto parse_string;
1557
Damien Miller1262b662013-08-21 02:44:24 +10001558 case oProxyUseFdpass:
1559 intptr = &options->proxy_use_fdpass;
1560 goto parse_flag;
1561
Damien Miller0faf7472013-10-17 11:47:23 +11001562 case oCanonicalDomains:
1563 value = options->num_canonical_domains != 0;
1564 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1565 valid_domain(arg, filename, linenum);
1566 if (!*activep || value)
1567 continue;
1568 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1569 fatal("%s line %d: too many hostname suffixes.",
1570 filename, linenum);
1571 options->canonical_domains[
1572 options->num_canonical_domains++] = xstrdup(arg);
1573 }
1574 break;
1575
Damien Miller38505592013-10-17 11:48:13 +11001576 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001577 value = options->num_permitted_cnames != 0;
1578 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1579 /* Either '*' for everything or 'list:list' */
1580 if (strcmp(arg, "*") == 0)
1581 arg2 = arg;
1582 else {
1583 lowercase(arg);
1584 if ((arg2 = strchr(arg, ':')) == NULL ||
1585 arg2[1] == '\0') {
1586 fatal("%s line %d: "
1587 "Invalid permitted CNAME \"%s\"",
1588 filename, linenum, arg);
1589 }
1590 *arg2 = '\0';
1591 arg2++;
1592 }
1593 if (!*activep || value)
1594 continue;
1595 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1596 fatal("%s line %d: too many permitted CNAMEs.",
1597 filename, linenum);
1598 cname = options->permitted_cnames +
1599 options->num_permitted_cnames++;
1600 cname->source_list = xstrdup(arg);
1601 cname->target_list = xstrdup(arg2);
1602 }
1603 break;
1604
Damien Miller38505592013-10-17 11:48:13 +11001605 case oCanonicalizeHostname:
1606 intptr = &options->canonicalize_hostname;
1607 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001608 goto parse_multistate;
1609
Damien Miller38505592013-10-17 11:48:13 +11001610 case oCanonicalizeMaxDots:
1611 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001612 goto parse_int;
1613
Damien Miller38505592013-10-17 11:48:13 +11001614 case oCanonicalizeFallbackLocal:
1615 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001616 goto parse_flag;
1617
Damien Miller7acefbb2014-07-18 14:11:24 +10001618 case oStreamLocalBindMask:
1619 arg = strdelim(&s);
1620 if (!arg || *arg == '\0')
1621 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1622 /* Parse mode in octal format */
1623 value = strtol(arg, &endofnumber, 8);
1624 if (arg == endofnumber || value < 0 || value > 0777)
1625 fatal("%.200s line %d: Bad mask.", filename, linenum);
1626 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1627 break;
1628
1629 case oStreamLocalBindUnlink:
1630 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1631 goto parse_flag;
1632
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001633 case oRevokedHostKeys:
1634 charptr = &options->revoked_host_keys;
1635 goto parse_string;
1636
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001637 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001638 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001639 arg = strdelim(&s);
1640 if (!arg || *arg == '\0')
1641 fatal("%.200s line %d: Missing argument.",
1642 filename, linenum);
1643 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1644 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1645 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001646 if (*activep && *intptr == -1)
1647 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001648 break;
1649
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001650 case oUpdateHostkeys:
1651 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001652 multistate_ptr = multistate_yesnoask;
1653 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001654
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001655 case oHostbasedKeyTypes:
1656 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001657 goto parse_keytypes;
1658
1659 case oPubkeyAcceptedKeyTypes:
1660 charptr = &options->pubkey_key_types;
1661 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001662
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001663 case oAddKeysToAgent:
1664 intptr = &options->add_keys_to_agent;
1665 multistate_ptr = multistate_yesnoaskconfirm;
1666 goto parse_multistate;
1667
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001668 case oIdentityAgent:
1669 charptr = &options->identity_agent;
1670 goto parse_string;
1671
Ben Lindstrom4daea862002-06-09 20:04:02 +00001672 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001673 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001674 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001675 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001676
Damien Millerf9b3feb2003-05-16 11:38:32 +10001677 case oUnsupported:
1678 error("%s line %d: Unsupported option \"%s\"",
1679 filename, linenum, keyword);
1680 return 0;
1681
Damien Miller95def091999-11-25 00:26:21 +11001682 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001683 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001684 }
1685
1686 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001687 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001688 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001689 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001690 }
Damien Miller95def091999-11-25 00:26:21 +11001691 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692}
1693
Damien Miller5428f641999-11-25 11:54:57 +11001694/*
1695 * Reads the config file and modifies the options accordingly. Options
1696 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001697 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001698 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001699int
Damien Miller194fd902013-10-15 12:13:05 +11001700read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001701 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001702{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001703 int active = 1;
1704
1705 return read_config_file_depth(filename, pw, host, original_host,
1706 options, flags, &active, 0);
1707}
1708
1709#define READCONF_MAX_DEPTH 16
1710static int
1711read_config_file_depth(const char *filename, struct passwd *pw,
1712 const char *host, const char *original_host, Options *options,
1713 int flags, int *activep, int depth)
1714{
Damien Miller95def091999-11-25 00:26:21 +11001715 FILE *f;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001716 char line[4096];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001717 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001718 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001719
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001720 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1721 fatal("Too many recursive configuration includes");
1722
Damien Miller57a44762004-04-20 20:11:57 +10001723 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001724 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001725
Darren Tuckeraefa3682013-04-05 11:18:35 +11001726 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001727 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001728
Damien Miller33793852004-06-15 10:27:55 +10001729 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001730 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001731 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001732 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001733 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001734 }
1735
Damien Miller95def091999-11-25 00:26:21 +11001736 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737
Damien Miller5428f641999-11-25 11:54:57 +11001738 /*
1739 * Mark that we are now processing the options. This flag is turned
1740 * on/off by Host specifications.
1741 */
Damien Miller95def091999-11-25 00:26:21 +11001742 linenum = 0;
1743 while (fgets(line, sizeof(line), f)) {
1744 /* Update line number counter. */
1745 linenum++;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001746 if (strlen(line) == sizeof(line) - 1)
1747 fatal("%s line %d too long", filename, linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001748 if (process_config_line_depth(options, pw, host, original_host,
1749 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001750 bad_options++;
1751 }
1752 fclose(f);
1753 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001754 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001755 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001756 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001757}
1758
Damien Miller13f97b22014-02-24 15:57:55 +11001759/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1760int
1761option_clear_or_none(const char *o)
1762{
1763 return o == NULL || strcasecmp(o, "none") == 0;
1764}
1765
Damien Miller5428f641999-11-25 11:54:57 +11001766/*
1767 * Initializes options to special values that indicate that they have not yet
1768 * been set. Read_config_file will only set options with this value. Options
1769 * are processed in the following order: command line, user config file,
1770 * system config file. Last, fill_default_options is called.
1771 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001772
Damien Miller4af51302000-04-16 11:18:38 +10001773void
Damien Miller95def091999-11-25 00:26:21 +11001774initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001775{
Damien Miller95def091999-11-25 00:26:21 +11001776 memset(options, 'X', sizeof(*options));
1777 options->forward_agent = -1;
1778 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001779 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001780 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001781 options->stdio_forward_host = NULL;
1782 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001783 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001784 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001785 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001786 options->fwd_opts.gateway_ports = -1;
1787 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1788 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001789 options->use_privileged_port = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001790 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001791 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001792 options->gss_authentication = -1;
1793 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001794 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001795 options->kbd_interactive_authentication = -1;
1796 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001797 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001798 options->batch_mode = -1;
1799 options->check_host_ip = -1;
1800 options->strict_host_key_checking = -1;
1801 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001802 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001803 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001804 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001805 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001806 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001807 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001808 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001809 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001810 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001811 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001812 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001813 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001814 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001815 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001816 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001817 options->jump_user = NULL;
1818 options->jump_host = NULL;
1819 options->jump_port = -1;
1820 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001821 options->user = NULL;
1822 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001823 options->num_system_hostfiles = 0;
1824 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001825 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001826 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001827 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001828 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001829 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001830 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001831 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001832 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001833 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001834 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001835 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001836 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001837 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001838 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001839 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001840 options->server_alive_interval = -1;
1841 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001842 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001843 options->control_path = NULL;
1844 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001845 options->control_persist = -1;
1846 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001847 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001848 options->tun_open = -1;
1849 options->tun_local = -1;
1850 options->tun_remote = -1;
1851 options->local_command = NULL;
1852 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001853 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001854 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001855 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001856 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001857 options->ip_qos_interactive = -1;
1858 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001859 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001860 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001861 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001862 options->num_canonical_domains = 0;
1863 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001864 options->canonicalize_max_dots = -1;
1865 options->canonicalize_fallback_local = -1;
1866 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001867 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001868 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001869 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001870 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001871 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872}
1873
Damien Miller5428f641999-11-25 11:54:57 +11001874/*
Damien Miller13f97b22014-02-24 15:57:55 +11001875 * A petite version of fill_default_options() that just fills the options
1876 * needed for hostname canonicalization to proceed.
1877 */
1878void
1879fill_default_options_for_canonicalization(Options *options)
1880{
1881 if (options->canonicalize_max_dots == -1)
1882 options->canonicalize_max_dots = 1;
1883 if (options->canonicalize_fallback_local == -1)
1884 options->canonicalize_fallback_local = 1;
1885 if (options->canonicalize_hostname == -1)
1886 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1887}
1888
1889/*
Damien Miller5428f641999-11-25 11:54:57 +11001890 * Called after processing other sources of option data, this fills those
1891 * options for which no value has been specified with their default values.
1892 */
Damien Miller4af51302000-04-16 11:18:38 +10001893void
Damien Miller95def091999-11-25 00:26:21 +11001894fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001895{
Damien Miller95def091999-11-25 00:26:21 +11001896 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001897 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001898 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001899 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001900 if (options->forward_x11_trusted == -1)
1901 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001902 if (options->forward_x11_timeout == -1)
1903 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001904 /*
1905 * stdio forwarding (-W) changes the default for these but we defer
1906 * setting the values so they can be overridden.
1907 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001908 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001909 options->exit_on_forward_failure =
1910 options->stdio_forward_host != NULL ? 1 : 0;
1911 if (options->clear_forwardings == -1)
1912 options->clear_forwardings =
1913 options->stdio_forward_host != NULL ? 1 : 0;
1914 if (options->clear_forwardings == 1)
1915 clear_forwardings(options);
1916
Damien Millerd3a18572000-06-07 19:55:44 +10001917 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001918 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001919 if (options->fwd_opts.gateway_ports == -1)
1920 options->fwd_opts.gateway_ports = 0;
1921 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1922 options->fwd_opts.streamlocal_bind_mask = 0177;
1923 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1924 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001925 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001926 options->use_privileged_port = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001927 if (options->pubkey_authentication == -1)
1928 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001929 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001930 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001931 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001932 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001933 if (options->gss_deleg_creds == -1)
1934 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001935 if (options->password_authentication == -1)
1936 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001937 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001938 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001939 if (options->hostbased_authentication == -1)
1940 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001941 if (options->batch_mode == -1)
1942 options->batch_mode = 0;
1943 if (options->check_host_ip == -1)
1944 options->check_host_ip = 1;
1945 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001946 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001947 if (options->compression == -1)
1948 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001949 if (options->tcp_keep_alive == -1)
1950 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001951 if (options->port == -1)
1952 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001953 if (options->address_family == -1)
1954 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001955 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001956 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001957 if (options->number_of_password_prompts == -1)
1958 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001959 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001960 if (options->add_keys_to_agent == -1)
1961 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001962 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001963 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1964 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001965#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001966 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001967#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001968 add_identity_file(options, "~/",
1969 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10001970 }
Damien Miller95def091999-11-25 00:26:21 +11001971 if (options->escape_char == -1)
1972 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001973 if (options->num_system_hostfiles == 0) {
1974 options->system_hostfiles[options->num_system_hostfiles++] =
1975 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1976 options->system_hostfiles[options->num_system_hostfiles++] =
1977 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1978 }
1979 if (options->num_user_hostfiles == 0) {
1980 options->user_hostfiles[options->num_user_hostfiles++] =
1981 xstrdup(_PATH_SSH_USER_HOSTFILE);
1982 options->user_hostfiles[options->num_user_hostfiles++] =
1983 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1984 }
Damien Millerfcd93202002-02-05 12:26:34 +11001985 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001986 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001987 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1988 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001989 if (options->no_host_authentication_for_localhost == - 1)
1990 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001991 if (options->identities_only == -1)
1992 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001993 if (options->enable_ssh_keysign == -1)
1994 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001995 if (options->rekey_limit == -1)
1996 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001997 if (options->rekey_interval == -1)
1998 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001999 if (options->verify_host_key_dns == -1)
2000 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002001 if (options->server_alive_interval == -1)
2002 options->server_alive_interval = 0;
2003 if (options->server_alive_count_max == -1)
2004 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002005 if (options->control_master == -1)
2006 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002007 if (options->control_persist == -1) {
2008 options->control_persist = 0;
2009 options->control_persist_timeout = 0;
2010 }
Damien Millere1776152005-03-01 21:47:37 +11002011 if (options->hash_known_hosts == -1)
2012 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002013 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002014 options->tun_open = SSH_TUNMODE_NO;
2015 if (options->tun_local == -1)
2016 options->tun_local = SSH_TUNID_ANY;
2017 if (options->tun_remote == -1)
2018 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002019 if (options->permit_local_command == -1)
2020 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002021 if (options->visual_host_key == -1)
2022 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002023 if (options->ip_qos_interactive == -1)
2024 options->ip_qos_interactive = IPTOS_LOWDELAY;
2025 if (options->ip_qos_bulk == -1)
2026 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002027 if (options->request_tty == -1)
2028 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002029 if (options->proxy_use_fdpass == -1)
2030 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002031 if (options->canonicalize_max_dots == -1)
2032 options->canonicalize_max_dots = 1;
2033 if (options->canonicalize_fallback_local == -1)
2034 options->canonicalize_fallback_local = 1;
2035 if (options->canonicalize_hostname == -1)
2036 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002037 if (options->fingerprint_hash == -1)
2038 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002039 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002040 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002041 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2042 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2043 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2044 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2045 &options->hostbased_key_types) != 0 ||
2046 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2047 &options->pubkey_key_types) != 0)
2048 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002049
Damien Millere9fc72e2013-10-15 12:14:12 +11002050#define CLEAR_ON_NONE(v) \
2051 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002052 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002053 free(v); \
2054 v = NULL; \
2055 } \
2056 } while(0)
2057 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002058 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002059 CLEAR_ON_NONE(options->proxy_command);
2060 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002061 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002062 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002063 /* options->user will be set in the main program if appropriate */
2064 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002065 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002066 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002067}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002068
Damien Miller7acefbb2014-07-18 14:11:24 +10002069struct fwdarg {
2070 char *arg;
2071 int ispath;
2072};
2073
2074/*
2075 * parse_fwd_field
2076 * parses the next field in a port forwarding specification.
2077 * sets fwd to the parsed field and advances p past the colon
2078 * or sets it to NULL at end of string.
2079 * returns 0 on success, else non-zero.
2080 */
2081static int
2082parse_fwd_field(char **p, struct fwdarg *fwd)
2083{
2084 char *ep, *cp = *p;
2085 int ispath = 0;
2086
2087 if (*cp == '\0') {
2088 *p = NULL;
2089 return -1; /* end of string */
2090 }
2091
2092 /*
2093 * A field escaped with square brackets is used literally.
2094 * XXX - allow ']' to be escaped via backslash?
2095 */
2096 if (*cp == '[') {
2097 /* find matching ']' */
2098 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2099 if (*ep == '/')
2100 ispath = 1;
2101 }
2102 /* no matching ']' or not at end of field. */
2103 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2104 return -1;
2105 /* NUL terminate the field and advance p past the colon */
2106 *ep++ = '\0';
2107 if (*ep != '\0')
2108 *ep++ = '\0';
2109 fwd->arg = cp + 1;
2110 fwd->ispath = ispath;
2111 *p = ep;
2112 return 0;
2113 }
2114
2115 for (cp = *p; *cp != '\0'; cp++) {
2116 switch (*cp) {
2117 case '\\':
2118 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002119 if (*cp == '\0')
2120 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002121 break;
2122 case '/':
2123 ispath = 1;
2124 break;
2125 case ':':
2126 *cp++ = '\0';
2127 goto done;
2128 }
2129 }
2130done:
2131 fwd->arg = *p;
2132 fwd->ispath = ispath;
2133 *p = cp;
2134 return 0;
2135}
2136
Damien Millerf91ee4c2005-03-01 21:24:33 +11002137/*
2138 * parse_forward
2139 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002140 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002141 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2142 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002143 * dynamicfwd == 1
2144 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002145 * returns number of arguments parsed or zero on error
2146 */
2147int
Damien Miller7acefbb2014-07-18 14:11:24 +10002148parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002149{
Damien Miller7acefbb2014-07-18 14:11:24 +10002150 struct fwdarg fwdargs[4];
2151 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002152 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002153
Damien Miller7acefbb2014-07-18 14:11:24 +10002154 memset(fwd, 0, sizeof(*fwd));
2155 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002156
2157 cp = p = xstrdup(fwdspec);
2158
2159 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002160 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002161 cp++;
2162
Damien Miller7acefbb2014-07-18 14:11:24 +10002163 for (i = 0; i < 4; ++i) {
2164 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002165 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002166 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002167
Damien Millerf4b39532008-11-03 19:28:21 +11002168 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002169 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002170 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002171 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002172
2173 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002174 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002175 if (fwdargs[0].ispath) {
2176 fwd->listen_path = xstrdup(fwdargs[0].arg);
2177 fwd->listen_port = PORT_STREAMLOCAL;
2178 } else {
2179 fwd->listen_host = NULL;
2180 fwd->listen_port = a2port(fwdargs[0].arg);
2181 }
Damien Millera699d952008-11-03 19:27:34 +11002182 fwd->connect_host = xstrdup("socks");
2183 break;
2184
2185 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002186 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2187 fwd->listen_path = xstrdup(fwdargs[0].arg);
2188 fwd->listen_port = PORT_STREAMLOCAL;
2189 fwd->connect_path = xstrdup(fwdargs[1].arg);
2190 fwd->connect_port = PORT_STREAMLOCAL;
2191 } else if (fwdargs[1].ispath) {
2192 fwd->listen_host = NULL;
2193 fwd->listen_port = a2port(fwdargs[0].arg);
2194 fwd->connect_path = xstrdup(fwdargs[1].arg);
2195 fwd->connect_port = PORT_STREAMLOCAL;
2196 } else {
2197 fwd->listen_host = xstrdup(fwdargs[0].arg);
2198 fwd->listen_port = a2port(fwdargs[1].arg);
2199 fwd->connect_host = xstrdup("socks");
2200 }
Damien Millera699d952008-11-03 19:27:34 +11002201 break;
2202
Damien Millerf91ee4c2005-03-01 21:24:33 +11002203 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002204 if (fwdargs[0].ispath) {
2205 fwd->listen_path = xstrdup(fwdargs[0].arg);
2206 fwd->listen_port = PORT_STREAMLOCAL;
2207 fwd->connect_host = xstrdup(fwdargs[1].arg);
2208 fwd->connect_port = a2port(fwdargs[2].arg);
2209 } else if (fwdargs[2].ispath) {
2210 fwd->listen_host = xstrdup(fwdargs[0].arg);
2211 fwd->listen_port = a2port(fwdargs[1].arg);
2212 fwd->connect_path = xstrdup(fwdargs[2].arg);
2213 fwd->connect_port = PORT_STREAMLOCAL;
2214 } else {
2215 fwd->listen_host = NULL;
2216 fwd->listen_port = a2port(fwdargs[0].arg);
2217 fwd->connect_host = xstrdup(fwdargs[1].arg);
2218 fwd->connect_port = a2port(fwdargs[2].arg);
2219 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002220 break;
2221
2222 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002223 fwd->listen_host = xstrdup(fwdargs[0].arg);
2224 fwd->listen_port = a2port(fwdargs[1].arg);
2225 fwd->connect_host = xstrdup(fwdargs[2].arg);
2226 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002227 break;
2228 default:
2229 i = 0; /* failure */
2230 }
2231
Darren Tuckera627d422013-06-02 07:31:17 +10002232 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002233
Damien Millera699d952008-11-03 19:27:34 +11002234 if (dynamicfwd) {
2235 if (!(i == 1 || i == 2))
2236 goto fail_free;
2237 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002238 if (!(i == 3 || i == 4)) {
2239 if (fwd->connect_path == NULL &&
2240 fwd->listen_path == NULL)
2241 goto fail_free;
2242 }
2243 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002244 goto fail_free;
2245 }
2246
Damien Miller7acefbb2014-07-18 14:11:24 +10002247 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2248 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002249 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002250 if (fwd->connect_host != NULL &&
2251 strlen(fwd->connect_host) >= NI_MAXHOST)
2252 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002253 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2254 if (fwd->connect_path != NULL &&
2255 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2256 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002257 if (fwd->listen_host != NULL &&
2258 strlen(fwd->listen_host) >= NI_MAXHOST)
2259 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002260 if (fwd->listen_path != NULL &&
2261 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2262 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002263
2264 return (i);
2265
2266 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002267 free(fwd->connect_host);
2268 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002269 free(fwd->connect_path);
2270 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002271 free(fwd->listen_host);
2272 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002273 free(fwd->listen_path);
2274 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002275 return (0);
2276}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002277
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002278int
2279parse_jump(const char *s, Options *o, int active)
2280{
2281 char *orig, *sdup, *cp;
2282 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002283 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002284
2285 active &= o->proxy_command == NULL && o->jump_host == NULL;
2286
2287 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002288 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002289 do {
2290 if ((cp = strrchr(sdup, ',')) == NULL)
2291 cp = sdup; /* last */
2292 else
2293 *cp++ = '\0';
2294
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002295 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002296 /* First argument and configuration is active */
2297 if (parse_user_host_port(cp, &user, &host, &port) != 0)
2298 goto out;
2299 } else {
2300 /* Subsequent argument or inactive configuration */
2301 if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2302 goto out;
2303 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002304 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002305 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002306 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002307 if (active) {
2308 o->jump_user = user;
2309 o->jump_host = host;
2310 o->jump_port = port;
2311 o->proxy_command = xstrdup("none");
2312 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002313 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2314 o->jump_extra = xstrdup(s);
2315 o->jump_extra[cp - s] = '\0';
2316 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002317 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002318 ret = 0;
2319 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002320 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002321 free(user);
2322 free(host);
2323 return ret;
2324}
2325
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002326/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2327static const char *
2328fmt_multistate_int(int val, const struct multistate *m)
2329{
2330 u_int i;
2331
2332 for (i = 0; m[i].key != NULL; i++) {
2333 if (m[i].value == val)
2334 return m[i].key;
2335 }
2336 return "UNKNOWN";
2337}
2338
2339static const char *
2340fmt_intarg(OpCodes code, int val)
2341{
2342 if (val == -1)
2343 return "unset";
2344 switch (code) {
2345 case oAddressFamily:
2346 return fmt_multistate_int(val, multistate_addressfamily);
2347 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002348 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002349 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002350 case oStrictHostKeyChecking:
2351 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002352 case oControlMaster:
2353 return fmt_multistate_int(val, multistate_controlmaster);
2354 case oTunnel:
2355 return fmt_multistate_int(val, multistate_tunnel);
2356 case oRequestTTY:
2357 return fmt_multistate_int(val, multistate_requesttty);
2358 case oCanonicalizeHostname:
2359 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002360 case oFingerprintHash:
2361 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002362 default:
2363 switch (val) {
2364 case 0:
2365 return "no";
2366 case 1:
2367 return "yes";
2368 default:
2369 return "UNKNOWN";
2370 }
2371 }
2372}
2373
2374static const char *
2375lookup_opcode_name(OpCodes code)
2376{
2377 u_int i;
2378
2379 for (i = 0; keywords[i].name != NULL; i++)
2380 if (keywords[i].opcode == code)
2381 return(keywords[i].name);
2382 return "UNKNOWN";
2383}
2384
2385static void
2386dump_cfg_int(OpCodes code, int val)
2387{
2388 printf("%s %d\n", lookup_opcode_name(code), val);
2389}
2390
2391static void
2392dump_cfg_fmtint(OpCodes code, int val)
2393{
2394 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2395}
2396
2397static void
2398dump_cfg_string(OpCodes code, const char *val)
2399{
2400 if (val == NULL)
2401 return;
2402 printf("%s %s\n", lookup_opcode_name(code), val);
2403}
2404
2405static void
2406dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2407{
2408 u_int i;
2409
2410 for (i = 0; i < count; i++)
2411 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2412}
2413
2414static void
2415dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2416{
2417 u_int i;
2418
2419 printf("%s", lookup_opcode_name(code));
2420 for (i = 0; i < count; i++)
2421 printf(" %s", vals[i]);
2422 printf("\n");
2423}
2424
2425static void
2426dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2427{
2428 const struct Forward *fwd;
2429 u_int i;
2430
2431 /* oDynamicForward */
2432 for (i = 0; i < count; i++) {
2433 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002434 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002435 strcmp(fwd->connect_host, "socks") != 0)
2436 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002437 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002438 strcmp(fwd->connect_host, "socks") == 0)
2439 continue;
2440 printf("%s", lookup_opcode_name(code));
2441 if (fwd->listen_port == PORT_STREAMLOCAL)
2442 printf(" %s", fwd->listen_path);
2443 else if (fwd->listen_host == NULL)
2444 printf(" %d", fwd->listen_port);
2445 else {
2446 printf(" [%s]:%d",
2447 fwd->listen_host, fwd->listen_port);
2448 }
2449 if (code != oDynamicForward) {
2450 if (fwd->connect_port == PORT_STREAMLOCAL)
2451 printf(" %s", fwd->connect_path);
2452 else if (fwd->connect_host == NULL)
2453 printf(" %d", fwd->connect_port);
2454 else {
2455 printf(" [%s]:%d",
2456 fwd->connect_host, fwd->connect_port);
2457 }
2458 }
2459 printf("\n");
2460 }
2461}
2462
2463void
2464dump_client_config(Options *o, const char *host)
2465{
2466 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002467 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002468
djm@openbsd.org60a92472015-08-21 23:53:08 +00002469 /* This is normally prepared in ssh_kex2 */
2470 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2471 fatal("%s: kex_assemble_names failed", __func__);
2472
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002473 /* Most interesting options first: user, host, port */
2474 dump_cfg_string(oUser, o->user);
2475 dump_cfg_string(oHostName, host);
2476 dump_cfg_int(oPort, o->port);
2477
2478 /* Flag options */
2479 dump_cfg_fmtint(oAddressFamily, o->address_family);
2480 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2481 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2482 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2483 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2484 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2485 dump_cfg_fmtint(oCompression, o->compression);
2486 dump_cfg_fmtint(oControlMaster, o->control_master);
2487 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002488 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002489 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002490 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002491 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2492 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2493 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2494 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2495#ifdef GSSAPI
2496 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2497 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2498#endif /* GSSAPI */
2499 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2500 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2501 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2502 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2503 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2504 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2505 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002506 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2507 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2508 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002509 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2510 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2511 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2512 dump_cfg_fmtint(oTunnel, o->tun_open);
2513 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2514 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2515 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002516 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002517
2518 /* Integer options */
2519 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002520 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2521 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2522 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2523 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2524 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2525
2526 /* String options */
2527 dump_cfg_string(oBindAddress, o->bind_address);
2528 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2529 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002530 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002531 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002532 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002533 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002534 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2535 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2536 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002537 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002538 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2539 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002540#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002541 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002542#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002543 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002544 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002545 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002546 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002547
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002548 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002549 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2550 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2551 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2552
2553 /* String array options */
2554 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2555 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2556 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2557 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2558 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2559
2560 /* Special cases */
2561
2562 /* oConnectTimeout */
2563 if (o->connection_timeout == -1)
2564 printf("connecttimeout none\n");
2565 else
2566 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2567
2568 /* oTunnelDevice */
2569 printf("tunneldevice");
2570 if (o->tun_local == SSH_TUNID_ANY)
2571 printf(" any");
2572 else
2573 printf(" %d", o->tun_local);
2574 if (o->tun_remote == SSH_TUNID_ANY)
2575 printf(":any");
2576 else
2577 printf(":%d", o->tun_remote);
2578 printf("\n");
2579
2580 /* oCanonicalizePermittedCNAMEs */
2581 if ( o->num_permitted_cnames > 0) {
2582 printf("canonicalizePermittedcnames");
2583 for (i = 0; i < o->num_permitted_cnames; i++) {
2584 printf(" %s:%s", o->permitted_cnames[i].source_list,
2585 o->permitted_cnames[i].target_list);
2586 }
2587 printf("\n");
2588 }
2589
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002590 /* oControlPersist */
2591 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2592 dump_cfg_fmtint(oControlPersist, o->control_persist);
2593 else
2594 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2595
2596 /* oEscapeChar */
2597 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2598 printf("escapechar none\n");
2599 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002600 vis(buf, o->escape_char, VIS_WHITE, 0);
2601 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002602 }
2603
2604 /* oIPQoS */
2605 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2606 printf("%s\n", iptos2str(o->ip_qos_bulk));
2607
2608 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002609 printf("rekeylimit %llu %d\n",
2610 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002611
2612 /* oStreamLocalBindMask */
2613 printf("streamlocalbindmask 0%o\n",
2614 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002615
2616 /* oProxyCommand / oProxyJump */
2617 if (o->jump_host == NULL)
2618 dump_cfg_string(oProxyCommand, o->proxy_command);
2619 else {
2620 /* Check for numeric addresses */
2621 i = strchr(o->jump_host, ':') != NULL ||
2622 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2623 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2624 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002625 /* optional additional jump spec */
2626 o->jump_extra == NULL ? "" : o->jump_extra,
2627 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002628 /* optional user */
2629 o->jump_user == NULL ? "" : o->jump_user,
2630 o->jump_user == NULL ? "" : "@",
2631 /* opening [ if hostname is numeric */
2632 i ? "[" : "",
2633 /* mandatory hostname */
2634 o->jump_host,
2635 /* closing ] if hostname is numeric */
2636 i ? "]" : "",
2637 /* optional port number */
2638 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002639 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002640 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002641}