blob: 8d20295473bcbe7640aa634324783066be6b25e2 [file] [log] [blame]
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001/* $OpenBSD: readconf.c,v 1.291 2018/06/10 23:45:41 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,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000164 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000165 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 },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000280 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000281 { "controlpath", oControlPath },
282 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000283 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100284 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000285 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100286 { "tunnel", oTunnel },
287 { "tunneldevice", oTunnelDevice },
288 { "localcommand", oLocalCommand },
289 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000290 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000291 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000292 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100293 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000294 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000295 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100296 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100297 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
298 { "canonicalizehostname", oCanonicalizeHostname },
299 { "canonicalizemaxdots", oCanonicalizeMaxDots },
300 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000301 { "streamlocalbindmask", oStreamLocalBindMask },
302 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000303 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000304 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000305 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000306 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000307 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000308 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000309 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100310
Ben Lindstrom65366a82001-12-06 16:32:47 +0000311 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100312};
313
Damien Miller5428f641999-11-25 11:54:57 +1100314/*
315 * Adds a local TCP/IP port forward to options. Never returns if there is an
316 * error.
317 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller4af51302000-04-16 11:18:38 +1000319void
Damien Miller7acefbb2014-07-18 14:11:24 +1000320add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Damien Miller7acefbb2014-07-18 14:11:24 +1000322 struct Forward *fwd;
Damien Miller95def091999-11-25 00:26:21 +1100323 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000324 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000325
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000326 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000327 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000328 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000329 /* Don't add duplicates */
330 for (i = 0; i < options->num_local_forwards; i++) {
331 if (forward_equals(newfwd, options->local_forwards + i))
332 return;
333 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000334 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000335 options->num_local_forwards + 1,
336 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100337 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338
Damien Miller1a0442f2008-11-05 16:30:06 +1100339 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100342 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100343 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000344 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345}
346
Damien Miller5428f641999-11-25 11:54:57 +1100347/*
348 * Adds a remote TCP/IP port forward to options. Never returns if there is
349 * an error.
350 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller4af51302000-04-16 11:18:38 +1000352void
Damien Miller7acefbb2014-07-18 14:11:24 +1000353add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354{
Damien Miller7acefbb2014-07-18 14:11:24 +1000355 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000356 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000357
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000358 /* Don't add duplicates */
359 for (i = 0; i < options->num_remote_forwards; i++) {
360 if (forward_equals(newfwd, options->remote_forwards + i))
361 return;
362 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000363 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000364 options->num_remote_forwards + 1,
365 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100366 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367
Damien Miller1a0442f2008-11-05 16:30:06 +1100368 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100369 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000370 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100371 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100372 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000373 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100374 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000375 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376}
377
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000378static void
379clear_forwardings(Options *options)
380{
381 int i;
382
Damien Millerf91ee4c2005-03-01 21:24:33 +1100383 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000385 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000387 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100388 }
Damien Miller232cfb12010-06-26 09:50:30 +1000389 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000390 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000391 options->local_forwards = NULL;
392 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000393 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100394 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000396 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000398 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100399 }
Damien Miller232cfb12010-06-26 09:50:30 +1000400 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000401 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000402 options->remote_forwards = NULL;
403 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000404 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100405 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000406}
407
Darren Tucker19104782013-04-05 11:13:08 +1100408void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000409add_certificate_file(Options *options, const char *path, int userprovided)
410{
411 int i;
412
413 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
414 fatal("Too many certificate files specified (max %d)",
415 SSH_MAX_CERTIFICATE_FILES);
416
417 /* Avoid registering duplicates */
418 for (i = 0; i < options->num_certificate_files; i++) {
419 if (options->certificate_file_userprovided[i] == userprovided &&
420 strcmp(options->certificate_files[i], path) == 0) {
421 debug2("%s: ignoring duplicate key %s", __func__, path);
422 return;
423 }
424 }
425
426 options->certificate_file_userprovided[options->num_certificate_files] =
427 userprovided;
428 options->certificate_files[options->num_certificate_files++] =
429 xstrdup(path);
430}
431
432void
Darren Tucker19104782013-04-05 11:13:08 +1100433add_identity_file(Options *options, const char *dir, const char *filename,
434 int userprovided)
435{
436 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000437 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100438
439 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
440 fatal("Too many identity files specified (max %d)",
441 SSH_MAX_IDENTITY_FILES);
442
443 if (dir == NULL) /* no dir, filename is absolute */
444 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000445 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
446 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100447
Damien Miller15271902014-05-15 13:47:56 +1000448 /* Avoid registering duplicates */
449 for (i = 0; i < options->num_identity_files; i++) {
450 if (options->identity_file_userprovided[i] == userprovided &&
451 strcmp(options->identity_files[i], path) == 0) {
452 debug2("%s: ignoring duplicate key %s", __func__, path);
453 free(path);
454 return;
455 }
456 }
457
Darren Tucker19104782013-04-05 11:13:08 +1100458 options->identity_file_userprovided[options->num_identity_files] =
459 userprovided;
460 options->identity_files[options->num_identity_files++] = path;
461}
462
Damien Miller194fd902013-10-15 12:13:05 +1100463int
464default_ssh_port(void)
465{
466 static int port;
467 struct servent *sp;
468
469 if (port == 0) {
470 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
471 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
472 }
473 return port;
474}
475
476/*
477 * Execute a command in a shell.
478 * Return its exit status or -1 on abnormal exit.
479 */
480static int
481execute_in_shell(const char *cmd)
482{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000483 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100484 pid_t pid;
485 int devnull, status;
486 extern uid_t original_real_uid;
487
488 if ((shell = getenv("SHELL")) == NULL)
489 shell = _PATH_BSHELL;
490
Damien Miller194fd902013-10-15 12:13:05 +1100491 /* Need this to redirect subprocess stdin/out */
492 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
493 fatal("open(/dev/null): %s", strerror(errno));
494
495 debug("Executing command: '%.500s'", cmd);
496
497 /* Fork and execute the command. */
498 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000499 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100500
501 /* Child. Permanently give up superuser privileges. */
502 permanently_drop_suid(original_real_uid);
503
504 /* Redirect child stdin and stdout. Leave stderr */
505 if (dup2(devnull, STDIN_FILENO) == -1)
506 fatal("dup2: %s", strerror(errno));
507 if (dup2(devnull, STDOUT_FILENO) == -1)
508 fatal("dup2: %s", strerror(errno));
509 if (devnull > STDERR_FILENO)
510 close(devnull);
511 closefrom(STDERR_FILENO + 1);
512
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000513 argv[0] = shell;
514 argv[1] = "-c";
515 argv[2] = xstrdup(cmd);
516 argv[3] = NULL;
517
Damien Miller194fd902013-10-15 12:13:05 +1100518 execv(argv[0], argv);
519 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
520 /* Die with signal to make this error apparent to parent. */
521 signal(SIGTERM, SIG_DFL);
522 kill(getpid(), SIGTERM);
523 _exit(1);
524 }
525 /* Parent. */
526 if (pid < 0)
527 fatal("%s: fork: %.100s", __func__, strerror(errno));
528
529 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100530
531 while (waitpid(pid, &status, 0) == -1) {
532 if (errno != EINTR && errno != EAGAIN)
533 fatal("%s: waitpid: %s", __func__, strerror(errno));
534 }
535 if (!WIFEXITED(status)) {
536 error("command '%.100s' exited abnormally", cmd);
537 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000538 }
Damien Miller194fd902013-10-15 12:13:05 +1100539 debug3("command returned status %d", WEXITSTATUS(status));
540 return WEXITSTATUS(status);
541}
542
543/*
544 * Parse and execute a Match directive.
545 */
546static int
547match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 const char *host_arg, const char *original_host, int post_canon,
549 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100550{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000551 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100552 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100554 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000555 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100556
557 /*
558 * Configuration is likely to be incomplete at this point so we
559 * must be prepared to use default values.
560 */
561 port = options->port <= 0 ? default_ssh_port() : options->port;
562 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000563 if (post_canon) {
564 host = xstrdup(options->hostname);
565 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100566 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = percent_expand(options->hostname,
568 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000569 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100570 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000573 debug2("checking match for '%s' host %s originally %s",
574 cp, host, original_host);
575 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
576 criteria = NULL;
577 this_result = 1;
578 if ((negate = attrib[0] == '!'))
579 attrib++;
580 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100581 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100583 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 error("%.200s line %d: '%s' cannot be combined "
585 "with other Match attributes",
586 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100587 result = -1;
588 goto out;
589 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 if (result)
591 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100592 goto out;
593 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000594 attributes++;
595 if (strcasecmp(attrib, "canonical") == 0) {
596 r = !!post_canon; /* force bitmask member to boolean */
597 if (r == (negate ? 1 : 0))
598 this_result = result = 0;
599 debug3("%.200s line %d: %smatched '%s'",
600 filename, linenum,
601 this_result ? "" : "not ", oattrib);
602 continue;
603 }
604 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100605 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
606 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100607 result = -1;
608 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100609 }
Damien Miller194fd902013-10-15 12:13:05 +1100610 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000612 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 if (r == (negate ? 1 : 0))
614 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100615 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000617 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 if (r == (negate ? 1 : 0))
619 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100620 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000622 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 if (r == (negate ? 1 : 0))
624 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100625 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000626 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000627 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000628 if (r == (negate ? 1 : 0))
629 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100630 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100631 if (gethostname(thishost, sizeof(thishost)) == -1)
632 fatal("gethostname: %s", strerror(errno));
633 strlcpy(shorthost, thishost, sizeof(shorthost));
634 shorthost[strcspn(thishost, ".")] = '\0';
635 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000636 snprintf(uidstr, sizeof(uidstr), "%llu",
637 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100638
639 cmd = percent_expand(arg,
640 "L", shorthost,
641 "d", pw->pw_dir,
642 "h", host,
643 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000644 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100645 "p", portstr,
646 "r", ruser,
647 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000648 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100649 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100650 if (result != 1) {
651 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000652 debug3("%.200s line %d: skipped exec "
653 "\"%.100s\"", filename, linenum, cmd);
654 free(cmd);
655 continue;
Damien Miller06287802014-02-24 15:56:45 +1100656 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000657 r = execute_in_shell(cmd);
658 if (r == -1) {
659 fatal("%.200s line %d: match exec "
660 "'%.100s' error", filename,
661 linenum, cmd);
662 }
663 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100664 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000665 /* Force exit status to boolean */
666 r = r == 0;
667 if (r == (negate ? 1 : 0))
668 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100669 } else {
670 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100671 result = -1;
672 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100673 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000674 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
675 filename, linenum, this_result ? "": "not ",
676 oattrib, criteria);
677 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100678 }
Damien Millercf31f382013-10-24 21:02:56 +1100679 if (attributes == 0) {
680 error("One or more attributes required for Match");
681 result = -1;
682 goto out;
683 }
Damien Miller084bcd22013-10-23 16:30:51 +1100684 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000685 if (result != -1)
686 debug2("match %sfound", result ? "" : "not ");
687 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100688 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100689 return result;
690}
691
djm@openbsd.org555294a2018-04-06 13:02:39 +0000692/* Remove environment variable by pattern */
693static void
694rm_env(Options *options, const char *arg, const char *filename, int linenum)
695{
696 int i, j;
697 char *cp;
698
699 /* Remove an environment variable */
700 for (i = 0; i < options->num_send_env; ) {
701 cp = xstrdup(options->send_env[i]);
702 if (!match_pattern(cp, arg + 1)) {
703 free(cp);
704 i++;
705 continue;
706 }
707 debug3("%s line %d: removing environment %s",
708 filename, linenum, cp);
709 free(cp);
710 free(options->send_env[i]);
711 options->send_env[i] = NULL;
712 for (j = i; j < options->num_send_env - 1; j++) {
713 options->send_env[j] = options->send_env[j + 1];
714 options->send_env[j + 1] = NULL;
715 }
716 options->num_send_env--;
717 /* NB. don't increment i */
718 }
719}
720
Damien Miller5428f641999-11-25 11:54:57 +1100721/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000722 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100723 */
Damien Miller4af51302000-04-16 11:18:38 +1000724static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000725parse_token(const char *cp, const char *filename, int linenum,
726 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727{
Darren Tucker07636982013-05-16 20:30:03 +1000728 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller95def091999-11-25 00:26:21 +1100730 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000731 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100732 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000733 if (ignored_unknown != NULL &&
734 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000735 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000736 error("%s: line %d: Bad configuration option: %s",
737 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100738 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739}
740
Damien Millere9fc72e2013-10-15 12:14:12 +1100741/* Multistate option parsing */
742struct multistate {
743 char *key;
744 int value;
745};
746static const struct multistate multistate_flag[] = {
747 { "true", 1 },
748 { "false", 0 },
749 { "yes", 1 },
750 { "no", 0 },
751 { NULL, -1 }
752};
753static const struct multistate multistate_yesnoask[] = {
754 { "true", 1 },
755 { "false", 0 },
756 { "yes", 1 },
757 { "no", 0 },
758 { "ask", 2 },
759 { NULL, -1 }
760};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000761static const struct multistate multistate_strict_hostkey[] = {
762 { "true", SSH_STRICT_HOSTKEY_YES },
763 { "false", SSH_STRICT_HOSTKEY_OFF },
764 { "yes", SSH_STRICT_HOSTKEY_YES },
765 { "no", SSH_STRICT_HOSTKEY_OFF },
766 { "ask", SSH_STRICT_HOSTKEY_ASK },
767 { "off", SSH_STRICT_HOSTKEY_OFF },
768 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
769 { NULL, -1 }
770};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000771static const struct multistate multistate_yesnoaskconfirm[] = {
772 { "true", 1 },
773 { "false", 0 },
774 { "yes", 1 },
775 { "no", 0 },
776 { "ask", 2 },
777 { "confirm", 3 },
778 { NULL, -1 }
779};
Damien Millere9fc72e2013-10-15 12:14:12 +1100780static const struct multistate multistate_addressfamily[] = {
781 { "inet", AF_INET },
782 { "inet6", AF_INET6 },
783 { "any", AF_UNSPEC },
784 { NULL, -1 }
785};
786static const struct multistate multistate_controlmaster[] = {
787 { "true", SSHCTL_MASTER_YES },
788 { "yes", SSHCTL_MASTER_YES },
789 { "false", SSHCTL_MASTER_NO },
790 { "no", SSHCTL_MASTER_NO },
791 { "auto", SSHCTL_MASTER_AUTO },
792 { "ask", SSHCTL_MASTER_ASK },
793 { "autoask", SSHCTL_MASTER_AUTO_ASK },
794 { NULL, -1 }
795};
796static const struct multistate multistate_tunnel[] = {
797 { "ethernet", SSH_TUNMODE_ETHERNET },
798 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
799 { "true", SSH_TUNMODE_DEFAULT },
800 { "yes", SSH_TUNMODE_DEFAULT },
801 { "false", SSH_TUNMODE_NO },
802 { "no", SSH_TUNMODE_NO },
803 { NULL, -1 }
804};
805static const struct multistate multistate_requesttty[] = {
806 { "true", REQUEST_TTY_YES },
807 { "yes", REQUEST_TTY_YES },
808 { "false", REQUEST_TTY_NO },
809 { "no", REQUEST_TTY_NO },
810 { "force", REQUEST_TTY_FORCE },
811 { "auto", REQUEST_TTY_AUTO },
812 { NULL, -1 }
813};
Damien Miller38505592013-10-17 11:48:13 +1100814static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100815 { "true", SSH_CANONICALISE_YES },
816 { "false", SSH_CANONICALISE_NO },
817 { "yes", SSH_CANONICALISE_YES },
818 { "no", SSH_CANONICALISE_NO },
819 { "always", SSH_CANONICALISE_ALWAYS },
820 { NULL, -1 }
821};
Damien Millere9fc72e2013-10-15 12:14:12 +1100822
Damien Miller5428f641999-11-25 11:54:57 +1100823/*
824 * Processes a single option line as used in the configuration files. This
825 * only sets those values that have not already been set.
826 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100827int
Damien Miller194fd902013-10-15 12:13:05 +1100828process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000829 const char *original_host, char *line, const char *filename,
830 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000832 return process_config_line_depth(options, pw, host, original_host,
833 line, filename, linenum, activep, flags, 0);
834}
835
836#define WHITESPACE " \t\r\n"
837static int
838process_config_line_depth(Options *options, struct passwd *pw, const char *host,
839 const char *original_host, char *line, const char *filename,
840 int linenum, int *activep, int flags, int depth)
841{
Damien Miller295ee632011-05-29 21:42:31 +1000842 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
843 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000844 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000845 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000846 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100847 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000848 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000849 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100850 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000851 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100852 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100853 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000854 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000855 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller194fd902013-10-15 12:13:05 +1100857 if (activep == NULL) { /* We are processing a command line directive */
858 cmdline = 1;
859 activep = &cmdline;
860 }
861
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000862 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000863 if ((len = strlen(line)) == 0)
864 return 0;
865 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000866 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000867 break;
868 line[len] = '\0';
869 }
870
Damien Millerbe484b52000-07-15 14:14:16 +1000871 s = line;
872 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100873 if ((keyword = strdelim(&s)) == NULL)
874 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000875 /* Ignore leading whitespace. */
876 if (*keyword == '\0')
877 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000878 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100879 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000880 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100881 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Darren Tucker07636982013-05-16 20:30:03 +1000883 opcode = parse_token(keyword, filename, linenum,
884 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Damien Miller95def091999-11-25 00:26:21 +1100886 switch (opcode) {
887 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100888 /* don't panic, but count bad options */
889 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000890 case oIgnore:
891 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000892 case oIgnoredUnknownOption:
893 debug("%s line %d: Ignored unknown option \"%s\"",
894 filename, linenum, keyword);
895 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000896 case oConnectTimeout:
897 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100898parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000899 arg = strdelim(&s);
900 if (!arg || *arg == '\0')
901 fatal("%s line %d: missing time value.",
902 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000903 if (strcmp(arg, "none") == 0)
904 value = -1;
905 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000906 fatal("%s line %d: invalid time value.",
907 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100908 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000909 *intptr = value;
910 break;
911
Damien Miller95def091999-11-25 00:26:21 +1100912 case oForwardAgent:
913 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100914 parse_flag:
915 multistate_ptr = multistate_flag;
916 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000917 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000918 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100919 fatal("%s line %d: missing argument.",
920 filename, linenum);
921 value = -1;
922 for (i = 0; multistate_ptr[i].key != NULL; i++) {
923 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
924 value = multistate_ptr[i].value;
925 break;
926 }
927 }
928 if (value == -1)
929 fatal("%s line %d: unsupported option \"%s\".",
930 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100931 if (*activep && *intptr == -1)
932 *intptr = value;
933 break;
934
935 case oForwardX11:
936 intptr = &options->forward_x11;
937 goto parse_flag;
938
Darren Tucker0a118da2003-10-15 15:54:32 +1000939 case oForwardX11Trusted:
940 intptr = &options->forward_x11_trusted;
941 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000942
Damien Miller1ab6a512010-06-26 10:02:24 +1000943 case oForwardX11Timeout:
944 intptr = &options->forward_x11_timeout;
945 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000946
Damien Miller95def091999-11-25 00:26:21 +1100947 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000948 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100949 goto parse_flag;
950
Darren Tuckere7d4b192006-07-12 22:17:10 +1000951 case oExitOnForwardFailure:
952 intptr = &options->exit_on_forward_failure;
953 goto parse_flag;
954
Damien Miller95def091999-11-25 00:26:21 +1100955 case oUsePrivilegedPort:
956 intptr = &options->use_privileged_port;
957 goto parse_flag;
958
Damien Miller95def091999-11-25 00:26:21 +1100959 case oPasswordAuthentication:
960 intptr = &options->password_authentication;
961 goto parse_flag;
962
Damien Miller874d77b2000-10-14 16:23:11 +1100963 case oKbdInteractiveAuthentication:
964 intptr = &options->kbd_interactive_authentication;
965 goto parse_flag;
966
967 case oKbdInteractiveDevices:
968 charptr = &options->kbd_interactive_devices;
969 goto parse_string;
970
Damien Miller0bc1bd82000-11-13 22:57:25 +1100971 case oPubkeyAuthentication:
972 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000973 goto parse_flag;
974
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000975 case oHostbasedAuthentication:
976 intptr = &options->hostbased_authentication;
977 goto parse_flag;
978
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000979 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000980 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100981 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000982
Darren Tucker0efd1552003-08-26 11:49:55 +1000983 case oGssAuthentication:
984 intptr = &options->gss_authentication;
985 goto parse_flag;
986
987 case oGssDelegateCreds:
988 intptr = &options->gss_deleg_creds;
989 goto parse_flag;
990
Damien Miller95def091999-11-25 00:26:21 +1100991 case oBatchMode:
992 intptr = &options->batch_mode;
993 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller95def091999-11-25 00:26:21 +1100995 case oCheckHostIP:
996 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000997 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller37876e92003-05-15 10:19:46 +1000999 case oVerifyHostKeyDNS:
1000 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001001 multistate_ptr = multistate_yesnoask;
1002 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001003
Damien Miller95def091999-11-25 00:26:21 +11001004 case oStrictHostKeyChecking:
1005 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001006 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001007 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller95def091999-11-25 00:26:21 +11001009 case oCompression:
1010 intptr = &options->compression;
1011 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller12c150e2003-12-17 16:31:10 +11001013 case oTCPKeepAlive:
1014 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001015 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001017 case oNoHostAuthenticationForLocalhost:
1018 intptr = &options->no_host_authentication_for_localhost;
1019 goto parse_flag;
1020
Damien Miller95def091999-11-25 00:26:21 +11001021 case oNumberOfPasswordPrompts:
1022 intptr = &options->number_of_password_prompts;
1023 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Millera5539d22003-04-09 20:50:06 +10001025 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001026 arg = strdelim(&s);
1027 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001028 fatal("%.200s line %d: Missing argument.", filename,
1029 linenum);
1030 if (strcmp(arg, "default") == 0) {
1031 val64 = 0;
1032 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001033 if (scan_scaled(arg, &val64) == -1)
1034 fatal("%.200s line %d: Bad number '%s': %s",
1035 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001036 if (val64 != 0 && val64 < 16)
1037 fatal("%.200s line %d: RekeyLimit too small",
1038 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001039 }
Damien Miller3dff1762008-02-10 22:25:52 +11001040 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001041 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001042 if (s != NULL) { /* optional rekey interval present */
1043 if (strcmp(s, "none") == 0) {
1044 (void)strdelim(&s); /* discard */
1045 break;
1046 }
1047 intptr = &options->rekey_interval;
1048 goto parse_time;
1049 }
Damien Millera5539d22003-04-09 20:50:06 +10001050 break;
1051
Damien Miller95def091999-11-25 00:26:21 +11001052 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001053 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001054 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001055 fatal("%.200s line %d: Missing argument.", filename, linenum);
1056 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001057 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001058 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001059 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001060 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001061 add_identity_file(options, NULL,
1062 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001063 }
1064 break;
1065
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001066 case oCertificateFile:
1067 arg = strdelim(&s);
1068 if (!arg || *arg == '\0')
1069 fatal("%.200s line %d: Missing argument.",
1070 filename, linenum);
1071 if (*activep) {
1072 intptr = &options->num_certificate_files;
1073 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1074 fatal("%.200s line %d: Too many certificate "
1075 "files specified (max %d).",
1076 filename, linenum,
1077 SSH_MAX_CERTIFICATE_FILES);
1078 }
1079 add_certificate_file(options, arg,
1080 flags & SSHCONF_USERCONF);
1081 }
1082 break;
1083
Damien Millerd3a18572000-06-07 19:55:44 +10001084 case oXAuthLocation:
1085 charptr=&options->xauth_location;
1086 goto parse_string;
1087
Damien Miller95def091999-11-25 00:26:21 +11001088 case oUser:
1089 charptr = &options->user;
1090parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001091 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001092 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001093 fatal("%.200s line %d: Missing argument.",
1094 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001095 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001096 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001097 break;
1098
1099 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001100 cpptr = (char **)&options->system_hostfiles;
1101 uintptr = &options->num_system_hostfiles;
1102 max_entries = SSH_MAX_HOSTS_FILES;
1103parse_char_array:
1104 if (*activep && *uintptr == 0) {
1105 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1106 if ((*uintptr) >= max_entries)
1107 fatal("%s line %d: "
1108 "too many authorized keys files.",
1109 filename, linenum);
1110 cpptr[(*uintptr)++] = xstrdup(arg);
1111 }
1112 }
1113 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001114
1115 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001116 cpptr = (char **)&options->user_hostfiles;
1117 uintptr = &options->num_user_hostfiles;
1118 max_entries = SSH_MAX_HOSTS_FILES;
1119 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001120
Damien Miller95def091999-11-25 00:26:21 +11001121 case oHostName:
1122 charptr = &options->hostname;
1123 goto parse_string;
1124
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001125 case oHostKeyAlias:
1126 charptr = &options->host_key_alias;
1127 goto parse_string;
1128
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001129 case oPreferredAuthentications:
1130 charptr = &options->preferred_authentications;
1131 goto parse_string;
1132
Ben Lindstrome0f88042001-04-30 13:06:24 +00001133 case oBindAddress:
1134 charptr = &options->bind_address;
1135 goto parse_string;
1136
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001137 case oBindInterface:
1138 charptr = &options->bind_interface;
1139 goto parse_string;
1140
Damien Miller7ea845e2010-02-12 09:21:02 +11001141 case oPKCS11Provider:
1142 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001143 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001144
Damien Miller95def091999-11-25 00:26:21 +11001145 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001146 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001147 /* Ignore ProxyCommand if ProxyJump already specified */
1148 if (options->jump_host != NULL)
1149 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001150parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001151 if (s == NULL)
1152 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001153 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001154 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001155 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001156 return 0;
1157
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001158 case oProxyJump:
1159 if (s == NULL) {
1160 fatal("%.200s line %d: Missing argument.",
1161 filename, linenum);
1162 }
1163 len = strspn(s, WHITESPACE "=");
1164 if (parse_jump(s + len, options, *activep) == -1) {
1165 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1166 filename, linenum, s + len);
1167 }
1168 return 0;
1169
Damien Miller95def091999-11-25 00:26:21 +11001170 case oPort:
1171 intptr = &options->port;
1172parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001173 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001174 if ((errstr = atoi_err(arg, &value)) != NULL)
1175 fatal("%s line %d: integer value %s.",
1176 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001177 if (*activep && *intptr == -1)
1178 *intptr = value;
1179 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180
Damien Miller95def091999-11-25 00:26:21 +11001181 case oConnectionAttempts:
1182 intptr = &options->connection_attempts;
1183 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001184
Damien Miller78928792000-04-12 20:17:38 +10001185 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001186 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001187 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001188 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001189 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001190 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001191 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001192 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001193 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001194 break;
1195
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001196 case oMacs:
1197 arg = strdelim(&s);
1198 if (!arg || *arg == '\0')
1199 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001200 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001201 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001202 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001203 if (*activep && options->macs == NULL)
1204 options->macs = xstrdup(arg);
1205 break;
1206
Damien Millerd5f62bf2010-09-24 22:11:14 +10001207 case oKexAlgorithms:
1208 arg = strdelim(&s);
1209 if (!arg || *arg == '\0')
1210 fatal("%.200s line %d: Missing argument.",
1211 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001212 if (*arg != '-' &&
1213 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001214 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1215 filename, linenum, arg ? arg : "<NONE>");
1216 if (*activep && options->kex_algorithms == NULL)
1217 options->kex_algorithms = xstrdup(arg);
1218 break;
1219
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001220 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001221 charptr = &options->hostkeyalgorithms;
1222parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001223 arg = strdelim(&s);
1224 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001225 fatal("%.200s line %d: Missing argument.",
1226 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001227 if (*arg != '-' &&
1228 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001229 fatal("%s line %d: Bad key types '%s'.",
1230 filename, linenum, arg ? arg : "<NONE>");
1231 if (*activep && *charptr == NULL)
1232 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001233 break;
1234
Damien Miller95def091999-11-25 00:26:21 +11001235 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001236 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001237 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001238 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001239 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001240 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001241 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001242 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1243 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001244 break;
1245
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001246 case oLogFacility:
1247 log_facility_ptr = &options->log_facility;
1248 arg = strdelim(&s);
1249 value = log_facility_number(arg);
1250 if (value == SYSLOG_FACILITY_NOT_SET)
1251 fatal("%.200s line %d: unsupported log facility '%s'",
1252 filename, linenum, arg ? arg : "<NONE>");
1253 if (*log_facility_ptr == -1)
1254 *log_facility_ptr = (SyslogFacility) value;
1255 break;
1256
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001257 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001258 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001259 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001260 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001261 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001262 fatal("%.200s line %d: Missing port argument.",
1263 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001264
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001265 remotefwd = (opcode == oRemoteForward);
1266 dynamicfwd = (opcode == oDynamicForward);
1267
1268 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001269 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001270 if (arg2 == NULL || *arg2 == '\0') {
1271 if (remotefwd)
1272 dynamicfwd = 1;
1273 else
1274 fatal("%.200s line %d: Missing target "
1275 "argument.", filename, linenum);
1276 } else {
1277 /* construct a string for parse_forward */
1278 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1279 arg2);
1280 }
Damien Millera699d952008-11-03 19:27:34 +11001281 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001282 if (dynamicfwd)
1283 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001284
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001285 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001286 fatal("%.200s line %d: Bad forwarding specification.",
1287 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001288
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001289 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001290 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001291 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001292 } else {
1293 add_local_forward(options, &fwd);
1294 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001295 }
Damien Miller95def091999-11-25 00:26:21 +11001296 break;
1297
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001298 case oClearAllForwardings:
1299 intptr = &options->clear_forwardings;
1300 goto parse_flag;
1301
Damien Miller95def091999-11-25 00:26:21 +11001302 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001303 if (cmdline)
1304 fatal("Host directive not supported as a command-line "
1305 "option");
Damien Miller95def091999-11-25 00:26:21 +11001306 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001307 arg2 = NULL;
1308 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001309 if ((flags & SSHCONF_NEVERMATCH) != 0)
1310 break;
Damien Millerfe924212011-05-15 08:44:45 +10001311 negated = *arg == '!';
1312 if (negated)
1313 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001314 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001315 if (negated) {
1316 debug("%.200s line %d: Skipping Host "
1317 "block because of negated match "
1318 "for %.100s", filename, linenum,
1319 arg);
1320 *activep = 0;
1321 break;
1322 }
1323 if (!*activep)
1324 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001325 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001326 }
Damien Millerfe924212011-05-15 08:44:45 +10001327 }
1328 if (*activep)
1329 debug("%.200s line %d: Applying options for %.100s",
1330 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001331 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001332 return 0;
1333
Damien Miller194fd902013-10-15 12:13:05 +11001334 case oMatch:
1335 if (cmdline)
1336 fatal("Host directive not supported as a command-line "
1337 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001338 value = match_cfg_line(options, &s, pw, host, original_host,
1339 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001340 if (value < 0)
1341 fatal("%.200s line %d: Bad Match condition", filename,
1342 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001343 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001344 break;
1345
Damien Miller95def091999-11-25 00:26:21 +11001346 case oEscapeChar:
1347 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001348 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001349 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001350 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001351 if (strcmp(arg, "none") == 0)
1352 value = SSH_ESCAPECHAR_NONE;
1353 else if (arg[1] == '\0')
1354 value = (u_char) arg[0];
1355 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001356 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1357 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001358 else {
1359 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001360 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001361 /* NOTREACHED */
1362 value = 0; /* Avoid compiler warning. */
1363 }
1364 if (*activep && *intptr == -1)
1365 *intptr = value;
1366 break;
1367
Damien Miller20a8f972003-05-18 20:50:30 +10001368 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001369 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001370 multistate_ptr = multistate_addressfamily;
1371 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001372
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001373 case oEnableSSHKeysign:
1374 intptr = &options->enable_ssh_keysign;
1375 goto parse_flag;
1376
Damien Millerbd394c32004-03-08 23:12:36 +11001377 case oIdentitiesOnly:
1378 intptr = &options->identities_only;
1379 goto parse_flag;
1380
Damien Miller509b0102003-12-17 16:33:10 +11001381 case oServerAliveInterval:
1382 intptr = &options->server_alive_interval;
1383 goto parse_time;
1384
1385 case oServerAliveCountMax:
1386 intptr = &options->server_alive_count_max;
1387 goto parse_int;
1388
Darren Tucker46bc0752004-05-02 22:11:30 +10001389 case oSendEnv:
1390 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1391 if (strchr(arg, '=') != NULL)
1392 fatal("%s line %d: Invalid environment name.",
1393 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001394 if (!*activep)
1395 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001396 if (*arg == '-') {
1397 /* Removing an env var */
1398 rm_env(options, arg, filename, linenum);
1399 continue;
1400 } else {
1401 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001402 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001403 fatal("%s line %d: too many send env.",
1404 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001405 options->send_env = xrecallocarray(
1406 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001407 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001408 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001409 options->send_env[options->num_send_env++] =
1410 xstrdup(arg);
1411 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001412 }
1413 break;
1414
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001415 case oSetEnv:
1416 value = options->num_setenv;
1417 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1418 if (strchr(arg, '=') == NULL)
1419 fatal("%s line %d: Invalid SetEnv.",
1420 filename, linenum);
1421 if (!*activep || value != 0)
1422 continue;
1423 /* Adding a setenv var */
1424 if (options->num_setenv >= INT_MAX)
1425 fatal("%s line %d: too many SetEnv.",
1426 filename, linenum);
1427 options->setenv = xrecallocarray(
1428 options->setenv, options->num_setenv,
1429 options->num_setenv + 1, sizeof(*options->setenv));
1430 options->setenv[options->num_setenv++] = xstrdup(arg);
1431 }
1432 break;
1433
Damien Miller0e220db2004-06-15 10:34:08 +10001434 case oControlPath:
1435 charptr = &options->control_path;
1436 goto parse_string;
1437
1438 case oControlMaster:
1439 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001440 multistate_ptr = multistate_controlmaster;
1441 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001442
Damien Millere11e1ea2010-08-03 16:04:46 +10001443 case oControlPersist:
1444 /* no/false/yes/true, or a time spec */
1445 intptr = &options->control_persist;
1446 arg = strdelim(&s);
1447 if (!arg || *arg == '\0')
1448 fatal("%.200s line %d: Missing ControlPersist"
1449 " argument.", filename, linenum);
1450 value = 0;
1451 value2 = 0; /* timeout */
1452 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1453 value = 0;
1454 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1455 value = 1;
1456 else if ((value2 = convtime(arg)) >= 0)
1457 value = 1;
1458 else
1459 fatal("%.200s line %d: Bad ControlPersist argument.",
1460 filename, linenum);
1461 if (*activep && *intptr == -1) {
1462 *intptr = value;
1463 options->control_persist_timeout = value2;
1464 }
1465 break;
1466
Damien Millere1776152005-03-01 21:47:37 +11001467 case oHashKnownHosts:
1468 intptr = &options->hash_known_hosts;
1469 goto parse_flag;
1470
Damien Millerd27b9472005-12-13 19:29:02 +11001471 case oTunnel:
1472 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001473 multistate_ptr = multistate_tunnel;
1474 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001475
1476 case oTunnelDevice:
1477 arg = strdelim(&s);
1478 if (!arg || *arg == '\0')
1479 fatal("%.200s line %d: Missing argument.", filename, linenum);
1480 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001481 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001482 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1483 if (*activep) {
1484 options->tun_local = value;
1485 options->tun_remote = value2;
1486 }
1487 break;
1488
1489 case oLocalCommand:
1490 charptr = &options->local_command;
1491 goto parse_command;
1492
1493 case oPermitLocalCommand:
1494 intptr = &options->permit_local_command;
1495 goto parse_flag;
1496
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001497 case oRemoteCommand:
1498 charptr = &options->remote_command;
1499 goto parse_command;
1500
Damien Miller10288242008-06-30 00:04:03 +10001501 case oVisualHostKey:
1502 intptr = &options->visual_host_key;
1503 goto parse_flag;
1504
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001505 case oInclude:
1506 if (cmdline)
1507 fatal("Include directive not supported as a "
1508 "command-line option");
1509 value = 0;
1510 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1511 /*
1512 * Ensure all paths are anchored. User configuration
1513 * files may begin with '~/' but system configurations
1514 * must not. If the path is relative, then treat it
1515 * as living in ~/.ssh for user configurations or
1516 * /etc/ssh for system ones.
1517 */
1518 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1519 fatal("%.200s line %d: bad include path %s.",
1520 filename, linenum, arg);
1521 if (*arg != '/' && *arg != '~') {
1522 xasprintf(&arg2, "%s/%s",
1523 (flags & SSHCONF_USERCONF) ?
1524 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1525 } else
1526 arg2 = xstrdup(arg);
1527 memset(&gl, 0, sizeof(gl));
1528 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1529 if (r == GLOB_NOMATCH) {
1530 debug("%.200s line %d: include %s matched no "
1531 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001532 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001533 continue;
1534 } else if (r != 0 || gl.gl_pathc < 0)
1535 fatal("%.200s line %d: glob failed for %s.",
1536 filename, linenum, arg2);
1537 free(arg2);
1538 oactive = *activep;
1539 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1540 debug3("%.200s line %d: Including file %s "
1541 "depth %d%s", filename, linenum,
1542 gl.gl_pathv[i], depth,
1543 oactive ? "" : " (parse only)");
1544 r = read_config_file_depth(gl.gl_pathv[i],
1545 pw, host, original_host, options,
1546 flags | SSHCONF_CHECKPERM |
1547 (oactive ? 0 : SSHCONF_NEVERMATCH),
1548 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001549 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001550 fatal("Can't open user config file "
1551 "%.100s: %.100s", gl.gl_pathv[i],
1552 strerror(errno));
1553 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001554 /*
1555 * don't let Match in includes clobber the
1556 * containing file's Match state.
1557 */
1558 *activep = oactive;
1559 if (r != 1)
1560 value = -1;
1561 }
1562 globfree(&gl);
1563 }
1564 if (value != 0)
1565 return value;
1566 break;
1567
Damien Miller0dac6fb2010-11-20 15:19:38 +11001568 case oIPQoS:
1569 arg = strdelim(&s);
1570 if ((value = parse_ipqos(arg)) == -1)
1571 fatal("%s line %d: Bad IPQoS value: %s",
1572 filename, linenum, arg);
1573 arg = strdelim(&s);
1574 if (arg == NULL)
1575 value2 = value;
1576 else if ((value2 = parse_ipqos(arg)) == -1)
1577 fatal("%s line %d: Bad IPQoS value: %s",
1578 filename, linenum, arg);
1579 if (*activep) {
1580 options->ip_qos_interactive = value;
1581 options->ip_qos_bulk = value2;
1582 }
1583 break;
1584
Damien Miller21771e22011-05-15 08:45:50 +10001585 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001586 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001587 multistate_ptr = multistate_requesttty;
1588 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001589
Darren Tucker07636982013-05-16 20:30:03 +10001590 case oIgnoreUnknown:
1591 charptr = &options->ignored_unknown;
1592 goto parse_string;
1593
Damien Miller1262b662013-08-21 02:44:24 +10001594 case oProxyUseFdpass:
1595 intptr = &options->proxy_use_fdpass;
1596 goto parse_flag;
1597
Damien Miller0faf7472013-10-17 11:47:23 +11001598 case oCanonicalDomains:
1599 value = options->num_canonical_domains != 0;
1600 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001601 if (!valid_domain(arg, 1, &errstr)) {
1602 fatal("%s line %d: %s", filename, linenum,
1603 errstr);
1604 }
Damien Miller0faf7472013-10-17 11:47:23 +11001605 if (!*activep || value)
1606 continue;
1607 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1608 fatal("%s line %d: too many hostname suffixes.",
1609 filename, linenum);
1610 options->canonical_domains[
1611 options->num_canonical_domains++] = xstrdup(arg);
1612 }
1613 break;
1614
Damien Miller38505592013-10-17 11:48:13 +11001615 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001616 value = options->num_permitted_cnames != 0;
1617 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1618 /* Either '*' for everything or 'list:list' */
1619 if (strcmp(arg, "*") == 0)
1620 arg2 = arg;
1621 else {
1622 lowercase(arg);
1623 if ((arg2 = strchr(arg, ':')) == NULL ||
1624 arg2[1] == '\0') {
1625 fatal("%s line %d: "
1626 "Invalid permitted CNAME \"%s\"",
1627 filename, linenum, arg);
1628 }
1629 *arg2 = '\0';
1630 arg2++;
1631 }
1632 if (!*activep || value)
1633 continue;
1634 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1635 fatal("%s line %d: too many permitted CNAMEs.",
1636 filename, linenum);
1637 cname = options->permitted_cnames +
1638 options->num_permitted_cnames++;
1639 cname->source_list = xstrdup(arg);
1640 cname->target_list = xstrdup(arg2);
1641 }
1642 break;
1643
Damien Miller38505592013-10-17 11:48:13 +11001644 case oCanonicalizeHostname:
1645 intptr = &options->canonicalize_hostname;
1646 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001647 goto parse_multistate;
1648
Damien Miller38505592013-10-17 11:48:13 +11001649 case oCanonicalizeMaxDots:
1650 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001651 goto parse_int;
1652
Damien Miller38505592013-10-17 11:48:13 +11001653 case oCanonicalizeFallbackLocal:
1654 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001655 goto parse_flag;
1656
Damien Miller7acefbb2014-07-18 14:11:24 +10001657 case oStreamLocalBindMask:
1658 arg = strdelim(&s);
1659 if (!arg || *arg == '\0')
1660 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1661 /* Parse mode in octal format */
1662 value = strtol(arg, &endofnumber, 8);
1663 if (arg == endofnumber || value < 0 || value > 0777)
1664 fatal("%.200s line %d: Bad mask.", filename, linenum);
1665 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1666 break;
1667
1668 case oStreamLocalBindUnlink:
1669 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1670 goto parse_flag;
1671
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001672 case oRevokedHostKeys:
1673 charptr = &options->revoked_host_keys;
1674 goto parse_string;
1675
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001676 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001677 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001678 arg = strdelim(&s);
1679 if (!arg || *arg == '\0')
1680 fatal("%.200s line %d: Missing argument.",
1681 filename, linenum);
1682 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1683 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1684 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001685 if (*activep && *intptr == -1)
1686 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001687 break;
1688
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001689 case oUpdateHostkeys:
1690 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001691 multistate_ptr = multistate_yesnoask;
1692 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001693
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001694 case oHostbasedKeyTypes:
1695 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001696 goto parse_keytypes;
1697
1698 case oPubkeyAcceptedKeyTypes:
1699 charptr = &options->pubkey_key_types;
1700 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001701
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001702 case oAddKeysToAgent:
1703 intptr = &options->add_keys_to_agent;
1704 multistate_ptr = multistate_yesnoaskconfirm;
1705 goto parse_multistate;
1706
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001707 case oIdentityAgent:
1708 charptr = &options->identity_agent;
1709 goto parse_string;
1710
Ben Lindstrom4daea862002-06-09 20:04:02 +00001711 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001712 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001713 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001714 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001715
Damien Millerf9b3feb2003-05-16 11:38:32 +10001716 case oUnsupported:
1717 error("%s line %d: Unsupported option \"%s\"",
1718 filename, linenum, keyword);
1719 return 0;
1720
Damien Miller95def091999-11-25 00:26:21 +11001721 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001722 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001723 }
1724
1725 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001726 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001727 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001728 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001729 }
Damien Miller95def091999-11-25 00:26:21 +11001730 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731}
1732
Damien Miller5428f641999-11-25 11:54:57 +11001733/*
1734 * Reads the config file and modifies the options accordingly. Options
1735 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001736 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001737 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001738int
Damien Miller194fd902013-10-15 12:13:05 +11001739read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001740 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001742 int active = 1;
1743
1744 return read_config_file_depth(filename, pw, host, original_host,
1745 options, flags, &active, 0);
1746}
1747
1748#define READCONF_MAX_DEPTH 16
1749static int
1750read_config_file_depth(const char *filename, struct passwd *pw,
1751 const char *host, const char *original_host, Options *options,
1752 int flags, int *activep, int depth)
1753{
Damien Miller95def091999-11-25 00:26:21 +11001754 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001755 char *line = NULL;
1756 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001757 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001758 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001759
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001760 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1761 fatal("Too many recursive configuration includes");
1762
Damien Miller57a44762004-04-20 20:11:57 +10001763 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001764 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001765
Darren Tuckeraefa3682013-04-05 11:18:35 +11001766 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001767 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001768
Damien Miller33793852004-06-15 10:27:55 +10001769 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001770 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001771 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001772 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001773 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001774 }
1775
Damien Miller95def091999-11-25 00:26:21 +11001776 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001777
Damien Miller5428f641999-11-25 11:54:57 +11001778 /*
1779 * Mark that we are now processing the options. This flag is turned
1780 * on/off by Host specifications.
1781 */
Damien Miller95def091999-11-25 00:26:21 +11001782 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001783 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001784 /* Update line number counter. */
1785 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001786 if (process_config_line_depth(options, pw, host, original_host,
1787 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001788 bad_options++;
1789 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001790 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001791 fclose(f);
1792 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001793 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001794 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001795 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001796}
1797
Damien Miller13f97b22014-02-24 15:57:55 +11001798/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1799int
1800option_clear_or_none(const char *o)
1801{
1802 return o == NULL || strcasecmp(o, "none") == 0;
1803}
1804
Damien Miller5428f641999-11-25 11:54:57 +11001805/*
1806 * Initializes options to special values that indicate that they have not yet
1807 * been set. Read_config_file will only set options with this value. Options
1808 * are processed in the following order: command line, user config file,
1809 * system config file. Last, fill_default_options is called.
1810 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Miller4af51302000-04-16 11:18:38 +10001812void
Damien Miller95def091999-11-25 00:26:21 +11001813initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814{
Damien Miller95def091999-11-25 00:26:21 +11001815 memset(options, 'X', sizeof(*options));
1816 options->forward_agent = -1;
1817 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001818 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001819 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001820 options->stdio_forward_host = NULL;
1821 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001822 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001823 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001824 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001825 options->fwd_opts.gateway_ports = -1;
1826 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1827 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001828 options->use_privileged_port = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001829 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001830 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001831 options->gss_authentication = -1;
1832 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001833 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001834 options->kbd_interactive_authentication = -1;
1835 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001836 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001837 options->batch_mode = -1;
1838 options->check_host_ip = -1;
1839 options->strict_host_key_checking = -1;
1840 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001841 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001842 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001843 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001844 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001845 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001846 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001847 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001848 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001849 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001850 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001851 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001852 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001853 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001854 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001855 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001856 options->jump_user = NULL;
1857 options->jump_host = NULL;
1858 options->jump_port = -1;
1859 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001860 options->user = NULL;
1861 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001862 options->num_system_hostfiles = 0;
1863 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001864 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001865 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001866 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001867 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001868 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001869 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001870 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001871 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001872 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001873 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001874 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001875 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001876 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001877 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001878 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001879 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001880 options->server_alive_interval = -1;
1881 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001882 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001883 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001884 options->setenv = NULL;
1885 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001886 options->control_path = NULL;
1887 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001888 options->control_persist = -1;
1889 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001890 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001891 options->tun_open = -1;
1892 options->tun_local = -1;
1893 options->tun_remote = -1;
1894 options->local_command = NULL;
1895 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001896 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001897 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001898 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001899 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001900 options->ip_qos_interactive = -1;
1901 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001902 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001903 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001904 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001905 options->num_canonical_domains = 0;
1906 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001907 options->canonicalize_max_dots = -1;
1908 options->canonicalize_fallback_local = -1;
1909 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001910 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001911 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001912 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001913 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001914 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001915}
1916
Damien Miller5428f641999-11-25 11:54:57 +11001917/*
Damien Miller13f97b22014-02-24 15:57:55 +11001918 * A petite version of fill_default_options() that just fills the options
1919 * needed for hostname canonicalization to proceed.
1920 */
1921void
1922fill_default_options_for_canonicalization(Options *options)
1923{
1924 if (options->canonicalize_max_dots == -1)
1925 options->canonicalize_max_dots = 1;
1926 if (options->canonicalize_fallback_local == -1)
1927 options->canonicalize_fallback_local = 1;
1928 if (options->canonicalize_hostname == -1)
1929 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1930}
1931
1932/*
Damien Miller5428f641999-11-25 11:54:57 +11001933 * Called after processing other sources of option data, this fills those
1934 * options for which no value has been specified with their default values.
1935 */
Damien Miller4af51302000-04-16 11:18:38 +10001936void
Damien Miller95def091999-11-25 00:26:21 +11001937fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938{
Damien Miller95def091999-11-25 00:26:21 +11001939 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001940 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001941 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001942 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001943 if (options->forward_x11_trusted == -1)
1944 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001945 if (options->forward_x11_timeout == -1)
1946 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001947 /*
1948 * stdio forwarding (-W) changes the default for these but we defer
1949 * setting the values so they can be overridden.
1950 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001951 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001952 options->exit_on_forward_failure =
1953 options->stdio_forward_host != NULL ? 1 : 0;
1954 if (options->clear_forwardings == -1)
1955 options->clear_forwardings =
1956 options->stdio_forward_host != NULL ? 1 : 0;
1957 if (options->clear_forwardings == 1)
1958 clear_forwardings(options);
1959
Damien Millerd3a18572000-06-07 19:55:44 +10001960 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001961 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001962 if (options->fwd_opts.gateway_ports == -1)
1963 options->fwd_opts.gateway_ports = 0;
1964 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1965 options->fwd_opts.streamlocal_bind_mask = 0177;
1966 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1967 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001968 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001969 options->use_privileged_port = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001970 if (options->pubkey_authentication == -1)
1971 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001972 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001973 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001974 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001975 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001976 if (options->gss_deleg_creds == -1)
1977 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001978 if (options->password_authentication == -1)
1979 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001980 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001981 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001982 if (options->hostbased_authentication == -1)
1983 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001984 if (options->batch_mode == -1)
1985 options->batch_mode = 0;
1986 if (options->check_host_ip == -1)
1987 options->check_host_ip = 1;
1988 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001989 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001990 if (options->compression == -1)
1991 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001992 if (options->tcp_keep_alive == -1)
1993 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001994 if (options->port == -1)
1995 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001996 if (options->address_family == -1)
1997 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001998 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001999 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002000 if (options->number_of_password_prompts == -1)
2001 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002002 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002003 if (options->add_keys_to_agent == -1)
2004 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002005 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002006 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2007 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002008#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002009 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002010#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002011 add_identity_file(options, "~/",
2012 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002013 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002014 }
Damien Miller95def091999-11-25 00:26:21 +11002015 if (options->escape_char == -1)
2016 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002017 if (options->num_system_hostfiles == 0) {
2018 options->system_hostfiles[options->num_system_hostfiles++] =
2019 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2020 options->system_hostfiles[options->num_system_hostfiles++] =
2021 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2022 }
2023 if (options->num_user_hostfiles == 0) {
2024 options->user_hostfiles[options->num_user_hostfiles++] =
2025 xstrdup(_PATH_SSH_USER_HOSTFILE);
2026 options->user_hostfiles[options->num_user_hostfiles++] =
2027 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2028 }
Damien Millerfcd93202002-02-05 12:26:34 +11002029 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002030 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002031 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2032 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002033 if (options->no_host_authentication_for_localhost == - 1)
2034 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002035 if (options->identities_only == -1)
2036 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002037 if (options->enable_ssh_keysign == -1)
2038 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002039 if (options->rekey_limit == -1)
2040 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002041 if (options->rekey_interval == -1)
2042 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002043 if (options->verify_host_key_dns == -1)
2044 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002045 if (options->server_alive_interval == -1)
2046 options->server_alive_interval = 0;
2047 if (options->server_alive_count_max == -1)
2048 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002049 if (options->control_master == -1)
2050 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002051 if (options->control_persist == -1) {
2052 options->control_persist = 0;
2053 options->control_persist_timeout = 0;
2054 }
Damien Millere1776152005-03-01 21:47:37 +11002055 if (options->hash_known_hosts == -1)
2056 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002057 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002058 options->tun_open = SSH_TUNMODE_NO;
2059 if (options->tun_local == -1)
2060 options->tun_local = SSH_TUNID_ANY;
2061 if (options->tun_remote == -1)
2062 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002063 if (options->permit_local_command == -1)
2064 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002065 if (options->visual_host_key == -1)
2066 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002067 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002068 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002069 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002070 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002071 if (options->request_tty == -1)
2072 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002073 if (options->proxy_use_fdpass == -1)
2074 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002075 if (options->canonicalize_max_dots == -1)
2076 options->canonicalize_max_dots = 1;
2077 if (options->canonicalize_fallback_local == -1)
2078 options->canonicalize_fallback_local = 1;
2079 if (options->canonicalize_hostname == -1)
2080 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002081 if (options->fingerprint_hash == -1)
2082 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002083 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002084 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002085 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2086 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2087 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2088 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2089 &options->hostbased_key_types) != 0 ||
2090 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2091 &options->pubkey_key_types) != 0)
2092 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002093
Damien Millere9fc72e2013-10-15 12:14:12 +11002094#define CLEAR_ON_NONE(v) \
2095 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002096 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002097 free(v); \
2098 v = NULL; \
2099 } \
2100 } while(0)
2101 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002102 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002103 CLEAR_ON_NONE(options->proxy_command);
2104 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002105 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002106 if (options->jump_host != NULL &&
2107 strcmp(options->jump_host, "none") == 0 &&
2108 options->jump_port == 0 && options->jump_user == NULL) {
2109 free(options->jump_host);
2110 options->jump_host = NULL;
2111 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002112 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002113 /* options->user will be set in the main program if appropriate */
2114 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002115 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002116 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002117}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002118
Damien Miller7acefbb2014-07-18 14:11:24 +10002119struct fwdarg {
2120 char *arg;
2121 int ispath;
2122};
2123
2124/*
2125 * parse_fwd_field
2126 * parses the next field in a port forwarding specification.
2127 * sets fwd to the parsed field and advances p past the colon
2128 * or sets it to NULL at end of string.
2129 * returns 0 on success, else non-zero.
2130 */
2131static int
2132parse_fwd_field(char **p, struct fwdarg *fwd)
2133{
2134 char *ep, *cp = *p;
2135 int ispath = 0;
2136
2137 if (*cp == '\0') {
2138 *p = NULL;
2139 return -1; /* end of string */
2140 }
2141
2142 /*
2143 * A field escaped with square brackets is used literally.
2144 * XXX - allow ']' to be escaped via backslash?
2145 */
2146 if (*cp == '[') {
2147 /* find matching ']' */
2148 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2149 if (*ep == '/')
2150 ispath = 1;
2151 }
2152 /* no matching ']' or not at end of field. */
2153 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2154 return -1;
2155 /* NUL terminate the field and advance p past the colon */
2156 *ep++ = '\0';
2157 if (*ep != '\0')
2158 *ep++ = '\0';
2159 fwd->arg = cp + 1;
2160 fwd->ispath = ispath;
2161 *p = ep;
2162 return 0;
2163 }
2164
2165 for (cp = *p; *cp != '\0'; cp++) {
2166 switch (*cp) {
2167 case '\\':
2168 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002169 if (*cp == '\0')
2170 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002171 break;
2172 case '/':
2173 ispath = 1;
2174 break;
2175 case ':':
2176 *cp++ = '\0';
2177 goto done;
2178 }
2179 }
2180done:
2181 fwd->arg = *p;
2182 fwd->ispath = ispath;
2183 *p = cp;
2184 return 0;
2185}
2186
Damien Millerf91ee4c2005-03-01 21:24:33 +11002187/*
2188 * parse_forward
2189 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002190 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002191 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2192 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002193 * dynamicfwd == 1
2194 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002195 * returns number of arguments parsed or zero on error
2196 */
2197int
Damien Miller7acefbb2014-07-18 14:11:24 +10002198parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002199{
Damien Miller7acefbb2014-07-18 14:11:24 +10002200 struct fwdarg fwdargs[4];
2201 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002202 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002203
Damien Miller7acefbb2014-07-18 14:11:24 +10002204 memset(fwd, 0, sizeof(*fwd));
2205 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002206
2207 cp = p = xstrdup(fwdspec);
2208
2209 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002210 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002211 cp++;
2212
Damien Miller7acefbb2014-07-18 14:11:24 +10002213 for (i = 0; i < 4; ++i) {
2214 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002215 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002216 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002217
Damien Millerf4b39532008-11-03 19:28:21 +11002218 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002219 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002220 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002221 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002222
2223 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002224 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002225 if (fwdargs[0].ispath) {
2226 fwd->listen_path = xstrdup(fwdargs[0].arg);
2227 fwd->listen_port = PORT_STREAMLOCAL;
2228 } else {
2229 fwd->listen_host = NULL;
2230 fwd->listen_port = a2port(fwdargs[0].arg);
2231 }
Damien Millera699d952008-11-03 19:27:34 +11002232 fwd->connect_host = xstrdup("socks");
2233 break;
2234
2235 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002236 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2237 fwd->listen_path = xstrdup(fwdargs[0].arg);
2238 fwd->listen_port = PORT_STREAMLOCAL;
2239 fwd->connect_path = xstrdup(fwdargs[1].arg);
2240 fwd->connect_port = PORT_STREAMLOCAL;
2241 } else if (fwdargs[1].ispath) {
2242 fwd->listen_host = NULL;
2243 fwd->listen_port = a2port(fwdargs[0].arg);
2244 fwd->connect_path = xstrdup(fwdargs[1].arg);
2245 fwd->connect_port = PORT_STREAMLOCAL;
2246 } else {
2247 fwd->listen_host = xstrdup(fwdargs[0].arg);
2248 fwd->listen_port = a2port(fwdargs[1].arg);
2249 fwd->connect_host = xstrdup("socks");
2250 }
Damien Millera699d952008-11-03 19:27:34 +11002251 break;
2252
Damien Millerf91ee4c2005-03-01 21:24:33 +11002253 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002254 if (fwdargs[0].ispath) {
2255 fwd->listen_path = xstrdup(fwdargs[0].arg);
2256 fwd->listen_port = PORT_STREAMLOCAL;
2257 fwd->connect_host = xstrdup(fwdargs[1].arg);
2258 fwd->connect_port = a2port(fwdargs[2].arg);
2259 } else if (fwdargs[2].ispath) {
2260 fwd->listen_host = xstrdup(fwdargs[0].arg);
2261 fwd->listen_port = a2port(fwdargs[1].arg);
2262 fwd->connect_path = xstrdup(fwdargs[2].arg);
2263 fwd->connect_port = PORT_STREAMLOCAL;
2264 } else {
2265 fwd->listen_host = NULL;
2266 fwd->listen_port = a2port(fwdargs[0].arg);
2267 fwd->connect_host = xstrdup(fwdargs[1].arg);
2268 fwd->connect_port = a2port(fwdargs[2].arg);
2269 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002270 break;
2271
2272 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002273 fwd->listen_host = xstrdup(fwdargs[0].arg);
2274 fwd->listen_port = a2port(fwdargs[1].arg);
2275 fwd->connect_host = xstrdup(fwdargs[2].arg);
2276 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002277 break;
2278 default:
2279 i = 0; /* failure */
2280 }
2281
Darren Tuckera627d422013-06-02 07:31:17 +10002282 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002283
Damien Millera699d952008-11-03 19:27:34 +11002284 if (dynamicfwd) {
2285 if (!(i == 1 || i == 2))
2286 goto fail_free;
2287 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002288 if (!(i == 3 || i == 4)) {
2289 if (fwd->connect_path == NULL &&
2290 fwd->listen_path == NULL)
2291 goto fail_free;
2292 }
2293 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002294 goto fail_free;
2295 }
2296
Damien Miller7acefbb2014-07-18 14:11:24 +10002297 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2298 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002299 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002300 if (fwd->connect_host != NULL &&
2301 strlen(fwd->connect_host) >= NI_MAXHOST)
2302 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002303 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2304 if (fwd->connect_path != NULL &&
2305 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2306 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002307 if (fwd->listen_host != NULL &&
2308 strlen(fwd->listen_host) >= NI_MAXHOST)
2309 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002310 if (fwd->listen_path != NULL &&
2311 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2312 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002313
2314 return (i);
2315
2316 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002317 free(fwd->connect_host);
2318 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002319 free(fwd->connect_path);
2320 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002321 free(fwd->listen_host);
2322 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002323 free(fwd->listen_path);
2324 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002325 return (0);
2326}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002327
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002328int
2329parse_jump(const char *s, Options *o, int active)
2330{
2331 char *orig, *sdup, *cp;
2332 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002333 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002334
2335 active &= o->proxy_command == NULL && o->jump_host == NULL;
2336
2337 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002338 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002339 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002340 if (strcasecmp(s, "none") == 0)
2341 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002342 if ((cp = strrchr(sdup, ',')) == NULL)
2343 cp = sdup; /* last */
2344 else
2345 *cp++ = '\0';
2346
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002347 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002348 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002349 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2350 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002351 goto out;
2352 } else {
2353 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002354 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2355 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002356 goto out;
2357 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002358 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002359 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002360 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002361 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002362 if (strcasecmp(s, "none") == 0) {
2363 o->jump_host = xstrdup("none");
2364 o->jump_port = 0;
2365 } else {
2366 o->jump_user = user;
2367 o->jump_host = host;
2368 o->jump_port = port;
2369 o->proxy_command = xstrdup("none");
2370 user = host = NULL;
2371 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2372 o->jump_extra = xstrdup(s);
2373 o->jump_extra[cp - s] = '\0';
2374 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002375 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002376 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002377 ret = 0;
2378 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002379 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002380 free(user);
2381 free(host);
2382 return ret;
2383}
2384
millert@openbsd.org887669e2017-10-21 23:06:24 +00002385int
2386parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2387{
2388 char *path;
2389 int r;
2390
2391 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2392 if (r == 0 && path != NULL)
2393 r = -1; /* path not allowed */
2394 return r;
2395}
2396
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002397/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2398static const char *
2399fmt_multistate_int(int val, const struct multistate *m)
2400{
2401 u_int i;
2402
2403 for (i = 0; m[i].key != NULL; i++) {
2404 if (m[i].value == val)
2405 return m[i].key;
2406 }
2407 return "UNKNOWN";
2408}
2409
2410static const char *
2411fmt_intarg(OpCodes code, int val)
2412{
2413 if (val == -1)
2414 return "unset";
2415 switch (code) {
2416 case oAddressFamily:
2417 return fmt_multistate_int(val, multistate_addressfamily);
2418 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002419 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002420 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002421 case oStrictHostKeyChecking:
2422 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002423 case oControlMaster:
2424 return fmt_multistate_int(val, multistate_controlmaster);
2425 case oTunnel:
2426 return fmt_multistate_int(val, multistate_tunnel);
2427 case oRequestTTY:
2428 return fmt_multistate_int(val, multistate_requesttty);
2429 case oCanonicalizeHostname:
2430 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002431 case oAddKeysToAgent:
2432 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002433 case oFingerprintHash:
2434 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002435 default:
2436 switch (val) {
2437 case 0:
2438 return "no";
2439 case 1:
2440 return "yes";
2441 default:
2442 return "UNKNOWN";
2443 }
2444 }
2445}
2446
2447static const char *
2448lookup_opcode_name(OpCodes code)
2449{
2450 u_int i;
2451
2452 for (i = 0; keywords[i].name != NULL; i++)
2453 if (keywords[i].opcode == code)
2454 return(keywords[i].name);
2455 return "UNKNOWN";
2456}
2457
2458static void
2459dump_cfg_int(OpCodes code, int val)
2460{
2461 printf("%s %d\n", lookup_opcode_name(code), val);
2462}
2463
2464static void
2465dump_cfg_fmtint(OpCodes code, int val)
2466{
2467 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2468}
2469
2470static void
2471dump_cfg_string(OpCodes code, const char *val)
2472{
2473 if (val == NULL)
2474 return;
2475 printf("%s %s\n", lookup_opcode_name(code), val);
2476}
2477
2478static void
2479dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2480{
2481 u_int i;
2482
2483 for (i = 0; i < count; i++)
2484 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2485}
2486
2487static void
2488dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2489{
2490 u_int i;
2491
2492 printf("%s", lookup_opcode_name(code));
2493 for (i = 0; i < count; i++)
2494 printf(" %s", vals[i]);
2495 printf("\n");
2496}
2497
2498static void
2499dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2500{
2501 const struct Forward *fwd;
2502 u_int i;
2503
2504 /* oDynamicForward */
2505 for (i = 0; i < count; i++) {
2506 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002507 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002508 strcmp(fwd->connect_host, "socks") != 0)
2509 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002510 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002511 strcmp(fwd->connect_host, "socks") == 0)
2512 continue;
2513 printf("%s", lookup_opcode_name(code));
2514 if (fwd->listen_port == PORT_STREAMLOCAL)
2515 printf(" %s", fwd->listen_path);
2516 else if (fwd->listen_host == NULL)
2517 printf(" %d", fwd->listen_port);
2518 else {
2519 printf(" [%s]:%d",
2520 fwd->listen_host, fwd->listen_port);
2521 }
2522 if (code != oDynamicForward) {
2523 if (fwd->connect_port == PORT_STREAMLOCAL)
2524 printf(" %s", fwd->connect_path);
2525 else if (fwd->connect_host == NULL)
2526 printf(" %d", fwd->connect_port);
2527 else {
2528 printf(" [%s]:%d",
2529 fwd->connect_host, fwd->connect_port);
2530 }
2531 }
2532 printf("\n");
2533 }
2534}
2535
2536void
2537dump_client_config(Options *o, const char *host)
2538{
2539 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002540 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002541
djm@openbsd.org60a92472015-08-21 23:53:08 +00002542 /* This is normally prepared in ssh_kex2 */
2543 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2544 fatal("%s: kex_assemble_names failed", __func__);
2545
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002546 /* Most interesting options first: user, host, port */
2547 dump_cfg_string(oUser, o->user);
2548 dump_cfg_string(oHostName, host);
2549 dump_cfg_int(oPort, o->port);
2550
2551 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002552 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002553 dump_cfg_fmtint(oAddressFamily, o->address_family);
2554 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2555 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2556 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2557 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2558 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2559 dump_cfg_fmtint(oCompression, o->compression);
2560 dump_cfg_fmtint(oControlMaster, o->control_master);
2561 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002562 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002563 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002564 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002565 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2566 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2567 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2568 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2569#ifdef GSSAPI
2570 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2571 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2572#endif /* GSSAPI */
2573 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2574 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2575 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2576 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2577 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2578 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2579 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002580 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2581 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2582 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002583 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2584 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2585 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2586 dump_cfg_fmtint(oTunnel, o->tun_open);
2587 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2588 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2589 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002590 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002591
2592 /* Integer options */
2593 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002594 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2595 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2596 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2597 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2598 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2599
2600 /* String options */
2601 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002602 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002603 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2604 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002605 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002606 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002607 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002608 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002609 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002610 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2611 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2612 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002613 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002614 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2615 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002616#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002617 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002618#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002619 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002620 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002621 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002622 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002623
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002624 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002625 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2626 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2627 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2628
2629 /* String array options */
2630 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2631 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002632 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002633 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2634 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2635 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002636 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002637
2638 /* Special cases */
2639
2640 /* oConnectTimeout */
2641 if (o->connection_timeout == -1)
2642 printf("connecttimeout none\n");
2643 else
2644 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2645
2646 /* oTunnelDevice */
2647 printf("tunneldevice");
2648 if (o->tun_local == SSH_TUNID_ANY)
2649 printf(" any");
2650 else
2651 printf(" %d", o->tun_local);
2652 if (o->tun_remote == SSH_TUNID_ANY)
2653 printf(":any");
2654 else
2655 printf(":%d", o->tun_remote);
2656 printf("\n");
2657
2658 /* oCanonicalizePermittedCNAMEs */
2659 if ( o->num_permitted_cnames > 0) {
2660 printf("canonicalizePermittedcnames");
2661 for (i = 0; i < o->num_permitted_cnames; i++) {
2662 printf(" %s:%s", o->permitted_cnames[i].source_list,
2663 o->permitted_cnames[i].target_list);
2664 }
2665 printf("\n");
2666 }
2667
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002668 /* oControlPersist */
2669 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2670 dump_cfg_fmtint(oControlPersist, o->control_persist);
2671 else
2672 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2673
2674 /* oEscapeChar */
2675 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2676 printf("escapechar none\n");
2677 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002678 vis(buf, o->escape_char, VIS_WHITE, 0);
2679 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002680 }
2681
2682 /* oIPQoS */
2683 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2684 printf("%s\n", iptos2str(o->ip_qos_bulk));
2685
2686 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002687 printf("rekeylimit %llu %d\n",
2688 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002689
2690 /* oStreamLocalBindMask */
2691 printf("streamlocalbindmask 0%o\n",
2692 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002693
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002694 /* oLogFacility */
2695 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2696
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002697 /* oProxyCommand / oProxyJump */
2698 if (o->jump_host == NULL)
2699 dump_cfg_string(oProxyCommand, o->proxy_command);
2700 else {
2701 /* Check for numeric addresses */
2702 i = strchr(o->jump_host, ':') != NULL ||
2703 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2704 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2705 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002706 /* optional additional jump spec */
2707 o->jump_extra == NULL ? "" : o->jump_extra,
2708 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002709 /* optional user */
2710 o->jump_user == NULL ? "" : o->jump_user,
2711 o->jump_user == NULL ? "" : "@",
2712 /* opening [ if hostname is numeric */
2713 i ? "[" : "",
2714 /* mandatory hostname */
2715 o->jump_host,
2716 /* closing ] if hostname is numeric */
2717 i ? "]" : "",
2718 /* optional port number */
2719 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002720 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002721 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002722}