blob: 9c4a234b5ed956447979115f91389efecca3a131 [file] [log] [blame]
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001/* $OpenBSD: readconf.c,v 1.288 2018/06/01 03:33:53 djm 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,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000159 oHostKeyAlgorithms, oBindAddress, oBindInterface, 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 },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000269 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100270 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000271 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000272 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100273 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000274 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000275 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000276 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100277 { "serveraliveinterval", oServerAliveInterval },
278 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000279 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000280 { "controlpath", oControlPath },
281 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000282 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100283 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000284 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100285 { "tunnel", oTunnel },
286 { "tunneldevice", oTunnelDevice },
287 { "localcommand", oLocalCommand },
288 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000289 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000290 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000291 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100292 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000293 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000294 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100295 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100296 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
297 { "canonicalizehostname", oCanonicalizeHostname },
298 { "canonicalizemaxdots", oCanonicalizeMaxDots },
299 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000300 { "streamlocalbindmask", oStreamLocalBindMask },
301 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000302 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000303 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000304 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000305 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000306 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000307 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000308 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100309
Ben Lindstrom65366a82001-12-06 16:32:47 +0000310 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100311};
312
Damien Miller5428f641999-11-25 11:54:57 +1100313/*
314 * Adds a local TCP/IP port forward to options. Never returns if there is an
315 * error.
316 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller4af51302000-04-16 11:18:38 +1000318void
Damien Miller7acefbb2014-07-18 14:11:24 +1000319add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Damien Miller7acefbb2014-07-18 14:11:24 +1000321 struct Forward *fwd;
Damien Miller95def091999-11-25 00:26:21 +1100322 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000323 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000324
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000325 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000326 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000327 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000328 /* Don't add duplicates */
329 for (i = 0; i < options->num_local_forwards; i++) {
330 if (forward_equals(newfwd, options->local_forwards + i))
331 return;
332 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000333 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000334 options->num_local_forwards + 1,
335 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100336 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337
Damien Miller1a0442f2008-11-05 16:30:06 +1100338 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100341 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100342 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000343 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344}
345
Damien Miller5428f641999-11-25 11:54:57 +1100346/*
347 * Adds a remote TCP/IP port forward to options. Never returns if there is
348 * an error.
349 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller4af51302000-04-16 11:18:38 +1000351void
Damien Miller7acefbb2014-07-18 14:11:24 +1000352add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller7acefbb2014-07-18 14:11:24 +1000354 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000355 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000356
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000357 /* Don't add duplicates */
358 for (i = 0; i < options->num_remote_forwards; i++) {
359 if (forward_equals(newfwd, options->remote_forwards + i))
360 return;
361 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000362 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000363 options->num_remote_forwards + 1,
364 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100365 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100366
Damien Miller1a0442f2008-11-05 16:30:06 +1100367 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000369 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100370 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100371 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000372 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100373 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000374 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375}
376
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000377static void
378clear_forwardings(Options *options)
379{
380 int i;
381
Damien Millerf91ee4c2005-03-01 21:24:33 +1100382 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000384 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000386 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100387 }
Damien Miller232cfb12010-06-26 09:50:30 +1000388 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000390 options->local_forwards = NULL;
391 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000392 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100393 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000394 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000395 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100398 }
Damien Miller232cfb12010-06-26 09:50:30 +1000399 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000400 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000401 options->remote_forwards = NULL;
402 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000403 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100404 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405}
406
Darren Tucker19104782013-04-05 11:13:08 +1100407void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000408add_certificate_file(Options *options, const char *path, int userprovided)
409{
410 int i;
411
412 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
413 fatal("Too many certificate files specified (max %d)",
414 SSH_MAX_CERTIFICATE_FILES);
415
416 /* Avoid registering duplicates */
417 for (i = 0; i < options->num_certificate_files; i++) {
418 if (options->certificate_file_userprovided[i] == userprovided &&
419 strcmp(options->certificate_files[i], path) == 0) {
420 debug2("%s: ignoring duplicate key %s", __func__, path);
421 return;
422 }
423 }
424
425 options->certificate_file_userprovided[options->num_certificate_files] =
426 userprovided;
427 options->certificate_files[options->num_certificate_files++] =
428 xstrdup(path);
429}
430
431void
Darren Tucker19104782013-04-05 11:13:08 +1100432add_identity_file(Options *options, const char *dir, const char *filename,
433 int userprovided)
434{
435 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000436 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100437
438 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
439 fatal("Too many identity files specified (max %d)",
440 SSH_MAX_IDENTITY_FILES);
441
442 if (dir == NULL) /* no dir, filename is absolute */
443 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000444 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
445 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100446
Damien Miller15271902014-05-15 13:47:56 +1000447 /* Avoid registering duplicates */
448 for (i = 0; i < options->num_identity_files; i++) {
449 if (options->identity_file_userprovided[i] == userprovided &&
450 strcmp(options->identity_files[i], path) == 0) {
451 debug2("%s: ignoring duplicate key %s", __func__, path);
452 free(path);
453 return;
454 }
455 }
456
Darren Tucker19104782013-04-05 11:13:08 +1100457 options->identity_file_userprovided[options->num_identity_files] =
458 userprovided;
459 options->identity_files[options->num_identity_files++] = path;
460}
461
Damien Miller194fd902013-10-15 12:13:05 +1100462int
463default_ssh_port(void)
464{
465 static int port;
466 struct servent *sp;
467
468 if (port == 0) {
469 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
470 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
471 }
472 return port;
473}
474
475/*
476 * Execute a command in a shell.
477 * Return its exit status or -1 on abnormal exit.
478 */
479static int
480execute_in_shell(const char *cmd)
481{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000482 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100483 pid_t pid;
484 int devnull, status;
485 extern uid_t original_real_uid;
486
487 if ((shell = getenv("SHELL")) == NULL)
488 shell = _PATH_BSHELL;
489
Damien Miller194fd902013-10-15 12:13:05 +1100490 /* Need this to redirect subprocess stdin/out */
491 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
492 fatal("open(/dev/null): %s", strerror(errno));
493
494 debug("Executing command: '%.500s'", cmd);
495
496 /* Fork and execute the command. */
497 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000498 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100499
500 /* Child. Permanently give up superuser privileges. */
501 permanently_drop_suid(original_real_uid);
502
503 /* Redirect child stdin and stdout. Leave stderr */
504 if (dup2(devnull, STDIN_FILENO) == -1)
505 fatal("dup2: %s", strerror(errno));
506 if (dup2(devnull, STDOUT_FILENO) == -1)
507 fatal("dup2: %s", strerror(errno));
508 if (devnull > STDERR_FILENO)
509 close(devnull);
510 closefrom(STDERR_FILENO + 1);
511
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000512 argv[0] = shell;
513 argv[1] = "-c";
514 argv[2] = xstrdup(cmd);
515 argv[3] = NULL;
516
Damien Miller194fd902013-10-15 12:13:05 +1100517 execv(argv[0], argv);
518 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
519 /* Die with signal to make this error apparent to parent. */
520 signal(SIGTERM, SIG_DFL);
521 kill(getpid(), SIGTERM);
522 _exit(1);
523 }
524 /* Parent. */
525 if (pid < 0)
526 fatal("%s: fork: %.100s", __func__, strerror(errno));
527
528 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100529
530 while (waitpid(pid, &status, 0) == -1) {
531 if (errno != EINTR && errno != EAGAIN)
532 fatal("%s: waitpid: %s", __func__, strerror(errno));
533 }
534 if (!WIFEXITED(status)) {
535 error("command '%.100s' exited abnormally", cmd);
536 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000537 }
Damien Miller194fd902013-10-15 12:13:05 +1100538 debug3("command returned status %d", WEXITSTATUS(status));
539 return WEXITSTATUS(status);
540}
541
542/*
543 * Parse and execute a Match directive.
544 */
545static int
546match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000547 const char *host_arg, const char *original_host, int post_canon,
548 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100549{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100551 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000552 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100553 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000554 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100555
556 /*
557 * Configuration is likely to be incomplete at this point so we
558 * must be prepared to use default values.
559 */
560 port = options->port <= 0 ? default_ssh_port() : options->port;
561 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000562 if (post_canon) {
563 host = xstrdup(options->hostname);
564 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100565 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100566 host = percent_expand(options->hostname,
567 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000568 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100569 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000570 }
Damien Miller194fd902013-10-15 12:13:05 +1100571
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000572 debug2("checking match for '%s' host %s originally %s",
573 cp, host, original_host);
574 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
575 criteria = NULL;
576 this_result = 1;
577 if ((negate = attrib[0] == '!'))
578 attrib++;
579 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100580 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100582 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 error("%.200s line %d: '%s' cannot be combined "
584 "with other Match attributes",
585 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100586 result = -1;
587 goto out;
588 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000589 if (result)
590 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100591 goto out;
592 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000593 attributes++;
594 if (strcasecmp(attrib, "canonical") == 0) {
595 r = !!post_canon; /* force bitmask member to boolean */
596 if (r == (negate ? 1 : 0))
597 this_result = result = 0;
598 debug3("%.200s line %d: %smatched '%s'",
599 filename, linenum,
600 this_result ? "" : "not ", oattrib);
601 continue;
602 }
603 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100604 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
605 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100606 result = -1;
607 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100608 }
Damien Miller194fd902013-10-15 12:13:05 +1100609 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000611 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000612 if (r == (negate ? 1 : 0))
613 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100614 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000616 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000617 if (r == (negate ? 1 : 0))
618 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100619 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000621 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000622 if (r == (negate ? 1 : 0))
623 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100624 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000625 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000626 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000627 if (r == (negate ? 1 : 0))
628 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100629 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100630 if (gethostname(thishost, sizeof(thishost)) == -1)
631 fatal("gethostname: %s", strerror(errno));
632 strlcpy(shorthost, thishost, sizeof(shorthost));
633 shorthost[strcspn(thishost, ".")] = '\0';
634 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000635 snprintf(uidstr, sizeof(uidstr), "%llu",
636 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100637
638 cmd = percent_expand(arg,
639 "L", shorthost,
640 "d", pw->pw_dir,
641 "h", host,
642 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000643 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100644 "p", portstr,
645 "r", ruser,
646 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000647 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100648 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100649 if (result != 1) {
650 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000651 debug3("%.200s line %d: skipped exec "
652 "\"%.100s\"", filename, linenum, cmd);
653 free(cmd);
654 continue;
Damien Miller06287802014-02-24 15:56:45 +1100655 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000656 r = execute_in_shell(cmd);
657 if (r == -1) {
658 fatal("%.200s line %d: match exec "
659 "'%.100s' error", filename,
660 linenum, cmd);
661 }
662 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100663 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000664 /* Force exit status to boolean */
665 r = r == 0;
666 if (r == (negate ? 1 : 0))
667 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100668 } else {
669 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100670 result = -1;
671 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100672 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000673 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
674 filename, linenum, this_result ? "": "not ",
675 oattrib, criteria);
676 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100677 }
Damien Millercf31f382013-10-24 21:02:56 +1100678 if (attributes == 0) {
679 error("One or more attributes required for Match");
680 result = -1;
681 goto out;
682 }
Damien Miller084bcd22013-10-23 16:30:51 +1100683 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000684 if (result != -1)
685 debug2("match %sfound", result ? "" : "not ");
686 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100687 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100688 return result;
689}
690
djm@openbsd.org555294a2018-04-06 13:02:39 +0000691/* Remove environment variable by pattern */
692static void
693rm_env(Options *options, const char *arg, const char *filename, int linenum)
694{
695 int i, j;
696 char *cp;
697
698 /* Remove an environment variable */
699 for (i = 0; i < options->num_send_env; ) {
700 cp = xstrdup(options->send_env[i]);
701 if (!match_pattern(cp, arg + 1)) {
702 free(cp);
703 i++;
704 continue;
705 }
706 debug3("%s line %d: removing environment %s",
707 filename, linenum, cp);
708 free(cp);
709 free(options->send_env[i]);
710 options->send_env[i] = NULL;
711 for (j = i; j < options->num_send_env - 1; j++) {
712 options->send_env[j] = options->send_env[j + 1];
713 options->send_env[j + 1] = NULL;
714 }
715 options->num_send_env--;
716 /* NB. don't increment i */
717 }
718}
719
Damien Miller5428f641999-11-25 11:54:57 +1100720/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000721 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100722 */
Damien Miller4af51302000-04-16 11:18:38 +1000723static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000724parse_token(const char *cp, const char *filename, int linenum,
725 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726{
Darren Tucker07636982013-05-16 20:30:03 +1000727 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Damien Miller95def091999-11-25 00:26:21 +1100729 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000730 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100731 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000732 if (ignored_unknown != NULL &&
733 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000734 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000735 error("%s: line %d: Bad configuration option: %s",
736 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100737 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738}
739
Damien Millere9fc72e2013-10-15 12:14:12 +1100740/* Multistate option parsing */
741struct multistate {
742 char *key;
743 int value;
744};
745static const struct multistate multistate_flag[] = {
746 { "true", 1 },
747 { "false", 0 },
748 { "yes", 1 },
749 { "no", 0 },
750 { NULL, -1 }
751};
752static const struct multistate multistate_yesnoask[] = {
753 { "true", 1 },
754 { "false", 0 },
755 { "yes", 1 },
756 { "no", 0 },
757 { "ask", 2 },
758 { NULL, -1 }
759};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000760static const struct multistate multistate_strict_hostkey[] = {
761 { "true", SSH_STRICT_HOSTKEY_YES },
762 { "false", SSH_STRICT_HOSTKEY_OFF },
763 { "yes", SSH_STRICT_HOSTKEY_YES },
764 { "no", SSH_STRICT_HOSTKEY_OFF },
765 { "ask", SSH_STRICT_HOSTKEY_ASK },
766 { "off", SSH_STRICT_HOSTKEY_OFF },
767 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
768 { NULL, -1 }
769};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000770static const struct multistate multistate_yesnoaskconfirm[] = {
771 { "true", 1 },
772 { "false", 0 },
773 { "yes", 1 },
774 { "no", 0 },
775 { "ask", 2 },
776 { "confirm", 3 },
777 { NULL, -1 }
778};
Damien Millere9fc72e2013-10-15 12:14:12 +1100779static const struct multistate multistate_addressfamily[] = {
780 { "inet", AF_INET },
781 { "inet6", AF_INET6 },
782 { "any", AF_UNSPEC },
783 { NULL, -1 }
784};
785static const struct multistate multistate_controlmaster[] = {
786 { "true", SSHCTL_MASTER_YES },
787 { "yes", SSHCTL_MASTER_YES },
788 { "false", SSHCTL_MASTER_NO },
789 { "no", SSHCTL_MASTER_NO },
790 { "auto", SSHCTL_MASTER_AUTO },
791 { "ask", SSHCTL_MASTER_ASK },
792 { "autoask", SSHCTL_MASTER_AUTO_ASK },
793 { NULL, -1 }
794};
795static const struct multistate multistate_tunnel[] = {
796 { "ethernet", SSH_TUNMODE_ETHERNET },
797 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
798 { "true", SSH_TUNMODE_DEFAULT },
799 { "yes", SSH_TUNMODE_DEFAULT },
800 { "false", SSH_TUNMODE_NO },
801 { "no", SSH_TUNMODE_NO },
802 { NULL, -1 }
803};
804static const struct multistate multistate_requesttty[] = {
805 { "true", REQUEST_TTY_YES },
806 { "yes", REQUEST_TTY_YES },
807 { "false", REQUEST_TTY_NO },
808 { "no", REQUEST_TTY_NO },
809 { "force", REQUEST_TTY_FORCE },
810 { "auto", REQUEST_TTY_AUTO },
811 { NULL, -1 }
812};
Damien Miller38505592013-10-17 11:48:13 +1100813static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100814 { "true", SSH_CANONICALISE_YES },
815 { "false", SSH_CANONICALISE_NO },
816 { "yes", SSH_CANONICALISE_YES },
817 { "no", SSH_CANONICALISE_NO },
818 { "always", SSH_CANONICALISE_ALWAYS },
819 { NULL, -1 }
820};
Damien Millere9fc72e2013-10-15 12:14:12 +1100821
Damien Miller5428f641999-11-25 11:54:57 +1100822/*
823 * Processes a single option line as used in the configuration files. This
824 * only sets those values that have not already been set.
825 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100826int
Damien Miller194fd902013-10-15 12:13:05 +1100827process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000828 const char *original_host, char *line, const char *filename,
829 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000831 return process_config_line_depth(options, pw, host, original_host,
832 line, filename, linenum, activep, flags, 0);
833}
834
835#define WHITESPACE " \t\r\n"
836static int
837process_config_line_depth(Options *options, struct passwd *pw, const char *host,
838 const char *original_host, char *line, const char *filename,
839 int linenum, int *activep, int flags, int depth)
840{
Damien Miller295ee632011-05-29 21:42:31 +1000841 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
842 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000843 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000844 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000845 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100846 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000847 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000848 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100849 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000850 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100851 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100852 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000853 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000854 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855
Damien Miller194fd902013-10-15 12:13:05 +1100856 if (activep == NULL) { /* We are processing a command line directive */
857 cmdline = 1;
858 activep = &cmdline;
859 }
860
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000861 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000862 if ((len = strlen(line)) == 0)
863 return 0;
864 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000865 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000866 break;
867 line[len] = '\0';
868 }
869
Damien Millerbe484b52000-07-15 14:14:16 +1000870 s = line;
871 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100872 if ((keyword = strdelim(&s)) == NULL)
873 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000874 /* Ignore leading whitespace. */
875 if (*keyword == '\0')
876 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000877 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100878 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000879 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100880 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
Darren Tucker07636982013-05-16 20:30:03 +1000882 opcode = parse_token(keyword, filename, linenum,
883 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Damien Miller95def091999-11-25 00:26:21 +1100885 switch (opcode) {
886 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100887 /* don't panic, but count bad options */
888 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000889 case oIgnore:
890 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000891 case oIgnoredUnknownOption:
892 debug("%s line %d: Ignored unknown option \"%s\"",
893 filename, linenum, keyword);
894 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000895 case oConnectTimeout:
896 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100897parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000898 arg = strdelim(&s);
899 if (!arg || *arg == '\0')
900 fatal("%s line %d: missing time value.",
901 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000902 if (strcmp(arg, "none") == 0)
903 value = -1;
904 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000905 fatal("%s line %d: invalid time value.",
906 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100907 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000908 *intptr = value;
909 break;
910
Damien Miller95def091999-11-25 00:26:21 +1100911 case oForwardAgent:
912 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100913 parse_flag:
914 multistate_ptr = multistate_flag;
915 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000916 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000917 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100918 fatal("%s line %d: missing argument.",
919 filename, linenum);
920 value = -1;
921 for (i = 0; multistate_ptr[i].key != NULL; i++) {
922 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
923 value = multistate_ptr[i].value;
924 break;
925 }
926 }
927 if (value == -1)
928 fatal("%s line %d: unsupported option \"%s\".",
929 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100930 if (*activep && *intptr == -1)
931 *intptr = value;
932 break;
933
934 case oForwardX11:
935 intptr = &options->forward_x11;
936 goto parse_flag;
937
Darren Tucker0a118da2003-10-15 15:54:32 +1000938 case oForwardX11Trusted:
939 intptr = &options->forward_x11_trusted;
940 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000941
Damien Miller1ab6a512010-06-26 10:02:24 +1000942 case oForwardX11Timeout:
943 intptr = &options->forward_x11_timeout;
944 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000945
Damien Miller95def091999-11-25 00:26:21 +1100946 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000947 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100948 goto parse_flag;
949
Darren Tuckere7d4b192006-07-12 22:17:10 +1000950 case oExitOnForwardFailure:
951 intptr = &options->exit_on_forward_failure;
952 goto parse_flag;
953
Damien Miller95def091999-11-25 00:26:21 +1100954 case oUsePrivilegedPort:
955 intptr = &options->use_privileged_port;
956 goto parse_flag;
957
Damien Miller95def091999-11-25 00:26:21 +1100958 case oPasswordAuthentication:
959 intptr = &options->password_authentication;
960 goto parse_flag;
961
Damien Miller874d77b2000-10-14 16:23:11 +1100962 case oKbdInteractiveAuthentication:
963 intptr = &options->kbd_interactive_authentication;
964 goto parse_flag;
965
966 case oKbdInteractiveDevices:
967 charptr = &options->kbd_interactive_devices;
968 goto parse_string;
969
Damien Miller0bc1bd82000-11-13 22:57:25 +1100970 case oPubkeyAuthentication:
971 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000972 goto parse_flag;
973
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000974 case oHostbasedAuthentication:
975 intptr = &options->hostbased_authentication;
976 goto parse_flag;
977
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000978 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000979 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100980 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000981
Darren Tucker0efd1552003-08-26 11:49:55 +1000982 case oGssAuthentication:
983 intptr = &options->gss_authentication;
984 goto parse_flag;
985
986 case oGssDelegateCreds:
987 intptr = &options->gss_deleg_creds;
988 goto parse_flag;
989
Damien Miller95def091999-11-25 00:26:21 +1100990 case oBatchMode:
991 intptr = &options->batch_mode;
992 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 case oCheckHostIP:
995 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000996 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller37876e92003-05-15 10:19:46 +1000998 case oVerifyHostKeyDNS:
999 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001000 multistate_ptr = multistate_yesnoask;
1001 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 case oStrictHostKeyChecking:
1004 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001005 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001006 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007
Damien Miller95def091999-11-25 00:26:21 +11001008 case oCompression:
1009 intptr = &options->compression;
1010 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
Damien Miller12c150e2003-12-17 16:31:10 +11001012 case oTCPKeepAlive:
1013 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001014 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001016 case oNoHostAuthenticationForLocalhost:
1017 intptr = &options->no_host_authentication_for_localhost;
1018 goto parse_flag;
1019
Damien Miller95def091999-11-25 00:26:21 +11001020 case oNumberOfPasswordPrompts:
1021 intptr = &options->number_of_password_prompts;
1022 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Millera5539d22003-04-09 20:50:06 +10001024 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001025 arg = strdelim(&s);
1026 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001027 fatal("%.200s line %d: Missing argument.", filename,
1028 linenum);
1029 if (strcmp(arg, "default") == 0) {
1030 val64 = 0;
1031 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001032 if (scan_scaled(arg, &val64) == -1)
1033 fatal("%.200s line %d: Bad number '%s': %s",
1034 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001035 if (val64 != 0 && val64 < 16)
1036 fatal("%.200s line %d: RekeyLimit too small",
1037 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001038 }
Damien Miller3dff1762008-02-10 22:25:52 +11001039 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001040 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001041 if (s != NULL) { /* optional rekey interval present */
1042 if (strcmp(s, "none") == 0) {
1043 (void)strdelim(&s); /* discard */
1044 break;
1045 }
1046 intptr = &options->rekey_interval;
1047 goto parse_time;
1048 }
Damien Millera5539d22003-04-09 20:50:06 +10001049 break;
1050
Damien Miller95def091999-11-25 00:26:21 +11001051 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001052 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001053 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001054 fatal("%.200s line %d: Missing argument.", filename, linenum);
1055 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001056 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001057 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001058 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001059 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001060 add_identity_file(options, NULL,
1061 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001062 }
1063 break;
1064
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001065 case oCertificateFile:
1066 arg = strdelim(&s);
1067 if (!arg || *arg == '\0')
1068 fatal("%.200s line %d: Missing argument.",
1069 filename, linenum);
1070 if (*activep) {
1071 intptr = &options->num_certificate_files;
1072 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1073 fatal("%.200s line %d: Too many certificate "
1074 "files specified (max %d).",
1075 filename, linenum,
1076 SSH_MAX_CERTIFICATE_FILES);
1077 }
1078 add_certificate_file(options, arg,
1079 flags & SSHCONF_USERCONF);
1080 }
1081 break;
1082
Damien Millerd3a18572000-06-07 19:55:44 +10001083 case oXAuthLocation:
1084 charptr=&options->xauth_location;
1085 goto parse_string;
1086
Damien Miller95def091999-11-25 00:26:21 +11001087 case oUser:
1088 charptr = &options->user;
1089parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001090 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001091 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001092 fatal("%.200s line %d: Missing argument.",
1093 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001094 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001095 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001096 break;
1097
1098 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001099 cpptr = (char **)&options->system_hostfiles;
1100 uintptr = &options->num_system_hostfiles;
1101 max_entries = SSH_MAX_HOSTS_FILES;
1102parse_char_array:
1103 if (*activep && *uintptr == 0) {
1104 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1105 if ((*uintptr) >= max_entries)
1106 fatal("%s line %d: "
1107 "too many authorized keys files.",
1108 filename, linenum);
1109 cpptr[(*uintptr)++] = xstrdup(arg);
1110 }
1111 }
1112 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001113
1114 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001115 cpptr = (char **)&options->user_hostfiles;
1116 uintptr = &options->num_user_hostfiles;
1117 max_entries = SSH_MAX_HOSTS_FILES;
1118 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001119
Damien Miller95def091999-11-25 00:26:21 +11001120 case oHostName:
1121 charptr = &options->hostname;
1122 goto parse_string;
1123
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001124 case oHostKeyAlias:
1125 charptr = &options->host_key_alias;
1126 goto parse_string;
1127
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001128 case oPreferredAuthentications:
1129 charptr = &options->preferred_authentications;
1130 goto parse_string;
1131
Ben Lindstrome0f88042001-04-30 13:06:24 +00001132 case oBindAddress:
1133 charptr = &options->bind_address;
1134 goto parse_string;
1135
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001136 case oBindInterface:
1137 charptr = &options->bind_interface;
1138 goto parse_string;
1139
Damien Miller7ea845e2010-02-12 09:21:02 +11001140 case oPKCS11Provider:
1141 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001142 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001143
Damien Miller95def091999-11-25 00:26:21 +11001144 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001145 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001146 /* Ignore ProxyCommand if ProxyJump already specified */
1147 if (options->jump_host != NULL)
1148 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001149parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001150 if (s == NULL)
1151 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001152 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001153 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001154 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001155 return 0;
1156
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001157 case oProxyJump:
1158 if (s == NULL) {
1159 fatal("%.200s line %d: Missing argument.",
1160 filename, linenum);
1161 }
1162 len = strspn(s, WHITESPACE "=");
1163 if (parse_jump(s + len, options, *activep) == -1) {
1164 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1165 filename, linenum, s + len);
1166 }
1167 return 0;
1168
Damien Miller95def091999-11-25 00:26:21 +11001169 case oPort:
1170 intptr = &options->port;
1171parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001172 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001173 if ((errstr = atoi_err(arg, &value)) != NULL)
1174 fatal("%s line %d: integer value %s.",
1175 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001176 if (*activep && *intptr == -1)
1177 *intptr = value;
1178 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179
Damien Miller95def091999-11-25 00:26:21 +11001180 case oConnectionAttempts:
1181 intptr = &options->connection_attempts;
1182 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001183
Damien Miller78928792000-04-12 20:17:38 +10001184 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001185 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001186 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001187 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001188 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001189 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001190 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001191 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001192 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001193 break;
1194
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001195 case oMacs:
1196 arg = strdelim(&s);
1197 if (!arg || *arg == '\0')
1198 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001199 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001200 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001201 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001202 if (*activep && options->macs == NULL)
1203 options->macs = xstrdup(arg);
1204 break;
1205
Damien Millerd5f62bf2010-09-24 22:11:14 +10001206 case oKexAlgorithms:
1207 arg = strdelim(&s);
1208 if (!arg || *arg == '\0')
1209 fatal("%.200s line %d: Missing argument.",
1210 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001211 if (*arg != '-' &&
1212 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001213 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1214 filename, linenum, arg ? arg : "<NONE>");
1215 if (*activep && options->kex_algorithms == NULL)
1216 options->kex_algorithms = xstrdup(arg);
1217 break;
1218
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001219 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001220 charptr = &options->hostkeyalgorithms;
1221parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001222 arg = strdelim(&s);
1223 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001224 fatal("%.200s line %d: Missing argument.",
1225 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001226 if (*arg != '-' &&
1227 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001228 fatal("%s line %d: Bad key types '%s'.",
1229 filename, linenum, arg ? arg : "<NONE>");
1230 if (*activep && *charptr == NULL)
1231 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001232 break;
1233
Damien Miller95def091999-11-25 00:26:21 +11001234 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001235 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001236 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001237 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001238 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001239 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001240 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001241 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1242 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001243 break;
1244
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001245 case oLogFacility:
1246 log_facility_ptr = &options->log_facility;
1247 arg = strdelim(&s);
1248 value = log_facility_number(arg);
1249 if (value == SYSLOG_FACILITY_NOT_SET)
1250 fatal("%.200s line %d: unsupported log facility '%s'",
1251 filename, linenum, arg ? arg : "<NONE>");
1252 if (*log_facility_ptr == -1)
1253 *log_facility_ptr = (SyslogFacility) value;
1254 break;
1255
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001256 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001257 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001258 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001259 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001260 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001261 fatal("%.200s line %d: Missing port argument.",
1262 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001263
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001264 remotefwd = (opcode == oRemoteForward);
1265 dynamicfwd = (opcode == oDynamicForward);
1266
1267 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001268 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001269 if (arg2 == NULL || *arg2 == '\0') {
1270 if (remotefwd)
1271 dynamicfwd = 1;
1272 else
1273 fatal("%.200s line %d: Missing target "
1274 "argument.", filename, linenum);
1275 } else {
1276 /* construct a string for parse_forward */
1277 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1278 arg2);
1279 }
Damien Millera699d952008-11-03 19:27:34 +11001280 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001281 if (dynamicfwd)
1282 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001283
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001284 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001285 fatal("%.200s line %d: Bad forwarding specification.",
1286 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001287
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001288 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001289 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001290 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001291 } else {
1292 add_local_forward(options, &fwd);
1293 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001294 }
Damien Miller95def091999-11-25 00:26:21 +11001295 break;
1296
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001297 case oClearAllForwardings:
1298 intptr = &options->clear_forwardings;
1299 goto parse_flag;
1300
Damien Miller95def091999-11-25 00:26:21 +11001301 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001302 if (cmdline)
1303 fatal("Host directive not supported as a command-line "
1304 "option");
Damien Miller95def091999-11-25 00:26:21 +11001305 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001306 arg2 = NULL;
1307 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001308 if ((flags & SSHCONF_NEVERMATCH) != 0)
1309 break;
Damien Millerfe924212011-05-15 08:44:45 +10001310 negated = *arg == '!';
1311 if (negated)
1312 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001313 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001314 if (negated) {
1315 debug("%.200s line %d: Skipping Host "
1316 "block because of negated match "
1317 "for %.100s", filename, linenum,
1318 arg);
1319 *activep = 0;
1320 break;
1321 }
1322 if (!*activep)
1323 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001324 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001325 }
Damien Millerfe924212011-05-15 08:44:45 +10001326 }
1327 if (*activep)
1328 debug("%.200s line %d: Applying options for %.100s",
1329 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001330 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001331 return 0;
1332
Damien Miller194fd902013-10-15 12:13:05 +11001333 case oMatch:
1334 if (cmdline)
1335 fatal("Host directive not supported as a command-line "
1336 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001337 value = match_cfg_line(options, &s, pw, host, original_host,
1338 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001339 if (value < 0)
1340 fatal("%.200s line %d: Bad Match condition", filename,
1341 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001342 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001343 break;
1344
Damien Miller95def091999-11-25 00:26:21 +11001345 case oEscapeChar:
1346 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001347 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001348 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001349 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001350 if (strcmp(arg, "none") == 0)
1351 value = SSH_ESCAPECHAR_NONE;
1352 else if (arg[1] == '\0')
1353 value = (u_char) arg[0];
1354 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001355 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1356 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001357 else {
1358 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001359 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001360 /* NOTREACHED */
1361 value = 0; /* Avoid compiler warning. */
1362 }
1363 if (*activep && *intptr == -1)
1364 *intptr = value;
1365 break;
1366
Damien Miller20a8f972003-05-18 20:50:30 +10001367 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001368 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001369 multistate_ptr = multistate_addressfamily;
1370 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001371
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001372 case oEnableSSHKeysign:
1373 intptr = &options->enable_ssh_keysign;
1374 goto parse_flag;
1375
Damien Millerbd394c32004-03-08 23:12:36 +11001376 case oIdentitiesOnly:
1377 intptr = &options->identities_only;
1378 goto parse_flag;
1379
Damien Miller509b0102003-12-17 16:33:10 +11001380 case oServerAliveInterval:
1381 intptr = &options->server_alive_interval;
1382 goto parse_time;
1383
1384 case oServerAliveCountMax:
1385 intptr = &options->server_alive_count_max;
1386 goto parse_int;
1387
Darren Tucker46bc0752004-05-02 22:11:30 +10001388 case oSendEnv:
1389 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1390 if (strchr(arg, '=') != NULL)
1391 fatal("%s line %d: Invalid environment name.",
1392 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001393 if (!*activep)
1394 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001395 if (*arg == '-') {
1396 /* Removing an env var */
1397 rm_env(options, arg, filename, linenum);
1398 continue;
1399 } else {
1400 /* Adding an env var */
1401 if (options->num_send_env >= MAX_SEND_ENV)
1402 fatal("%s line %d: too many send env.",
1403 filename, linenum);
1404 options->send_env[options->num_send_env++] =
1405 xstrdup(arg);
1406 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001407 }
1408 break;
1409
Damien Miller0e220db2004-06-15 10:34:08 +10001410 case oControlPath:
1411 charptr = &options->control_path;
1412 goto parse_string;
1413
1414 case oControlMaster:
1415 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001416 multistate_ptr = multistate_controlmaster;
1417 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001418
Damien Millere11e1ea2010-08-03 16:04:46 +10001419 case oControlPersist:
1420 /* no/false/yes/true, or a time spec */
1421 intptr = &options->control_persist;
1422 arg = strdelim(&s);
1423 if (!arg || *arg == '\0')
1424 fatal("%.200s line %d: Missing ControlPersist"
1425 " argument.", filename, linenum);
1426 value = 0;
1427 value2 = 0; /* timeout */
1428 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1429 value = 0;
1430 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1431 value = 1;
1432 else if ((value2 = convtime(arg)) >= 0)
1433 value = 1;
1434 else
1435 fatal("%.200s line %d: Bad ControlPersist argument.",
1436 filename, linenum);
1437 if (*activep && *intptr == -1) {
1438 *intptr = value;
1439 options->control_persist_timeout = value2;
1440 }
1441 break;
1442
Damien Millere1776152005-03-01 21:47:37 +11001443 case oHashKnownHosts:
1444 intptr = &options->hash_known_hosts;
1445 goto parse_flag;
1446
Damien Millerd27b9472005-12-13 19:29:02 +11001447 case oTunnel:
1448 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001449 multistate_ptr = multistate_tunnel;
1450 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001451
1452 case oTunnelDevice:
1453 arg = strdelim(&s);
1454 if (!arg || *arg == '\0')
1455 fatal("%.200s line %d: Missing argument.", filename, linenum);
1456 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001457 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001458 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1459 if (*activep) {
1460 options->tun_local = value;
1461 options->tun_remote = value2;
1462 }
1463 break;
1464
1465 case oLocalCommand:
1466 charptr = &options->local_command;
1467 goto parse_command;
1468
1469 case oPermitLocalCommand:
1470 intptr = &options->permit_local_command;
1471 goto parse_flag;
1472
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001473 case oRemoteCommand:
1474 charptr = &options->remote_command;
1475 goto parse_command;
1476
Damien Miller10288242008-06-30 00:04:03 +10001477 case oVisualHostKey:
1478 intptr = &options->visual_host_key;
1479 goto parse_flag;
1480
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001481 case oInclude:
1482 if (cmdline)
1483 fatal("Include directive not supported as a "
1484 "command-line option");
1485 value = 0;
1486 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1487 /*
1488 * Ensure all paths are anchored. User configuration
1489 * files may begin with '~/' but system configurations
1490 * must not. If the path is relative, then treat it
1491 * as living in ~/.ssh for user configurations or
1492 * /etc/ssh for system ones.
1493 */
1494 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1495 fatal("%.200s line %d: bad include path %s.",
1496 filename, linenum, arg);
1497 if (*arg != '/' && *arg != '~') {
1498 xasprintf(&arg2, "%s/%s",
1499 (flags & SSHCONF_USERCONF) ?
1500 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1501 } else
1502 arg2 = xstrdup(arg);
1503 memset(&gl, 0, sizeof(gl));
1504 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1505 if (r == GLOB_NOMATCH) {
1506 debug("%.200s line %d: include %s matched no "
1507 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001508 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001509 continue;
1510 } else if (r != 0 || gl.gl_pathc < 0)
1511 fatal("%.200s line %d: glob failed for %s.",
1512 filename, linenum, arg2);
1513 free(arg2);
1514 oactive = *activep;
1515 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1516 debug3("%.200s line %d: Including file %s "
1517 "depth %d%s", filename, linenum,
1518 gl.gl_pathv[i], depth,
1519 oactive ? "" : " (parse only)");
1520 r = read_config_file_depth(gl.gl_pathv[i],
1521 pw, host, original_host, options,
1522 flags | SSHCONF_CHECKPERM |
1523 (oactive ? 0 : SSHCONF_NEVERMATCH),
1524 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001525 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001526 fatal("Can't open user config file "
1527 "%.100s: %.100s", gl.gl_pathv[i],
1528 strerror(errno));
1529 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001530 /*
1531 * don't let Match in includes clobber the
1532 * containing file's Match state.
1533 */
1534 *activep = oactive;
1535 if (r != 1)
1536 value = -1;
1537 }
1538 globfree(&gl);
1539 }
1540 if (value != 0)
1541 return value;
1542 break;
1543
Damien Miller0dac6fb2010-11-20 15:19:38 +11001544 case oIPQoS:
1545 arg = strdelim(&s);
1546 if ((value = parse_ipqos(arg)) == -1)
1547 fatal("%s line %d: Bad IPQoS value: %s",
1548 filename, linenum, arg);
1549 arg = strdelim(&s);
1550 if (arg == NULL)
1551 value2 = value;
1552 else if ((value2 = parse_ipqos(arg)) == -1)
1553 fatal("%s line %d: Bad IPQoS value: %s",
1554 filename, linenum, arg);
1555 if (*activep) {
1556 options->ip_qos_interactive = value;
1557 options->ip_qos_bulk = value2;
1558 }
1559 break;
1560
Damien Miller21771e22011-05-15 08:45:50 +10001561 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001562 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001563 multistate_ptr = multistate_requesttty;
1564 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001565
Darren Tucker07636982013-05-16 20:30:03 +10001566 case oIgnoreUnknown:
1567 charptr = &options->ignored_unknown;
1568 goto parse_string;
1569
Damien Miller1262b662013-08-21 02:44:24 +10001570 case oProxyUseFdpass:
1571 intptr = &options->proxy_use_fdpass;
1572 goto parse_flag;
1573
Damien Miller0faf7472013-10-17 11:47:23 +11001574 case oCanonicalDomains:
1575 value = options->num_canonical_domains != 0;
1576 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001577 if (!valid_domain(arg, 1, &errstr)) {
1578 fatal("%s line %d: %s", filename, linenum,
1579 errstr);
1580 }
Damien Miller0faf7472013-10-17 11:47:23 +11001581 if (!*activep || value)
1582 continue;
1583 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1584 fatal("%s line %d: too many hostname suffixes.",
1585 filename, linenum);
1586 options->canonical_domains[
1587 options->num_canonical_domains++] = xstrdup(arg);
1588 }
1589 break;
1590
Damien Miller38505592013-10-17 11:48:13 +11001591 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001592 value = options->num_permitted_cnames != 0;
1593 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1594 /* Either '*' for everything or 'list:list' */
1595 if (strcmp(arg, "*") == 0)
1596 arg2 = arg;
1597 else {
1598 lowercase(arg);
1599 if ((arg2 = strchr(arg, ':')) == NULL ||
1600 arg2[1] == '\0') {
1601 fatal("%s line %d: "
1602 "Invalid permitted CNAME \"%s\"",
1603 filename, linenum, arg);
1604 }
1605 *arg2 = '\0';
1606 arg2++;
1607 }
1608 if (!*activep || value)
1609 continue;
1610 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1611 fatal("%s line %d: too many permitted CNAMEs.",
1612 filename, linenum);
1613 cname = options->permitted_cnames +
1614 options->num_permitted_cnames++;
1615 cname->source_list = xstrdup(arg);
1616 cname->target_list = xstrdup(arg2);
1617 }
1618 break;
1619
Damien Miller38505592013-10-17 11:48:13 +11001620 case oCanonicalizeHostname:
1621 intptr = &options->canonicalize_hostname;
1622 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001623 goto parse_multistate;
1624
Damien Miller38505592013-10-17 11:48:13 +11001625 case oCanonicalizeMaxDots:
1626 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001627 goto parse_int;
1628
Damien Miller38505592013-10-17 11:48:13 +11001629 case oCanonicalizeFallbackLocal:
1630 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001631 goto parse_flag;
1632
Damien Miller7acefbb2014-07-18 14:11:24 +10001633 case oStreamLocalBindMask:
1634 arg = strdelim(&s);
1635 if (!arg || *arg == '\0')
1636 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1637 /* Parse mode in octal format */
1638 value = strtol(arg, &endofnumber, 8);
1639 if (arg == endofnumber || value < 0 || value > 0777)
1640 fatal("%.200s line %d: Bad mask.", filename, linenum);
1641 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1642 break;
1643
1644 case oStreamLocalBindUnlink:
1645 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1646 goto parse_flag;
1647
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001648 case oRevokedHostKeys:
1649 charptr = &options->revoked_host_keys;
1650 goto parse_string;
1651
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001652 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001653 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001654 arg = strdelim(&s);
1655 if (!arg || *arg == '\0')
1656 fatal("%.200s line %d: Missing argument.",
1657 filename, linenum);
1658 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1659 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1660 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001661 if (*activep && *intptr == -1)
1662 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001663 break;
1664
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001665 case oUpdateHostkeys:
1666 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001667 multistate_ptr = multistate_yesnoask;
1668 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001669
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001670 case oHostbasedKeyTypes:
1671 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001672 goto parse_keytypes;
1673
1674 case oPubkeyAcceptedKeyTypes:
1675 charptr = &options->pubkey_key_types;
1676 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001677
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001678 case oAddKeysToAgent:
1679 intptr = &options->add_keys_to_agent;
1680 multistate_ptr = multistate_yesnoaskconfirm;
1681 goto parse_multistate;
1682
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001683 case oIdentityAgent:
1684 charptr = &options->identity_agent;
1685 goto parse_string;
1686
Ben Lindstrom4daea862002-06-09 20:04:02 +00001687 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001688 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001689 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001690 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001691
Damien Millerf9b3feb2003-05-16 11:38:32 +10001692 case oUnsupported:
1693 error("%s line %d: Unsupported option \"%s\"",
1694 filename, linenum, keyword);
1695 return 0;
1696
Damien Miller95def091999-11-25 00:26:21 +11001697 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001698 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001699 }
1700
1701 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001702 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001703 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001704 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001705 }
Damien Miller95def091999-11-25 00:26:21 +11001706 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001707}
1708
Damien Miller5428f641999-11-25 11:54:57 +11001709/*
1710 * Reads the config file and modifies the options accordingly. Options
1711 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001712 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001713 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001714int
Damien Miller194fd902013-10-15 12:13:05 +11001715read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001716 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001718 int active = 1;
1719
1720 return read_config_file_depth(filename, pw, host, original_host,
1721 options, flags, &active, 0);
1722}
1723
1724#define READCONF_MAX_DEPTH 16
1725static int
1726read_config_file_depth(const char *filename, struct passwd *pw,
1727 const char *host, const char *original_host, Options *options,
1728 int flags, int *activep, int depth)
1729{
Damien Miller95def091999-11-25 00:26:21 +11001730 FILE *f;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001731 char line[4096];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001732 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001733 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001734
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001735 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1736 fatal("Too many recursive configuration includes");
1737
Damien Miller57a44762004-04-20 20:11:57 +10001738 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001739 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740
Darren Tuckeraefa3682013-04-05 11:18:35 +11001741 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001742 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001743
Damien Miller33793852004-06-15 10:27:55 +10001744 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001745 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001746 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001747 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001748 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001749 }
1750
Damien Miller95def091999-11-25 00:26:21 +11001751 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001752
Damien Miller5428f641999-11-25 11:54:57 +11001753 /*
1754 * Mark that we are now processing the options. This flag is turned
1755 * on/off by Host specifications.
1756 */
Damien Miller95def091999-11-25 00:26:21 +11001757 linenum = 0;
1758 while (fgets(line, sizeof(line), f)) {
1759 /* Update line number counter. */
1760 linenum++;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001761 if (strlen(line) == sizeof(line) - 1)
1762 fatal("%s line %d too long", filename, linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001763 if (process_config_line_depth(options, pw, host, original_host,
1764 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001765 bad_options++;
1766 }
1767 fclose(f);
1768 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001769 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001770 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001771 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001772}
1773
Damien Miller13f97b22014-02-24 15:57:55 +11001774/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1775int
1776option_clear_or_none(const char *o)
1777{
1778 return o == NULL || strcasecmp(o, "none") == 0;
1779}
1780
Damien Miller5428f641999-11-25 11:54:57 +11001781/*
1782 * Initializes options to special values that indicate that they have not yet
1783 * been set. Read_config_file will only set options with this value. Options
1784 * are processed in the following order: command line, user config file,
1785 * system config file. Last, fill_default_options is called.
1786 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001787
Damien Miller4af51302000-04-16 11:18:38 +10001788void
Damien Miller95def091999-11-25 00:26:21 +11001789initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001790{
Damien Miller95def091999-11-25 00:26:21 +11001791 memset(options, 'X', sizeof(*options));
1792 options->forward_agent = -1;
1793 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001794 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001795 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001796 options->stdio_forward_host = NULL;
1797 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001798 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001799 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001800 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001801 options->fwd_opts.gateway_ports = -1;
1802 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1803 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001804 options->use_privileged_port = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001805 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001806 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001807 options->gss_authentication = -1;
1808 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001809 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001810 options->kbd_interactive_authentication = -1;
1811 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001812 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001813 options->batch_mode = -1;
1814 options->check_host_ip = -1;
1815 options->strict_host_key_checking = -1;
1816 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001817 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001818 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001819 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001820 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001821 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001822 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001823 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001824 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001825 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001826 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001827 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001828 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001829 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001830 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001831 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001832 options->jump_user = NULL;
1833 options->jump_host = NULL;
1834 options->jump_port = -1;
1835 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001836 options->user = NULL;
1837 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001838 options->num_system_hostfiles = 0;
1839 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001840 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001841 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001842 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001843 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001844 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001845 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001846 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001847 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001848 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001849 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001850 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001851 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001852 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001853 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001854 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001855 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001856 options->server_alive_interval = -1;
1857 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001858 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001859 options->control_path = NULL;
1860 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001861 options->control_persist = -1;
1862 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001863 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001864 options->tun_open = -1;
1865 options->tun_local = -1;
1866 options->tun_remote = -1;
1867 options->local_command = NULL;
1868 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001869 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001870 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001871 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001872 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001873 options->ip_qos_interactive = -1;
1874 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001875 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001876 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001877 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001878 options->num_canonical_domains = 0;
1879 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001880 options->canonicalize_max_dots = -1;
1881 options->canonicalize_fallback_local = -1;
1882 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001883 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001884 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001885 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001886 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001887 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001888}
1889
Damien Miller5428f641999-11-25 11:54:57 +11001890/*
Damien Miller13f97b22014-02-24 15:57:55 +11001891 * A petite version of fill_default_options() that just fills the options
1892 * needed for hostname canonicalization to proceed.
1893 */
1894void
1895fill_default_options_for_canonicalization(Options *options)
1896{
1897 if (options->canonicalize_max_dots == -1)
1898 options->canonicalize_max_dots = 1;
1899 if (options->canonicalize_fallback_local == -1)
1900 options->canonicalize_fallback_local = 1;
1901 if (options->canonicalize_hostname == -1)
1902 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1903}
1904
1905/*
Damien Miller5428f641999-11-25 11:54:57 +11001906 * Called after processing other sources of option data, this fills those
1907 * options for which no value has been specified with their default values.
1908 */
Damien Miller4af51302000-04-16 11:18:38 +10001909void
Damien Miller95def091999-11-25 00:26:21 +11001910fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001911{
Damien Miller95def091999-11-25 00:26:21 +11001912 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001913 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001914 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001915 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001916 if (options->forward_x11_trusted == -1)
1917 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001918 if (options->forward_x11_timeout == -1)
1919 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001920 /*
1921 * stdio forwarding (-W) changes the default for these but we defer
1922 * setting the values so they can be overridden.
1923 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001924 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001925 options->exit_on_forward_failure =
1926 options->stdio_forward_host != NULL ? 1 : 0;
1927 if (options->clear_forwardings == -1)
1928 options->clear_forwardings =
1929 options->stdio_forward_host != NULL ? 1 : 0;
1930 if (options->clear_forwardings == 1)
1931 clear_forwardings(options);
1932
Damien Millerd3a18572000-06-07 19:55:44 +10001933 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001934 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001935 if (options->fwd_opts.gateway_ports == -1)
1936 options->fwd_opts.gateway_ports = 0;
1937 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1938 options->fwd_opts.streamlocal_bind_mask = 0177;
1939 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1940 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001941 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001942 options->use_privileged_port = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001943 if (options->pubkey_authentication == -1)
1944 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001945 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001946 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001947 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001948 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001949 if (options->gss_deleg_creds == -1)
1950 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001951 if (options->password_authentication == -1)
1952 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001953 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001954 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001955 if (options->hostbased_authentication == -1)
1956 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001957 if (options->batch_mode == -1)
1958 options->batch_mode = 0;
1959 if (options->check_host_ip == -1)
1960 options->check_host_ip = 1;
1961 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001962 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001963 if (options->compression == -1)
1964 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001965 if (options->tcp_keep_alive == -1)
1966 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001967 if (options->port == -1)
1968 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001969 if (options->address_family == -1)
1970 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001971 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001972 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001973 if (options->number_of_password_prompts == -1)
1974 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001975 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001976 if (options->add_keys_to_agent == -1)
1977 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001978 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001979 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1980 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001981#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001982 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001983#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001984 add_identity_file(options, "~/",
1985 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001986 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10001987 }
Damien Miller95def091999-11-25 00:26:21 +11001988 if (options->escape_char == -1)
1989 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001990 if (options->num_system_hostfiles == 0) {
1991 options->system_hostfiles[options->num_system_hostfiles++] =
1992 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1993 options->system_hostfiles[options->num_system_hostfiles++] =
1994 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1995 }
1996 if (options->num_user_hostfiles == 0) {
1997 options->user_hostfiles[options->num_user_hostfiles++] =
1998 xstrdup(_PATH_SSH_USER_HOSTFILE);
1999 options->user_hostfiles[options->num_user_hostfiles++] =
2000 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2001 }
Damien Millerfcd93202002-02-05 12:26:34 +11002002 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002003 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002004 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2005 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002006 if (options->no_host_authentication_for_localhost == - 1)
2007 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002008 if (options->identities_only == -1)
2009 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002010 if (options->enable_ssh_keysign == -1)
2011 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002012 if (options->rekey_limit == -1)
2013 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002014 if (options->rekey_interval == -1)
2015 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002016 if (options->verify_host_key_dns == -1)
2017 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002018 if (options->server_alive_interval == -1)
2019 options->server_alive_interval = 0;
2020 if (options->server_alive_count_max == -1)
2021 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002022 if (options->control_master == -1)
2023 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002024 if (options->control_persist == -1) {
2025 options->control_persist = 0;
2026 options->control_persist_timeout = 0;
2027 }
Damien Millere1776152005-03-01 21:47:37 +11002028 if (options->hash_known_hosts == -1)
2029 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002030 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002031 options->tun_open = SSH_TUNMODE_NO;
2032 if (options->tun_local == -1)
2033 options->tun_local = SSH_TUNID_ANY;
2034 if (options->tun_remote == -1)
2035 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002036 if (options->permit_local_command == -1)
2037 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002038 if (options->visual_host_key == -1)
2039 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002040 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002041 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002042 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002043 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002044 if (options->request_tty == -1)
2045 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002046 if (options->proxy_use_fdpass == -1)
2047 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002048 if (options->canonicalize_max_dots == -1)
2049 options->canonicalize_max_dots = 1;
2050 if (options->canonicalize_fallback_local == -1)
2051 options->canonicalize_fallback_local = 1;
2052 if (options->canonicalize_hostname == -1)
2053 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002054 if (options->fingerprint_hash == -1)
2055 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002056 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002057 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002058 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2059 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2060 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2061 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2062 &options->hostbased_key_types) != 0 ||
2063 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2064 &options->pubkey_key_types) != 0)
2065 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002066
Damien Millere9fc72e2013-10-15 12:14:12 +11002067#define CLEAR_ON_NONE(v) \
2068 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002069 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002070 free(v); \
2071 v = NULL; \
2072 } \
2073 } while(0)
2074 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002075 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002076 CLEAR_ON_NONE(options->proxy_command);
2077 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002078 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002079 if (options->jump_host != NULL &&
2080 strcmp(options->jump_host, "none") == 0 &&
2081 options->jump_port == 0 && options->jump_user == NULL) {
2082 free(options->jump_host);
2083 options->jump_host = NULL;
2084 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002085 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002086 /* options->user will be set in the main program if appropriate */
2087 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002088 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002089 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002090}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002091
Damien Miller7acefbb2014-07-18 14:11:24 +10002092struct fwdarg {
2093 char *arg;
2094 int ispath;
2095};
2096
2097/*
2098 * parse_fwd_field
2099 * parses the next field in a port forwarding specification.
2100 * sets fwd to the parsed field and advances p past the colon
2101 * or sets it to NULL at end of string.
2102 * returns 0 on success, else non-zero.
2103 */
2104static int
2105parse_fwd_field(char **p, struct fwdarg *fwd)
2106{
2107 char *ep, *cp = *p;
2108 int ispath = 0;
2109
2110 if (*cp == '\0') {
2111 *p = NULL;
2112 return -1; /* end of string */
2113 }
2114
2115 /*
2116 * A field escaped with square brackets is used literally.
2117 * XXX - allow ']' to be escaped via backslash?
2118 */
2119 if (*cp == '[') {
2120 /* find matching ']' */
2121 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2122 if (*ep == '/')
2123 ispath = 1;
2124 }
2125 /* no matching ']' or not at end of field. */
2126 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2127 return -1;
2128 /* NUL terminate the field and advance p past the colon */
2129 *ep++ = '\0';
2130 if (*ep != '\0')
2131 *ep++ = '\0';
2132 fwd->arg = cp + 1;
2133 fwd->ispath = ispath;
2134 *p = ep;
2135 return 0;
2136 }
2137
2138 for (cp = *p; *cp != '\0'; cp++) {
2139 switch (*cp) {
2140 case '\\':
2141 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002142 if (*cp == '\0')
2143 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002144 break;
2145 case '/':
2146 ispath = 1;
2147 break;
2148 case ':':
2149 *cp++ = '\0';
2150 goto done;
2151 }
2152 }
2153done:
2154 fwd->arg = *p;
2155 fwd->ispath = ispath;
2156 *p = cp;
2157 return 0;
2158}
2159
Damien Millerf91ee4c2005-03-01 21:24:33 +11002160/*
2161 * parse_forward
2162 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002163 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002164 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2165 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002166 * dynamicfwd == 1
2167 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002168 * returns number of arguments parsed or zero on error
2169 */
2170int
Damien Miller7acefbb2014-07-18 14:11:24 +10002171parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002172{
Damien Miller7acefbb2014-07-18 14:11:24 +10002173 struct fwdarg fwdargs[4];
2174 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002175 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002176
Damien Miller7acefbb2014-07-18 14:11:24 +10002177 memset(fwd, 0, sizeof(*fwd));
2178 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002179
2180 cp = p = xstrdup(fwdspec);
2181
2182 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002183 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002184 cp++;
2185
Damien Miller7acefbb2014-07-18 14:11:24 +10002186 for (i = 0; i < 4; ++i) {
2187 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002188 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002189 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002190
Damien Millerf4b39532008-11-03 19:28:21 +11002191 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002192 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002193 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002194 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002195
2196 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002197 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002198 if (fwdargs[0].ispath) {
2199 fwd->listen_path = xstrdup(fwdargs[0].arg);
2200 fwd->listen_port = PORT_STREAMLOCAL;
2201 } else {
2202 fwd->listen_host = NULL;
2203 fwd->listen_port = a2port(fwdargs[0].arg);
2204 }
Damien Millera699d952008-11-03 19:27:34 +11002205 fwd->connect_host = xstrdup("socks");
2206 break;
2207
2208 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002209 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2210 fwd->listen_path = xstrdup(fwdargs[0].arg);
2211 fwd->listen_port = PORT_STREAMLOCAL;
2212 fwd->connect_path = xstrdup(fwdargs[1].arg);
2213 fwd->connect_port = PORT_STREAMLOCAL;
2214 } else if (fwdargs[1].ispath) {
2215 fwd->listen_host = NULL;
2216 fwd->listen_port = a2port(fwdargs[0].arg);
2217 fwd->connect_path = xstrdup(fwdargs[1].arg);
2218 fwd->connect_port = PORT_STREAMLOCAL;
2219 } else {
2220 fwd->listen_host = xstrdup(fwdargs[0].arg);
2221 fwd->listen_port = a2port(fwdargs[1].arg);
2222 fwd->connect_host = xstrdup("socks");
2223 }
Damien Millera699d952008-11-03 19:27:34 +11002224 break;
2225
Damien Millerf91ee4c2005-03-01 21:24:33 +11002226 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002227 if (fwdargs[0].ispath) {
2228 fwd->listen_path = xstrdup(fwdargs[0].arg);
2229 fwd->listen_port = PORT_STREAMLOCAL;
2230 fwd->connect_host = xstrdup(fwdargs[1].arg);
2231 fwd->connect_port = a2port(fwdargs[2].arg);
2232 } else if (fwdargs[2].ispath) {
2233 fwd->listen_host = xstrdup(fwdargs[0].arg);
2234 fwd->listen_port = a2port(fwdargs[1].arg);
2235 fwd->connect_path = xstrdup(fwdargs[2].arg);
2236 fwd->connect_port = PORT_STREAMLOCAL;
2237 } else {
2238 fwd->listen_host = NULL;
2239 fwd->listen_port = a2port(fwdargs[0].arg);
2240 fwd->connect_host = xstrdup(fwdargs[1].arg);
2241 fwd->connect_port = a2port(fwdargs[2].arg);
2242 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002243 break;
2244
2245 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002246 fwd->listen_host = xstrdup(fwdargs[0].arg);
2247 fwd->listen_port = a2port(fwdargs[1].arg);
2248 fwd->connect_host = xstrdup(fwdargs[2].arg);
2249 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002250 break;
2251 default:
2252 i = 0; /* failure */
2253 }
2254
Darren Tuckera627d422013-06-02 07:31:17 +10002255 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002256
Damien Millera699d952008-11-03 19:27:34 +11002257 if (dynamicfwd) {
2258 if (!(i == 1 || i == 2))
2259 goto fail_free;
2260 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002261 if (!(i == 3 || i == 4)) {
2262 if (fwd->connect_path == NULL &&
2263 fwd->listen_path == NULL)
2264 goto fail_free;
2265 }
2266 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002267 goto fail_free;
2268 }
2269
Damien Miller7acefbb2014-07-18 14:11:24 +10002270 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2271 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002272 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002273 if (fwd->connect_host != NULL &&
2274 strlen(fwd->connect_host) >= NI_MAXHOST)
2275 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002276 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2277 if (fwd->connect_path != NULL &&
2278 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2279 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002280 if (fwd->listen_host != NULL &&
2281 strlen(fwd->listen_host) >= NI_MAXHOST)
2282 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002283 if (fwd->listen_path != NULL &&
2284 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2285 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002286
2287 return (i);
2288
2289 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002290 free(fwd->connect_host);
2291 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002292 free(fwd->connect_path);
2293 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002294 free(fwd->listen_host);
2295 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002296 free(fwd->listen_path);
2297 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002298 return (0);
2299}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002300
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002301int
2302parse_jump(const char *s, Options *o, int active)
2303{
2304 char *orig, *sdup, *cp;
2305 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002306 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002307
2308 active &= o->proxy_command == NULL && o->jump_host == NULL;
2309
2310 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002311 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002312 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002313 if (strcasecmp(s, "none") == 0)
2314 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002315 if ((cp = strrchr(sdup, ',')) == NULL)
2316 cp = sdup; /* last */
2317 else
2318 *cp++ = '\0';
2319
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002320 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002321 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002322 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2323 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002324 goto out;
2325 } else {
2326 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002327 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2328 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002329 goto out;
2330 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002331 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002332 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002333 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002334 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002335 if (strcasecmp(s, "none") == 0) {
2336 o->jump_host = xstrdup("none");
2337 o->jump_port = 0;
2338 } else {
2339 o->jump_user = user;
2340 o->jump_host = host;
2341 o->jump_port = port;
2342 o->proxy_command = xstrdup("none");
2343 user = host = NULL;
2344 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2345 o->jump_extra = xstrdup(s);
2346 o->jump_extra[cp - s] = '\0';
2347 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002348 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002349 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002350 ret = 0;
2351 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002352 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002353 free(user);
2354 free(host);
2355 return ret;
2356}
2357
millert@openbsd.org887669e2017-10-21 23:06:24 +00002358int
2359parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2360{
2361 char *path;
2362 int r;
2363
2364 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2365 if (r == 0 && path != NULL)
2366 r = -1; /* path not allowed */
2367 return r;
2368}
2369
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002370/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2371static const char *
2372fmt_multistate_int(int val, const struct multistate *m)
2373{
2374 u_int i;
2375
2376 for (i = 0; m[i].key != NULL; i++) {
2377 if (m[i].value == val)
2378 return m[i].key;
2379 }
2380 return "UNKNOWN";
2381}
2382
2383static const char *
2384fmt_intarg(OpCodes code, int val)
2385{
2386 if (val == -1)
2387 return "unset";
2388 switch (code) {
2389 case oAddressFamily:
2390 return fmt_multistate_int(val, multistate_addressfamily);
2391 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002392 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002393 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002394 case oStrictHostKeyChecking:
2395 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002396 case oControlMaster:
2397 return fmt_multistate_int(val, multistate_controlmaster);
2398 case oTunnel:
2399 return fmt_multistate_int(val, multistate_tunnel);
2400 case oRequestTTY:
2401 return fmt_multistate_int(val, multistate_requesttty);
2402 case oCanonicalizeHostname:
2403 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002404 case oAddKeysToAgent:
2405 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002406 case oFingerprintHash:
2407 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002408 default:
2409 switch (val) {
2410 case 0:
2411 return "no";
2412 case 1:
2413 return "yes";
2414 default:
2415 return "UNKNOWN";
2416 }
2417 }
2418}
2419
2420static const char *
2421lookup_opcode_name(OpCodes code)
2422{
2423 u_int i;
2424
2425 for (i = 0; keywords[i].name != NULL; i++)
2426 if (keywords[i].opcode == code)
2427 return(keywords[i].name);
2428 return "UNKNOWN";
2429}
2430
2431static void
2432dump_cfg_int(OpCodes code, int val)
2433{
2434 printf("%s %d\n", lookup_opcode_name(code), val);
2435}
2436
2437static void
2438dump_cfg_fmtint(OpCodes code, int val)
2439{
2440 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2441}
2442
2443static void
2444dump_cfg_string(OpCodes code, const char *val)
2445{
2446 if (val == NULL)
2447 return;
2448 printf("%s %s\n", lookup_opcode_name(code), val);
2449}
2450
2451static void
2452dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2453{
2454 u_int i;
2455
2456 for (i = 0; i < count; i++)
2457 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2458}
2459
2460static void
2461dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2462{
2463 u_int i;
2464
2465 printf("%s", lookup_opcode_name(code));
2466 for (i = 0; i < count; i++)
2467 printf(" %s", vals[i]);
2468 printf("\n");
2469}
2470
2471static void
2472dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2473{
2474 const struct Forward *fwd;
2475 u_int i;
2476
2477 /* oDynamicForward */
2478 for (i = 0; i < count; i++) {
2479 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002480 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002481 strcmp(fwd->connect_host, "socks") != 0)
2482 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002483 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002484 strcmp(fwd->connect_host, "socks") == 0)
2485 continue;
2486 printf("%s", lookup_opcode_name(code));
2487 if (fwd->listen_port == PORT_STREAMLOCAL)
2488 printf(" %s", fwd->listen_path);
2489 else if (fwd->listen_host == NULL)
2490 printf(" %d", fwd->listen_port);
2491 else {
2492 printf(" [%s]:%d",
2493 fwd->listen_host, fwd->listen_port);
2494 }
2495 if (code != oDynamicForward) {
2496 if (fwd->connect_port == PORT_STREAMLOCAL)
2497 printf(" %s", fwd->connect_path);
2498 else if (fwd->connect_host == NULL)
2499 printf(" %d", fwd->connect_port);
2500 else {
2501 printf(" [%s]:%d",
2502 fwd->connect_host, fwd->connect_port);
2503 }
2504 }
2505 printf("\n");
2506 }
2507}
2508
2509void
2510dump_client_config(Options *o, const char *host)
2511{
2512 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002513 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002514
djm@openbsd.org60a92472015-08-21 23:53:08 +00002515 /* This is normally prepared in ssh_kex2 */
2516 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2517 fatal("%s: kex_assemble_names failed", __func__);
2518
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002519 /* Most interesting options first: user, host, port */
2520 dump_cfg_string(oUser, o->user);
2521 dump_cfg_string(oHostName, host);
2522 dump_cfg_int(oPort, o->port);
2523
2524 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002525 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002526 dump_cfg_fmtint(oAddressFamily, o->address_family);
2527 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2528 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2529 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2530 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2531 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2532 dump_cfg_fmtint(oCompression, o->compression);
2533 dump_cfg_fmtint(oControlMaster, o->control_master);
2534 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002535 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002536 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002537 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002538 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2539 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2540 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2541 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2542#ifdef GSSAPI
2543 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2544 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2545#endif /* GSSAPI */
2546 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2547 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2548 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2549 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2550 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2551 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2552 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002553 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2554 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2555 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002556 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2557 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2558 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2559 dump_cfg_fmtint(oTunnel, o->tun_open);
2560 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2561 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2562 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002563 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002564
2565 /* Integer options */
2566 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002567 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2568 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2569 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2570 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2571 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2572
2573 /* String options */
2574 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002575 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002576 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2577 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002578 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002579 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002580 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002581 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002582 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002583 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2584 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2585 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002586 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002587 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2588 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002589#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002590 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002591#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002592 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002593 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002594 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002595 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002596
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002597 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002598 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2599 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2600 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2601
2602 /* String array options */
2603 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2604 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002605 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002606 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2607 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2608 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2609
2610 /* Special cases */
2611
2612 /* oConnectTimeout */
2613 if (o->connection_timeout == -1)
2614 printf("connecttimeout none\n");
2615 else
2616 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2617
2618 /* oTunnelDevice */
2619 printf("tunneldevice");
2620 if (o->tun_local == SSH_TUNID_ANY)
2621 printf(" any");
2622 else
2623 printf(" %d", o->tun_local);
2624 if (o->tun_remote == SSH_TUNID_ANY)
2625 printf(":any");
2626 else
2627 printf(":%d", o->tun_remote);
2628 printf("\n");
2629
2630 /* oCanonicalizePermittedCNAMEs */
2631 if ( o->num_permitted_cnames > 0) {
2632 printf("canonicalizePermittedcnames");
2633 for (i = 0; i < o->num_permitted_cnames; i++) {
2634 printf(" %s:%s", o->permitted_cnames[i].source_list,
2635 o->permitted_cnames[i].target_list);
2636 }
2637 printf("\n");
2638 }
2639
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002640 /* oControlPersist */
2641 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2642 dump_cfg_fmtint(oControlPersist, o->control_persist);
2643 else
2644 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2645
2646 /* oEscapeChar */
2647 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2648 printf("escapechar none\n");
2649 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002650 vis(buf, o->escape_char, VIS_WHITE, 0);
2651 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002652 }
2653
2654 /* oIPQoS */
2655 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2656 printf("%s\n", iptos2str(o->ip_qos_bulk));
2657
2658 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002659 printf("rekeylimit %llu %d\n",
2660 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002661
2662 /* oStreamLocalBindMask */
2663 printf("streamlocalbindmask 0%o\n",
2664 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002665
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002666 /* oLogFacility */
2667 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2668
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002669 /* oProxyCommand / oProxyJump */
2670 if (o->jump_host == NULL)
2671 dump_cfg_string(oProxyCommand, o->proxy_command);
2672 else {
2673 /* Check for numeric addresses */
2674 i = strchr(o->jump_host, ':') != NULL ||
2675 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2676 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2677 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002678 /* optional additional jump spec */
2679 o->jump_extra == NULL ? "" : o->jump_extra,
2680 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002681 /* optional user */
2682 o->jump_user == NULL ? "" : o->jump_user,
2683 o->jump_user == NULL ? "" : "@",
2684 /* opening [ if hostname is numeric */
2685 i ? "[" : "",
2686 /* mandatory hostname */
2687 o->jump_host,
2688 /* closing ] if hostname is numeric */
2689 i ? "]" : "",
2690 /* optional port number */
2691 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002692 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002693 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002694}