blob: ce832526aa51328e7114da2abe9b97b8636814da [file] [log] [blame]
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +00001/* $OpenBSD: readconf.c,v 1.294 2018/07/19 10:28:47 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;
Damien Miller95def091999-11-25 00:26:21 +1100323 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000324 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000325
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000326 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000327 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000328 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000329 /* Don't add duplicates */
330 for (i = 0; i < options->num_local_forwards; i++) {
331 if (forward_equals(newfwd, options->local_forwards + i))
332 return;
333 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000334 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000335 options->num_local_forwards + 1,
336 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100337 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338
Damien Miller1a0442f2008-11-05 16:30:06 +1100339 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100342 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100343 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000344 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345}
346
Damien Miller5428f641999-11-25 11:54:57 +1100347/*
348 * Adds a remote TCP/IP port forward to options. Never returns if there is
349 * an error.
350 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller4af51302000-04-16 11:18:38 +1000352void
Damien Miller7acefbb2014-07-18 14:11:24 +1000353add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354{
Damien Miller7acefbb2014-07-18 14:11:24 +1000355 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000356 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000357
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000358 /* Don't add duplicates */
359 for (i = 0; i < options->num_remote_forwards; i++) {
360 if (forward_equals(newfwd, options->remote_forwards + i))
361 return;
362 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000363 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000364 options->num_remote_forwards + 1,
365 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100366 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367
Damien Miller1a0442f2008-11-05 16:30:06 +1100368 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100369 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000370 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100371 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100372 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000373 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100374 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000375 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376}
377
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000378static void
379clear_forwardings(Options *options)
380{
381 int i;
382
Damien Millerf91ee4c2005-03-01 21:24:33 +1100383 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000385 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000387 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100388 }
Damien Miller232cfb12010-06-26 09:50:30 +1000389 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000390 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000391 options->local_forwards = NULL;
392 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000393 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100394 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000396 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000398 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100399 }
Damien Miller232cfb12010-06-26 09:50:30 +1000400 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000401 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000402 options->remote_forwards = NULL;
403 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000404 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100405 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000406}
407
Darren Tucker19104782013-04-05 11:13:08 +1100408void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000409add_certificate_file(Options *options, const char *path, int userprovided)
410{
411 int i;
412
413 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
414 fatal("Too many certificate files specified (max %d)",
415 SSH_MAX_CERTIFICATE_FILES);
416
417 /* Avoid registering duplicates */
418 for (i = 0; i < options->num_certificate_files; i++) {
419 if (options->certificate_file_userprovided[i] == userprovided &&
420 strcmp(options->certificate_files[i], path) == 0) {
421 debug2("%s: ignoring duplicate key %s", __func__, path);
422 return;
423 }
424 }
425
426 options->certificate_file_userprovided[options->num_certificate_files] =
427 userprovided;
428 options->certificate_files[options->num_certificate_files++] =
429 xstrdup(path);
430}
431
432void
Darren Tucker19104782013-04-05 11:13:08 +1100433add_identity_file(Options *options, const char *dir, const char *filename,
434 int userprovided)
435{
436 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000437 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100438
439 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
440 fatal("Too many identity files specified (max %d)",
441 SSH_MAX_IDENTITY_FILES);
442
443 if (dir == NULL) /* no dir, filename is absolute */
444 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000445 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
446 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100447
Damien Miller15271902014-05-15 13:47:56 +1000448 /* Avoid registering duplicates */
449 for (i = 0; i < options->num_identity_files; i++) {
450 if (options->identity_file_userprovided[i] == userprovided &&
451 strcmp(options->identity_files[i], path) == 0) {
452 debug2("%s: ignoring duplicate key %s", __func__, path);
453 free(path);
454 return;
455 }
456 }
457
Darren Tucker19104782013-04-05 11:13:08 +1100458 options->identity_file_userprovided[options->num_identity_files] =
459 userprovided;
460 options->identity_files[options->num_identity_files++] = path;
461}
462
Damien Miller194fd902013-10-15 12:13:05 +1100463int
464default_ssh_port(void)
465{
466 static int port;
467 struct servent *sp;
468
469 if (port == 0) {
470 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
471 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
472 }
473 return port;
474}
475
476/*
477 * Execute a command in a shell.
478 * Return its exit status or -1 on abnormal exit.
479 */
480static int
481execute_in_shell(const char *cmd)
482{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000483 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100484 pid_t pid;
485 int devnull, status;
486 extern uid_t original_real_uid;
487
488 if ((shell = getenv("SHELL")) == NULL)
489 shell = _PATH_BSHELL;
490
Damien Miller194fd902013-10-15 12:13:05 +1100491 /* Need this to redirect subprocess stdin/out */
492 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
493 fatal("open(/dev/null): %s", strerror(errno));
494
495 debug("Executing command: '%.500s'", cmd);
496
497 /* Fork and execute the command. */
498 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000499 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100500
Damien Miller194fd902013-10-15 12:13:05 +1100501 /* Redirect child stdin and stdout. Leave stderr */
502 if (dup2(devnull, STDIN_FILENO) == -1)
503 fatal("dup2: %s", strerror(errno));
504 if (dup2(devnull, STDOUT_FILENO) == -1)
505 fatal("dup2: %s", strerror(errno));
506 if (devnull > STDERR_FILENO)
507 close(devnull);
508 closefrom(STDERR_FILENO + 1);
509
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000510 argv[0] = shell;
511 argv[1] = "-c";
512 argv[2] = xstrdup(cmd);
513 argv[3] = NULL;
514
Damien Miller194fd902013-10-15 12:13:05 +1100515 execv(argv[0], argv);
516 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
517 /* Die with signal to make this error apparent to parent. */
518 signal(SIGTERM, SIG_DFL);
519 kill(getpid(), SIGTERM);
520 _exit(1);
521 }
522 /* Parent. */
523 if (pid < 0)
524 fatal("%s: fork: %.100s", __func__, strerror(errno));
525
526 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100527
528 while (waitpid(pid, &status, 0) == -1) {
529 if (errno != EINTR && errno != EAGAIN)
530 fatal("%s: waitpid: %s", __func__, strerror(errno));
531 }
532 if (!WIFEXITED(status)) {
533 error("command '%.100s' exited abnormally", cmd);
534 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000535 }
Damien Miller194fd902013-10-15 12:13:05 +1100536 debug3("command returned status %d", WEXITSTATUS(status));
537 return WEXITSTATUS(status);
538}
539
540/*
541 * Parse and execute a Match directive.
542 */
543static int
544match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000545 const char *host_arg, const char *original_host, int post_canon,
546 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100547{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100549 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100551 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000552 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100553
554 /*
555 * Configuration is likely to be incomplete at this point so we
556 * must be prepared to use default values.
557 */
558 port = options->port <= 0 ? default_ssh_port() : options->port;
559 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000560 if (post_canon) {
561 host = xstrdup(options->hostname);
562 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100563 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100564 host = percent_expand(options->hostname,
565 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000566 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000568 }
Damien Miller194fd902013-10-15 12:13:05 +1100569
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000570 debug2("checking match for '%s' host %s originally %s",
571 cp, host, original_host);
572 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
573 criteria = NULL;
574 this_result = 1;
575 if ((negate = attrib[0] == '!'))
576 attrib++;
577 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100578 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100580 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 error("%.200s line %d: '%s' cannot be combined "
582 "with other Match attributes",
583 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100584 result = -1;
585 goto out;
586 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000587 if (result)
588 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100589 goto out;
590 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 attributes++;
592 if (strcasecmp(attrib, "canonical") == 0) {
593 r = !!post_canon; /* force bitmask member to boolean */
594 if (r == (negate ? 1 : 0))
595 this_result = result = 0;
596 debug3("%.200s line %d: %smatched '%s'",
597 filename, linenum,
598 this_result ? "" : "not ", oattrib);
599 continue;
600 }
601 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100602 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
603 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100604 result = -1;
605 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100606 }
Damien Miller194fd902013-10-15 12:13:05 +1100607 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000609 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 if (r == (negate ? 1 : 0))
611 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100612 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000614 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 if (r == (negate ? 1 : 0))
616 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100617 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000619 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 if (r == (negate ? 1 : 0))
621 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100622 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000624 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000625 if (r == (negate ? 1 : 0))
626 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100627 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100628 if (gethostname(thishost, sizeof(thishost)) == -1)
629 fatal("gethostname: %s", strerror(errno));
630 strlcpy(shorthost, thishost, sizeof(shorthost));
631 shorthost[strcspn(thishost, ".")] = '\0';
632 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000633 snprintf(uidstr, sizeof(uidstr), "%llu",
634 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100635
636 cmd = percent_expand(arg,
637 "L", shorthost,
638 "d", pw->pw_dir,
639 "h", host,
640 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000641 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100642 "p", portstr,
643 "r", ruser,
644 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000645 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100646 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100647 if (result != 1) {
648 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000649 debug3("%.200s line %d: skipped exec "
650 "\"%.100s\"", filename, linenum, cmd);
651 free(cmd);
652 continue;
Damien Miller06287802014-02-24 15:56:45 +1100653 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000654 r = execute_in_shell(cmd);
655 if (r == -1) {
656 fatal("%.200s line %d: match exec "
657 "'%.100s' error", filename,
658 linenum, cmd);
659 }
660 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100661 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000662 /* Force exit status to boolean */
663 r = r == 0;
664 if (r == (negate ? 1 : 0))
665 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100666 } else {
667 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100668 result = -1;
669 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100670 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000671 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
672 filename, linenum, this_result ? "": "not ",
673 oattrib, criteria);
674 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100675 }
Damien Millercf31f382013-10-24 21:02:56 +1100676 if (attributes == 0) {
677 error("One or more attributes required for Match");
678 result = -1;
679 goto out;
680 }
Damien Miller084bcd22013-10-23 16:30:51 +1100681 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000682 if (result != -1)
683 debug2("match %sfound", result ? "" : "not ");
684 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100685 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100686 return result;
687}
688
djm@openbsd.org555294a2018-04-06 13:02:39 +0000689/* Remove environment variable by pattern */
690static void
691rm_env(Options *options, const char *arg, const char *filename, int linenum)
692{
693 int i, j;
694 char *cp;
695
696 /* Remove an environment variable */
697 for (i = 0; i < options->num_send_env; ) {
698 cp = xstrdup(options->send_env[i]);
699 if (!match_pattern(cp, arg + 1)) {
700 free(cp);
701 i++;
702 continue;
703 }
704 debug3("%s line %d: removing environment %s",
705 filename, linenum, cp);
706 free(cp);
707 free(options->send_env[i]);
708 options->send_env[i] = NULL;
709 for (j = i; j < options->num_send_env - 1; j++) {
710 options->send_env[j] = options->send_env[j + 1];
711 options->send_env[j + 1] = NULL;
712 }
713 options->num_send_env--;
714 /* NB. don't increment i */
715 }
716}
717
Damien Miller5428f641999-11-25 11:54:57 +1100718/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000719 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100720 */
Damien Miller4af51302000-04-16 11:18:38 +1000721static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000722parse_token(const char *cp, const char *filename, int linenum,
723 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724{
Darren Tucker07636982013-05-16 20:30:03 +1000725 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000728 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100729 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000730 if (ignored_unknown != NULL &&
731 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000732 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000733 error("%s: line %d: Bad configuration option: %s",
734 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100735 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736}
737
Damien Millere9fc72e2013-10-15 12:14:12 +1100738/* Multistate option parsing */
739struct multistate {
740 char *key;
741 int value;
742};
743static const struct multistate multistate_flag[] = {
744 { "true", 1 },
745 { "false", 0 },
746 { "yes", 1 },
747 { "no", 0 },
748 { NULL, -1 }
749};
750static const struct multistate multistate_yesnoask[] = {
751 { "true", 1 },
752 { "false", 0 },
753 { "yes", 1 },
754 { "no", 0 },
755 { "ask", 2 },
756 { NULL, -1 }
757};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000758static const struct multistate multistate_strict_hostkey[] = {
759 { "true", SSH_STRICT_HOSTKEY_YES },
760 { "false", SSH_STRICT_HOSTKEY_OFF },
761 { "yes", SSH_STRICT_HOSTKEY_YES },
762 { "no", SSH_STRICT_HOSTKEY_OFF },
763 { "ask", SSH_STRICT_HOSTKEY_ASK },
764 { "off", SSH_STRICT_HOSTKEY_OFF },
765 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
766 { NULL, -1 }
767};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000768static const struct multistate multistate_yesnoaskconfirm[] = {
769 { "true", 1 },
770 { "false", 0 },
771 { "yes", 1 },
772 { "no", 0 },
773 { "ask", 2 },
774 { "confirm", 3 },
775 { NULL, -1 }
776};
Damien Millere9fc72e2013-10-15 12:14:12 +1100777static const struct multistate multistate_addressfamily[] = {
778 { "inet", AF_INET },
779 { "inet6", AF_INET6 },
780 { "any", AF_UNSPEC },
781 { NULL, -1 }
782};
783static const struct multistate multistate_controlmaster[] = {
784 { "true", SSHCTL_MASTER_YES },
785 { "yes", SSHCTL_MASTER_YES },
786 { "false", SSHCTL_MASTER_NO },
787 { "no", SSHCTL_MASTER_NO },
788 { "auto", SSHCTL_MASTER_AUTO },
789 { "ask", SSHCTL_MASTER_ASK },
790 { "autoask", SSHCTL_MASTER_AUTO_ASK },
791 { NULL, -1 }
792};
793static const struct multistate multistate_tunnel[] = {
794 { "ethernet", SSH_TUNMODE_ETHERNET },
795 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
796 { "true", SSH_TUNMODE_DEFAULT },
797 { "yes", SSH_TUNMODE_DEFAULT },
798 { "false", SSH_TUNMODE_NO },
799 { "no", SSH_TUNMODE_NO },
800 { NULL, -1 }
801};
802static const struct multistate multistate_requesttty[] = {
803 { "true", REQUEST_TTY_YES },
804 { "yes", REQUEST_TTY_YES },
805 { "false", REQUEST_TTY_NO },
806 { "no", REQUEST_TTY_NO },
807 { "force", REQUEST_TTY_FORCE },
808 { "auto", REQUEST_TTY_AUTO },
809 { NULL, -1 }
810};
Damien Miller38505592013-10-17 11:48:13 +1100811static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100812 { "true", SSH_CANONICALISE_YES },
813 { "false", SSH_CANONICALISE_NO },
814 { "yes", SSH_CANONICALISE_YES },
815 { "no", SSH_CANONICALISE_NO },
816 { "always", SSH_CANONICALISE_ALWAYS },
817 { NULL, -1 }
818};
Damien Millere9fc72e2013-10-15 12:14:12 +1100819
Damien Miller5428f641999-11-25 11:54:57 +1100820/*
821 * Processes a single option line as used in the configuration files. This
822 * only sets those values that have not already been set.
823 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100824int
Damien Miller194fd902013-10-15 12:13:05 +1100825process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000826 const char *original_host, char *line, const char *filename,
827 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000829 return process_config_line_depth(options, pw, host, original_host,
830 line, filename, linenum, activep, flags, 0);
831}
832
833#define WHITESPACE " \t\r\n"
834static int
835process_config_line_depth(Options *options, struct passwd *pw, const char *host,
836 const char *original_host, char *line, const char *filename,
837 int linenum, int *activep, int flags, int depth)
838{
Damien Miller295ee632011-05-29 21:42:31 +1000839 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
840 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000841 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000842 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000843 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100844 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000845 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000846 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100847 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000848 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100849 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100850 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000851 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000852 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Miller194fd902013-10-15 12:13:05 +1100854 if (activep == NULL) { /* We are processing a command line directive */
855 cmdline = 1;
856 activep = &cmdline;
857 }
858
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000859 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000860 if ((len = strlen(line)) == 0)
861 return 0;
862 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000863 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000864 break;
865 line[len] = '\0';
866 }
867
Damien Millerbe484b52000-07-15 14:14:16 +1000868 s = line;
869 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100870 if ((keyword = strdelim(&s)) == NULL)
871 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000872 /* Ignore leading whitespace. */
873 if (*keyword == '\0')
874 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000875 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100876 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000877 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100878 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Darren Tucker07636982013-05-16 20:30:03 +1000880 opcode = parse_token(keyword, filename, linenum,
881 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Damien Miller95def091999-11-25 00:26:21 +1100883 switch (opcode) {
884 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100885 /* don't panic, but count bad options */
886 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000887 case oIgnore:
888 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000889 case oIgnoredUnknownOption:
890 debug("%s line %d: Ignored unknown option \"%s\"",
891 filename, linenum, keyword);
892 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000893 case oConnectTimeout:
894 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100895parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000896 arg = strdelim(&s);
897 if (!arg || *arg == '\0')
898 fatal("%s line %d: missing time value.",
899 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000900 if (strcmp(arg, "none") == 0)
901 value = -1;
902 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000903 fatal("%s line %d: invalid time value.",
904 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100905 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000906 *intptr = value;
907 break;
908
Damien Miller95def091999-11-25 00:26:21 +1100909 case oForwardAgent:
910 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100911 parse_flag:
912 multistate_ptr = multistate_flag;
913 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000914 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000915 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100916 fatal("%s line %d: missing argument.",
917 filename, linenum);
918 value = -1;
919 for (i = 0; multistate_ptr[i].key != NULL; i++) {
920 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
921 value = multistate_ptr[i].value;
922 break;
923 }
924 }
925 if (value == -1)
926 fatal("%s line %d: unsupported option \"%s\".",
927 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100928 if (*activep && *intptr == -1)
929 *intptr = value;
930 break;
931
932 case oForwardX11:
933 intptr = &options->forward_x11;
934 goto parse_flag;
935
Darren Tucker0a118da2003-10-15 15:54:32 +1000936 case oForwardX11Trusted:
937 intptr = &options->forward_x11_trusted;
938 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000939
Damien Miller1ab6a512010-06-26 10:02:24 +1000940 case oForwardX11Timeout:
941 intptr = &options->forward_x11_timeout;
942 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000943
Damien Miller95def091999-11-25 00:26:21 +1100944 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000945 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100946 goto parse_flag;
947
Darren Tuckere7d4b192006-07-12 22:17:10 +1000948 case oExitOnForwardFailure:
949 intptr = &options->exit_on_forward_failure;
950 goto parse_flag;
951
Damien Miller95def091999-11-25 00:26:21 +1100952 case oPasswordAuthentication:
953 intptr = &options->password_authentication;
954 goto parse_flag;
955
Damien Miller874d77b2000-10-14 16:23:11 +1100956 case oKbdInteractiveAuthentication:
957 intptr = &options->kbd_interactive_authentication;
958 goto parse_flag;
959
960 case oKbdInteractiveDevices:
961 charptr = &options->kbd_interactive_devices;
962 goto parse_string;
963
Damien Miller0bc1bd82000-11-13 22:57:25 +1100964 case oPubkeyAuthentication:
965 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000966 goto parse_flag;
967
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000968 case oHostbasedAuthentication:
969 intptr = &options->hostbased_authentication;
970 goto parse_flag;
971
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000972 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000973 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100974 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000975
Darren Tucker0efd1552003-08-26 11:49:55 +1000976 case oGssAuthentication:
977 intptr = &options->gss_authentication;
978 goto parse_flag;
979
980 case oGssDelegateCreds:
981 intptr = &options->gss_deleg_creds;
982 goto parse_flag;
983
Damien Miller95def091999-11-25 00:26:21 +1100984 case oBatchMode:
985 intptr = &options->batch_mode;
986 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller95def091999-11-25 00:26:21 +1100988 case oCheckHostIP:
989 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000990 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller37876e92003-05-15 10:19:46 +1000992 case oVerifyHostKeyDNS:
993 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100994 multistate_ptr = multistate_yesnoask;
995 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 case oStrictHostKeyChecking:
998 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000999 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001000 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
Damien Miller95def091999-11-25 00:26:21 +11001002 case oCompression:
1003 intptr = &options->compression;
1004 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Damien Miller12c150e2003-12-17 16:31:10 +11001006 case oTCPKeepAlive:
1007 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001008 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001010 case oNoHostAuthenticationForLocalhost:
1011 intptr = &options->no_host_authentication_for_localhost;
1012 goto parse_flag;
1013
Damien Miller95def091999-11-25 00:26:21 +11001014 case oNumberOfPasswordPrompts:
1015 intptr = &options->number_of_password_prompts;
1016 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Millera5539d22003-04-09 20:50:06 +10001018 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001019 arg = strdelim(&s);
1020 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001021 fatal("%.200s line %d: Missing argument.", filename,
1022 linenum);
1023 if (strcmp(arg, "default") == 0) {
1024 val64 = 0;
1025 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001026 if (scan_scaled(arg, &val64) == -1)
1027 fatal("%.200s line %d: Bad number '%s': %s",
1028 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001029 if (val64 != 0 && val64 < 16)
1030 fatal("%.200s line %d: RekeyLimit too small",
1031 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001032 }
Damien Miller3dff1762008-02-10 22:25:52 +11001033 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001034 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001035 if (s != NULL) { /* optional rekey interval present */
1036 if (strcmp(s, "none") == 0) {
1037 (void)strdelim(&s); /* discard */
1038 break;
1039 }
1040 intptr = &options->rekey_interval;
1041 goto parse_time;
1042 }
Damien Millera5539d22003-04-09 20:50:06 +10001043 break;
1044
Damien Miller95def091999-11-25 00:26:21 +11001045 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001046 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001047 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001048 fatal("%.200s line %d: Missing argument.", filename, linenum);
1049 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001050 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001051 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001052 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001053 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001054 add_identity_file(options, NULL,
1055 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001056 }
1057 break;
1058
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001059 case oCertificateFile:
1060 arg = strdelim(&s);
1061 if (!arg || *arg == '\0')
1062 fatal("%.200s line %d: Missing argument.",
1063 filename, linenum);
1064 if (*activep) {
1065 intptr = &options->num_certificate_files;
1066 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1067 fatal("%.200s line %d: Too many certificate "
1068 "files specified (max %d).",
1069 filename, linenum,
1070 SSH_MAX_CERTIFICATE_FILES);
1071 }
1072 add_certificate_file(options, arg,
1073 flags & SSHCONF_USERCONF);
1074 }
1075 break;
1076
Damien Millerd3a18572000-06-07 19:55:44 +10001077 case oXAuthLocation:
1078 charptr=&options->xauth_location;
1079 goto parse_string;
1080
Damien Miller95def091999-11-25 00:26:21 +11001081 case oUser:
1082 charptr = &options->user;
1083parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001084 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001085 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001086 fatal("%.200s line %d: Missing argument.",
1087 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001088 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001089 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001090 break;
1091
1092 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001093 cpptr = (char **)&options->system_hostfiles;
1094 uintptr = &options->num_system_hostfiles;
1095 max_entries = SSH_MAX_HOSTS_FILES;
1096parse_char_array:
1097 if (*activep && *uintptr == 0) {
1098 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1099 if ((*uintptr) >= max_entries)
1100 fatal("%s line %d: "
1101 "too many authorized keys files.",
1102 filename, linenum);
1103 cpptr[(*uintptr)++] = xstrdup(arg);
1104 }
1105 }
1106 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001107
1108 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001109 cpptr = (char **)&options->user_hostfiles;
1110 uintptr = &options->num_user_hostfiles;
1111 max_entries = SSH_MAX_HOSTS_FILES;
1112 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001113
Damien Miller95def091999-11-25 00:26:21 +11001114 case oHostName:
1115 charptr = &options->hostname;
1116 goto parse_string;
1117
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001118 case oHostKeyAlias:
1119 charptr = &options->host_key_alias;
1120 goto parse_string;
1121
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001122 case oPreferredAuthentications:
1123 charptr = &options->preferred_authentications;
1124 goto parse_string;
1125
Ben Lindstrome0f88042001-04-30 13:06:24 +00001126 case oBindAddress:
1127 charptr = &options->bind_address;
1128 goto parse_string;
1129
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001130 case oBindInterface:
1131 charptr = &options->bind_interface;
1132 goto parse_string;
1133
Damien Miller7ea845e2010-02-12 09:21:02 +11001134 case oPKCS11Provider:
1135 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001136 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001137
Damien Miller95def091999-11-25 00:26:21 +11001138 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001139 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001140 /* Ignore ProxyCommand if ProxyJump already specified */
1141 if (options->jump_host != NULL)
1142 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001143parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001144 if (s == NULL)
1145 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001146 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001147 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001148 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001149 return 0;
1150
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001151 case oProxyJump:
1152 if (s == NULL) {
1153 fatal("%.200s line %d: Missing argument.",
1154 filename, linenum);
1155 }
1156 len = strspn(s, WHITESPACE "=");
1157 if (parse_jump(s + len, options, *activep) == -1) {
1158 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1159 filename, linenum, s + len);
1160 }
1161 return 0;
1162
Damien Miller95def091999-11-25 00:26:21 +11001163 case oPort:
1164 intptr = &options->port;
1165parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001166 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001167 if ((errstr = atoi_err(arg, &value)) != NULL)
1168 fatal("%s line %d: integer value %s.",
1169 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001170 if (*activep && *intptr == -1)
1171 *intptr = value;
1172 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173
Damien Miller95def091999-11-25 00:26:21 +11001174 case oConnectionAttempts:
1175 intptr = &options->connection_attempts;
1176 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001177
Damien Miller78928792000-04-12 20:17:38 +10001178 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001179 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001180 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001181 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001182 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001183 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001184 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001185 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001186 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001187 break;
1188
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001189 case oMacs:
1190 arg = strdelim(&s);
1191 if (!arg || *arg == '\0')
1192 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001193 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001194 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001195 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001196 if (*activep && options->macs == NULL)
1197 options->macs = xstrdup(arg);
1198 break;
1199
Damien Millerd5f62bf2010-09-24 22:11:14 +10001200 case oKexAlgorithms:
1201 arg = strdelim(&s);
1202 if (!arg || *arg == '\0')
1203 fatal("%.200s line %d: Missing argument.",
1204 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001205 if (*arg != '-' &&
1206 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001207 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1208 filename, linenum, arg ? arg : "<NONE>");
1209 if (*activep && options->kex_algorithms == NULL)
1210 options->kex_algorithms = xstrdup(arg);
1211 break;
1212
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001213 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001214 charptr = &options->hostkeyalgorithms;
1215parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001216 arg = strdelim(&s);
1217 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001218 fatal("%.200s line %d: Missing argument.",
1219 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001220 if (*arg != '-' &&
1221 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001222 fatal("%s line %d: Bad key types '%s'.",
1223 filename, linenum, arg ? arg : "<NONE>");
1224 if (*activep && *charptr == NULL)
1225 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001226 break;
1227
Damien Miller95def091999-11-25 00:26:21 +11001228 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001229 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001230 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001231 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001232 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001233 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001234 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001235 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1236 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001237 break;
1238
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001239 case oLogFacility:
1240 log_facility_ptr = &options->log_facility;
1241 arg = strdelim(&s);
1242 value = log_facility_number(arg);
1243 if (value == SYSLOG_FACILITY_NOT_SET)
1244 fatal("%.200s line %d: unsupported log facility '%s'",
1245 filename, linenum, arg ? arg : "<NONE>");
1246 if (*log_facility_ptr == -1)
1247 *log_facility_ptr = (SyslogFacility) value;
1248 break;
1249
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001250 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001251 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001252 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001253 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001254 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001255 fatal("%.200s line %d: Missing port argument.",
1256 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001257
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001258 remotefwd = (opcode == oRemoteForward);
1259 dynamicfwd = (opcode == oDynamicForward);
1260
1261 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001262 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001263 if (arg2 == NULL || *arg2 == '\0') {
1264 if (remotefwd)
1265 dynamicfwd = 1;
1266 else
1267 fatal("%.200s line %d: Missing target "
1268 "argument.", filename, linenum);
1269 } else {
1270 /* construct a string for parse_forward */
1271 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1272 arg2);
1273 }
Damien Millera699d952008-11-03 19:27:34 +11001274 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001275 if (dynamicfwd)
1276 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001277
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001278 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001279 fatal("%.200s line %d: Bad forwarding specification.",
1280 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001281
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001282 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001283 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001284 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001285 } else {
1286 add_local_forward(options, &fwd);
1287 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001288 }
Damien Miller95def091999-11-25 00:26:21 +11001289 break;
1290
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001291 case oClearAllForwardings:
1292 intptr = &options->clear_forwardings;
1293 goto parse_flag;
1294
Damien Miller95def091999-11-25 00:26:21 +11001295 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001296 if (cmdline)
1297 fatal("Host directive not supported as a command-line "
1298 "option");
Damien Miller95def091999-11-25 00:26:21 +11001299 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001300 arg2 = NULL;
1301 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001302 if ((flags & SSHCONF_NEVERMATCH) != 0)
1303 break;
Damien Millerfe924212011-05-15 08:44:45 +10001304 negated = *arg == '!';
1305 if (negated)
1306 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001307 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001308 if (negated) {
1309 debug("%.200s line %d: Skipping Host "
1310 "block because of negated match "
1311 "for %.100s", filename, linenum,
1312 arg);
1313 *activep = 0;
1314 break;
1315 }
1316 if (!*activep)
1317 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001318 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001319 }
Damien Millerfe924212011-05-15 08:44:45 +10001320 }
1321 if (*activep)
1322 debug("%.200s line %d: Applying options for %.100s",
1323 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001324 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001325 return 0;
1326
Damien Miller194fd902013-10-15 12:13:05 +11001327 case oMatch:
1328 if (cmdline)
1329 fatal("Host directive not supported as a command-line "
1330 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001331 value = match_cfg_line(options, &s, pw, host, original_host,
1332 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001333 if (value < 0)
1334 fatal("%.200s line %d: Bad Match condition", filename,
1335 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001336 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001337 break;
1338
Damien Miller95def091999-11-25 00:26:21 +11001339 case oEscapeChar:
1340 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001341 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001342 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001343 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001344 if (strcmp(arg, "none") == 0)
1345 value = SSH_ESCAPECHAR_NONE;
1346 else if (arg[1] == '\0')
1347 value = (u_char) arg[0];
1348 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001349 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1350 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001351 else {
1352 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001353 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001354 /* NOTREACHED */
1355 value = 0; /* Avoid compiler warning. */
1356 }
1357 if (*activep && *intptr == -1)
1358 *intptr = value;
1359 break;
1360
Damien Miller20a8f972003-05-18 20:50:30 +10001361 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001362 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001363 multistate_ptr = multistate_addressfamily;
1364 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001365
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001366 case oEnableSSHKeysign:
1367 intptr = &options->enable_ssh_keysign;
1368 goto parse_flag;
1369
Damien Millerbd394c32004-03-08 23:12:36 +11001370 case oIdentitiesOnly:
1371 intptr = &options->identities_only;
1372 goto parse_flag;
1373
Damien Miller509b0102003-12-17 16:33:10 +11001374 case oServerAliveInterval:
1375 intptr = &options->server_alive_interval;
1376 goto parse_time;
1377
1378 case oServerAliveCountMax:
1379 intptr = &options->server_alive_count_max;
1380 goto parse_int;
1381
Darren Tucker46bc0752004-05-02 22:11:30 +10001382 case oSendEnv:
1383 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1384 if (strchr(arg, '=') != NULL)
1385 fatal("%s line %d: Invalid environment name.",
1386 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001387 if (!*activep)
1388 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001389 if (*arg == '-') {
1390 /* Removing an env var */
1391 rm_env(options, arg, filename, linenum);
1392 continue;
1393 } else {
1394 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001395 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001396 fatal("%s line %d: too many send env.",
1397 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001398 options->send_env = xrecallocarray(
1399 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001400 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001401 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001402 options->send_env[options->num_send_env++] =
1403 xstrdup(arg);
1404 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001405 }
1406 break;
1407
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001408 case oSetEnv:
1409 value = options->num_setenv;
1410 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1411 if (strchr(arg, '=') == NULL)
1412 fatal("%s line %d: Invalid SetEnv.",
1413 filename, linenum);
1414 if (!*activep || value != 0)
1415 continue;
1416 /* Adding a setenv var */
1417 if (options->num_setenv >= INT_MAX)
1418 fatal("%s line %d: too many SetEnv.",
1419 filename, linenum);
1420 options->setenv = xrecallocarray(
1421 options->setenv, options->num_setenv,
1422 options->num_setenv + 1, sizeof(*options->setenv));
1423 options->setenv[options->num_setenv++] = xstrdup(arg);
1424 }
1425 break;
1426
Damien Miller0e220db2004-06-15 10:34:08 +10001427 case oControlPath:
1428 charptr = &options->control_path;
1429 goto parse_string;
1430
1431 case oControlMaster:
1432 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001433 multistate_ptr = multistate_controlmaster;
1434 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001435
Damien Millere11e1ea2010-08-03 16:04:46 +10001436 case oControlPersist:
1437 /* no/false/yes/true, or a time spec */
1438 intptr = &options->control_persist;
1439 arg = strdelim(&s);
1440 if (!arg || *arg == '\0')
1441 fatal("%.200s line %d: Missing ControlPersist"
1442 " argument.", filename, linenum);
1443 value = 0;
1444 value2 = 0; /* timeout */
1445 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1446 value = 0;
1447 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1448 value = 1;
1449 else if ((value2 = convtime(arg)) >= 0)
1450 value = 1;
1451 else
1452 fatal("%.200s line %d: Bad ControlPersist argument.",
1453 filename, linenum);
1454 if (*activep && *intptr == -1) {
1455 *intptr = value;
1456 options->control_persist_timeout = value2;
1457 }
1458 break;
1459
Damien Millere1776152005-03-01 21:47:37 +11001460 case oHashKnownHosts:
1461 intptr = &options->hash_known_hosts;
1462 goto parse_flag;
1463
Damien Millerd27b9472005-12-13 19:29:02 +11001464 case oTunnel:
1465 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001466 multistate_ptr = multistate_tunnel;
1467 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001468
1469 case oTunnelDevice:
1470 arg = strdelim(&s);
1471 if (!arg || *arg == '\0')
1472 fatal("%.200s line %d: Missing argument.", filename, linenum);
1473 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001474 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001475 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1476 if (*activep) {
1477 options->tun_local = value;
1478 options->tun_remote = value2;
1479 }
1480 break;
1481
1482 case oLocalCommand:
1483 charptr = &options->local_command;
1484 goto parse_command;
1485
1486 case oPermitLocalCommand:
1487 intptr = &options->permit_local_command;
1488 goto parse_flag;
1489
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001490 case oRemoteCommand:
1491 charptr = &options->remote_command;
1492 goto parse_command;
1493
Damien Miller10288242008-06-30 00:04:03 +10001494 case oVisualHostKey:
1495 intptr = &options->visual_host_key;
1496 goto parse_flag;
1497
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001498 case oInclude:
1499 if (cmdline)
1500 fatal("Include directive not supported as a "
1501 "command-line option");
1502 value = 0;
1503 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1504 /*
1505 * Ensure all paths are anchored. User configuration
1506 * files may begin with '~/' but system configurations
1507 * must not. If the path is relative, then treat it
1508 * as living in ~/.ssh for user configurations or
1509 * /etc/ssh for system ones.
1510 */
1511 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1512 fatal("%.200s line %d: bad include path %s.",
1513 filename, linenum, arg);
1514 if (*arg != '/' && *arg != '~') {
1515 xasprintf(&arg2, "%s/%s",
1516 (flags & SSHCONF_USERCONF) ?
1517 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1518 } else
1519 arg2 = xstrdup(arg);
1520 memset(&gl, 0, sizeof(gl));
1521 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1522 if (r == GLOB_NOMATCH) {
1523 debug("%.200s line %d: include %s matched no "
1524 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001525 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001526 continue;
1527 } else if (r != 0 || gl.gl_pathc < 0)
1528 fatal("%.200s line %d: glob failed for %s.",
1529 filename, linenum, arg2);
1530 free(arg2);
1531 oactive = *activep;
1532 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1533 debug3("%.200s line %d: Including file %s "
1534 "depth %d%s", filename, linenum,
1535 gl.gl_pathv[i], depth,
1536 oactive ? "" : " (parse only)");
1537 r = read_config_file_depth(gl.gl_pathv[i],
1538 pw, host, original_host, options,
1539 flags | SSHCONF_CHECKPERM |
1540 (oactive ? 0 : SSHCONF_NEVERMATCH),
1541 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001542 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001543 fatal("Can't open user config file "
1544 "%.100s: %.100s", gl.gl_pathv[i],
1545 strerror(errno));
1546 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001547 /*
1548 * don't let Match in includes clobber the
1549 * containing file's Match state.
1550 */
1551 *activep = oactive;
1552 if (r != 1)
1553 value = -1;
1554 }
1555 globfree(&gl);
1556 }
1557 if (value != 0)
1558 return value;
1559 break;
1560
Damien Miller0dac6fb2010-11-20 15:19:38 +11001561 case oIPQoS:
1562 arg = strdelim(&s);
1563 if ((value = parse_ipqos(arg)) == -1)
1564 fatal("%s line %d: Bad IPQoS value: %s",
1565 filename, linenum, arg);
1566 arg = strdelim(&s);
1567 if (arg == NULL)
1568 value2 = value;
1569 else if ((value2 = parse_ipqos(arg)) == -1)
1570 fatal("%s line %d: Bad IPQoS value: %s",
1571 filename, linenum, arg);
1572 if (*activep) {
1573 options->ip_qos_interactive = value;
1574 options->ip_qos_bulk = value2;
1575 }
1576 break;
1577
Damien Miller21771e22011-05-15 08:45:50 +10001578 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001579 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001580 multistate_ptr = multistate_requesttty;
1581 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001582
Darren Tucker07636982013-05-16 20:30:03 +10001583 case oIgnoreUnknown:
1584 charptr = &options->ignored_unknown;
1585 goto parse_string;
1586
Damien Miller1262b662013-08-21 02:44:24 +10001587 case oProxyUseFdpass:
1588 intptr = &options->proxy_use_fdpass;
1589 goto parse_flag;
1590
Damien Miller0faf7472013-10-17 11:47:23 +11001591 case oCanonicalDomains:
1592 value = options->num_canonical_domains != 0;
1593 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001594 if (!valid_domain(arg, 1, &errstr)) {
1595 fatal("%s line %d: %s", filename, linenum,
1596 errstr);
1597 }
Damien Miller0faf7472013-10-17 11:47:23 +11001598 if (!*activep || value)
1599 continue;
1600 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1601 fatal("%s line %d: too many hostname suffixes.",
1602 filename, linenum);
1603 options->canonical_domains[
1604 options->num_canonical_domains++] = xstrdup(arg);
1605 }
1606 break;
1607
Damien Miller38505592013-10-17 11:48:13 +11001608 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001609 value = options->num_permitted_cnames != 0;
1610 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1611 /* Either '*' for everything or 'list:list' */
1612 if (strcmp(arg, "*") == 0)
1613 arg2 = arg;
1614 else {
1615 lowercase(arg);
1616 if ((arg2 = strchr(arg, ':')) == NULL ||
1617 arg2[1] == '\0') {
1618 fatal("%s line %d: "
1619 "Invalid permitted CNAME \"%s\"",
1620 filename, linenum, arg);
1621 }
1622 *arg2 = '\0';
1623 arg2++;
1624 }
1625 if (!*activep || value)
1626 continue;
1627 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1628 fatal("%s line %d: too many permitted CNAMEs.",
1629 filename, linenum);
1630 cname = options->permitted_cnames +
1631 options->num_permitted_cnames++;
1632 cname->source_list = xstrdup(arg);
1633 cname->target_list = xstrdup(arg2);
1634 }
1635 break;
1636
Damien Miller38505592013-10-17 11:48:13 +11001637 case oCanonicalizeHostname:
1638 intptr = &options->canonicalize_hostname;
1639 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001640 goto parse_multistate;
1641
Damien Miller38505592013-10-17 11:48:13 +11001642 case oCanonicalizeMaxDots:
1643 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001644 goto parse_int;
1645
Damien Miller38505592013-10-17 11:48:13 +11001646 case oCanonicalizeFallbackLocal:
1647 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001648 goto parse_flag;
1649
Damien Miller7acefbb2014-07-18 14:11:24 +10001650 case oStreamLocalBindMask:
1651 arg = strdelim(&s);
1652 if (!arg || *arg == '\0')
1653 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1654 /* Parse mode in octal format */
1655 value = strtol(arg, &endofnumber, 8);
1656 if (arg == endofnumber || value < 0 || value > 0777)
1657 fatal("%.200s line %d: Bad mask.", filename, linenum);
1658 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1659 break;
1660
1661 case oStreamLocalBindUnlink:
1662 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1663 goto parse_flag;
1664
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001665 case oRevokedHostKeys:
1666 charptr = &options->revoked_host_keys;
1667 goto parse_string;
1668
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001669 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001670 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001671 arg = strdelim(&s);
1672 if (!arg || *arg == '\0')
1673 fatal("%.200s line %d: Missing argument.",
1674 filename, linenum);
1675 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1676 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1677 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001678 if (*activep && *intptr == -1)
1679 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001680 break;
1681
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001682 case oUpdateHostkeys:
1683 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001684 multistate_ptr = multistate_yesnoask;
1685 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001686
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001687 case oHostbasedKeyTypes:
1688 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001689 goto parse_keytypes;
1690
1691 case oPubkeyAcceptedKeyTypes:
1692 charptr = &options->pubkey_key_types;
1693 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001694
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001695 case oAddKeysToAgent:
1696 intptr = &options->add_keys_to_agent;
1697 multistate_ptr = multistate_yesnoaskconfirm;
1698 goto parse_multistate;
1699
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001700 case oIdentityAgent:
1701 charptr = &options->identity_agent;
1702 goto parse_string;
1703
Ben Lindstrom4daea862002-06-09 20:04:02 +00001704 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001705 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001706 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001707 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001708
Damien Millerf9b3feb2003-05-16 11:38:32 +10001709 case oUnsupported:
1710 error("%s line %d: Unsupported option \"%s\"",
1711 filename, linenum, keyword);
1712 return 0;
1713
Damien Miller95def091999-11-25 00:26:21 +11001714 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001715 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001716 }
1717
1718 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001719 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001720 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001721 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001722 }
Damien Miller95def091999-11-25 00:26:21 +11001723 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724}
1725
Damien Miller5428f641999-11-25 11:54:57 +11001726/*
1727 * Reads the config file and modifies the options accordingly. Options
1728 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001729 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001730 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001731int
Damien Miller194fd902013-10-15 12:13:05 +11001732read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001733 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001734{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001735 int active = 1;
1736
1737 return read_config_file_depth(filename, pw, host, original_host,
1738 options, flags, &active, 0);
1739}
1740
1741#define READCONF_MAX_DEPTH 16
1742static int
1743read_config_file_depth(const char *filename, struct passwd *pw,
1744 const char *host, const char *original_host, Options *options,
1745 int flags, int *activep, int depth)
1746{
Damien Miller95def091999-11-25 00:26:21 +11001747 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001748 char *line = NULL;
1749 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001750 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001751 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001752
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001753 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1754 fatal("Too many recursive configuration includes");
1755
Damien Miller57a44762004-04-20 20:11:57 +10001756 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001757 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758
Darren Tuckeraefa3682013-04-05 11:18:35 +11001759 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001760 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001761
Damien Miller33793852004-06-15 10:27:55 +10001762 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001763 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001764 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001765 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001766 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001767 }
1768
Damien Miller95def091999-11-25 00:26:21 +11001769 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001770
Damien Miller5428f641999-11-25 11:54:57 +11001771 /*
1772 * Mark that we are now processing the options. This flag is turned
1773 * on/off by Host specifications.
1774 */
Damien Miller95def091999-11-25 00:26:21 +11001775 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001776 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001777 /* Update line number counter. */
1778 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001779 if (process_config_line_depth(options, pw, host, original_host,
1780 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001781 bad_options++;
1782 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001783 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001784 fclose(f);
1785 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001786 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001787 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001788 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001789}
1790
Damien Miller13f97b22014-02-24 15:57:55 +11001791/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1792int
1793option_clear_or_none(const char *o)
1794{
1795 return o == NULL || strcasecmp(o, "none") == 0;
1796}
1797
Damien Miller5428f641999-11-25 11:54:57 +11001798/*
1799 * Initializes options to special values that indicate that they have not yet
1800 * been set. Read_config_file will only set options with this value. Options
1801 * are processed in the following order: command line, user config file,
1802 * system config file. Last, fill_default_options is called.
1803 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804
Damien Miller4af51302000-04-16 11:18:38 +10001805void
Damien Miller95def091999-11-25 00:26:21 +11001806initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807{
Damien Miller95def091999-11-25 00:26:21 +11001808 memset(options, 'X', sizeof(*options));
1809 options->forward_agent = -1;
1810 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001811 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001812 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001813 options->stdio_forward_host = NULL;
1814 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001815 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001816 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001817 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001818 options->fwd_opts.gateway_ports = -1;
1819 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1820 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001821 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001822 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001823 options->gss_authentication = -1;
1824 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001825 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001826 options->kbd_interactive_authentication = -1;
1827 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001828 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001829 options->batch_mode = -1;
1830 options->check_host_ip = -1;
1831 options->strict_host_key_checking = -1;
1832 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001833 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001834 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001835 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001836 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001837 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001838 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001839 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001840 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001841 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001842 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001843 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001844 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001845 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001846 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001847 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001848 options->jump_user = NULL;
1849 options->jump_host = NULL;
1850 options->jump_port = -1;
1851 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001852 options->user = NULL;
1853 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001854 options->num_system_hostfiles = 0;
1855 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001856 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001857 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001858 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001859 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001860 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001861 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001862 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001863 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001864 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001865 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001866 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001867 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001868 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001869 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001870 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001871 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001872 options->server_alive_interval = -1;
1873 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001874 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001875 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001876 options->setenv = NULL;
1877 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001878 options->control_path = NULL;
1879 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001880 options->control_persist = -1;
1881 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001882 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001883 options->tun_open = -1;
1884 options->tun_local = -1;
1885 options->tun_remote = -1;
1886 options->local_command = NULL;
1887 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001888 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001889 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001890 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001891 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001892 options->ip_qos_interactive = -1;
1893 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001894 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001895 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001896 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001897 options->num_canonical_domains = 0;
1898 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001899 options->canonicalize_max_dots = -1;
1900 options->canonicalize_fallback_local = -1;
1901 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001902 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001903 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001904 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001905 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001906 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907}
1908
Damien Miller5428f641999-11-25 11:54:57 +11001909/*
Damien Miller13f97b22014-02-24 15:57:55 +11001910 * A petite version of fill_default_options() that just fills the options
1911 * needed for hostname canonicalization to proceed.
1912 */
1913void
1914fill_default_options_for_canonicalization(Options *options)
1915{
1916 if (options->canonicalize_max_dots == -1)
1917 options->canonicalize_max_dots = 1;
1918 if (options->canonicalize_fallback_local == -1)
1919 options->canonicalize_fallback_local = 1;
1920 if (options->canonicalize_hostname == -1)
1921 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1922}
1923
1924/*
Damien Miller5428f641999-11-25 11:54:57 +11001925 * Called after processing other sources of option data, this fills those
1926 * options for which no value has been specified with their default values.
1927 */
Damien Miller4af51302000-04-16 11:18:38 +10001928void
Damien Miller95def091999-11-25 00:26:21 +11001929fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001930{
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001931 char *all_cipher, *all_mac, *all_kex, *all_key;
1932
Damien Miller95def091999-11-25 00:26:21 +11001933 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001934 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001935 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001936 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001937 if (options->forward_x11_trusted == -1)
1938 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001939 if (options->forward_x11_timeout == -1)
1940 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001941 /*
1942 * stdio forwarding (-W) changes the default for these but we defer
1943 * setting the values so they can be overridden.
1944 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001945 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001946 options->exit_on_forward_failure =
1947 options->stdio_forward_host != NULL ? 1 : 0;
1948 if (options->clear_forwardings == -1)
1949 options->clear_forwardings =
1950 options->stdio_forward_host != NULL ? 1 : 0;
1951 if (options->clear_forwardings == 1)
1952 clear_forwardings(options);
1953
Damien Millerd3a18572000-06-07 19:55:44 +10001954 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001955 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001956 if (options->fwd_opts.gateway_ports == -1)
1957 options->fwd_opts.gateway_ports = 0;
1958 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1959 options->fwd_opts.streamlocal_bind_mask = 0177;
1960 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1961 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001962 if (options->pubkey_authentication == -1)
1963 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001964 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001965 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001966 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001967 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001968 if (options->gss_deleg_creds == -1)
1969 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001970 if (options->password_authentication == -1)
1971 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001972 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001973 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001974 if (options->hostbased_authentication == -1)
1975 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001976 if (options->batch_mode == -1)
1977 options->batch_mode = 0;
1978 if (options->check_host_ip == -1)
1979 options->check_host_ip = 1;
1980 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001981 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001982 if (options->compression == -1)
1983 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001984 if (options->tcp_keep_alive == -1)
1985 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001986 if (options->port == -1)
1987 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001988 if (options->address_family == -1)
1989 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001990 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001991 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001992 if (options->number_of_password_prompts == -1)
1993 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001994 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001995 if (options->add_keys_to_agent == -1)
1996 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001997 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001998 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1999 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002000#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002001 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002002#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002003 add_identity_file(options, "~/",
2004 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002005 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002006 }
Damien Miller95def091999-11-25 00:26:21 +11002007 if (options->escape_char == -1)
2008 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002009 if (options->num_system_hostfiles == 0) {
2010 options->system_hostfiles[options->num_system_hostfiles++] =
2011 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2012 options->system_hostfiles[options->num_system_hostfiles++] =
2013 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2014 }
2015 if (options->num_user_hostfiles == 0) {
2016 options->user_hostfiles[options->num_user_hostfiles++] =
2017 xstrdup(_PATH_SSH_USER_HOSTFILE);
2018 options->user_hostfiles[options->num_user_hostfiles++] =
2019 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2020 }
Damien Millerfcd93202002-02-05 12:26:34 +11002021 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002022 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002023 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2024 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002025 if (options->no_host_authentication_for_localhost == - 1)
2026 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002027 if (options->identities_only == -1)
2028 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002029 if (options->enable_ssh_keysign == -1)
2030 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002031 if (options->rekey_limit == -1)
2032 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002033 if (options->rekey_interval == -1)
2034 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002035 if (options->verify_host_key_dns == -1)
2036 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002037 if (options->server_alive_interval == -1)
2038 options->server_alive_interval = 0;
2039 if (options->server_alive_count_max == -1)
2040 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002041 if (options->control_master == -1)
2042 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002043 if (options->control_persist == -1) {
2044 options->control_persist = 0;
2045 options->control_persist_timeout = 0;
2046 }
Damien Millere1776152005-03-01 21:47:37 +11002047 if (options->hash_known_hosts == -1)
2048 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002049 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002050 options->tun_open = SSH_TUNMODE_NO;
2051 if (options->tun_local == -1)
2052 options->tun_local = SSH_TUNID_ANY;
2053 if (options->tun_remote == -1)
2054 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002055 if (options->permit_local_command == -1)
2056 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002057 if (options->visual_host_key == -1)
2058 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002059 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002060 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002061 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002062 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002063 if (options->request_tty == -1)
2064 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002065 if (options->proxy_use_fdpass == -1)
2066 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002067 if (options->canonicalize_max_dots == -1)
2068 options->canonicalize_max_dots = 1;
2069 if (options->canonicalize_fallback_local == -1)
2070 options->canonicalize_fallback_local = 1;
2071 if (options->canonicalize_hostname == -1)
2072 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002073 if (options->fingerprint_hash == -1)
2074 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002075 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002076 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002077
2078 /* Expand KEX name lists */
2079 all_cipher = cipher_alg_list(',', 0);
2080 all_mac = mac_alg_list(',');
2081 all_kex = kex_alg_list(',');
2082 all_key = sshkey_alg_list(0, 0, 1, ',');
2083 if (kex_assemble_names(&options->ciphers,
2084 KEX_CLIENT_ENCRYPT, all_cipher) != 0 ||
2085 kex_assemble_names(&options->macs,
2086 KEX_CLIENT_MAC, all_mac) != 0 ||
2087 kex_assemble_names(&options->kex_algorithms,
2088 KEX_CLIENT_KEX, all_kex) != 0 ||
2089 kex_assemble_names(&options->hostbased_key_types,
2090 KEX_DEFAULT_PK_ALG, all_key) != 0 ||
2091 kex_assemble_names(&options->pubkey_key_types,
2092 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002093 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002094 free(all_cipher);
2095 free(all_mac);
2096 free(all_kex);
2097 free(all_key);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002098
Damien Millere9fc72e2013-10-15 12:14:12 +11002099#define CLEAR_ON_NONE(v) \
2100 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002101 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002102 free(v); \
2103 v = NULL; \
2104 } \
2105 } while(0)
2106 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002107 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002108 CLEAR_ON_NONE(options->proxy_command);
2109 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002110 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002111 if (options->jump_host != NULL &&
2112 strcmp(options->jump_host, "none") == 0 &&
2113 options->jump_port == 0 && options->jump_user == NULL) {
2114 free(options->jump_host);
2115 options->jump_host = NULL;
2116 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002117 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002118 /* options->user will be set in the main program if appropriate */
2119 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002120 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002121 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002122}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002123
Damien Miller7acefbb2014-07-18 14:11:24 +10002124struct fwdarg {
2125 char *arg;
2126 int ispath;
2127};
2128
2129/*
2130 * parse_fwd_field
2131 * parses the next field in a port forwarding specification.
2132 * sets fwd to the parsed field and advances p past the colon
2133 * or sets it to NULL at end of string.
2134 * returns 0 on success, else non-zero.
2135 */
2136static int
2137parse_fwd_field(char **p, struct fwdarg *fwd)
2138{
2139 char *ep, *cp = *p;
2140 int ispath = 0;
2141
2142 if (*cp == '\0') {
2143 *p = NULL;
2144 return -1; /* end of string */
2145 }
2146
2147 /*
2148 * A field escaped with square brackets is used literally.
2149 * XXX - allow ']' to be escaped via backslash?
2150 */
2151 if (*cp == '[') {
2152 /* find matching ']' */
2153 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2154 if (*ep == '/')
2155 ispath = 1;
2156 }
2157 /* no matching ']' or not at end of field. */
2158 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2159 return -1;
2160 /* NUL terminate the field and advance p past the colon */
2161 *ep++ = '\0';
2162 if (*ep != '\0')
2163 *ep++ = '\0';
2164 fwd->arg = cp + 1;
2165 fwd->ispath = ispath;
2166 *p = ep;
2167 return 0;
2168 }
2169
2170 for (cp = *p; *cp != '\0'; cp++) {
2171 switch (*cp) {
2172 case '\\':
2173 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002174 if (*cp == '\0')
2175 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002176 break;
2177 case '/':
2178 ispath = 1;
2179 break;
2180 case ':':
2181 *cp++ = '\0';
2182 goto done;
2183 }
2184 }
2185done:
2186 fwd->arg = *p;
2187 fwd->ispath = ispath;
2188 *p = cp;
2189 return 0;
2190}
2191
Damien Millerf91ee4c2005-03-01 21:24:33 +11002192/*
2193 * parse_forward
2194 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002195 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002196 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2197 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002198 * dynamicfwd == 1
2199 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002200 * returns number of arguments parsed or zero on error
2201 */
2202int
Damien Miller7acefbb2014-07-18 14:11:24 +10002203parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002204{
Damien Miller7acefbb2014-07-18 14:11:24 +10002205 struct fwdarg fwdargs[4];
2206 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002207 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002208
Damien Miller7acefbb2014-07-18 14:11:24 +10002209 memset(fwd, 0, sizeof(*fwd));
2210 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002211
2212 cp = p = xstrdup(fwdspec);
2213
2214 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002215 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002216 cp++;
2217
Damien Miller7acefbb2014-07-18 14:11:24 +10002218 for (i = 0; i < 4; ++i) {
2219 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002220 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002221 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002222
Damien Millerf4b39532008-11-03 19:28:21 +11002223 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002224 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002225 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002226 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002227
2228 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002229 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002230 if (fwdargs[0].ispath) {
2231 fwd->listen_path = xstrdup(fwdargs[0].arg);
2232 fwd->listen_port = PORT_STREAMLOCAL;
2233 } else {
2234 fwd->listen_host = NULL;
2235 fwd->listen_port = a2port(fwdargs[0].arg);
2236 }
Damien Millera699d952008-11-03 19:27:34 +11002237 fwd->connect_host = xstrdup("socks");
2238 break;
2239
2240 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002241 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2242 fwd->listen_path = xstrdup(fwdargs[0].arg);
2243 fwd->listen_port = PORT_STREAMLOCAL;
2244 fwd->connect_path = xstrdup(fwdargs[1].arg);
2245 fwd->connect_port = PORT_STREAMLOCAL;
2246 } else if (fwdargs[1].ispath) {
2247 fwd->listen_host = NULL;
2248 fwd->listen_port = a2port(fwdargs[0].arg);
2249 fwd->connect_path = xstrdup(fwdargs[1].arg);
2250 fwd->connect_port = PORT_STREAMLOCAL;
2251 } else {
2252 fwd->listen_host = xstrdup(fwdargs[0].arg);
2253 fwd->listen_port = a2port(fwdargs[1].arg);
2254 fwd->connect_host = xstrdup("socks");
2255 }
Damien Millera699d952008-11-03 19:27:34 +11002256 break;
2257
Damien Millerf91ee4c2005-03-01 21:24:33 +11002258 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002259 if (fwdargs[0].ispath) {
2260 fwd->listen_path = xstrdup(fwdargs[0].arg);
2261 fwd->listen_port = PORT_STREAMLOCAL;
2262 fwd->connect_host = xstrdup(fwdargs[1].arg);
2263 fwd->connect_port = a2port(fwdargs[2].arg);
2264 } else if (fwdargs[2].ispath) {
2265 fwd->listen_host = xstrdup(fwdargs[0].arg);
2266 fwd->listen_port = a2port(fwdargs[1].arg);
2267 fwd->connect_path = xstrdup(fwdargs[2].arg);
2268 fwd->connect_port = PORT_STREAMLOCAL;
2269 } else {
2270 fwd->listen_host = NULL;
2271 fwd->listen_port = a2port(fwdargs[0].arg);
2272 fwd->connect_host = xstrdup(fwdargs[1].arg);
2273 fwd->connect_port = a2port(fwdargs[2].arg);
2274 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002275 break;
2276
2277 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002278 fwd->listen_host = xstrdup(fwdargs[0].arg);
2279 fwd->listen_port = a2port(fwdargs[1].arg);
2280 fwd->connect_host = xstrdup(fwdargs[2].arg);
2281 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002282 break;
2283 default:
2284 i = 0; /* failure */
2285 }
2286
Darren Tuckera627d422013-06-02 07:31:17 +10002287 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002288
Damien Millera699d952008-11-03 19:27:34 +11002289 if (dynamicfwd) {
2290 if (!(i == 1 || i == 2))
2291 goto fail_free;
2292 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002293 if (!(i == 3 || i == 4)) {
2294 if (fwd->connect_path == NULL &&
2295 fwd->listen_path == NULL)
2296 goto fail_free;
2297 }
2298 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002299 goto fail_free;
2300 }
2301
Damien Miller7acefbb2014-07-18 14:11:24 +10002302 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2303 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002304 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002305 if (fwd->connect_host != NULL &&
2306 strlen(fwd->connect_host) >= NI_MAXHOST)
2307 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002308 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2309 if (fwd->connect_path != NULL &&
2310 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2311 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002312 if (fwd->listen_host != NULL &&
2313 strlen(fwd->listen_host) >= NI_MAXHOST)
2314 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002315 if (fwd->listen_path != NULL &&
2316 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2317 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002318
2319 return (i);
2320
2321 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002322 free(fwd->connect_host);
2323 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002324 free(fwd->connect_path);
2325 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002326 free(fwd->listen_host);
2327 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002328 free(fwd->listen_path);
2329 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002330 return (0);
2331}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002332
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002333int
2334parse_jump(const char *s, Options *o, int active)
2335{
2336 char *orig, *sdup, *cp;
2337 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002338 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002339
2340 active &= o->proxy_command == NULL && o->jump_host == NULL;
2341
2342 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002343 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002344 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002345 if (strcasecmp(s, "none") == 0)
2346 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002347 if ((cp = strrchr(sdup, ',')) == NULL)
2348 cp = sdup; /* last */
2349 else
2350 *cp++ = '\0';
2351
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002352 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002353 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002354 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2355 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002356 goto out;
2357 } else {
2358 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002359 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2360 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002361 goto out;
2362 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002363 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002364 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002365 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002366 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002367 if (strcasecmp(s, "none") == 0) {
2368 o->jump_host = xstrdup("none");
2369 o->jump_port = 0;
2370 } else {
2371 o->jump_user = user;
2372 o->jump_host = host;
2373 o->jump_port = port;
2374 o->proxy_command = xstrdup("none");
2375 user = host = NULL;
2376 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2377 o->jump_extra = xstrdup(s);
2378 o->jump_extra[cp - s] = '\0';
2379 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002380 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002381 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002382 ret = 0;
2383 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002384 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002385 free(user);
2386 free(host);
2387 return ret;
2388}
2389
millert@openbsd.org887669e2017-10-21 23:06:24 +00002390int
2391parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2392{
2393 char *path;
2394 int r;
2395
2396 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2397 if (r == 0 && path != NULL)
2398 r = -1; /* path not allowed */
2399 return r;
2400}
2401
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002402/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2403static const char *
2404fmt_multistate_int(int val, const struct multistate *m)
2405{
2406 u_int i;
2407
2408 for (i = 0; m[i].key != NULL; i++) {
2409 if (m[i].value == val)
2410 return m[i].key;
2411 }
2412 return "UNKNOWN";
2413}
2414
2415static const char *
2416fmt_intarg(OpCodes code, int val)
2417{
2418 if (val == -1)
2419 return "unset";
2420 switch (code) {
2421 case oAddressFamily:
2422 return fmt_multistate_int(val, multistate_addressfamily);
2423 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002424 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002425 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002426 case oStrictHostKeyChecking:
2427 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002428 case oControlMaster:
2429 return fmt_multistate_int(val, multistate_controlmaster);
2430 case oTunnel:
2431 return fmt_multistate_int(val, multistate_tunnel);
2432 case oRequestTTY:
2433 return fmt_multistate_int(val, multistate_requesttty);
2434 case oCanonicalizeHostname:
2435 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002436 case oAddKeysToAgent:
2437 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002438 case oFingerprintHash:
2439 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002440 default:
2441 switch (val) {
2442 case 0:
2443 return "no";
2444 case 1:
2445 return "yes";
2446 default:
2447 return "UNKNOWN";
2448 }
2449 }
2450}
2451
2452static const char *
2453lookup_opcode_name(OpCodes code)
2454{
2455 u_int i;
2456
2457 for (i = 0; keywords[i].name != NULL; i++)
2458 if (keywords[i].opcode == code)
2459 return(keywords[i].name);
2460 return "UNKNOWN";
2461}
2462
2463static void
2464dump_cfg_int(OpCodes code, int val)
2465{
2466 printf("%s %d\n", lookup_opcode_name(code), val);
2467}
2468
2469static void
2470dump_cfg_fmtint(OpCodes code, int val)
2471{
2472 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2473}
2474
2475static void
2476dump_cfg_string(OpCodes code, const char *val)
2477{
2478 if (val == NULL)
2479 return;
2480 printf("%s %s\n", lookup_opcode_name(code), val);
2481}
2482
2483static void
2484dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2485{
2486 u_int i;
2487
2488 for (i = 0; i < count; i++)
2489 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2490}
2491
2492static void
2493dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2494{
2495 u_int i;
2496
2497 printf("%s", lookup_opcode_name(code));
2498 for (i = 0; i < count; i++)
2499 printf(" %s", vals[i]);
2500 printf("\n");
2501}
2502
2503static void
2504dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2505{
2506 const struct Forward *fwd;
2507 u_int i;
2508
2509 /* oDynamicForward */
2510 for (i = 0; i < count; i++) {
2511 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002512 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002513 strcmp(fwd->connect_host, "socks") != 0)
2514 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002515 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002516 strcmp(fwd->connect_host, "socks") == 0)
2517 continue;
2518 printf("%s", lookup_opcode_name(code));
2519 if (fwd->listen_port == PORT_STREAMLOCAL)
2520 printf(" %s", fwd->listen_path);
2521 else if (fwd->listen_host == NULL)
2522 printf(" %d", fwd->listen_port);
2523 else {
2524 printf(" [%s]:%d",
2525 fwd->listen_host, fwd->listen_port);
2526 }
2527 if (code != oDynamicForward) {
2528 if (fwd->connect_port == PORT_STREAMLOCAL)
2529 printf(" %s", fwd->connect_path);
2530 else if (fwd->connect_host == NULL)
2531 printf(" %d", fwd->connect_port);
2532 else {
2533 printf(" [%s]:%d",
2534 fwd->connect_host, fwd->connect_port);
2535 }
2536 }
2537 printf("\n");
2538 }
2539}
2540
2541void
2542dump_client_config(Options *o, const char *host)
2543{
2544 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002545 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002546
djm@openbsd.org60a92472015-08-21 23:53:08 +00002547 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002548 all_key = sshkey_alg_list(0, 0, 1, ',');
2549 if (kex_assemble_names( &o->hostkeyalgorithms,
2550 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002551 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002552 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002553
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002554 /* Most interesting options first: user, host, port */
2555 dump_cfg_string(oUser, o->user);
2556 dump_cfg_string(oHostName, host);
2557 dump_cfg_int(oPort, o->port);
2558
2559 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002560 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002561 dump_cfg_fmtint(oAddressFamily, o->address_family);
2562 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2563 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2564 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2565 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2566 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2567 dump_cfg_fmtint(oCompression, o->compression);
2568 dump_cfg_fmtint(oControlMaster, o->control_master);
2569 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002570 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002571 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002572 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002573 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2574 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2575 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2576 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2577#ifdef GSSAPI
2578 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2579 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2580#endif /* GSSAPI */
2581 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2582 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2583 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2584 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2585 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2586 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2587 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002588 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2589 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2590 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002591 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2592 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2593 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2594 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002595 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2596 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002597 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002598
2599 /* Integer options */
2600 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002601 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2602 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2603 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2604 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2605 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2606
2607 /* String options */
2608 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002609 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002610 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2611 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002612 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002613 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002614 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002615 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002616 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002617 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2618 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2619 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002620 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002621 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2622 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002623#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002624 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002625#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002626 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002627 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002628 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002629 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002630
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002631 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002632 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2633 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2634 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2635
2636 /* String array options */
2637 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2638 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002639 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002640 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2641 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2642 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002643 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002644
2645 /* Special cases */
2646
2647 /* oConnectTimeout */
2648 if (o->connection_timeout == -1)
2649 printf("connecttimeout none\n");
2650 else
2651 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2652
2653 /* oTunnelDevice */
2654 printf("tunneldevice");
2655 if (o->tun_local == SSH_TUNID_ANY)
2656 printf(" any");
2657 else
2658 printf(" %d", o->tun_local);
2659 if (o->tun_remote == SSH_TUNID_ANY)
2660 printf(":any");
2661 else
2662 printf(":%d", o->tun_remote);
2663 printf("\n");
2664
2665 /* oCanonicalizePermittedCNAMEs */
2666 if ( o->num_permitted_cnames > 0) {
2667 printf("canonicalizePermittedcnames");
2668 for (i = 0; i < o->num_permitted_cnames; i++) {
2669 printf(" %s:%s", o->permitted_cnames[i].source_list,
2670 o->permitted_cnames[i].target_list);
2671 }
2672 printf("\n");
2673 }
2674
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002675 /* oControlPersist */
2676 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2677 dump_cfg_fmtint(oControlPersist, o->control_persist);
2678 else
2679 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2680
2681 /* oEscapeChar */
2682 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2683 printf("escapechar none\n");
2684 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002685 vis(buf, o->escape_char, VIS_WHITE, 0);
2686 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002687 }
2688
2689 /* oIPQoS */
2690 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2691 printf("%s\n", iptos2str(o->ip_qos_bulk));
2692
2693 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002694 printf("rekeylimit %llu %d\n",
2695 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002696
2697 /* oStreamLocalBindMask */
2698 printf("streamlocalbindmask 0%o\n",
2699 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002700
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002701 /* oLogFacility */
2702 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2703
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002704 /* oProxyCommand / oProxyJump */
2705 if (o->jump_host == NULL)
2706 dump_cfg_string(oProxyCommand, o->proxy_command);
2707 else {
2708 /* Check for numeric addresses */
2709 i = strchr(o->jump_host, ':') != NULL ||
2710 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2711 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2712 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002713 /* optional additional jump spec */
2714 o->jump_extra == NULL ? "" : o->jump_extra,
2715 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002716 /* optional user */
2717 o->jump_user == NULL ? "" : o->jump_user,
2718 o->jump_user == NULL ? "" : "@",
2719 /* opening [ if hostname is numeric */
2720 i ? "[" : "",
2721 /* mandatory hostname */
2722 o->jump_host,
2723 /* closing ] if hostname is numeric */
2724 i ? "]" : "",
2725 /* optional port number */
2726 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002727 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002728 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002729}