blob: 4ab312fff41ec50e5d86e9354ce57cf74f9bd9b6 [file] [log] [blame]
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00001/* $OpenBSD: readconf.c,v 1.296 2018/07/27 05:34:42 dtucker 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 },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000193 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000194
195 /* Unsupported options */
196 { "afstokenpassing", oUnsupported },
197 { "kerberosauthentication", oUnsupported },
198 { "kerberostgtpassing", oUnsupported },
199
200 /* Sometimes-unsupported options */
201#if defined(GSSAPI)
202 { "gssapiauthentication", oGssAuthentication },
203 { "gssapidelegatecredentials", oGssDelegateCreds },
204# else
205 { "gssapiauthentication", oUnsupported },
206 { "gssapidelegatecredentials", oUnsupported },
207#endif
208#ifdef ENABLE_PKCS11
209 { "smartcarddevice", oPKCS11Provider },
210 { "pkcs11provider", oPKCS11Provider },
211# else
212 { "smartcarddevice", oUnsupported },
213 { "pkcs11provider", oUnsupported },
214#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000215 { "rsaauthentication", oUnsupported },
216 { "rhostsrsaauthentication", oUnsupported },
217 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000218
Damien Miller95def091999-11-25 00:26:21 +1100219 { "forwardagent", oForwardAgent },
220 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000221 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000222 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000223 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000224 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100225 { "gatewayports", oGatewayPorts },
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;
Darren Tucker5f41f032016-04-08 21:14:13 +1000323 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000324
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000325 /* Don't add duplicates */
326 for (i = 0; i < options->num_local_forwards; i++) {
327 if (forward_equals(newfwd, options->local_forwards + i))
328 return;
329 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000330 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000331 options->num_local_forwards + 1,
332 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100333 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334
Damien Miller1a0442f2008-11-05 16:30:06 +1100335 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000337 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100338 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
Damien Miller5428f641999-11-25 11:54:57 +1100343/*
344 * Adds a remote TCP/IP port forward to options. Never returns if there is
345 * an error.
346 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller4af51302000-04-16 11:18:38 +1000348void
Damien Miller7acefbb2014-07-18 14:11:24 +1000349add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Miller7acefbb2014-07-18 14:11:24 +1000351 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000352 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000353
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000354 /* Don't add duplicates */
355 for (i = 0; i < options->num_remote_forwards; i++) {
356 if (forward_equals(newfwd, options->remote_forwards + i))
357 return;
358 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000359 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000360 options->num_remote_forwards + 1,
361 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100362 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100363
Damien Miller1a0442f2008-11-05 16:30:06 +1100364 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000366 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100367 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000369 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100370 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000371 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000374static void
375clear_forwardings(Options *options)
376{
377 int i;
378
Damien Millerf91ee4c2005-03-01 21:24:33 +1100379 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000380 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000381 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000383 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100384 }
Damien Miller232cfb12010-06-26 09:50:30 +1000385 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000387 options->local_forwards = NULL;
388 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000389 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100390 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000392 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000394 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100395 }
Damien Miller232cfb12010-06-26 09:50:30 +1000396 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000398 options->remote_forwards = NULL;
399 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000400 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100401 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402}
403
Darren Tucker19104782013-04-05 11:13:08 +1100404void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000405add_certificate_file(Options *options, const char *path, int userprovided)
406{
407 int i;
408
409 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
410 fatal("Too many certificate files specified (max %d)",
411 SSH_MAX_CERTIFICATE_FILES);
412
413 /* Avoid registering duplicates */
414 for (i = 0; i < options->num_certificate_files; i++) {
415 if (options->certificate_file_userprovided[i] == userprovided &&
416 strcmp(options->certificate_files[i], path) == 0) {
417 debug2("%s: ignoring duplicate key %s", __func__, path);
418 return;
419 }
420 }
421
422 options->certificate_file_userprovided[options->num_certificate_files] =
423 userprovided;
424 options->certificate_files[options->num_certificate_files++] =
425 xstrdup(path);
426}
427
428void
Darren Tucker19104782013-04-05 11:13:08 +1100429add_identity_file(Options *options, const char *dir, const char *filename,
430 int userprovided)
431{
432 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000433 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100434
435 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
436 fatal("Too many identity files specified (max %d)",
437 SSH_MAX_IDENTITY_FILES);
438
439 if (dir == NULL) /* no dir, filename is absolute */
440 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000441 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
442 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100443
Damien Miller15271902014-05-15 13:47:56 +1000444 /* Avoid registering duplicates */
445 for (i = 0; i < options->num_identity_files; i++) {
446 if (options->identity_file_userprovided[i] == userprovided &&
447 strcmp(options->identity_files[i], path) == 0) {
448 debug2("%s: ignoring duplicate key %s", __func__, path);
449 free(path);
450 return;
451 }
452 }
453
Darren Tucker19104782013-04-05 11:13:08 +1100454 options->identity_file_userprovided[options->num_identity_files] =
455 userprovided;
456 options->identity_files[options->num_identity_files++] = path;
457}
458
Damien Miller194fd902013-10-15 12:13:05 +1100459int
460default_ssh_port(void)
461{
462 static int port;
463 struct servent *sp;
464
465 if (port == 0) {
466 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
467 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
468 }
469 return port;
470}
471
472/*
473 * Execute a command in a shell.
474 * Return its exit status or -1 on abnormal exit.
475 */
476static int
477execute_in_shell(const char *cmd)
478{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000479 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100480 pid_t pid;
481 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100482
483 if ((shell = getenv("SHELL")) == NULL)
484 shell = _PATH_BSHELL;
485
Damien Miller194fd902013-10-15 12:13:05 +1100486 /* Need this to redirect subprocess stdin/out */
487 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
488 fatal("open(/dev/null): %s", strerror(errno));
489
490 debug("Executing command: '%.500s'", cmd);
491
492 /* Fork and execute the command. */
493 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000494 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100495
Damien Miller194fd902013-10-15 12:13:05 +1100496 /* Redirect child stdin and stdout. Leave stderr */
497 if (dup2(devnull, STDIN_FILENO) == -1)
498 fatal("dup2: %s", strerror(errno));
499 if (dup2(devnull, STDOUT_FILENO) == -1)
500 fatal("dup2: %s", strerror(errno));
501 if (devnull > STDERR_FILENO)
502 close(devnull);
503 closefrom(STDERR_FILENO + 1);
504
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000505 argv[0] = shell;
506 argv[1] = "-c";
507 argv[2] = xstrdup(cmd);
508 argv[3] = NULL;
509
Damien Miller194fd902013-10-15 12:13:05 +1100510 execv(argv[0], argv);
511 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
512 /* Die with signal to make this error apparent to parent. */
513 signal(SIGTERM, SIG_DFL);
514 kill(getpid(), SIGTERM);
515 _exit(1);
516 }
517 /* Parent. */
518 if (pid < 0)
519 fatal("%s: fork: %.100s", __func__, strerror(errno));
520
521 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100522
523 while (waitpid(pid, &status, 0) == -1) {
524 if (errno != EINTR && errno != EAGAIN)
525 fatal("%s: waitpid: %s", __func__, strerror(errno));
526 }
527 if (!WIFEXITED(status)) {
528 error("command '%.100s' exited abnormally", cmd);
529 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000530 }
Damien Miller194fd902013-10-15 12:13:05 +1100531 debug3("command returned status %d", WEXITSTATUS(status));
532 return WEXITSTATUS(status);
533}
534
535/*
536 * Parse and execute a Match directive.
537 */
538static int
539match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000540 const char *host_arg, const char *original_host, int post_canon,
541 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100542{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000543 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100544 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000545 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100546 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000547 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100548
549 /*
550 * Configuration is likely to be incomplete at this point so we
551 * must be prepared to use default values.
552 */
553 port = options->port <= 0 ? default_ssh_port() : options->port;
554 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000555 if (post_canon) {
556 host = xstrdup(options->hostname);
557 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100558 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100559 host = percent_expand(options->hostname,
560 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000561 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100562 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000563 }
Damien Miller194fd902013-10-15 12:13:05 +1100564
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000565 debug2("checking match for '%s' host %s originally %s",
566 cp, host, original_host);
567 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
568 criteria = NULL;
569 this_result = 1;
570 if ((negate = attrib[0] == '!'))
571 attrib++;
572 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100573 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000574 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100575 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000576 error("%.200s line %d: '%s' cannot be combined "
577 "with other Match attributes",
578 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100579 result = -1;
580 goto out;
581 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (result)
583 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100584 goto out;
585 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 attributes++;
587 if (strcasecmp(attrib, "canonical") == 0) {
588 r = !!post_canon; /* force bitmask member to boolean */
589 if (r == (negate ? 1 : 0))
590 this_result = result = 0;
591 debug3("%.200s line %d: %smatched '%s'",
592 filename, linenum,
593 this_result ? "" : "not ", oattrib);
594 continue;
595 }
596 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100597 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
598 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100599 result = -1;
600 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100601 }
Damien Miller194fd902013-10-15 12:13:05 +1100602 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000603 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000604 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000605 if (r == (negate ? 1 : 0))
606 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100607 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000609 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 if (r == (negate ? 1 : 0))
611 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100612 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000614 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 if (r == (negate ? 1 : 0))
616 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100617 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000619 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 if (r == (negate ? 1 : 0))
621 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100622 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100623 if (gethostname(thishost, sizeof(thishost)) == -1)
624 fatal("gethostname: %s", strerror(errno));
625 strlcpy(shorthost, thishost, sizeof(shorthost));
626 shorthost[strcspn(thishost, ".")] = '\0';
627 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000628 snprintf(uidstr, sizeof(uidstr), "%llu",
629 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100630
631 cmd = percent_expand(arg,
632 "L", shorthost,
633 "d", pw->pw_dir,
634 "h", host,
635 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100637 "p", portstr,
638 "r", ruser,
639 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000640 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100641 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100642 if (result != 1) {
643 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000644 debug3("%.200s line %d: skipped exec "
645 "\"%.100s\"", filename, linenum, cmd);
646 free(cmd);
647 continue;
Damien Miller06287802014-02-24 15:56:45 +1100648 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000649 r = execute_in_shell(cmd);
650 if (r == -1) {
651 fatal("%.200s line %d: match exec "
652 "'%.100s' error", filename,
653 linenum, cmd);
654 }
655 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100656 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000657 /* Force exit status to boolean */
658 r = r == 0;
659 if (r == (negate ? 1 : 0))
660 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100661 } else {
662 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100663 result = -1;
664 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100665 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000666 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
667 filename, linenum, this_result ? "": "not ",
668 oattrib, criteria);
669 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100670 }
Damien Millercf31f382013-10-24 21:02:56 +1100671 if (attributes == 0) {
672 error("One or more attributes required for Match");
673 result = -1;
674 goto out;
675 }
Damien Miller084bcd22013-10-23 16:30:51 +1100676 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000677 if (result != -1)
678 debug2("match %sfound", result ? "" : "not ");
679 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100680 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100681 return result;
682}
683
djm@openbsd.org555294a2018-04-06 13:02:39 +0000684/* Remove environment variable by pattern */
685static void
686rm_env(Options *options, const char *arg, const char *filename, int linenum)
687{
688 int i, j;
689 char *cp;
690
691 /* Remove an environment variable */
692 for (i = 0; i < options->num_send_env; ) {
693 cp = xstrdup(options->send_env[i]);
694 if (!match_pattern(cp, arg + 1)) {
695 free(cp);
696 i++;
697 continue;
698 }
699 debug3("%s line %d: removing environment %s",
700 filename, linenum, cp);
701 free(cp);
702 free(options->send_env[i]);
703 options->send_env[i] = NULL;
704 for (j = i; j < options->num_send_env - 1; j++) {
705 options->send_env[j] = options->send_env[j + 1];
706 options->send_env[j + 1] = NULL;
707 }
708 options->num_send_env--;
709 /* NB. don't increment i */
710 }
711}
712
Damien Miller5428f641999-11-25 11:54:57 +1100713/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000714 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100715 */
Damien Miller4af51302000-04-16 11:18:38 +1000716static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000717parse_token(const char *cp, const char *filename, int linenum,
718 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719{
Darren Tucker07636982013-05-16 20:30:03 +1000720 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller95def091999-11-25 00:26:21 +1100722 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000723 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100724 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000725 if (ignored_unknown != NULL &&
726 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000727 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000728 error("%s: line %d: Bad configuration option: %s",
729 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100730 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731}
732
Damien Millere9fc72e2013-10-15 12:14:12 +1100733/* Multistate option parsing */
734struct multistate {
735 char *key;
736 int value;
737};
738static const struct multistate multistate_flag[] = {
739 { "true", 1 },
740 { "false", 0 },
741 { "yes", 1 },
742 { "no", 0 },
743 { NULL, -1 }
744};
745static const struct multistate multistate_yesnoask[] = {
746 { "true", 1 },
747 { "false", 0 },
748 { "yes", 1 },
749 { "no", 0 },
750 { "ask", 2 },
751 { NULL, -1 }
752};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000753static const struct multistate multistate_strict_hostkey[] = {
754 { "true", SSH_STRICT_HOSTKEY_YES },
755 { "false", SSH_STRICT_HOSTKEY_OFF },
756 { "yes", SSH_STRICT_HOSTKEY_YES },
757 { "no", SSH_STRICT_HOSTKEY_OFF },
758 { "ask", SSH_STRICT_HOSTKEY_ASK },
759 { "off", SSH_STRICT_HOSTKEY_OFF },
760 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
761 { NULL, -1 }
762};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000763static const struct multistate multistate_yesnoaskconfirm[] = {
764 { "true", 1 },
765 { "false", 0 },
766 { "yes", 1 },
767 { "no", 0 },
768 { "ask", 2 },
769 { "confirm", 3 },
770 { NULL, -1 }
771};
Damien Millere9fc72e2013-10-15 12:14:12 +1100772static const struct multistate multistate_addressfamily[] = {
773 { "inet", AF_INET },
774 { "inet6", AF_INET6 },
775 { "any", AF_UNSPEC },
776 { NULL, -1 }
777};
778static const struct multistate multistate_controlmaster[] = {
779 { "true", SSHCTL_MASTER_YES },
780 { "yes", SSHCTL_MASTER_YES },
781 { "false", SSHCTL_MASTER_NO },
782 { "no", SSHCTL_MASTER_NO },
783 { "auto", SSHCTL_MASTER_AUTO },
784 { "ask", SSHCTL_MASTER_ASK },
785 { "autoask", SSHCTL_MASTER_AUTO_ASK },
786 { NULL, -1 }
787};
788static const struct multistate multistate_tunnel[] = {
789 { "ethernet", SSH_TUNMODE_ETHERNET },
790 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
791 { "true", SSH_TUNMODE_DEFAULT },
792 { "yes", SSH_TUNMODE_DEFAULT },
793 { "false", SSH_TUNMODE_NO },
794 { "no", SSH_TUNMODE_NO },
795 { NULL, -1 }
796};
797static const struct multistate multistate_requesttty[] = {
798 { "true", REQUEST_TTY_YES },
799 { "yes", REQUEST_TTY_YES },
800 { "false", REQUEST_TTY_NO },
801 { "no", REQUEST_TTY_NO },
802 { "force", REQUEST_TTY_FORCE },
803 { "auto", REQUEST_TTY_AUTO },
804 { NULL, -1 }
805};
Damien Miller38505592013-10-17 11:48:13 +1100806static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100807 { "true", SSH_CANONICALISE_YES },
808 { "false", SSH_CANONICALISE_NO },
809 { "yes", SSH_CANONICALISE_YES },
810 { "no", SSH_CANONICALISE_NO },
811 { "always", SSH_CANONICALISE_ALWAYS },
812 { NULL, -1 }
813};
Damien Millere9fc72e2013-10-15 12:14:12 +1100814
Damien Miller5428f641999-11-25 11:54:57 +1100815/*
816 * Processes a single option line as used in the configuration files. This
817 * only sets those values that have not already been set.
818 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100819int
Damien Miller194fd902013-10-15 12:13:05 +1100820process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000821 const char *original_host, char *line, const char *filename,
822 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000824 return process_config_line_depth(options, pw, host, original_host,
825 line, filename, linenum, activep, flags, 0);
826}
827
828#define WHITESPACE " \t\r\n"
829static int
830process_config_line_depth(Options *options, struct passwd *pw, const char *host,
831 const char *original_host, char *line, const char *filename,
832 int linenum, int *activep, int flags, int depth)
833{
Damien Miller295ee632011-05-29 21:42:31 +1000834 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
835 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000836 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000837 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000838 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100839 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000840 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000841 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100842 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000843 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100844 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100845 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000846 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000847 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller194fd902013-10-15 12:13:05 +1100849 if (activep == NULL) { /* We are processing a command line directive */
850 cmdline = 1;
851 activep = &cmdline;
852 }
853
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000854 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000855 if ((len = strlen(line)) == 0)
856 return 0;
857 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000858 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000859 break;
860 line[len] = '\0';
861 }
862
Damien Millerbe484b52000-07-15 14:14:16 +1000863 s = line;
864 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100865 if ((keyword = strdelim(&s)) == NULL)
866 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000867 /* Ignore leading whitespace. */
868 if (*keyword == '\0')
869 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000870 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100871 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000872 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100873 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Darren Tucker07636982013-05-16 20:30:03 +1000875 opcode = parse_token(keyword, filename, linenum,
876 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878 switch (opcode) {
879 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100880 /* don't panic, but count bad options */
881 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000882 case oIgnore:
883 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000884 case oIgnoredUnknownOption:
885 debug("%s line %d: Ignored unknown option \"%s\"",
886 filename, linenum, keyword);
887 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000888 case oConnectTimeout:
889 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100890parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000891 arg = strdelim(&s);
892 if (!arg || *arg == '\0')
893 fatal("%s line %d: missing time value.",
894 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000895 if (strcmp(arg, "none") == 0)
896 value = -1;
897 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000898 fatal("%s line %d: invalid time value.",
899 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100900 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000901 *intptr = value;
902 break;
903
Damien Miller95def091999-11-25 00:26:21 +1100904 case oForwardAgent:
905 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100906 parse_flag:
907 multistate_ptr = multistate_flag;
908 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000909 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000910 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100911 fatal("%s line %d: missing argument.",
912 filename, linenum);
913 value = -1;
914 for (i = 0; multistate_ptr[i].key != NULL; i++) {
915 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
916 value = multistate_ptr[i].value;
917 break;
918 }
919 }
920 if (value == -1)
921 fatal("%s line %d: unsupported option \"%s\".",
922 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100923 if (*activep && *intptr == -1)
924 *intptr = value;
925 break;
926
927 case oForwardX11:
928 intptr = &options->forward_x11;
929 goto parse_flag;
930
Darren Tucker0a118da2003-10-15 15:54:32 +1000931 case oForwardX11Trusted:
932 intptr = &options->forward_x11_trusted;
933 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000934
Damien Miller1ab6a512010-06-26 10:02:24 +1000935 case oForwardX11Timeout:
936 intptr = &options->forward_x11_timeout;
937 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000938
Damien Miller95def091999-11-25 00:26:21 +1100939 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000940 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100941 goto parse_flag;
942
Darren Tuckere7d4b192006-07-12 22:17:10 +1000943 case oExitOnForwardFailure:
944 intptr = &options->exit_on_forward_failure;
945 goto parse_flag;
946
Damien Miller95def091999-11-25 00:26:21 +1100947 case oPasswordAuthentication:
948 intptr = &options->password_authentication;
949 goto parse_flag;
950
Damien Miller874d77b2000-10-14 16:23:11 +1100951 case oKbdInteractiveAuthentication:
952 intptr = &options->kbd_interactive_authentication;
953 goto parse_flag;
954
955 case oKbdInteractiveDevices:
956 charptr = &options->kbd_interactive_devices;
957 goto parse_string;
958
Damien Miller0bc1bd82000-11-13 22:57:25 +1100959 case oPubkeyAuthentication:
960 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000961 goto parse_flag;
962
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000963 case oHostbasedAuthentication:
964 intptr = &options->hostbased_authentication;
965 goto parse_flag;
966
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000967 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000968 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100969 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000970
Darren Tucker0efd1552003-08-26 11:49:55 +1000971 case oGssAuthentication:
972 intptr = &options->gss_authentication;
973 goto parse_flag;
974
975 case oGssDelegateCreds:
976 intptr = &options->gss_deleg_creds;
977 goto parse_flag;
978
Damien Miller95def091999-11-25 00:26:21 +1100979 case oBatchMode:
980 intptr = &options->batch_mode;
981 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 case oCheckHostIP:
984 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000985 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller37876e92003-05-15 10:19:46 +1000987 case oVerifyHostKeyDNS:
988 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100989 multistate_ptr = multistate_yesnoask;
990 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 case oStrictHostKeyChecking:
993 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000994 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +1100995 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 case oCompression:
998 intptr = &options->compression;
999 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller12c150e2003-12-17 16:31:10 +11001001 case oTCPKeepAlive:
1002 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001003 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001005 case oNoHostAuthenticationForLocalhost:
1006 intptr = &options->no_host_authentication_for_localhost;
1007 goto parse_flag;
1008
Damien Miller95def091999-11-25 00:26:21 +11001009 case oNumberOfPasswordPrompts:
1010 intptr = &options->number_of_password_prompts;
1011 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Millera5539d22003-04-09 20:50:06 +10001013 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001014 arg = strdelim(&s);
1015 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001016 fatal("%.200s line %d: Missing argument.", filename,
1017 linenum);
1018 if (strcmp(arg, "default") == 0) {
1019 val64 = 0;
1020 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001021 if (scan_scaled(arg, &val64) == -1)
1022 fatal("%.200s line %d: Bad number '%s': %s",
1023 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001024 if (val64 != 0 && val64 < 16)
1025 fatal("%.200s line %d: RekeyLimit too small",
1026 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001027 }
Damien Miller3dff1762008-02-10 22:25:52 +11001028 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001029 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001030 if (s != NULL) { /* optional rekey interval present */
1031 if (strcmp(s, "none") == 0) {
1032 (void)strdelim(&s); /* discard */
1033 break;
1034 }
1035 intptr = &options->rekey_interval;
1036 goto parse_time;
1037 }
Damien Millera5539d22003-04-09 20:50:06 +10001038 break;
1039
Damien Miller95def091999-11-25 00:26:21 +11001040 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001041 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001042 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001043 fatal("%.200s line %d: Missing argument.", filename, linenum);
1044 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001045 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001046 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001047 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001048 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001049 add_identity_file(options, NULL,
1050 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001051 }
1052 break;
1053
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001054 case oCertificateFile:
1055 arg = strdelim(&s);
1056 if (!arg || *arg == '\0')
1057 fatal("%.200s line %d: Missing argument.",
1058 filename, linenum);
1059 if (*activep) {
1060 intptr = &options->num_certificate_files;
1061 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1062 fatal("%.200s line %d: Too many certificate "
1063 "files specified (max %d).",
1064 filename, linenum,
1065 SSH_MAX_CERTIFICATE_FILES);
1066 }
1067 add_certificate_file(options, arg,
1068 flags & SSHCONF_USERCONF);
1069 }
1070 break;
1071
Damien Millerd3a18572000-06-07 19:55:44 +10001072 case oXAuthLocation:
1073 charptr=&options->xauth_location;
1074 goto parse_string;
1075
Damien Miller95def091999-11-25 00:26:21 +11001076 case oUser:
1077 charptr = &options->user;
1078parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001079 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001080 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001081 fatal("%.200s line %d: Missing argument.",
1082 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001083 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001084 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001085 break;
1086
1087 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001088 cpptr = (char **)&options->system_hostfiles;
1089 uintptr = &options->num_system_hostfiles;
1090 max_entries = SSH_MAX_HOSTS_FILES;
1091parse_char_array:
1092 if (*activep && *uintptr == 0) {
1093 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1094 if ((*uintptr) >= max_entries)
1095 fatal("%s line %d: "
1096 "too many authorized keys files.",
1097 filename, linenum);
1098 cpptr[(*uintptr)++] = xstrdup(arg);
1099 }
1100 }
1101 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001102
1103 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001104 cpptr = (char **)&options->user_hostfiles;
1105 uintptr = &options->num_user_hostfiles;
1106 max_entries = SSH_MAX_HOSTS_FILES;
1107 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001108
Damien Miller95def091999-11-25 00:26:21 +11001109 case oHostName:
1110 charptr = &options->hostname;
1111 goto parse_string;
1112
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001113 case oHostKeyAlias:
1114 charptr = &options->host_key_alias;
1115 goto parse_string;
1116
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001117 case oPreferredAuthentications:
1118 charptr = &options->preferred_authentications;
1119 goto parse_string;
1120
Ben Lindstrome0f88042001-04-30 13:06:24 +00001121 case oBindAddress:
1122 charptr = &options->bind_address;
1123 goto parse_string;
1124
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001125 case oBindInterface:
1126 charptr = &options->bind_interface;
1127 goto parse_string;
1128
Damien Miller7ea845e2010-02-12 09:21:02 +11001129 case oPKCS11Provider:
1130 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001131 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001132
Damien Miller95def091999-11-25 00:26:21 +11001133 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001134 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001135 /* Ignore ProxyCommand if ProxyJump already specified */
1136 if (options->jump_host != NULL)
1137 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001138parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001139 if (s == NULL)
1140 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001141 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001142 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001143 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001144 return 0;
1145
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001146 case oProxyJump:
1147 if (s == NULL) {
1148 fatal("%.200s line %d: Missing argument.",
1149 filename, linenum);
1150 }
1151 len = strspn(s, WHITESPACE "=");
1152 if (parse_jump(s + len, options, *activep) == -1) {
1153 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1154 filename, linenum, s + len);
1155 }
1156 return 0;
1157
Damien Miller95def091999-11-25 00:26:21 +11001158 case oPort:
1159 intptr = &options->port;
1160parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001161 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001162 if ((errstr = atoi_err(arg, &value)) != NULL)
1163 fatal("%s line %d: integer value %s.",
1164 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001165 if (*activep && *intptr == -1)
1166 *intptr = value;
1167 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168
Damien Miller95def091999-11-25 00:26:21 +11001169 case oConnectionAttempts:
1170 intptr = &options->connection_attempts;
1171 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001172
Damien Miller78928792000-04-12 20:17:38 +10001173 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001174 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001175 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001176 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001177 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001178 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001179 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001180 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001181 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001182 break;
1183
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001184 case oMacs:
1185 arg = strdelim(&s);
1186 if (!arg || *arg == '\0')
1187 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001188 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001189 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001190 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001191 if (*activep && options->macs == NULL)
1192 options->macs = xstrdup(arg);
1193 break;
1194
Damien Millerd5f62bf2010-09-24 22:11:14 +10001195 case oKexAlgorithms:
1196 arg = strdelim(&s);
1197 if (!arg || *arg == '\0')
1198 fatal("%.200s line %d: Missing argument.",
1199 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001200 if (*arg != '-' &&
1201 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001202 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1203 filename, linenum, arg ? arg : "<NONE>");
1204 if (*activep && options->kex_algorithms == NULL)
1205 options->kex_algorithms = xstrdup(arg);
1206 break;
1207
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001208 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001209 charptr = &options->hostkeyalgorithms;
1210parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001211 arg = strdelim(&s);
1212 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001213 fatal("%.200s line %d: Missing argument.",
1214 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001215 if (*arg != '-' &&
1216 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001217 fatal("%s line %d: Bad key types '%s'.",
1218 filename, linenum, arg ? arg : "<NONE>");
1219 if (*activep && *charptr == NULL)
1220 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001221 break;
1222
Damien Miller95def091999-11-25 00:26:21 +11001223 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001224 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001225 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001226 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001227 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001228 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001229 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001230 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1231 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001232 break;
1233
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001234 case oLogFacility:
1235 log_facility_ptr = &options->log_facility;
1236 arg = strdelim(&s);
1237 value = log_facility_number(arg);
1238 if (value == SYSLOG_FACILITY_NOT_SET)
1239 fatal("%.200s line %d: unsupported log facility '%s'",
1240 filename, linenum, arg ? arg : "<NONE>");
1241 if (*log_facility_ptr == -1)
1242 *log_facility_ptr = (SyslogFacility) value;
1243 break;
1244
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001245 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001246 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001247 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001248 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001249 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001250 fatal("%.200s line %d: Missing port argument.",
1251 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001252
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001253 remotefwd = (opcode == oRemoteForward);
1254 dynamicfwd = (opcode == oDynamicForward);
1255
1256 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001257 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001258 if (arg2 == NULL || *arg2 == '\0') {
1259 if (remotefwd)
1260 dynamicfwd = 1;
1261 else
1262 fatal("%.200s line %d: Missing target "
1263 "argument.", filename, linenum);
1264 } else {
1265 /* construct a string for parse_forward */
1266 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1267 arg2);
1268 }
Damien Millera699d952008-11-03 19:27:34 +11001269 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001270 if (dynamicfwd)
1271 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001272
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001273 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001274 fatal("%.200s line %d: Bad forwarding specification.",
1275 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001276
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001277 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001278 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001279 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001280 } else {
1281 add_local_forward(options, &fwd);
1282 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001283 }
Damien Miller95def091999-11-25 00:26:21 +11001284 break;
1285
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001286 case oClearAllForwardings:
1287 intptr = &options->clear_forwardings;
1288 goto parse_flag;
1289
Damien Miller95def091999-11-25 00:26:21 +11001290 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001291 if (cmdline)
1292 fatal("Host directive not supported as a command-line "
1293 "option");
Damien Miller95def091999-11-25 00:26:21 +11001294 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001295 arg2 = NULL;
1296 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001297 if ((flags & SSHCONF_NEVERMATCH) != 0)
1298 break;
Damien Millerfe924212011-05-15 08:44:45 +10001299 negated = *arg == '!';
1300 if (negated)
1301 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001302 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001303 if (negated) {
1304 debug("%.200s line %d: Skipping Host "
1305 "block because of negated match "
1306 "for %.100s", filename, linenum,
1307 arg);
1308 *activep = 0;
1309 break;
1310 }
1311 if (!*activep)
1312 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001313 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001314 }
Damien Millerfe924212011-05-15 08:44:45 +10001315 }
1316 if (*activep)
1317 debug("%.200s line %d: Applying options for %.100s",
1318 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001319 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001320 return 0;
1321
Damien Miller194fd902013-10-15 12:13:05 +11001322 case oMatch:
1323 if (cmdline)
1324 fatal("Host directive not supported as a command-line "
1325 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001326 value = match_cfg_line(options, &s, pw, host, original_host,
1327 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001328 if (value < 0)
1329 fatal("%.200s line %d: Bad Match condition", filename,
1330 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001331 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001332 break;
1333
Damien Miller95def091999-11-25 00:26:21 +11001334 case oEscapeChar:
1335 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001336 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001337 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001338 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001339 if (strcmp(arg, "none") == 0)
1340 value = SSH_ESCAPECHAR_NONE;
1341 else if (arg[1] == '\0')
1342 value = (u_char) arg[0];
1343 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001344 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1345 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001346 else {
1347 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001348 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001349 /* NOTREACHED */
1350 value = 0; /* Avoid compiler warning. */
1351 }
1352 if (*activep && *intptr == -1)
1353 *intptr = value;
1354 break;
1355
Damien Miller20a8f972003-05-18 20:50:30 +10001356 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001357 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001358 multistate_ptr = multistate_addressfamily;
1359 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001360
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001361 case oEnableSSHKeysign:
1362 intptr = &options->enable_ssh_keysign;
1363 goto parse_flag;
1364
Damien Millerbd394c32004-03-08 23:12:36 +11001365 case oIdentitiesOnly:
1366 intptr = &options->identities_only;
1367 goto parse_flag;
1368
Damien Miller509b0102003-12-17 16:33:10 +11001369 case oServerAliveInterval:
1370 intptr = &options->server_alive_interval;
1371 goto parse_time;
1372
1373 case oServerAliveCountMax:
1374 intptr = &options->server_alive_count_max;
1375 goto parse_int;
1376
Darren Tucker46bc0752004-05-02 22:11:30 +10001377 case oSendEnv:
1378 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1379 if (strchr(arg, '=') != NULL)
1380 fatal("%s line %d: Invalid environment name.",
1381 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001382 if (!*activep)
1383 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001384 if (*arg == '-') {
1385 /* Removing an env var */
1386 rm_env(options, arg, filename, linenum);
1387 continue;
1388 } else {
1389 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001390 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001391 fatal("%s line %d: too many send env.",
1392 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001393 options->send_env = xrecallocarray(
1394 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001395 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001396 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001397 options->send_env[options->num_send_env++] =
1398 xstrdup(arg);
1399 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001400 }
1401 break;
1402
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001403 case oSetEnv:
1404 value = options->num_setenv;
1405 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1406 if (strchr(arg, '=') == NULL)
1407 fatal("%s line %d: Invalid SetEnv.",
1408 filename, linenum);
1409 if (!*activep || value != 0)
1410 continue;
1411 /* Adding a setenv var */
1412 if (options->num_setenv >= INT_MAX)
1413 fatal("%s line %d: too many SetEnv.",
1414 filename, linenum);
1415 options->setenv = xrecallocarray(
1416 options->setenv, options->num_setenv,
1417 options->num_setenv + 1, sizeof(*options->setenv));
1418 options->setenv[options->num_setenv++] = xstrdup(arg);
1419 }
1420 break;
1421
Damien Miller0e220db2004-06-15 10:34:08 +10001422 case oControlPath:
1423 charptr = &options->control_path;
1424 goto parse_string;
1425
1426 case oControlMaster:
1427 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001428 multistate_ptr = multistate_controlmaster;
1429 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001430
Damien Millere11e1ea2010-08-03 16:04:46 +10001431 case oControlPersist:
1432 /* no/false/yes/true, or a time spec */
1433 intptr = &options->control_persist;
1434 arg = strdelim(&s);
1435 if (!arg || *arg == '\0')
1436 fatal("%.200s line %d: Missing ControlPersist"
1437 " argument.", filename, linenum);
1438 value = 0;
1439 value2 = 0; /* timeout */
1440 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1441 value = 0;
1442 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1443 value = 1;
1444 else if ((value2 = convtime(arg)) >= 0)
1445 value = 1;
1446 else
1447 fatal("%.200s line %d: Bad ControlPersist argument.",
1448 filename, linenum);
1449 if (*activep && *intptr == -1) {
1450 *intptr = value;
1451 options->control_persist_timeout = value2;
1452 }
1453 break;
1454
Damien Millere1776152005-03-01 21:47:37 +11001455 case oHashKnownHosts:
1456 intptr = &options->hash_known_hosts;
1457 goto parse_flag;
1458
Damien Millerd27b9472005-12-13 19:29:02 +11001459 case oTunnel:
1460 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001461 multistate_ptr = multistate_tunnel;
1462 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001463
1464 case oTunnelDevice:
1465 arg = strdelim(&s);
1466 if (!arg || *arg == '\0')
1467 fatal("%.200s line %d: Missing argument.", filename, linenum);
1468 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001469 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001470 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1471 if (*activep) {
1472 options->tun_local = value;
1473 options->tun_remote = value2;
1474 }
1475 break;
1476
1477 case oLocalCommand:
1478 charptr = &options->local_command;
1479 goto parse_command;
1480
1481 case oPermitLocalCommand:
1482 intptr = &options->permit_local_command;
1483 goto parse_flag;
1484
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001485 case oRemoteCommand:
1486 charptr = &options->remote_command;
1487 goto parse_command;
1488
Damien Miller10288242008-06-30 00:04:03 +10001489 case oVisualHostKey:
1490 intptr = &options->visual_host_key;
1491 goto parse_flag;
1492
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001493 case oInclude:
1494 if (cmdline)
1495 fatal("Include directive not supported as a "
1496 "command-line option");
1497 value = 0;
1498 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1499 /*
1500 * Ensure all paths are anchored. User configuration
1501 * files may begin with '~/' but system configurations
1502 * must not. If the path is relative, then treat it
1503 * as living in ~/.ssh for user configurations or
1504 * /etc/ssh for system ones.
1505 */
1506 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1507 fatal("%.200s line %d: bad include path %s.",
1508 filename, linenum, arg);
1509 if (*arg != '/' && *arg != '~') {
1510 xasprintf(&arg2, "%s/%s",
1511 (flags & SSHCONF_USERCONF) ?
1512 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1513 } else
1514 arg2 = xstrdup(arg);
1515 memset(&gl, 0, sizeof(gl));
1516 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1517 if (r == GLOB_NOMATCH) {
1518 debug("%.200s line %d: include %s matched no "
1519 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001520 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001521 continue;
1522 } else if (r != 0 || gl.gl_pathc < 0)
1523 fatal("%.200s line %d: glob failed for %s.",
1524 filename, linenum, arg2);
1525 free(arg2);
1526 oactive = *activep;
1527 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1528 debug3("%.200s line %d: Including file %s "
1529 "depth %d%s", filename, linenum,
1530 gl.gl_pathv[i], depth,
1531 oactive ? "" : " (parse only)");
1532 r = read_config_file_depth(gl.gl_pathv[i],
1533 pw, host, original_host, options,
1534 flags | SSHCONF_CHECKPERM |
1535 (oactive ? 0 : SSHCONF_NEVERMATCH),
1536 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001537 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001538 fatal("Can't open user config file "
1539 "%.100s: %.100s", gl.gl_pathv[i],
1540 strerror(errno));
1541 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001542 /*
1543 * don't let Match in includes clobber the
1544 * containing file's Match state.
1545 */
1546 *activep = oactive;
1547 if (r != 1)
1548 value = -1;
1549 }
1550 globfree(&gl);
1551 }
1552 if (value != 0)
1553 return value;
1554 break;
1555
Damien Miller0dac6fb2010-11-20 15:19:38 +11001556 case oIPQoS:
1557 arg = strdelim(&s);
1558 if ((value = parse_ipqos(arg)) == -1)
1559 fatal("%s line %d: Bad IPQoS value: %s",
1560 filename, linenum, arg);
1561 arg = strdelim(&s);
1562 if (arg == NULL)
1563 value2 = value;
1564 else if ((value2 = parse_ipqos(arg)) == -1)
1565 fatal("%s line %d: Bad IPQoS value: %s",
1566 filename, linenum, arg);
1567 if (*activep) {
1568 options->ip_qos_interactive = value;
1569 options->ip_qos_bulk = value2;
1570 }
1571 break;
1572
Damien Miller21771e22011-05-15 08:45:50 +10001573 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001574 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001575 multistate_ptr = multistate_requesttty;
1576 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001577
Darren Tucker07636982013-05-16 20:30:03 +10001578 case oIgnoreUnknown:
1579 charptr = &options->ignored_unknown;
1580 goto parse_string;
1581
Damien Miller1262b662013-08-21 02:44:24 +10001582 case oProxyUseFdpass:
1583 intptr = &options->proxy_use_fdpass;
1584 goto parse_flag;
1585
Damien Miller0faf7472013-10-17 11:47:23 +11001586 case oCanonicalDomains:
1587 value = options->num_canonical_domains != 0;
1588 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001589 if (!valid_domain(arg, 1, &errstr)) {
1590 fatal("%s line %d: %s", filename, linenum,
1591 errstr);
1592 }
Damien Miller0faf7472013-10-17 11:47:23 +11001593 if (!*activep || value)
1594 continue;
1595 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1596 fatal("%s line %d: too many hostname suffixes.",
1597 filename, linenum);
1598 options->canonical_domains[
1599 options->num_canonical_domains++] = xstrdup(arg);
1600 }
1601 break;
1602
Damien Miller38505592013-10-17 11:48:13 +11001603 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001604 value = options->num_permitted_cnames != 0;
1605 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1606 /* Either '*' for everything or 'list:list' */
1607 if (strcmp(arg, "*") == 0)
1608 arg2 = arg;
1609 else {
1610 lowercase(arg);
1611 if ((arg2 = strchr(arg, ':')) == NULL ||
1612 arg2[1] == '\0') {
1613 fatal("%s line %d: "
1614 "Invalid permitted CNAME \"%s\"",
1615 filename, linenum, arg);
1616 }
1617 *arg2 = '\0';
1618 arg2++;
1619 }
1620 if (!*activep || value)
1621 continue;
1622 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1623 fatal("%s line %d: too many permitted CNAMEs.",
1624 filename, linenum);
1625 cname = options->permitted_cnames +
1626 options->num_permitted_cnames++;
1627 cname->source_list = xstrdup(arg);
1628 cname->target_list = xstrdup(arg2);
1629 }
1630 break;
1631
Damien Miller38505592013-10-17 11:48:13 +11001632 case oCanonicalizeHostname:
1633 intptr = &options->canonicalize_hostname;
1634 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001635 goto parse_multistate;
1636
Damien Miller38505592013-10-17 11:48:13 +11001637 case oCanonicalizeMaxDots:
1638 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001639 goto parse_int;
1640
Damien Miller38505592013-10-17 11:48:13 +11001641 case oCanonicalizeFallbackLocal:
1642 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001643 goto parse_flag;
1644
Damien Miller7acefbb2014-07-18 14:11:24 +10001645 case oStreamLocalBindMask:
1646 arg = strdelim(&s);
1647 if (!arg || *arg == '\0')
1648 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1649 /* Parse mode in octal format */
1650 value = strtol(arg, &endofnumber, 8);
1651 if (arg == endofnumber || value < 0 || value > 0777)
1652 fatal("%.200s line %d: Bad mask.", filename, linenum);
1653 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1654 break;
1655
1656 case oStreamLocalBindUnlink:
1657 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1658 goto parse_flag;
1659
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001660 case oRevokedHostKeys:
1661 charptr = &options->revoked_host_keys;
1662 goto parse_string;
1663
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001664 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001665 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001666 arg = strdelim(&s);
1667 if (!arg || *arg == '\0')
1668 fatal("%.200s line %d: Missing argument.",
1669 filename, linenum);
1670 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1671 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1672 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001673 if (*activep && *intptr == -1)
1674 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001675 break;
1676
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001677 case oUpdateHostkeys:
1678 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001679 multistate_ptr = multistate_yesnoask;
1680 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001681
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001682 case oHostbasedKeyTypes:
1683 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001684 goto parse_keytypes;
1685
1686 case oPubkeyAcceptedKeyTypes:
1687 charptr = &options->pubkey_key_types;
1688 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001689
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001690 case oAddKeysToAgent:
1691 intptr = &options->add_keys_to_agent;
1692 multistate_ptr = multistate_yesnoaskconfirm;
1693 goto parse_multistate;
1694
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001695 case oIdentityAgent:
1696 charptr = &options->identity_agent;
1697 goto parse_string;
1698
Ben Lindstrom4daea862002-06-09 20:04:02 +00001699 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001700 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001701 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001702 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001703
Damien Millerf9b3feb2003-05-16 11:38:32 +10001704 case oUnsupported:
1705 error("%s line %d: Unsupported option \"%s\"",
1706 filename, linenum, keyword);
1707 return 0;
1708
Damien Miller95def091999-11-25 00:26:21 +11001709 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001710 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001711 }
1712
1713 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001714 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001715 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001716 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001717 }
Damien Miller95def091999-11-25 00:26:21 +11001718 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001719}
1720
Damien Miller5428f641999-11-25 11:54:57 +11001721/*
1722 * Reads the config file and modifies the options accordingly. Options
1723 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001724 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001725 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001726int
Damien Miller194fd902013-10-15 12:13:05 +11001727read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001728 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001730 int active = 1;
1731
1732 return read_config_file_depth(filename, pw, host, original_host,
1733 options, flags, &active, 0);
1734}
1735
1736#define READCONF_MAX_DEPTH 16
1737static int
1738read_config_file_depth(const char *filename, struct passwd *pw,
1739 const char *host, const char *original_host, Options *options,
1740 int flags, int *activep, int depth)
1741{
Damien Miller95def091999-11-25 00:26:21 +11001742 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001743 char *line = NULL;
1744 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001745 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001746 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001747
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001748 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1749 fatal("Too many recursive configuration includes");
1750
Damien Miller57a44762004-04-20 20:11:57 +10001751 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001752 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001753
Darren Tuckeraefa3682013-04-05 11:18:35 +11001754 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001755 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001756
Damien Miller33793852004-06-15 10:27:55 +10001757 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001758 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001759 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001760 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001761 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001762 }
1763
Damien Miller95def091999-11-25 00:26:21 +11001764 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001765
Damien Miller5428f641999-11-25 11:54:57 +11001766 /*
1767 * Mark that we are now processing the options. This flag is turned
1768 * on/off by Host specifications.
1769 */
Damien Miller95def091999-11-25 00:26:21 +11001770 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001771 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001772 /* Update line number counter. */
1773 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001774 if (process_config_line_depth(options, pw, host, original_host,
1775 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001776 bad_options++;
1777 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001778 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001779 fclose(f);
1780 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001781 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001782 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001783 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001784}
1785
Damien Miller13f97b22014-02-24 15:57:55 +11001786/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1787int
1788option_clear_or_none(const char *o)
1789{
1790 return o == NULL || strcasecmp(o, "none") == 0;
1791}
1792
Damien Miller5428f641999-11-25 11:54:57 +11001793/*
1794 * Initializes options to special values that indicate that they have not yet
1795 * been set. Read_config_file will only set options with this value. Options
1796 * are processed in the following order: command line, user config file,
1797 * system config file. Last, fill_default_options is called.
1798 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799
Damien Miller4af51302000-04-16 11:18:38 +10001800void
Damien Miller95def091999-11-25 00:26:21 +11001801initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802{
Damien Miller95def091999-11-25 00:26:21 +11001803 memset(options, 'X', sizeof(*options));
1804 options->forward_agent = -1;
1805 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001806 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001807 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001808 options->stdio_forward_host = NULL;
1809 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001810 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001811 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001812 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001813 options->fwd_opts.gateway_ports = -1;
1814 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1815 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001816 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001817 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001818 options->gss_authentication = -1;
1819 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001820 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001821 options->kbd_interactive_authentication = -1;
1822 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001823 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001824 options->batch_mode = -1;
1825 options->check_host_ip = -1;
1826 options->strict_host_key_checking = -1;
1827 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001828 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001829 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001830 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001831 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001832 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001833 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001834 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001835 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001836 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001837 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001838 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001839 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001840 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001841 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001842 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001843 options->jump_user = NULL;
1844 options->jump_host = NULL;
1845 options->jump_port = -1;
1846 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001847 options->user = NULL;
1848 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001849 options->num_system_hostfiles = 0;
1850 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001851 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001852 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001853 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001854 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001855 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001856 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001857 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001858 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001859 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001860 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001861 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001862 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001863 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001864 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001865 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001866 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001867 options->server_alive_interval = -1;
1868 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001869 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001870 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001871 options->setenv = NULL;
1872 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001873 options->control_path = NULL;
1874 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001875 options->control_persist = -1;
1876 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001877 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001878 options->tun_open = -1;
1879 options->tun_local = -1;
1880 options->tun_remote = -1;
1881 options->local_command = NULL;
1882 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001883 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001884 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001885 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001886 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001887 options->ip_qos_interactive = -1;
1888 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001889 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001890 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001891 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001892 options->num_canonical_domains = 0;
1893 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001894 options->canonicalize_max_dots = -1;
1895 options->canonicalize_fallback_local = -1;
1896 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001897 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001898 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001899 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001900 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001901 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001902}
1903
Damien Miller5428f641999-11-25 11:54:57 +11001904/*
Damien Miller13f97b22014-02-24 15:57:55 +11001905 * A petite version of fill_default_options() that just fills the options
1906 * needed for hostname canonicalization to proceed.
1907 */
1908void
1909fill_default_options_for_canonicalization(Options *options)
1910{
1911 if (options->canonicalize_max_dots == -1)
1912 options->canonicalize_max_dots = 1;
1913 if (options->canonicalize_fallback_local == -1)
1914 options->canonicalize_fallback_local = 1;
1915 if (options->canonicalize_hostname == -1)
1916 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1917}
1918
1919/*
Damien Miller5428f641999-11-25 11:54:57 +11001920 * Called after processing other sources of option data, this fills those
1921 * options for which no value has been specified with their default values.
1922 */
Damien Miller4af51302000-04-16 11:18:38 +10001923void
Damien Miller95def091999-11-25 00:26:21 +11001924fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001925{
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001926 char *all_cipher, *all_mac, *all_kex, *all_key;
1927
Damien Miller95def091999-11-25 00:26:21 +11001928 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001929 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001930 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001931 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001932 if (options->forward_x11_trusted == -1)
1933 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001934 if (options->forward_x11_timeout == -1)
1935 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001936 /*
1937 * stdio forwarding (-W) changes the default for these but we defer
1938 * setting the values so they can be overridden.
1939 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001940 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001941 options->exit_on_forward_failure =
1942 options->stdio_forward_host != NULL ? 1 : 0;
1943 if (options->clear_forwardings == -1)
1944 options->clear_forwardings =
1945 options->stdio_forward_host != NULL ? 1 : 0;
1946 if (options->clear_forwardings == 1)
1947 clear_forwardings(options);
1948
Damien Millerd3a18572000-06-07 19:55:44 +10001949 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001950 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001951 if (options->fwd_opts.gateway_ports == -1)
1952 options->fwd_opts.gateway_ports = 0;
1953 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1954 options->fwd_opts.streamlocal_bind_mask = 0177;
1955 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1956 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001957 if (options->pubkey_authentication == -1)
1958 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001959 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001960 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001961 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001962 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001963 if (options->gss_deleg_creds == -1)
1964 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001965 if (options->password_authentication == -1)
1966 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001967 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001968 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001969 if (options->hostbased_authentication == -1)
1970 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001971 if (options->batch_mode == -1)
1972 options->batch_mode = 0;
1973 if (options->check_host_ip == -1)
1974 options->check_host_ip = 1;
1975 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001976 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001977 if (options->compression == -1)
1978 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001979 if (options->tcp_keep_alive == -1)
1980 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001981 if (options->port == -1)
1982 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001983 if (options->address_family == -1)
1984 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001985 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001986 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001987 if (options->number_of_password_prompts == -1)
1988 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001989 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001990 if (options->add_keys_to_agent == -1)
1991 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001992 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001993 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1994 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001995#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001996 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001997#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001998 add_identity_file(options, "~/",
1999 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002000 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002001 }
Damien Miller95def091999-11-25 00:26:21 +11002002 if (options->escape_char == -1)
2003 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002004 if (options->num_system_hostfiles == 0) {
2005 options->system_hostfiles[options->num_system_hostfiles++] =
2006 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2007 options->system_hostfiles[options->num_system_hostfiles++] =
2008 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2009 }
2010 if (options->num_user_hostfiles == 0) {
2011 options->user_hostfiles[options->num_user_hostfiles++] =
2012 xstrdup(_PATH_SSH_USER_HOSTFILE);
2013 options->user_hostfiles[options->num_user_hostfiles++] =
2014 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2015 }
Damien Millerfcd93202002-02-05 12:26:34 +11002016 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002017 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002018 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2019 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002020 if (options->no_host_authentication_for_localhost == - 1)
2021 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002022 if (options->identities_only == -1)
2023 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002024 if (options->enable_ssh_keysign == -1)
2025 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002026 if (options->rekey_limit == -1)
2027 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002028 if (options->rekey_interval == -1)
2029 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002030 if (options->verify_host_key_dns == -1)
2031 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002032 if (options->server_alive_interval == -1)
2033 options->server_alive_interval = 0;
2034 if (options->server_alive_count_max == -1)
2035 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002036 if (options->control_master == -1)
2037 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002038 if (options->control_persist == -1) {
2039 options->control_persist = 0;
2040 options->control_persist_timeout = 0;
2041 }
Damien Millere1776152005-03-01 21:47:37 +11002042 if (options->hash_known_hosts == -1)
2043 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002044 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002045 options->tun_open = SSH_TUNMODE_NO;
2046 if (options->tun_local == -1)
2047 options->tun_local = SSH_TUNID_ANY;
2048 if (options->tun_remote == -1)
2049 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002050 if (options->permit_local_command == -1)
2051 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002052 if (options->visual_host_key == -1)
2053 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002054 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002055 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002056 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002057 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002058 if (options->request_tty == -1)
2059 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002060 if (options->proxy_use_fdpass == -1)
2061 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002062 if (options->canonicalize_max_dots == -1)
2063 options->canonicalize_max_dots = 1;
2064 if (options->canonicalize_fallback_local == -1)
2065 options->canonicalize_fallback_local = 1;
2066 if (options->canonicalize_hostname == -1)
2067 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002068 if (options->fingerprint_hash == -1)
2069 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002070 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002071 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002072
2073 /* Expand KEX name lists */
2074 all_cipher = cipher_alg_list(',', 0);
2075 all_mac = mac_alg_list(',');
2076 all_kex = kex_alg_list(',');
2077 all_key = sshkey_alg_list(0, 0, 1, ',');
2078 if (kex_assemble_names(&options->ciphers,
2079 KEX_CLIENT_ENCRYPT, all_cipher) != 0 ||
2080 kex_assemble_names(&options->macs,
2081 KEX_CLIENT_MAC, all_mac) != 0 ||
2082 kex_assemble_names(&options->kex_algorithms,
2083 KEX_CLIENT_KEX, all_kex) != 0 ||
2084 kex_assemble_names(&options->hostbased_key_types,
2085 KEX_DEFAULT_PK_ALG, all_key) != 0 ||
2086 kex_assemble_names(&options->pubkey_key_types,
2087 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002088 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002089 free(all_cipher);
2090 free(all_mac);
2091 free(all_kex);
2092 free(all_key);
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.org312d2f22018-07-04 13:49:31 +00002540 char buf[8], *all_key;
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 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002543 all_key = sshkey_alg_list(0, 0, 1, ',');
2544 if (kex_assemble_names( &o->hostkeyalgorithms,
2545 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002546 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002547 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002548
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002549 /* Most interesting options first: user, host, port */
2550 dump_cfg_string(oUser, o->user);
2551 dump_cfg_string(oHostName, host);
2552 dump_cfg_int(oPort, o->port);
2553
2554 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002555 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002556 dump_cfg_fmtint(oAddressFamily, o->address_family);
2557 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2558 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2559 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2560 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2561 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2562 dump_cfg_fmtint(oCompression, o->compression);
2563 dump_cfg_fmtint(oControlMaster, o->control_master);
2564 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002565 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002566 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002567 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002568 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2569 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2570 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2571 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2572#ifdef GSSAPI
2573 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2574 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2575#endif /* GSSAPI */
2576 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2577 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2578 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2579 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2580 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2581 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2582 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002583 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2584 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2585 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002586 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2587 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2588 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2589 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002590 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2591 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002592 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002593
2594 /* Integer options */
2595 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002596 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2597 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2598 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2599 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2600 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2601
2602 /* String options */
2603 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002604 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002605 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2606 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002607 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002608 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002609 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002610 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002611 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002612 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2613 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2614 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002615 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002616 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2617 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002618#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002619 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002620#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002621 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002622 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002623 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002624 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002625
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002626 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002627 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2628 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2629 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2630
2631 /* String array options */
2632 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2633 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002634 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002635 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2636 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2637 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002638 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002639
2640 /* Special cases */
2641
2642 /* oConnectTimeout */
2643 if (o->connection_timeout == -1)
2644 printf("connecttimeout none\n");
2645 else
2646 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2647
2648 /* oTunnelDevice */
2649 printf("tunneldevice");
2650 if (o->tun_local == SSH_TUNID_ANY)
2651 printf(" any");
2652 else
2653 printf(" %d", o->tun_local);
2654 if (o->tun_remote == SSH_TUNID_ANY)
2655 printf(":any");
2656 else
2657 printf(":%d", o->tun_remote);
2658 printf("\n");
2659
2660 /* oCanonicalizePermittedCNAMEs */
2661 if ( o->num_permitted_cnames > 0) {
2662 printf("canonicalizePermittedcnames");
2663 for (i = 0; i < o->num_permitted_cnames; i++) {
2664 printf(" %s:%s", o->permitted_cnames[i].source_list,
2665 o->permitted_cnames[i].target_list);
2666 }
2667 printf("\n");
2668 }
2669
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002670 /* oControlPersist */
2671 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2672 dump_cfg_fmtint(oControlPersist, o->control_persist);
2673 else
2674 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2675
2676 /* oEscapeChar */
2677 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2678 printf("escapechar none\n");
2679 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002680 vis(buf, o->escape_char, VIS_WHITE, 0);
2681 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002682 }
2683
2684 /* oIPQoS */
2685 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2686 printf("%s\n", iptos2str(o->ip_qos_bulk));
2687
2688 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002689 printf("rekeylimit %llu %d\n",
2690 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002691
2692 /* oStreamLocalBindMask */
2693 printf("streamlocalbindmask 0%o\n",
2694 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002695
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002696 /* oLogFacility */
2697 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2698
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002699 /* oProxyCommand / oProxyJump */
2700 if (o->jump_host == NULL)
2701 dump_cfg_string(oProxyCommand, o->proxy_command);
2702 else {
2703 /* Check for numeric addresses */
2704 i = strchr(o->jump_host, ':') != NULL ||
2705 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2706 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2707 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002708 /* optional additional jump spec */
2709 o->jump_extra == NULL ? "" : o->jump_extra,
2710 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002711 /* optional user */
2712 o->jump_user == NULL ? "" : o->jump_user,
2713 o->jump_user == NULL ? "" : "@",
2714 /* opening [ if hostname is numeric */
2715 i ? "[" : "",
2716 /* mandatory hostname */
2717 o->jump_host,
2718 /* closing ] if hostname is numeric */
2719 i ? "]" : "",
2720 /* optional port number */
2721 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002722 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002723 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002724}