blob: 63baa7d7874d378596de10c7228f02bc9d9bd2bf [file] [log] [blame]
millert@openbsd.org887669e2017-10-21 23:06:24 +00001/* $OpenBSD: readconf.c,v 1.280 2017/10/21 23:06:24 millert 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 Miller5428f641999-11-25 11:54:57 +1100686/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000687 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100688 */
Damien Miller4af51302000-04-16 11:18:38 +1000689static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000690parse_token(const char *cp, const char *filename, int linenum,
691 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692{
Darren Tucker07636982013-05-16 20:30:03 +1000693 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller95def091999-11-25 00:26:21 +1100695 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000696 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100697 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000698 if (ignored_unknown != NULL &&
699 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000700 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000701 error("%s: line %d: Bad configuration option: %s",
702 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100703 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704}
705
Damien Millere9fc72e2013-10-15 12:14:12 +1100706/* Multistate option parsing */
707struct multistate {
708 char *key;
709 int value;
710};
711static const struct multistate multistate_flag[] = {
712 { "true", 1 },
713 { "false", 0 },
714 { "yes", 1 },
715 { "no", 0 },
716 { NULL, -1 }
717};
718static const struct multistate multistate_yesnoask[] = {
719 { "true", 1 },
720 { "false", 0 },
721 { "yes", 1 },
722 { "no", 0 },
723 { "ask", 2 },
724 { NULL, -1 }
725};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000726static const struct multistate multistate_strict_hostkey[] = {
727 { "true", SSH_STRICT_HOSTKEY_YES },
728 { "false", SSH_STRICT_HOSTKEY_OFF },
729 { "yes", SSH_STRICT_HOSTKEY_YES },
730 { "no", SSH_STRICT_HOSTKEY_OFF },
731 { "ask", SSH_STRICT_HOSTKEY_ASK },
732 { "off", SSH_STRICT_HOSTKEY_OFF },
733 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
734 { NULL, -1 }
735};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000736static const struct multistate multistate_yesnoaskconfirm[] = {
737 { "true", 1 },
738 { "false", 0 },
739 { "yes", 1 },
740 { "no", 0 },
741 { "ask", 2 },
742 { "confirm", 3 },
743 { NULL, -1 }
744};
Damien Millere9fc72e2013-10-15 12:14:12 +1100745static const struct multistate multistate_addressfamily[] = {
746 { "inet", AF_INET },
747 { "inet6", AF_INET6 },
748 { "any", AF_UNSPEC },
749 { NULL, -1 }
750};
751static const struct multistate multistate_controlmaster[] = {
752 { "true", SSHCTL_MASTER_YES },
753 { "yes", SSHCTL_MASTER_YES },
754 { "false", SSHCTL_MASTER_NO },
755 { "no", SSHCTL_MASTER_NO },
756 { "auto", SSHCTL_MASTER_AUTO },
757 { "ask", SSHCTL_MASTER_ASK },
758 { "autoask", SSHCTL_MASTER_AUTO_ASK },
759 { NULL, -1 }
760};
761static const struct multistate multistate_tunnel[] = {
762 { "ethernet", SSH_TUNMODE_ETHERNET },
763 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
764 { "true", SSH_TUNMODE_DEFAULT },
765 { "yes", SSH_TUNMODE_DEFAULT },
766 { "false", SSH_TUNMODE_NO },
767 { "no", SSH_TUNMODE_NO },
768 { NULL, -1 }
769};
770static const struct multistate multistate_requesttty[] = {
771 { "true", REQUEST_TTY_YES },
772 { "yes", REQUEST_TTY_YES },
773 { "false", REQUEST_TTY_NO },
774 { "no", REQUEST_TTY_NO },
775 { "force", REQUEST_TTY_FORCE },
776 { "auto", REQUEST_TTY_AUTO },
777 { NULL, -1 }
778};
Damien Miller38505592013-10-17 11:48:13 +1100779static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100780 { "true", SSH_CANONICALISE_YES },
781 { "false", SSH_CANONICALISE_NO },
782 { "yes", SSH_CANONICALISE_YES },
783 { "no", SSH_CANONICALISE_NO },
784 { "always", SSH_CANONICALISE_ALWAYS },
785 { NULL, -1 }
786};
Damien Millere9fc72e2013-10-15 12:14:12 +1100787
Damien Miller5428f641999-11-25 11:54:57 +1100788/*
789 * Processes a single option line as used in the configuration files. This
790 * only sets those values that have not already been set.
791 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100792int
Damien Miller194fd902013-10-15 12:13:05 +1100793process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000794 const char *original_host, char *line, const char *filename,
795 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000797 return process_config_line_depth(options, pw, host, original_host,
798 line, filename, linenum, activep, flags, 0);
799}
800
801#define WHITESPACE " \t\r\n"
802static int
803process_config_line_depth(Options *options, struct passwd *pw, const char *host,
804 const char *original_host, char *line, const char *filename,
805 int linenum, int *activep, int flags, int depth)
806{
Damien Miller295ee632011-05-29 21:42:31 +1000807 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
808 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000809 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000810 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000811 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100812 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000813 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000814 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100815 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000816 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100817 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100818 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000819 glob_t gl;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820
Damien Miller194fd902013-10-15 12:13:05 +1100821 if (activep == NULL) { /* We are processing a command line directive */
822 cmdline = 1;
823 activep = &cmdline;
824 }
825
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000826 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000827 if ((len = strlen(line)) == 0)
828 return 0;
829 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000830 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000831 break;
832 line[len] = '\0';
833 }
834
Damien Millerbe484b52000-07-15 14:14:16 +1000835 s = line;
836 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100837 if ((keyword = strdelim(&s)) == NULL)
838 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000839 /* Ignore leading whitespace. */
840 if (*keyword == '\0')
841 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100843 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000844 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100845 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846
Darren Tucker07636982013-05-16 20:30:03 +1000847 opcode = parse_token(keyword, filename, linenum,
848 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849
Damien Miller95def091999-11-25 00:26:21 +1100850 switch (opcode) {
851 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100852 /* don't panic, but count bad options */
853 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000854 case oIgnore:
855 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000856 case oIgnoredUnknownOption:
857 debug("%s line %d: Ignored unknown option \"%s\"",
858 filename, linenum, keyword);
859 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000860 case oConnectTimeout:
861 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100862parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000863 arg = strdelim(&s);
864 if (!arg || *arg == '\0')
865 fatal("%s line %d: missing time value.",
866 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000867 if (strcmp(arg, "none") == 0)
868 value = -1;
869 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000870 fatal("%s line %d: invalid time value.",
871 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100872 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000873 *intptr = value;
874 break;
875
Damien Miller95def091999-11-25 00:26:21 +1100876 case oForwardAgent:
877 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100878 parse_flag:
879 multistate_ptr = multistate_flag;
880 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000881 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000882 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100883 fatal("%s line %d: missing argument.",
884 filename, linenum);
885 value = -1;
886 for (i = 0; multistate_ptr[i].key != NULL; i++) {
887 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
888 value = multistate_ptr[i].value;
889 break;
890 }
891 }
892 if (value == -1)
893 fatal("%s line %d: unsupported option \"%s\".",
894 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100895 if (*activep && *intptr == -1)
896 *intptr = value;
897 break;
898
899 case oForwardX11:
900 intptr = &options->forward_x11;
901 goto parse_flag;
902
Darren Tucker0a118da2003-10-15 15:54:32 +1000903 case oForwardX11Trusted:
904 intptr = &options->forward_x11_trusted;
905 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000906
Damien Miller1ab6a512010-06-26 10:02:24 +1000907 case oForwardX11Timeout:
908 intptr = &options->forward_x11_timeout;
909 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000910
Damien Miller95def091999-11-25 00:26:21 +1100911 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000912 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100913 goto parse_flag;
914
Darren Tuckere7d4b192006-07-12 22:17:10 +1000915 case oExitOnForwardFailure:
916 intptr = &options->exit_on_forward_failure;
917 goto parse_flag;
918
Damien Miller95def091999-11-25 00:26:21 +1100919 case oUsePrivilegedPort:
920 intptr = &options->use_privileged_port;
921 goto parse_flag;
922
Damien Miller95def091999-11-25 00:26:21 +1100923 case oPasswordAuthentication:
924 intptr = &options->password_authentication;
925 goto parse_flag;
926
Damien Miller874d77b2000-10-14 16:23:11 +1100927 case oKbdInteractiveAuthentication:
928 intptr = &options->kbd_interactive_authentication;
929 goto parse_flag;
930
931 case oKbdInteractiveDevices:
932 charptr = &options->kbd_interactive_devices;
933 goto parse_string;
934
Damien Miller0bc1bd82000-11-13 22:57:25 +1100935 case oPubkeyAuthentication:
936 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000937 goto parse_flag;
938
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000939 case oHostbasedAuthentication:
940 intptr = &options->hostbased_authentication;
941 goto parse_flag;
942
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000943 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000944 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100945 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000946
Darren Tucker0efd1552003-08-26 11:49:55 +1000947 case oGssAuthentication:
948 intptr = &options->gss_authentication;
949 goto parse_flag;
950
951 case oGssDelegateCreds:
952 intptr = &options->gss_deleg_creds;
953 goto parse_flag;
954
Damien Miller95def091999-11-25 00:26:21 +1100955 case oBatchMode:
956 intptr = &options->batch_mode;
957 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Miller95def091999-11-25 00:26:21 +1100959 case oCheckHostIP:
960 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000961 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Miller37876e92003-05-15 10:19:46 +1000963 case oVerifyHostKeyDNS:
964 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100965 multistate_ptr = multistate_yesnoask;
966 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968 case oStrictHostKeyChecking:
969 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000970 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +1100971 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 case oCompression:
974 intptr = &options->compression;
975 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller12c150e2003-12-17 16:31:10 +1100977 case oTCPKeepAlive:
978 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100979 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000981 case oNoHostAuthenticationForLocalhost:
982 intptr = &options->no_host_authentication_for_localhost;
983 goto parse_flag;
984
Damien Miller95def091999-11-25 00:26:21 +1100985 case oNumberOfPasswordPrompts:
986 intptr = &options->number_of_password_prompts;
987 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Millera5539d22003-04-09 20:50:06 +1000989 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000990 arg = strdelim(&s);
991 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000992 fatal("%.200s line %d: Missing argument.", filename,
993 linenum);
994 if (strcmp(arg, "default") == 0) {
995 val64 = 0;
996 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000997 if (scan_scaled(arg, &val64) == -1)
998 fatal("%.200s line %d: Bad number '%s': %s",
999 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001000 if (val64 != 0 && val64 < 16)
1001 fatal("%.200s line %d: RekeyLimit too small",
1002 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001003 }
Damien Miller3dff1762008-02-10 22:25:52 +11001004 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001005 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001006 if (s != NULL) { /* optional rekey interval present */
1007 if (strcmp(s, "none") == 0) {
1008 (void)strdelim(&s); /* discard */
1009 break;
1010 }
1011 intptr = &options->rekey_interval;
1012 goto parse_time;
1013 }
Damien Millera5539d22003-04-09 20:50:06 +10001014 break;
1015
Damien Miller95def091999-11-25 00:26:21 +11001016 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001017 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001018 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001019 fatal("%.200s line %d: Missing argument.", filename, linenum);
1020 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001021 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001022 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001023 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001024 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001025 add_identity_file(options, NULL,
1026 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001027 }
1028 break;
1029
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001030 case oCertificateFile:
1031 arg = strdelim(&s);
1032 if (!arg || *arg == '\0')
1033 fatal("%.200s line %d: Missing argument.",
1034 filename, linenum);
1035 if (*activep) {
1036 intptr = &options->num_certificate_files;
1037 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1038 fatal("%.200s line %d: Too many certificate "
1039 "files specified (max %d).",
1040 filename, linenum,
1041 SSH_MAX_CERTIFICATE_FILES);
1042 }
1043 add_certificate_file(options, arg,
1044 flags & SSHCONF_USERCONF);
1045 }
1046 break;
1047
Damien Millerd3a18572000-06-07 19:55:44 +10001048 case oXAuthLocation:
1049 charptr=&options->xauth_location;
1050 goto parse_string;
1051
Damien Miller95def091999-11-25 00:26:21 +11001052 case oUser:
1053 charptr = &options->user;
1054parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001055 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001056 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001057 fatal("%.200s line %d: Missing argument.",
1058 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001059 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001060 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001061 break;
1062
1063 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001064 cpptr = (char **)&options->system_hostfiles;
1065 uintptr = &options->num_system_hostfiles;
1066 max_entries = SSH_MAX_HOSTS_FILES;
1067parse_char_array:
1068 if (*activep && *uintptr == 0) {
1069 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1070 if ((*uintptr) >= max_entries)
1071 fatal("%s line %d: "
1072 "too many authorized keys files.",
1073 filename, linenum);
1074 cpptr[(*uintptr)++] = xstrdup(arg);
1075 }
1076 }
1077 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001078
1079 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001080 cpptr = (char **)&options->user_hostfiles;
1081 uintptr = &options->num_user_hostfiles;
1082 max_entries = SSH_MAX_HOSTS_FILES;
1083 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001084
Damien Miller95def091999-11-25 00:26:21 +11001085 case oHostName:
1086 charptr = &options->hostname;
1087 goto parse_string;
1088
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001089 case oHostKeyAlias:
1090 charptr = &options->host_key_alias;
1091 goto parse_string;
1092
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001093 case oPreferredAuthentications:
1094 charptr = &options->preferred_authentications;
1095 goto parse_string;
1096
Ben Lindstrome0f88042001-04-30 13:06:24 +00001097 case oBindAddress:
1098 charptr = &options->bind_address;
1099 goto parse_string;
1100
Damien Miller7ea845e2010-02-12 09:21:02 +11001101 case oPKCS11Provider:
1102 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001103 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001104
Damien Miller95def091999-11-25 00:26:21 +11001105 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001106 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001107 /* Ignore ProxyCommand if ProxyJump already specified */
1108 if (options->jump_host != NULL)
1109 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001110parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001111 if (s == NULL)
1112 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001113 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001114 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001115 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001116 return 0;
1117
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001118 case oProxyJump:
1119 if (s == NULL) {
1120 fatal("%.200s line %d: Missing argument.",
1121 filename, linenum);
1122 }
1123 len = strspn(s, WHITESPACE "=");
1124 if (parse_jump(s + len, options, *activep) == -1) {
1125 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1126 filename, linenum, s + len);
1127 }
1128 return 0;
1129
Damien Miller95def091999-11-25 00:26:21 +11001130 case oPort:
1131 intptr = &options->port;
1132parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001133 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001134 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001135 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001136 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001137 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001138
1139 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001140 value = strtol(arg, &endofnumber, 0);
1141 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001142 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001143 if (*activep && *intptr == -1)
1144 *intptr = value;
1145 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146
Damien Miller95def091999-11-25 00:26:21 +11001147 case oConnectionAttempts:
1148 intptr = &options->connection_attempts;
1149 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001150
Damien Miller78928792000-04-12 20:17:38 +10001151 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001152 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001153 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001154 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001155 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001156 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001157 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001158 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001159 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001160 break;
1161
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001162 case oMacs:
1163 arg = strdelim(&s);
1164 if (!arg || *arg == '\0')
1165 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001166 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001167 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001168 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001169 if (*activep && options->macs == NULL)
1170 options->macs = xstrdup(arg);
1171 break;
1172
Damien Millerd5f62bf2010-09-24 22:11:14 +10001173 case oKexAlgorithms:
1174 arg = strdelim(&s);
1175 if (!arg || *arg == '\0')
1176 fatal("%.200s line %d: Missing argument.",
1177 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001178 if (*arg != '-' &&
1179 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001180 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1181 filename, linenum, arg ? arg : "<NONE>");
1182 if (*activep && options->kex_algorithms == NULL)
1183 options->kex_algorithms = xstrdup(arg);
1184 break;
1185
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001186 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001187 charptr = &options->hostkeyalgorithms;
1188parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001189 arg = strdelim(&s);
1190 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001191 fatal("%.200s line %d: Missing argument.",
1192 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001193 if (*arg != '-' &&
1194 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001195 fatal("%s line %d: Bad key types '%s'.",
1196 filename, linenum, arg ? arg : "<NONE>");
1197 if (*activep && *charptr == NULL)
1198 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001199 break;
1200
Damien Miller95def091999-11-25 00:26:21 +11001201 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001202 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001203 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001204 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001205 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001206 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001207 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001208 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1209 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001210 break;
1211
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001212 case oLogFacility:
1213 log_facility_ptr = &options->log_facility;
1214 arg = strdelim(&s);
1215 value = log_facility_number(arg);
1216 if (value == SYSLOG_FACILITY_NOT_SET)
1217 fatal("%.200s line %d: unsupported log facility '%s'",
1218 filename, linenum, arg ? arg : "<NONE>");
1219 if (*log_facility_ptr == -1)
1220 *log_facility_ptr = (SyslogFacility) value;
1221 break;
1222
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001223 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001224 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001225 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001226 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001227 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001228 fatal("%.200s line %d: Missing port argument.",
1229 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001230
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001231 remotefwd = (opcode == oRemoteForward);
1232 dynamicfwd = (opcode == oDynamicForward);
1233
1234 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001235 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001236 if (arg2 == NULL || *arg2 == '\0') {
1237 if (remotefwd)
1238 dynamicfwd = 1;
1239 else
1240 fatal("%.200s line %d: Missing target "
1241 "argument.", filename, linenum);
1242 } else {
1243 /* construct a string for parse_forward */
1244 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1245 arg2);
1246 }
Damien Millera699d952008-11-03 19:27:34 +11001247 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001248 if (dynamicfwd)
1249 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001250
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001251 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001252 fatal("%.200s line %d: Bad forwarding specification.",
1253 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001254
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001255 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001256 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001257 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001258 } else {
1259 add_local_forward(options, &fwd);
1260 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001261 }
Damien Miller95def091999-11-25 00:26:21 +11001262 break;
1263
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001264 case oClearAllForwardings:
1265 intptr = &options->clear_forwardings;
1266 goto parse_flag;
1267
Damien Miller95def091999-11-25 00:26:21 +11001268 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001269 if (cmdline)
1270 fatal("Host directive not supported as a command-line "
1271 "option");
Damien Miller95def091999-11-25 00:26:21 +11001272 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001273 arg2 = NULL;
1274 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001275 if ((flags & SSHCONF_NEVERMATCH) != 0)
1276 break;
Damien Millerfe924212011-05-15 08:44:45 +10001277 negated = *arg == '!';
1278 if (negated)
1279 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001280 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001281 if (negated) {
1282 debug("%.200s line %d: Skipping Host "
1283 "block because of negated match "
1284 "for %.100s", filename, linenum,
1285 arg);
1286 *activep = 0;
1287 break;
1288 }
1289 if (!*activep)
1290 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001291 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001292 }
Damien Millerfe924212011-05-15 08:44:45 +10001293 }
1294 if (*activep)
1295 debug("%.200s line %d: Applying options for %.100s",
1296 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001297 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001298 return 0;
1299
Damien Miller194fd902013-10-15 12:13:05 +11001300 case oMatch:
1301 if (cmdline)
1302 fatal("Host directive not supported as a command-line "
1303 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001304 value = match_cfg_line(options, &s, pw, host, original_host,
1305 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001306 if (value < 0)
1307 fatal("%.200s line %d: Bad Match condition", filename,
1308 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001309 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001310 break;
1311
Damien Miller95def091999-11-25 00:26:21 +11001312 case oEscapeChar:
1313 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001314 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001315 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001316 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001317 if (strcmp(arg, "none") == 0)
1318 value = SSH_ESCAPECHAR_NONE;
1319 else if (arg[1] == '\0')
1320 value = (u_char) arg[0];
1321 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001322 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1323 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001324 else {
1325 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001326 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001327 /* NOTREACHED */
1328 value = 0; /* Avoid compiler warning. */
1329 }
1330 if (*activep && *intptr == -1)
1331 *intptr = value;
1332 break;
1333
Damien Miller20a8f972003-05-18 20:50:30 +10001334 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001335 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001336 multistate_ptr = multistate_addressfamily;
1337 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001338
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001339 case oEnableSSHKeysign:
1340 intptr = &options->enable_ssh_keysign;
1341 goto parse_flag;
1342
Damien Millerbd394c32004-03-08 23:12:36 +11001343 case oIdentitiesOnly:
1344 intptr = &options->identities_only;
1345 goto parse_flag;
1346
Damien Miller509b0102003-12-17 16:33:10 +11001347 case oServerAliveInterval:
1348 intptr = &options->server_alive_interval;
1349 goto parse_time;
1350
1351 case oServerAliveCountMax:
1352 intptr = &options->server_alive_count_max;
1353 goto parse_int;
1354
Darren Tucker46bc0752004-05-02 22:11:30 +10001355 case oSendEnv:
1356 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1357 if (strchr(arg, '=') != NULL)
1358 fatal("%s line %d: Invalid environment name.",
1359 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001360 if (!*activep)
1361 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001362 if (options->num_send_env >= MAX_SEND_ENV)
1363 fatal("%s line %d: too many send env.",
1364 filename, linenum);
1365 options->send_env[options->num_send_env++] =
1366 xstrdup(arg);
1367 }
1368 break;
1369
Damien Miller0e220db2004-06-15 10:34:08 +10001370 case oControlPath:
1371 charptr = &options->control_path;
1372 goto parse_string;
1373
1374 case oControlMaster:
1375 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001376 multistate_ptr = multistate_controlmaster;
1377 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001378
Damien Millere11e1ea2010-08-03 16:04:46 +10001379 case oControlPersist:
1380 /* no/false/yes/true, or a time spec */
1381 intptr = &options->control_persist;
1382 arg = strdelim(&s);
1383 if (!arg || *arg == '\0')
1384 fatal("%.200s line %d: Missing ControlPersist"
1385 " argument.", filename, linenum);
1386 value = 0;
1387 value2 = 0; /* timeout */
1388 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1389 value = 0;
1390 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1391 value = 1;
1392 else if ((value2 = convtime(arg)) >= 0)
1393 value = 1;
1394 else
1395 fatal("%.200s line %d: Bad ControlPersist argument.",
1396 filename, linenum);
1397 if (*activep && *intptr == -1) {
1398 *intptr = value;
1399 options->control_persist_timeout = value2;
1400 }
1401 break;
1402
Damien Millere1776152005-03-01 21:47:37 +11001403 case oHashKnownHosts:
1404 intptr = &options->hash_known_hosts;
1405 goto parse_flag;
1406
Damien Millerd27b9472005-12-13 19:29:02 +11001407 case oTunnel:
1408 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001409 multistate_ptr = multistate_tunnel;
1410 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001411
1412 case oTunnelDevice:
1413 arg = strdelim(&s);
1414 if (!arg || *arg == '\0')
1415 fatal("%.200s line %d: Missing argument.", filename, linenum);
1416 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001417 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001418 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1419 if (*activep) {
1420 options->tun_local = value;
1421 options->tun_remote = value2;
1422 }
1423 break;
1424
1425 case oLocalCommand:
1426 charptr = &options->local_command;
1427 goto parse_command;
1428
1429 case oPermitLocalCommand:
1430 intptr = &options->permit_local_command;
1431 goto parse_flag;
1432
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001433 case oRemoteCommand:
1434 charptr = &options->remote_command;
1435 goto parse_command;
1436
Damien Miller10288242008-06-30 00:04:03 +10001437 case oVisualHostKey:
1438 intptr = &options->visual_host_key;
1439 goto parse_flag;
1440
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001441 case oInclude:
1442 if (cmdline)
1443 fatal("Include directive not supported as a "
1444 "command-line option");
1445 value = 0;
1446 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1447 /*
1448 * Ensure all paths are anchored. User configuration
1449 * files may begin with '~/' but system configurations
1450 * must not. If the path is relative, then treat it
1451 * as living in ~/.ssh for user configurations or
1452 * /etc/ssh for system ones.
1453 */
1454 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1455 fatal("%.200s line %d: bad include path %s.",
1456 filename, linenum, arg);
1457 if (*arg != '/' && *arg != '~') {
1458 xasprintf(&arg2, "%s/%s",
1459 (flags & SSHCONF_USERCONF) ?
1460 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1461 } else
1462 arg2 = xstrdup(arg);
1463 memset(&gl, 0, sizeof(gl));
1464 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1465 if (r == GLOB_NOMATCH) {
1466 debug("%.200s line %d: include %s matched no "
1467 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001468 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001469 continue;
1470 } else if (r != 0 || gl.gl_pathc < 0)
1471 fatal("%.200s line %d: glob failed for %s.",
1472 filename, linenum, arg2);
1473 free(arg2);
1474 oactive = *activep;
1475 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1476 debug3("%.200s line %d: Including file %s "
1477 "depth %d%s", filename, linenum,
1478 gl.gl_pathv[i], depth,
1479 oactive ? "" : " (parse only)");
1480 r = read_config_file_depth(gl.gl_pathv[i],
1481 pw, host, original_host, options,
1482 flags | SSHCONF_CHECKPERM |
1483 (oactive ? 0 : SSHCONF_NEVERMATCH),
1484 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001485 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001486 fatal("Can't open user config file "
1487 "%.100s: %.100s", gl.gl_pathv[i],
1488 strerror(errno));
1489 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001490 /*
1491 * don't let Match in includes clobber the
1492 * containing file's Match state.
1493 */
1494 *activep = oactive;
1495 if (r != 1)
1496 value = -1;
1497 }
1498 globfree(&gl);
1499 }
1500 if (value != 0)
1501 return value;
1502 break;
1503
Damien Miller0dac6fb2010-11-20 15:19:38 +11001504 case oIPQoS:
1505 arg = strdelim(&s);
1506 if ((value = parse_ipqos(arg)) == -1)
1507 fatal("%s line %d: Bad IPQoS value: %s",
1508 filename, linenum, arg);
1509 arg = strdelim(&s);
1510 if (arg == NULL)
1511 value2 = value;
1512 else if ((value2 = parse_ipqos(arg)) == -1)
1513 fatal("%s line %d: Bad IPQoS value: %s",
1514 filename, linenum, arg);
1515 if (*activep) {
1516 options->ip_qos_interactive = value;
1517 options->ip_qos_bulk = value2;
1518 }
1519 break;
1520
Damien Miller21771e22011-05-15 08:45:50 +10001521 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001522 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001523 multistate_ptr = multistate_requesttty;
1524 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001525
Darren Tucker07636982013-05-16 20:30:03 +10001526 case oIgnoreUnknown:
1527 charptr = &options->ignored_unknown;
1528 goto parse_string;
1529
Damien Miller1262b662013-08-21 02:44:24 +10001530 case oProxyUseFdpass:
1531 intptr = &options->proxy_use_fdpass;
1532 goto parse_flag;
1533
Damien Miller0faf7472013-10-17 11:47:23 +11001534 case oCanonicalDomains:
1535 value = options->num_canonical_domains != 0;
1536 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001537 const char *errstr;
1538 if (!valid_domain(arg, 1, &errstr)) {
1539 fatal("%s line %d: %s", filename, linenum,
1540 errstr);
1541 }
Damien Miller0faf7472013-10-17 11:47:23 +11001542 if (!*activep || value)
1543 continue;
1544 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1545 fatal("%s line %d: too many hostname suffixes.",
1546 filename, linenum);
1547 options->canonical_domains[
1548 options->num_canonical_domains++] = xstrdup(arg);
1549 }
1550 break;
1551
Damien Miller38505592013-10-17 11:48:13 +11001552 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001553 value = options->num_permitted_cnames != 0;
1554 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1555 /* Either '*' for everything or 'list:list' */
1556 if (strcmp(arg, "*") == 0)
1557 arg2 = arg;
1558 else {
1559 lowercase(arg);
1560 if ((arg2 = strchr(arg, ':')) == NULL ||
1561 arg2[1] == '\0') {
1562 fatal("%s line %d: "
1563 "Invalid permitted CNAME \"%s\"",
1564 filename, linenum, arg);
1565 }
1566 *arg2 = '\0';
1567 arg2++;
1568 }
1569 if (!*activep || value)
1570 continue;
1571 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1572 fatal("%s line %d: too many permitted CNAMEs.",
1573 filename, linenum);
1574 cname = options->permitted_cnames +
1575 options->num_permitted_cnames++;
1576 cname->source_list = xstrdup(arg);
1577 cname->target_list = xstrdup(arg2);
1578 }
1579 break;
1580
Damien Miller38505592013-10-17 11:48:13 +11001581 case oCanonicalizeHostname:
1582 intptr = &options->canonicalize_hostname;
1583 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001584 goto parse_multistate;
1585
Damien Miller38505592013-10-17 11:48:13 +11001586 case oCanonicalizeMaxDots:
1587 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001588 goto parse_int;
1589
Damien Miller38505592013-10-17 11:48:13 +11001590 case oCanonicalizeFallbackLocal:
1591 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001592 goto parse_flag;
1593
Damien Miller7acefbb2014-07-18 14:11:24 +10001594 case oStreamLocalBindMask:
1595 arg = strdelim(&s);
1596 if (!arg || *arg == '\0')
1597 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1598 /* Parse mode in octal format */
1599 value = strtol(arg, &endofnumber, 8);
1600 if (arg == endofnumber || value < 0 || value > 0777)
1601 fatal("%.200s line %d: Bad mask.", filename, linenum);
1602 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1603 break;
1604
1605 case oStreamLocalBindUnlink:
1606 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1607 goto parse_flag;
1608
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001609 case oRevokedHostKeys:
1610 charptr = &options->revoked_host_keys;
1611 goto parse_string;
1612
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001613 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001614 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001615 arg = strdelim(&s);
1616 if (!arg || *arg == '\0')
1617 fatal("%.200s line %d: Missing argument.",
1618 filename, linenum);
1619 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1620 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1621 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001622 if (*activep && *intptr == -1)
1623 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001624 break;
1625
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001626 case oUpdateHostkeys:
1627 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001628 multistate_ptr = multistate_yesnoask;
1629 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001630
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001631 case oHostbasedKeyTypes:
1632 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001633 goto parse_keytypes;
1634
1635 case oPubkeyAcceptedKeyTypes:
1636 charptr = &options->pubkey_key_types;
1637 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001638
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001639 case oAddKeysToAgent:
1640 intptr = &options->add_keys_to_agent;
1641 multistate_ptr = multistate_yesnoaskconfirm;
1642 goto parse_multistate;
1643
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001644 case oIdentityAgent:
1645 charptr = &options->identity_agent;
1646 goto parse_string;
1647
Ben Lindstrom4daea862002-06-09 20:04:02 +00001648 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001649 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001650 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001651 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001652
Damien Millerf9b3feb2003-05-16 11:38:32 +10001653 case oUnsupported:
1654 error("%s line %d: Unsupported option \"%s\"",
1655 filename, linenum, keyword);
1656 return 0;
1657
Damien Miller95def091999-11-25 00:26:21 +11001658 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001659 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001660 }
1661
1662 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001663 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001664 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001665 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001666 }
Damien Miller95def091999-11-25 00:26:21 +11001667 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668}
1669
Damien Miller5428f641999-11-25 11:54:57 +11001670/*
1671 * Reads the config file and modifies the options accordingly. Options
1672 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001673 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001674 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001675int
Damien Miller194fd902013-10-15 12:13:05 +11001676read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001677 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001678{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001679 int active = 1;
1680
1681 return read_config_file_depth(filename, pw, host, original_host,
1682 options, flags, &active, 0);
1683}
1684
1685#define READCONF_MAX_DEPTH 16
1686static int
1687read_config_file_depth(const char *filename, struct passwd *pw,
1688 const char *host, const char *original_host, Options *options,
1689 int flags, int *activep, int depth)
1690{
Damien Miller95def091999-11-25 00:26:21 +11001691 FILE *f;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001692 char line[4096];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001693 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001694 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001696 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1697 fatal("Too many recursive configuration includes");
1698
Damien Miller57a44762004-04-20 20:11:57 +10001699 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001700 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001701
Darren Tuckeraefa3682013-04-05 11:18:35 +11001702 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001703 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001704
Damien Miller33793852004-06-15 10:27:55 +10001705 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001706 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001707 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001708 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001709 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001710 }
1711
Damien Miller95def091999-11-25 00:26:21 +11001712 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001713
Damien Miller5428f641999-11-25 11:54:57 +11001714 /*
1715 * Mark that we are now processing the options. This flag is turned
1716 * on/off by Host specifications.
1717 */
Damien Miller95def091999-11-25 00:26:21 +11001718 linenum = 0;
1719 while (fgets(line, sizeof(line), f)) {
1720 /* Update line number counter. */
1721 linenum++;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001722 if (strlen(line) == sizeof(line) - 1)
1723 fatal("%s line %d too long", filename, linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001724 if (process_config_line_depth(options, pw, host, original_host,
1725 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001726 bad_options++;
1727 }
1728 fclose(f);
1729 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001730 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001731 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001732 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001733}
1734
Damien Miller13f97b22014-02-24 15:57:55 +11001735/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1736int
1737option_clear_or_none(const char *o)
1738{
1739 return o == NULL || strcasecmp(o, "none") == 0;
1740}
1741
Damien Miller5428f641999-11-25 11:54:57 +11001742/*
1743 * Initializes options to special values that indicate that they have not yet
1744 * been set. Read_config_file will only set options with this value. Options
1745 * are processed in the following order: command line, user config file,
1746 * system config file. Last, fill_default_options is called.
1747 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748
Damien Miller4af51302000-04-16 11:18:38 +10001749void
Damien Miller95def091999-11-25 00:26:21 +11001750initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001751{
Damien Miller95def091999-11-25 00:26:21 +11001752 memset(options, 'X', sizeof(*options));
1753 options->forward_agent = -1;
1754 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001755 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001756 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001757 options->stdio_forward_host = NULL;
1758 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001759 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001760 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001761 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001762 options->fwd_opts.gateway_ports = -1;
1763 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1764 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001765 options->use_privileged_port = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001766 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001767 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001768 options->gss_authentication = -1;
1769 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001770 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001771 options->kbd_interactive_authentication = -1;
1772 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001773 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001774 options->batch_mode = -1;
1775 options->check_host_ip = -1;
1776 options->strict_host_key_checking = -1;
1777 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001778 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001779 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001780 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001781 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001782 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001783 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001784 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001785 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001786 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001787 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001788 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001789 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001790 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001791 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001792 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001793 options->jump_user = NULL;
1794 options->jump_host = NULL;
1795 options->jump_port = -1;
1796 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001797 options->user = NULL;
1798 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001799 options->num_system_hostfiles = 0;
1800 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001801 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001802 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001803 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001804 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001805 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001806 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001807 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001808 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001809 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001810 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001811 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001812 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001813 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001814 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001815 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001816 options->server_alive_interval = -1;
1817 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001818 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001819 options->control_path = NULL;
1820 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001821 options->control_persist = -1;
1822 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001823 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001824 options->tun_open = -1;
1825 options->tun_local = -1;
1826 options->tun_remote = -1;
1827 options->local_command = NULL;
1828 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001829 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001830 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001831 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001832 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001833 options->ip_qos_interactive = -1;
1834 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001835 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001836 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001837 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001838 options->num_canonical_domains = 0;
1839 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001840 options->canonicalize_max_dots = -1;
1841 options->canonicalize_fallback_local = -1;
1842 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001843 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001844 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001845 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001846 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001847 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848}
1849
Damien Miller5428f641999-11-25 11:54:57 +11001850/*
Damien Miller13f97b22014-02-24 15:57:55 +11001851 * A petite version of fill_default_options() that just fills the options
1852 * needed for hostname canonicalization to proceed.
1853 */
1854void
1855fill_default_options_for_canonicalization(Options *options)
1856{
1857 if (options->canonicalize_max_dots == -1)
1858 options->canonicalize_max_dots = 1;
1859 if (options->canonicalize_fallback_local == -1)
1860 options->canonicalize_fallback_local = 1;
1861 if (options->canonicalize_hostname == -1)
1862 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1863}
1864
1865/*
Damien Miller5428f641999-11-25 11:54:57 +11001866 * Called after processing other sources of option data, this fills those
1867 * options for which no value has been specified with their default values.
1868 */
Damien Miller4af51302000-04-16 11:18:38 +10001869void
Damien Miller95def091999-11-25 00:26:21 +11001870fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001871{
Damien Miller95def091999-11-25 00:26:21 +11001872 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001873 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001874 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001875 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001876 if (options->forward_x11_trusted == -1)
1877 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001878 if (options->forward_x11_timeout == -1)
1879 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001880 /*
1881 * stdio forwarding (-W) changes the default for these but we defer
1882 * setting the values so they can be overridden.
1883 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001884 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001885 options->exit_on_forward_failure =
1886 options->stdio_forward_host != NULL ? 1 : 0;
1887 if (options->clear_forwardings == -1)
1888 options->clear_forwardings =
1889 options->stdio_forward_host != NULL ? 1 : 0;
1890 if (options->clear_forwardings == 1)
1891 clear_forwardings(options);
1892
Damien Millerd3a18572000-06-07 19:55:44 +10001893 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001894 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001895 if (options->fwd_opts.gateway_ports == -1)
1896 options->fwd_opts.gateway_ports = 0;
1897 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1898 options->fwd_opts.streamlocal_bind_mask = 0177;
1899 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1900 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001901 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001902 options->use_privileged_port = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 if (options->pubkey_authentication == -1)
1904 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001905 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001906 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001907 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001908 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001909 if (options->gss_deleg_creds == -1)
1910 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001911 if (options->password_authentication == -1)
1912 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001913 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001914 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001915 if (options->hostbased_authentication == -1)
1916 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001917 if (options->batch_mode == -1)
1918 options->batch_mode = 0;
1919 if (options->check_host_ip == -1)
1920 options->check_host_ip = 1;
1921 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001922 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001923 if (options->compression == -1)
1924 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001925 if (options->tcp_keep_alive == -1)
1926 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001927 if (options->port == -1)
1928 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001929 if (options->address_family == -1)
1930 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001931 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001932 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001933 if (options->number_of_password_prompts == -1)
1934 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001935 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001936 if (options->add_keys_to_agent == -1)
1937 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001938 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001939 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1940 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001941#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001942 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001943#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001944 add_identity_file(options, "~/",
1945 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10001946 }
Damien Miller95def091999-11-25 00:26:21 +11001947 if (options->escape_char == -1)
1948 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001949 if (options->num_system_hostfiles == 0) {
1950 options->system_hostfiles[options->num_system_hostfiles++] =
1951 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1952 options->system_hostfiles[options->num_system_hostfiles++] =
1953 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1954 }
1955 if (options->num_user_hostfiles == 0) {
1956 options->user_hostfiles[options->num_user_hostfiles++] =
1957 xstrdup(_PATH_SSH_USER_HOSTFILE);
1958 options->user_hostfiles[options->num_user_hostfiles++] =
1959 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1960 }
Damien Millerfcd93202002-02-05 12:26:34 +11001961 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001962 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001963 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1964 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001965 if (options->no_host_authentication_for_localhost == - 1)
1966 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001967 if (options->identities_only == -1)
1968 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001969 if (options->enable_ssh_keysign == -1)
1970 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001971 if (options->rekey_limit == -1)
1972 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001973 if (options->rekey_interval == -1)
1974 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001975 if (options->verify_host_key_dns == -1)
1976 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001977 if (options->server_alive_interval == -1)
1978 options->server_alive_interval = 0;
1979 if (options->server_alive_count_max == -1)
1980 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001981 if (options->control_master == -1)
1982 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001983 if (options->control_persist == -1) {
1984 options->control_persist = 0;
1985 options->control_persist_timeout = 0;
1986 }
Damien Millere1776152005-03-01 21:47:37 +11001987 if (options->hash_known_hosts == -1)
1988 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001989 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001990 options->tun_open = SSH_TUNMODE_NO;
1991 if (options->tun_local == -1)
1992 options->tun_local = SSH_TUNID_ANY;
1993 if (options->tun_remote == -1)
1994 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001995 if (options->permit_local_command == -1)
1996 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001997 if (options->visual_host_key == -1)
1998 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001999 if (options->ip_qos_interactive == -1)
2000 options->ip_qos_interactive = IPTOS_LOWDELAY;
2001 if (options->ip_qos_bulk == -1)
2002 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002003 if (options->request_tty == -1)
2004 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002005 if (options->proxy_use_fdpass == -1)
2006 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002007 if (options->canonicalize_max_dots == -1)
2008 options->canonicalize_max_dots = 1;
2009 if (options->canonicalize_fallback_local == -1)
2010 options->canonicalize_fallback_local = 1;
2011 if (options->canonicalize_hostname == -1)
2012 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002013 if (options->fingerprint_hash == -1)
2014 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002015 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002016 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002017 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2018 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2019 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2020 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2021 &options->hostbased_key_types) != 0 ||
2022 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2023 &options->pubkey_key_types) != 0)
2024 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002025
Damien Millere9fc72e2013-10-15 12:14:12 +11002026#define CLEAR_ON_NONE(v) \
2027 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002028 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002029 free(v); \
2030 v = NULL; \
2031 } \
2032 } while(0)
2033 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002034 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002035 CLEAR_ON_NONE(options->proxy_command);
2036 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002037 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002038 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002039 /* options->user will be set in the main program if appropriate */
2040 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002041 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002042 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002043}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002044
Damien Miller7acefbb2014-07-18 14:11:24 +10002045struct fwdarg {
2046 char *arg;
2047 int ispath;
2048};
2049
2050/*
2051 * parse_fwd_field
2052 * parses the next field in a port forwarding specification.
2053 * sets fwd to the parsed field and advances p past the colon
2054 * or sets it to NULL at end of string.
2055 * returns 0 on success, else non-zero.
2056 */
2057static int
2058parse_fwd_field(char **p, struct fwdarg *fwd)
2059{
2060 char *ep, *cp = *p;
2061 int ispath = 0;
2062
2063 if (*cp == '\0') {
2064 *p = NULL;
2065 return -1; /* end of string */
2066 }
2067
2068 /*
2069 * A field escaped with square brackets is used literally.
2070 * XXX - allow ']' to be escaped via backslash?
2071 */
2072 if (*cp == '[') {
2073 /* find matching ']' */
2074 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2075 if (*ep == '/')
2076 ispath = 1;
2077 }
2078 /* no matching ']' or not at end of field. */
2079 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2080 return -1;
2081 /* NUL terminate the field and advance p past the colon */
2082 *ep++ = '\0';
2083 if (*ep != '\0')
2084 *ep++ = '\0';
2085 fwd->arg = cp + 1;
2086 fwd->ispath = ispath;
2087 *p = ep;
2088 return 0;
2089 }
2090
2091 for (cp = *p; *cp != '\0'; cp++) {
2092 switch (*cp) {
2093 case '\\':
2094 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002095 if (*cp == '\0')
2096 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002097 break;
2098 case '/':
2099 ispath = 1;
2100 break;
2101 case ':':
2102 *cp++ = '\0';
2103 goto done;
2104 }
2105 }
2106done:
2107 fwd->arg = *p;
2108 fwd->ispath = ispath;
2109 *p = cp;
2110 return 0;
2111}
2112
Damien Millerf91ee4c2005-03-01 21:24:33 +11002113/*
2114 * parse_forward
2115 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002116 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002117 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2118 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002119 * dynamicfwd == 1
2120 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002121 * returns number of arguments parsed or zero on error
2122 */
2123int
Damien Miller7acefbb2014-07-18 14:11:24 +10002124parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002125{
Damien Miller7acefbb2014-07-18 14:11:24 +10002126 struct fwdarg fwdargs[4];
2127 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002128 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002129
Damien Miller7acefbb2014-07-18 14:11:24 +10002130 memset(fwd, 0, sizeof(*fwd));
2131 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002132
2133 cp = p = xstrdup(fwdspec);
2134
2135 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002136 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002137 cp++;
2138
Damien Miller7acefbb2014-07-18 14:11:24 +10002139 for (i = 0; i < 4; ++i) {
2140 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002141 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002142 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002143
Damien Millerf4b39532008-11-03 19:28:21 +11002144 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002145 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002146 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002147 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002148
2149 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002150 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002151 if (fwdargs[0].ispath) {
2152 fwd->listen_path = xstrdup(fwdargs[0].arg);
2153 fwd->listen_port = PORT_STREAMLOCAL;
2154 } else {
2155 fwd->listen_host = NULL;
2156 fwd->listen_port = a2port(fwdargs[0].arg);
2157 }
Damien Millera699d952008-11-03 19:27:34 +11002158 fwd->connect_host = xstrdup("socks");
2159 break;
2160
2161 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002162 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2163 fwd->listen_path = xstrdup(fwdargs[0].arg);
2164 fwd->listen_port = PORT_STREAMLOCAL;
2165 fwd->connect_path = xstrdup(fwdargs[1].arg);
2166 fwd->connect_port = PORT_STREAMLOCAL;
2167 } else if (fwdargs[1].ispath) {
2168 fwd->listen_host = NULL;
2169 fwd->listen_port = a2port(fwdargs[0].arg);
2170 fwd->connect_path = xstrdup(fwdargs[1].arg);
2171 fwd->connect_port = PORT_STREAMLOCAL;
2172 } else {
2173 fwd->listen_host = xstrdup(fwdargs[0].arg);
2174 fwd->listen_port = a2port(fwdargs[1].arg);
2175 fwd->connect_host = xstrdup("socks");
2176 }
Damien Millera699d952008-11-03 19:27:34 +11002177 break;
2178
Damien Millerf91ee4c2005-03-01 21:24:33 +11002179 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002180 if (fwdargs[0].ispath) {
2181 fwd->listen_path = xstrdup(fwdargs[0].arg);
2182 fwd->listen_port = PORT_STREAMLOCAL;
2183 fwd->connect_host = xstrdup(fwdargs[1].arg);
2184 fwd->connect_port = a2port(fwdargs[2].arg);
2185 } else if (fwdargs[2].ispath) {
2186 fwd->listen_host = xstrdup(fwdargs[0].arg);
2187 fwd->listen_port = a2port(fwdargs[1].arg);
2188 fwd->connect_path = xstrdup(fwdargs[2].arg);
2189 fwd->connect_port = PORT_STREAMLOCAL;
2190 } else {
2191 fwd->listen_host = NULL;
2192 fwd->listen_port = a2port(fwdargs[0].arg);
2193 fwd->connect_host = xstrdup(fwdargs[1].arg);
2194 fwd->connect_port = a2port(fwdargs[2].arg);
2195 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002196 break;
2197
2198 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002199 fwd->listen_host = xstrdup(fwdargs[0].arg);
2200 fwd->listen_port = a2port(fwdargs[1].arg);
2201 fwd->connect_host = xstrdup(fwdargs[2].arg);
2202 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002203 break;
2204 default:
2205 i = 0; /* failure */
2206 }
2207
Darren Tuckera627d422013-06-02 07:31:17 +10002208 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002209
Damien Millera699d952008-11-03 19:27:34 +11002210 if (dynamicfwd) {
2211 if (!(i == 1 || i == 2))
2212 goto fail_free;
2213 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002214 if (!(i == 3 || i == 4)) {
2215 if (fwd->connect_path == NULL &&
2216 fwd->listen_path == NULL)
2217 goto fail_free;
2218 }
2219 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002220 goto fail_free;
2221 }
2222
Damien Miller7acefbb2014-07-18 14:11:24 +10002223 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2224 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002225 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002226 if (fwd->connect_host != NULL &&
2227 strlen(fwd->connect_host) >= NI_MAXHOST)
2228 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002229 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2230 if (fwd->connect_path != NULL &&
2231 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2232 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002233 if (fwd->listen_host != NULL &&
2234 strlen(fwd->listen_host) >= NI_MAXHOST)
2235 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002236 if (fwd->listen_path != NULL &&
2237 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2238 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002239
2240 return (i);
2241
2242 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002243 free(fwd->connect_host);
2244 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002245 free(fwd->connect_path);
2246 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002247 free(fwd->listen_host);
2248 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002249 free(fwd->listen_path);
2250 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002251 return (0);
2252}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002253
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002254int
2255parse_jump(const char *s, Options *o, int active)
2256{
2257 char *orig, *sdup, *cp;
2258 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002259 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002260
2261 active &= o->proxy_command == NULL && o->jump_host == NULL;
2262
2263 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002264 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002265 do {
2266 if ((cp = strrchr(sdup, ',')) == NULL)
2267 cp = sdup; /* last */
2268 else
2269 *cp++ = '\0';
2270
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002271 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002272 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002273 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2274 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002275 goto out;
2276 } else {
2277 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002278 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2279 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002280 goto out;
2281 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002282 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002283 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002284 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002285 if (active) {
2286 o->jump_user = user;
2287 o->jump_host = host;
2288 o->jump_port = port;
2289 o->proxy_command = xstrdup("none");
2290 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002291 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2292 o->jump_extra = xstrdup(s);
2293 o->jump_extra[cp - s] = '\0';
2294 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002295 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002296 ret = 0;
2297 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002298 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002299 free(user);
2300 free(host);
2301 return ret;
2302}
2303
millert@openbsd.org887669e2017-10-21 23:06:24 +00002304int
2305parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2306{
2307 char *path;
2308 int r;
2309
2310 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2311 if (r == 0 && path != NULL)
2312 r = -1; /* path not allowed */
2313 return r;
2314}
2315
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002316/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2317static const char *
2318fmt_multistate_int(int val, const struct multistate *m)
2319{
2320 u_int i;
2321
2322 for (i = 0; m[i].key != NULL; i++) {
2323 if (m[i].value == val)
2324 return m[i].key;
2325 }
2326 return "UNKNOWN";
2327}
2328
2329static const char *
2330fmt_intarg(OpCodes code, int val)
2331{
2332 if (val == -1)
2333 return "unset";
2334 switch (code) {
2335 case oAddressFamily:
2336 return fmt_multistate_int(val, multistate_addressfamily);
2337 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002338 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002339 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002340 case oStrictHostKeyChecking:
2341 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002342 case oControlMaster:
2343 return fmt_multistate_int(val, multistate_controlmaster);
2344 case oTunnel:
2345 return fmt_multistate_int(val, multistate_tunnel);
2346 case oRequestTTY:
2347 return fmt_multistate_int(val, multistate_requesttty);
2348 case oCanonicalizeHostname:
2349 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002350 case oFingerprintHash:
2351 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002352 default:
2353 switch (val) {
2354 case 0:
2355 return "no";
2356 case 1:
2357 return "yes";
2358 default:
2359 return "UNKNOWN";
2360 }
2361 }
2362}
2363
2364static const char *
2365lookup_opcode_name(OpCodes code)
2366{
2367 u_int i;
2368
2369 for (i = 0; keywords[i].name != NULL; i++)
2370 if (keywords[i].opcode == code)
2371 return(keywords[i].name);
2372 return "UNKNOWN";
2373}
2374
2375static void
2376dump_cfg_int(OpCodes code, int val)
2377{
2378 printf("%s %d\n", lookup_opcode_name(code), val);
2379}
2380
2381static void
2382dump_cfg_fmtint(OpCodes code, int val)
2383{
2384 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2385}
2386
2387static void
2388dump_cfg_string(OpCodes code, const char *val)
2389{
2390 if (val == NULL)
2391 return;
2392 printf("%s %s\n", lookup_opcode_name(code), val);
2393}
2394
2395static void
2396dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2397{
2398 u_int i;
2399
2400 for (i = 0; i < count; i++)
2401 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2402}
2403
2404static void
2405dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2406{
2407 u_int i;
2408
2409 printf("%s", lookup_opcode_name(code));
2410 for (i = 0; i < count; i++)
2411 printf(" %s", vals[i]);
2412 printf("\n");
2413}
2414
2415static void
2416dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2417{
2418 const struct Forward *fwd;
2419 u_int i;
2420
2421 /* oDynamicForward */
2422 for (i = 0; i < count; i++) {
2423 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002424 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002425 strcmp(fwd->connect_host, "socks") != 0)
2426 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002427 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002428 strcmp(fwd->connect_host, "socks") == 0)
2429 continue;
2430 printf("%s", lookup_opcode_name(code));
2431 if (fwd->listen_port == PORT_STREAMLOCAL)
2432 printf(" %s", fwd->listen_path);
2433 else if (fwd->listen_host == NULL)
2434 printf(" %d", fwd->listen_port);
2435 else {
2436 printf(" [%s]:%d",
2437 fwd->listen_host, fwd->listen_port);
2438 }
2439 if (code != oDynamicForward) {
2440 if (fwd->connect_port == PORT_STREAMLOCAL)
2441 printf(" %s", fwd->connect_path);
2442 else if (fwd->connect_host == NULL)
2443 printf(" %d", fwd->connect_port);
2444 else {
2445 printf(" [%s]:%d",
2446 fwd->connect_host, fwd->connect_port);
2447 }
2448 }
2449 printf("\n");
2450 }
2451}
2452
2453void
2454dump_client_config(Options *o, const char *host)
2455{
2456 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002457 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002458
djm@openbsd.org60a92472015-08-21 23:53:08 +00002459 /* This is normally prepared in ssh_kex2 */
2460 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2461 fatal("%s: kex_assemble_names failed", __func__);
2462
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002463 /* Most interesting options first: user, host, port */
2464 dump_cfg_string(oUser, o->user);
2465 dump_cfg_string(oHostName, host);
2466 dump_cfg_int(oPort, o->port);
2467
2468 /* Flag options */
2469 dump_cfg_fmtint(oAddressFamily, o->address_family);
2470 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2471 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2472 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2473 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2474 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2475 dump_cfg_fmtint(oCompression, o->compression);
2476 dump_cfg_fmtint(oControlMaster, o->control_master);
2477 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002478 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002479 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002480 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002481 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2482 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2483 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2484 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2485#ifdef GSSAPI
2486 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2487 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2488#endif /* GSSAPI */
2489 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2490 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2491 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2492 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2493 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2494 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2495 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002496 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2497 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2498 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002499 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2500 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2501 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2502 dump_cfg_fmtint(oTunnel, o->tun_open);
2503 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2504 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2505 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002506 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002507
2508 /* Integer options */
2509 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002510 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2511 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2512 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2513 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2514 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2515
2516 /* String options */
2517 dump_cfg_string(oBindAddress, o->bind_address);
2518 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2519 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002520 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002521 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002522 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002523 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002524 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2525 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2526 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002527 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002528 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2529 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002530#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002531 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002532#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002533 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002534 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002535 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002536 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002537
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002538 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002539 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2540 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2541 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2542
2543 /* String array options */
2544 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2545 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2546 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2547 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2548 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2549
2550 /* Special cases */
2551
2552 /* oConnectTimeout */
2553 if (o->connection_timeout == -1)
2554 printf("connecttimeout none\n");
2555 else
2556 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2557
2558 /* oTunnelDevice */
2559 printf("tunneldevice");
2560 if (o->tun_local == SSH_TUNID_ANY)
2561 printf(" any");
2562 else
2563 printf(" %d", o->tun_local);
2564 if (o->tun_remote == SSH_TUNID_ANY)
2565 printf(":any");
2566 else
2567 printf(":%d", o->tun_remote);
2568 printf("\n");
2569
2570 /* oCanonicalizePermittedCNAMEs */
2571 if ( o->num_permitted_cnames > 0) {
2572 printf("canonicalizePermittedcnames");
2573 for (i = 0; i < o->num_permitted_cnames; i++) {
2574 printf(" %s:%s", o->permitted_cnames[i].source_list,
2575 o->permitted_cnames[i].target_list);
2576 }
2577 printf("\n");
2578 }
2579
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002580 /* oControlPersist */
2581 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2582 dump_cfg_fmtint(oControlPersist, o->control_persist);
2583 else
2584 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2585
2586 /* oEscapeChar */
2587 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2588 printf("escapechar none\n");
2589 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002590 vis(buf, o->escape_char, VIS_WHITE, 0);
2591 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002592 }
2593
2594 /* oIPQoS */
2595 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2596 printf("%s\n", iptos2str(o->ip_qos_bulk));
2597
2598 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002599 printf("rekeylimit %llu %d\n",
2600 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002601
2602 /* oStreamLocalBindMask */
2603 printf("streamlocalbindmask 0%o\n",
2604 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002605
2606 /* oProxyCommand / oProxyJump */
2607 if (o->jump_host == NULL)
2608 dump_cfg_string(oProxyCommand, o->proxy_command);
2609 else {
2610 /* Check for numeric addresses */
2611 i = strchr(o->jump_host, ':') != NULL ||
2612 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2613 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2614 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002615 /* optional additional jump spec */
2616 o->jump_extra == NULL ? "" : o->jump_extra,
2617 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002618 /* optional user */
2619 o->jump_user == NULL ? "" : o->jump_user,
2620 o->jump_user == NULL ? "" : "@",
2621 /* opening [ if hostname is numeric */
2622 i ? "[" : "",
2623 /* mandatory hostname */
2624 o->jump_host,
2625 /* closing ] if hostname is numeric */
2626 i ? "]" : "",
2627 /* optional port number */
2628 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002629 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002630 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002631}