blob: 7331ef5ad6cae3f566785c8584f73dd75e3db007 [file] [log] [blame]
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001/* $OpenBSD: readconf.c,v 1.302 2018/11/23 05:08:07 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +000056#include "ssherr.h"
Damien Miller78928792000-04-12 20:17:38 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
59#include "pathnames.h"
60#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000061#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100062#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "readconf.h"
64#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "kex.h"
66#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110067#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000068#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000069#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Format of the configuration file:
72
73 # Configuration data is parsed as follows:
74 # 1. command line options
75 # 2. user-specific file
76 # 3. system-wide file
77 # Any configuration value is only changed the first time it is set.
78 # Thus, host-specific definitions should be at the beginning of the
79 # configuration file, and defaults at the end.
80
81 # Host-specific declarations. These may override anything above. A single
82 # host may match multiple declarations; these are processed in the order
83 # that they are given in.
84
85 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host fake.com
89 HostName another.host.name.real.org
90 User blaah
91 Port 34289
92 ForwardX11 no
93 ForwardAgent no
94
95 Host books.com
96 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000097 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host fascist.blob.com
100 Port 23123
101 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 PasswordAuthentication no
103
104 Host puukko.hut.fi
105 User t35124p
106 ProxyCommand ssh-proxy %h %p
107
108 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000112 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 PasswordAuthentication no
114
Damien Millerd27b9472005-12-13 19:29:02 +1100115 Host vpn.fake.com
116 Tunnel yes
117 TunnelDevice 3
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 # Defaults for various options
120 Host *
121 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 PasswordAuthentication yes
124 RSAAuthentication yes
125 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100127 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 IdentityFile ~/.ssh/identity
129 Port 22
130 EscapeChar ~
131
132*/
133
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000134static int read_config_file_depth(const char *filename, struct passwd *pw,
135 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000136 int flags, int *activep, int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000137static int process_config_line_depth(Options *options, struct passwd *pw,
138 const char *host, const char *original_host, char *line,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000139 const char *filename, int linenum, int *activep, int flags,
140 int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000141
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142/* Keyword tokens. */
143
Damien Miller95def091999-11-25 00:26:21 +1100144typedef enum {
145 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000146 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000147 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
148 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000149 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000150 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100151 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000152 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100153 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100154 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
155 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100156 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000157 oUsePrivilegedPort, oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000158 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000159 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000160 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000161 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000162 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000163 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000164 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100165 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000166 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000167 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000168 oTunnel, oTunnelDevice,
169 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000170 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000171 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100172 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
173 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000174 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000175 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000176 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000177 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178} OpCodes;
179
180/* Textual representations of the tokens. */
181
Damien Miller95def091999-11-25 00:26:21 +1100182static struct {
183 const char *name;
184 OpCodes opcode;
185} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000186 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000187 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000188 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000189 { "fallbacktorsh", oDeprecated },
190 { "globalknownhostsfile2", oDeprecated },
191 { "rhostsauthentication", oDeprecated },
192 { "userknownhostsfile2", oDeprecated },
193 { "useroaming", oDeprecated },
194 { "usersh", oDeprecated },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000195 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000196
197 /* Unsupported options */
198 { "afstokenpassing", oUnsupported },
199 { "kerberosauthentication", oUnsupported },
200 { "kerberostgtpassing", oUnsupported },
201
202 /* Sometimes-unsupported options */
203#if defined(GSSAPI)
204 { "gssapiauthentication", oGssAuthentication },
205 { "gssapidelegatecredentials", oGssDelegateCreds },
206# else
207 { "gssapiauthentication", oUnsupported },
208 { "gssapidelegatecredentials", oUnsupported },
209#endif
210#ifdef ENABLE_PKCS11
211 { "smartcarddevice", oPKCS11Provider },
212 { "pkcs11provider", oPKCS11Provider },
213# else
214 { "smartcarddevice", oUnsupported },
215 { "pkcs11provider", oUnsupported },
216#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000217 { "rsaauthentication", oUnsupported },
218 { "rhostsrsaauthentication", oUnsupported },
219 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000220
Damien Miller95def091999-11-25 00:26:21 +1100221 { "forwardagent", oForwardAgent },
222 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000223 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000224 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000225 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000226 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100227 { "gatewayports", oGatewayPorts },
Damien Miller95def091999-11-25 00:26:21 +1100228 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100229 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
230 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100231 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000232 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000233 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000234 { "challengeresponseauthentication", oChallengeResponseAuthentication },
Damien Miller87f08be2018-07-20 13:18:28 +1000235 { "skeyauthentication", oUnsupported },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000236 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100237 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100238 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100239 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000240 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000241 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000242 { "identityagent", oIdentityAgent },
Damien Miller95def091999-11-25 00:26:21 +1100243 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000244 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100245 { "proxycommand", oProxyCommand },
246 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000247 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000248 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100249 { "remoteforward", oRemoteForward },
250 { "localforward", oLocalForward },
251 { "user", oUser },
252 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100253 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100254 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100255 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100256 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100257 { "connectionattempts", oConnectionAttempts },
258 { "batchmode", oBatchMode },
259 { "checkhostip", oCheckHostIP },
260 { "stricthostkeychecking", oStrictHostKeyChecking },
261 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100262 { "tcpkeepalive", oTCPKeepAlive },
263 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100264 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000265 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100266 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000267 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000268 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000269 { "hostkeyalgorithms", oHostKeyAlgorithms },
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000270 { "casignaturealgorithms", oCASignatureAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000271 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000272 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100273 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000274 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000275 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100276 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000277 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000278 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000279 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100280 { "serveraliveinterval", oServerAliveInterval },
281 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000282 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000283 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000284 { "controlpath", oControlPath },
285 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000286 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100287 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000288 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100289 { "tunnel", oTunnel },
290 { "tunneldevice", oTunnelDevice },
291 { "localcommand", oLocalCommand },
292 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000293 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000294 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000295 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100296 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000297 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000298 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100299 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100300 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
301 { "canonicalizehostname", oCanonicalizeHostname },
302 { "canonicalizemaxdots", oCanonicalizeMaxDots },
303 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000304 { "streamlocalbindmask", oStreamLocalBindMask },
305 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000306 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000307 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000308 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000309 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000310 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000311 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000312 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100313
Ben Lindstrom65366a82001-12-06 16:32:47 +0000314 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100315};
316
Damien Miller5428f641999-11-25 11:54:57 +1100317/*
318 * Adds a local TCP/IP port forward to options. Never returns if there is an
319 * error.
320 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Miller4af51302000-04-16 11:18:38 +1000322void
Damien Miller7acefbb2014-07-18 14:11:24 +1000323add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Damien Miller7acefbb2014-07-18 14:11:24 +1000325 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000326 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000327
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000328 /* Don't add duplicates */
329 for (i = 0; i < options->num_local_forwards; i++) {
330 if (forward_equals(newfwd, options->local_forwards + i))
331 return;
332 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000333 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000334 options->num_local_forwards + 1,
335 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100336 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337
Damien Miller1a0442f2008-11-05 16:30:06 +1100338 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100341 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100342 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000343 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344}
345
Damien Miller5428f641999-11-25 11:54:57 +1100346/*
347 * Adds a remote TCP/IP port forward to options. Never returns if there is
348 * an error.
349 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller4af51302000-04-16 11:18:38 +1000351void
Damien Miller7acefbb2014-07-18 14:11:24 +1000352add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller7acefbb2014-07-18 14:11:24 +1000354 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000355 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000356
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000357 /* Don't add duplicates */
358 for (i = 0; i < options->num_remote_forwards; i++) {
359 if (forward_equals(newfwd, options->remote_forwards + i))
360 return;
361 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000362 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000363 options->num_remote_forwards + 1,
364 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100365 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100366
Damien Miller1a0442f2008-11-05 16:30:06 +1100367 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000369 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100370 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100371 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000372 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100373 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000374 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375}
376
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000377static void
378clear_forwardings(Options *options)
379{
380 int i;
381
Damien Millerf91ee4c2005-03-01 21:24:33 +1100382 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000384 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000386 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100387 }
Damien Miller232cfb12010-06-26 09:50:30 +1000388 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000390 options->local_forwards = NULL;
391 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000392 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100393 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000394 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000395 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100398 }
Damien Miller232cfb12010-06-26 09:50:30 +1000399 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000400 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000401 options->remote_forwards = NULL;
402 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000403 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100404 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405}
406
Darren Tucker19104782013-04-05 11:13:08 +1100407void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000408add_certificate_file(Options *options, const char *path, int userprovided)
409{
410 int i;
411
412 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
413 fatal("Too many certificate files specified (max %d)",
414 SSH_MAX_CERTIFICATE_FILES);
415
416 /* Avoid registering duplicates */
417 for (i = 0; i < options->num_certificate_files; i++) {
418 if (options->certificate_file_userprovided[i] == userprovided &&
419 strcmp(options->certificate_files[i], path) == 0) {
420 debug2("%s: ignoring duplicate key %s", __func__, path);
421 return;
422 }
423 }
424
425 options->certificate_file_userprovided[options->num_certificate_files] =
426 userprovided;
427 options->certificate_files[options->num_certificate_files++] =
428 xstrdup(path);
429}
430
431void
Darren Tucker19104782013-04-05 11:13:08 +1100432add_identity_file(Options *options, const char *dir, const char *filename,
433 int userprovided)
434{
435 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000436 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100437
438 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
439 fatal("Too many identity files specified (max %d)",
440 SSH_MAX_IDENTITY_FILES);
441
442 if (dir == NULL) /* no dir, filename is absolute */
443 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000444 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
445 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100446
Damien Miller15271902014-05-15 13:47:56 +1000447 /* Avoid registering duplicates */
448 for (i = 0; i < options->num_identity_files; i++) {
449 if (options->identity_file_userprovided[i] == userprovided &&
450 strcmp(options->identity_files[i], path) == 0) {
451 debug2("%s: ignoring duplicate key %s", __func__, path);
452 free(path);
453 return;
454 }
455 }
456
Darren Tucker19104782013-04-05 11:13:08 +1100457 options->identity_file_userprovided[options->num_identity_files] =
458 userprovided;
459 options->identity_files[options->num_identity_files++] = path;
460}
461
Damien Miller194fd902013-10-15 12:13:05 +1100462int
463default_ssh_port(void)
464{
465 static int port;
466 struct servent *sp;
467
468 if (port == 0) {
469 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
470 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
471 }
472 return port;
473}
474
475/*
476 * Execute a command in a shell.
477 * Return its exit status or -1 on abnormal exit.
478 */
479static int
480execute_in_shell(const char *cmd)
481{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000482 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100483 pid_t pid;
484 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100485
486 if ((shell = getenv("SHELL")) == NULL)
487 shell = _PATH_BSHELL;
488
Damien Miller194fd902013-10-15 12:13:05 +1100489 /* Need this to redirect subprocess stdin/out */
490 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
491 fatal("open(/dev/null): %s", strerror(errno));
492
493 debug("Executing command: '%.500s'", cmd);
494
495 /* Fork and execute the command. */
496 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000497 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100498
Damien Miller194fd902013-10-15 12:13:05 +1100499 /* Redirect child stdin and stdout. Leave stderr */
500 if (dup2(devnull, STDIN_FILENO) == -1)
501 fatal("dup2: %s", strerror(errno));
502 if (dup2(devnull, STDOUT_FILENO) == -1)
503 fatal("dup2: %s", strerror(errno));
504 if (devnull > STDERR_FILENO)
505 close(devnull);
506 closefrom(STDERR_FILENO + 1);
507
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000508 argv[0] = shell;
509 argv[1] = "-c";
510 argv[2] = xstrdup(cmd);
511 argv[3] = NULL;
512
Damien Miller194fd902013-10-15 12:13:05 +1100513 execv(argv[0], argv);
514 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
515 /* Die with signal to make this error apparent to parent. */
516 signal(SIGTERM, SIG_DFL);
517 kill(getpid(), SIGTERM);
518 _exit(1);
519 }
520 /* Parent. */
521 if (pid < 0)
522 fatal("%s: fork: %.100s", __func__, strerror(errno));
523
524 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100525
526 while (waitpid(pid, &status, 0) == -1) {
527 if (errno != EINTR && errno != EAGAIN)
528 fatal("%s: waitpid: %s", __func__, strerror(errno));
529 }
530 if (!WIFEXITED(status)) {
531 error("command '%.100s' exited abnormally", cmd);
532 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000533 }
Damien Miller194fd902013-10-15 12:13:05 +1100534 debug3("command returned status %d", WEXITSTATUS(status));
535 return WEXITSTATUS(status);
536}
537
538/*
539 * Parse and execute a Match directive.
540 */
541static int
542match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000543 const char *host_arg, const char *original_host, int final_pass,
544 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100545{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000546 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100547 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100549 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000550 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100551
552 /*
553 * Configuration is likely to be incomplete at this point so we
554 * must be prepared to use default values.
555 */
556 port = options->port <= 0 ? default_ssh_port() : options->port;
557 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000558 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000559 host = xstrdup(options->hostname);
560 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100561 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100562 host = percent_expand(options->hostname,
563 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000564 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100565 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000566 }
Damien Miller194fd902013-10-15 12:13:05 +1100567
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000568 debug2("checking match for '%s' host %s originally %s",
569 cp, host, original_host);
570 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
571 criteria = NULL;
572 this_result = 1;
573 if ((negate = attrib[0] == '!'))
574 attrib++;
575 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100576 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000577 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100578 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 error("%.200s line %d: '%s' cannot be combined "
580 "with other Match attributes",
581 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100582 result = -1;
583 goto out;
584 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000585 if (result)
586 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100587 goto out;
588 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000589 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000590 if (strcasecmp(attrib, "canonical") == 0 ||
591 strcasecmp(attrib, "final") == 0) {
592 /*
593 * If the config requests "Match final" then remember
594 * this so we can perform a second pass later.
595 */
596 if (strcasecmp(attrib, "final") == 0 &&
597 want_final_pass != NULL)
598 *want_final_pass = 1;
599 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000600 if (r == (negate ? 1 : 0))
601 this_result = result = 0;
602 debug3("%.200s line %d: %smatched '%s'",
603 filename, linenum,
604 this_result ? "" : "not ", oattrib);
605 continue;
606 }
607 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100608 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
609 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100610 result = -1;
611 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100612 }
Damien Miller194fd902013-10-15 12:13:05 +1100613 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000614 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000615 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 if (r == (negate ? 1 : 0))
617 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100618 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000620 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 if (r == (negate ? 1 : 0))
622 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100623 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000624 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000625 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000626 if (r == (negate ? 1 : 0))
627 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100628 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000630 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000631 if (r == (negate ? 1 : 0))
632 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100633 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100634 if (gethostname(thishost, sizeof(thishost)) == -1)
635 fatal("gethostname: %s", strerror(errno));
636 strlcpy(shorthost, thishost, sizeof(shorthost));
637 shorthost[strcspn(thishost, ".")] = '\0';
638 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000639 snprintf(uidstr, sizeof(uidstr), "%llu",
640 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100641
642 cmd = percent_expand(arg,
643 "L", shorthost,
644 "d", pw->pw_dir,
645 "h", host,
646 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000647 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100648 "p", portstr,
649 "r", ruser,
650 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000651 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100652 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100653 if (result != 1) {
654 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000655 debug3("%.200s line %d: skipped exec "
656 "\"%.100s\"", filename, linenum, cmd);
657 free(cmd);
658 continue;
Damien Miller06287802014-02-24 15:56:45 +1100659 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000660 r = execute_in_shell(cmd);
661 if (r == -1) {
662 fatal("%.200s line %d: match exec "
663 "'%.100s' error", filename,
664 linenum, cmd);
665 }
666 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100667 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000668 /* Force exit status to boolean */
669 r = r == 0;
670 if (r == (negate ? 1 : 0))
671 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100672 } else {
673 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100674 result = -1;
675 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100676 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000677 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
678 filename, linenum, this_result ? "": "not ",
679 oattrib, criteria);
680 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100681 }
Damien Millercf31f382013-10-24 21:02:56 +1100682 if (attributes == 0) {
683 error("One or more attributes required for Match");
684 result = -1;
685 goto out;
686 }
Damien Miller084bcd22013-10-23 16:30:51 +1100687 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000688 if (result != -1)
689 debug2("match %sfound", result ? "" : "not ");
690 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100691 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100692 return result;
693}
694
djm@openbsd.org555294a2018-04-06 13:02:39 +0000695/* Remove environment variable by pattern */
696static void
697rm_env(Options *options, const char *arg, const char *filename, int linenum)
698{
699 int i, j;
700 char *cp;
701
702 /* Remove an environment variable */
703 for (i = 0; i < options->num_send_env; ) {
704 cp = xstrdup(options->send_env[i]);
705 if (!match_pattern(cp, arg + 1)) {
706 free(cp);
707 i++;
708 continue;
709 }
710 debug3("%s line %d: removing environment %s",
711 filename, linenum, cp);
712 free(cp);
713 free(options->send_env[i]);
714 options->send_env[i] = NULL;
715 for (j = i; j < options->num_send_env - 1; j++) {
716 options->send_env[j] = options->send_env[j + 1];
717 options->send_env[j + 1] = NULL;
718 }
719 options->num_send_env--;
720 /* NB. don't increment i */
721 }
722}
723
Damien Miller5428f641999-11-25 11:54:57 +1100724/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000725 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100726 */
Damien Miller4af51302000-04-16 11:18:38 +1000727static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000728parse_token(const char *cp, const char *filename, int linenum,
729 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730{
Darren Tucker07636982013-05-16 20:30:03 +1000731 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732
Damien Miller95def091999-11-25 00:26:21 +1100733 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000734 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100735 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000736 if (ignored_unknown != NULL &&
737 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000738 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000739 error("%s: line %d: Bad configuration option: %s",
740 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100741 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742}
743
Damien Millere9fc72e2013-10-15 12:14:12 +1100744/* Multistate option parsing */
745struct multistate {
746 char *key;
747 int value;
748};
749static const struct multistate multistate_flag[] = {
750 { "true", 1 },
751 { "false", 0 },
752 { "yes", 1 },
753 { "no", 0 },
754 { NULL, -1 }
755};
756static const struct multistate multistate_yesnoask[] = {
757 { "true", 1 },
758 { "false", 0 },
759 { "yes", 1 },
760 { "no", 0 },
761 { "ask", 2 },
762 { NULL, -1 }
763};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000764static const struct multistate multistate_strict_hostkey[] = {
765 { "true", SSH_STRICT_HOSTKEY_YES },
766 { "false", SSH_STRICT_HOSTKEY_OFF },
767 { "yes", SSH_STRICT_HOSTKEY_YES },
768 { "no", SSH_STRICT_HOSTKEY_OFF },
769 { "ask", SSH_STRICT_HOSTKEY_ASK },
770 { "off", SSH_STRICT_HOSTKEY_OFF },
771 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
772 { NULL, -1 }
773};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000774static const struct multistate multistate_yesnoaskconfirm[] = {
775 { "true", 1 },
776 { "false", 0 },
777 { "yes", 1 },
778 { "no", 0 },
779 { "ask", 2 },
780 { "confirm", 3 },
781 { NULL, -1 }
782};
Damien Millere9fc72e2013-10-15 12:14:12 +1100783static const struct multistate multistate_addressfamily[] = {
784 { "inet", AF_INET },
785 { "inet6", AF_INET6 },
786 { "any", AF_UNSPEC },
787 { NULL, -1 }
788};
789static const struct multistate multistate_controlmaster[] = {
790 { "true", SSHCTL_MASTER_YES },
791 { "yes", SSHCTL_MASTER_YES },
792 { "false", SSHCTL_MASTER_NO },
793 { "no", SSHCTL_MASTER_NO },
794 { "auto", SSHCTL_MASTER_AUTO },
795 { "ask", SSHCTL_MASTER_ASK },
796 { "autoask", SSHCTL_MASTER_AUTO_ASK },
797 { NULL, -1 }
798};
799static const struct multistate multistate_tunnel[] = {
800 { "ethernet", SSH_TUNMODE_ETHERNET },
801 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
802 { "true", SSH_TUNMODE_DEFAULT },
803 { "yes", SSH_TUNMODE_DEFAULT },
804 { "false", SSH_TUNMODE_NO },
805 { "no", SSH_TUNMODE_NO },
806 { NULL, -1 }
807};
808static const struct multistate multistate_requesttty[] = {
809 { "true", REQUEST_TTY_YES },
810 { "yes", REQUEST_TTY_YES },
811 { "false", REQUEST_TTY_NO },
812 { "no", REQUEST_TTY_NO },
813 { "force", REQUEST_TTY_FORCE },
814 { "auto", REQUEST_TTY_AUTO },
815 { NULL, -1 }
816};
Damien Miller38505592013-10-17 11:48:13 +1100817static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100818 { "true", SSH_CANONICALISE_YES },
819 { "false", SSH_CANONICALISE_NO },
820 { "yes", SSH_CANONICALISE_YES },
821 { "no", SSH_CANONICALISE_NO },
822 { "always", SSH_CANONICALISE_ALWAYS },
823 { NULL, -1 }
824};
Damien Millere9fc72e2013-10-15 12:14:12 +1100825
Damien Miller5428f641999-11-25 11:54:57 +1100826/*
827 * Processes a single option line as used in the configuration files. This
828 * only sets those values that have not already been set.
829 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100830int
Damien Miller194fd902013-10-15 12:13:05 +1100831process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000832 const char *original_host, char *line, const char *filename,
833 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000835 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000836 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000837}
838
839#define WHITESPACE " \t\r\n"
840static int
841process_config_line_depth(Options *options, struct passwd *pw, const char *host,
842 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000843 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000844{
Damien Miller295ee632011-05-29 21:42:31 +1000845 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
846 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000847 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000848 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000849 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100850 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000851 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000852 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100853 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000854 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100855 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100856 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000857 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000858 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859
Damien Miller194fd902013-10-15 12:13:05 +1100860 if (activep == NULL) { /* We are processing a command line directive */
861 cmdline = 1;
862 activep = &cmdline;
863 }
864
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000865 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000866 if ((len = strlen(line)) == 0)
867 return 0;
868 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000869 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000870 break;
871 line[len] = '\0';
872 }
873
Damien Millerbe484b52000-07-15 14:14:16 +1000874 s = line;
875 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100876 if ((keyword = strdelim(&s)) == NULL)
877 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000878 /* Ignore leading whitespace. */
879 if (*keyword == '\0')
880 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000881 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100882 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000883 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100884 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Darren Tucker07636982013-05-16 20:30:03 +1000886 opcode = parse_token(keyword, filename, linenum,
887 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
Damien Miller95def091999-11-25 00:26:21 +1100889 switch (opcode) {
890 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100891 /* don't panic, but count bad options */
892 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000893 case oIgnore:
894 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000895 case oIgnoredUnknownOption:
896 debug("%s line %d: Ignored unknown option \"%s\"",
897 filename, linenum, keyword);
898 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000899 case oConnectTimeout:
900 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100901parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000902 arg = strdelim(&s);
903 if (!arg || *arg == '\0')
904 fatal("%s line %d: missing time value.",
905 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000906 if (strcmp(arg, "none") == 0)
907 value = -1;
908 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000909 fatal("%s line %d: invalid time value.",
910 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100911 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000912 *intptr = value;
913 break;
914
Damien Miller95def091999-11-25 00:26:21 +1100915 case oForwardAgent:
916 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100917 parse_flag:
918 multistate_ptr = multistate_flag;
919 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000920 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000921 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100922 fatal("%s line %d: missing argument.",
923 filename, linenum);
924 value = -1;
925 for (i = 0; multistate_ptr[i].key != NULL; i++) {
926 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
927 value = multistate_ptr[i].value;
928 break;
929 }
930 }
931 if (value == -1)
932 fatal("%s line %d: unsupported option \"%s\".",
933 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100934 if (*activep && *intptr == -1)
935 *intptr = value;
936 break;
937
938 case oForwardX11:
939 intptr = &options->forward_x11;
940 goto parse_flag;
941
Darren Tucker0a118da2003-10-15 15:54:32 +1000942 case oForwardX11Trusted:
943 intptr = &options->forward_x11_trusted;
944 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000945
Damien Miller1ab6a512010-06-26 10:02:24 +1000946 case oForwardX11Timeout:
947 intptr = &options->forward_x11_timeout;
948 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000949
Damien Miller95def091999-11-25 00:26:21 +1100950 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000951 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100952 goto parse_flag;
953
Darren Tuckere7d4b192006-07-12 22:17:10 +1000954 case oExitOnForwardFailure:
955 intptr = &options->exit_on_forward_failure;
956 goto parse_flag;
957
Damien Miller95def091999-11-25 00:26:21 +1100958 case oPasswordAuthentication:
959 intptr = &options->password_authentication;
960 goto parse_flag;
961
Damien Miller874d77b2000-10-14 16:23:11 +1100962 case oKbdInteractiveAuthentication:
963 intptr = &options->kbd_interactive_authentication;
964 goto parse_flag;
965
966 case oKbdInteractiveDevices:
967 charptr = &options->kbd_interactive_devices;
968 goto parse_string;
969
Damien Miller0bc1bd82000-11-13 22:57:25 +1100970 case oPubkeyAuthentication:
971 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000972 goto parse_flag;
973
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000974 case oHostbasedAuthentication:
975 intptr = &options->hostbased_authentication;
976 goto parse_flag;
977
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000978 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000979 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100980 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000981
Darren Tucker0efd1552003-08-26 11:49:55 +1000982 case oGssAuthentication:
983 intptr = &options->gss_authentication;
984 goto parse_flag;
985
986 case oGssDelegateCreds:
987 intptr = &options->gss_deleg_creds;
988 goto parse_flag;
989
Damien Miller95def091999-11-25 00:26:21 +1100990 case oBatchMode:
991 intptr = &options->batch_mode;
992 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 case oCheckHostIP:
995 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000996 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller37876e92003-05-15 10:19:46 +1000998 case oVerifyHostKeyDNS:
999 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001000 multistate_ptr = multistate_yesnoask;
1001 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 case oStrictHostKeyChecking:
1004 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001005 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001006 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007
Damien Miller95def091999-11-25 00:26:21 +11001008 case oCompression:
1009 intptr = &options->compression;
1010 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
Damien Miller12c150e2003-12-17 16:31:10 +11001012 case oTCPKeepAlive:
1013 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001014 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001016 case oNoHostAuthenticationForLocalhost:
1017 intptr = &options->no_host_authentication_for_localhost;
1018 goto parse_flag;
1019
Damien Miller95def091999-11-25 00:26:21 +11001020 case oNumberOfPasswordPrompts:
1021 intptr = &options->number_of_password_prompts;
1022 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Millera5539d22003-04-09 20:50:06 +10001024 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001025 arg = strdelim(&s);
1026 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001027 fatal("%.200s line %d: Missing argument.", filename,
1028 linenum);
1029 if (strcmp(arg, "default") == 0) {
1030 val64 = 0;
1031 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001032 if (scan_scaled(arg, &val64) == -1)
1033 fatal("%.200s line %d: Bad number '%s': %s",
1034 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001035 if (val64 != 0 && val64 < 16)
1036 fatal("%.200s line %d: RekeyLimit too small",
1037 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001038 }
Damien Miller3dff1762008-02-10 22:25:52 +11001039 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001040 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001041 if (s != NULL) { /* optional rekey interval present */
1042 if (strcmp(s, "none") == 0) {
1043 (void)strdelim(&s); /* discard */
1044 break;
1045 }
1046 intptr = &options->rekey_interval;
1047 goto parse_time;
1048 }
Damien Millera5539d22003-04-09 20:50:06 +10001049 break;
1050
Damien Miller95def091999-11-25 00:26:21 +11001051 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001052 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001053 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001054 fatal("%.200s line %d: Missing argument.", filename, linenum);
1055 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001056 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001057 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001058 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001059 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001060 add_identity_file(options, NULL,
1061 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001062 }
1063 break;
1064
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001065 case oCertificateFile:
1066 arg = strdelim(&s);
1067 if (!arg || *arg == '\0')
1068 fatal("%.200s line %d: Missing argument.",
1069 filename, linenum);
1070 if (*activep) {
1071 intptr = &options->num_certificate_files;
1072 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1073 fatal("%.200s line %d: Too many certificate "
1074 "files specified (max %d).",
1075 filename, linenum,
1076 SSH_MAX_CERTIFICATE_FILES);
1077 }
1078 add_certificate_file(options, arg,
1079 flags & SSHCONF_USERCONF);
1080 }
1081 break;
1082
Damien Millerd3a18572000-06-07 19:55:44 +10001083 case oXAuthLocation:
1084 charptr=&options->xauth_location;
1085 goto parse_string;
1086
Damien Miller95def091999-11-25 00:26:21 +11001087 case oUser:
1088 charptr = &options->user;
1089parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001090 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001091 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001092 fatal("%.200s line %d: Missing argument.",
1093 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001094 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001095 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001096 break;
1097
1098 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001099 cpptr = (char **)&options->system_hostfiles;
1100 uintptr = &options->num_system_hostfiles;
1101 max_entries = SSH_MAX_HOSTS_FILES;
1102parse_char_array:
1103 if (*activep && *uintptr == 0) {
1104 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1105 if ((*uintptr) >= max_entries)
1106 fatal("%s line %d: "
1107 "too many authorized keys files.",
1108 filename, linenum);
1109 cpptr[(*uintptr)++] = xstrdup(arg);
1110 }
1111 }
1112 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001113
1114 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001115 cpptr = (char **)&options->user_hostfiles;
1116 uintptr = &options->num_user_hostfiles;
1117 max_entries = SSH_MAX_HOSTS_FILES;
1118 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001119
Damien Miller95def091999-11-25 00:26:21 +11001120 case oHostName:
1121 charptr = &options->hostname;
1122 goto parse_string;
1123
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001124 case oHostKeyAlias:
1125 charptr = &options->host_key_alias;
1126 goto parse_string;
1127
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001128 case oPreferredAuthentications:
1129 charptr = &options->preferred_authentications;
1130 goto parse_string;
1131
Ben Lindstrome0f88042001-04-30 13:06:24 +00001132 case oBindAddress:
1133 charptr = &options->bind_address;
1134 goto parse_string;
1135
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001136 case oBindInterface:
1137 charptr = &options->bind_interface;
1138 goto parse_string;
1139
Damien Miller7ea845e2010-02-12 09:21:02 +11001140 case oPKCS11Provider:
1141 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001142 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001143
Damien Miller95def091999-11-25 00:26:21 +11001144 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001145 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001146 /* Ignore ProxyCommand if ProxyJump already specified */
1147 if (options->jump_host != NULL)
1148 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001149parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001150 if (s == NULL)
1151 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001152 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001153 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001154 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001155 return 0;
1156
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001157 case oProxyJump:
1158 if (s == NULL) {
1159 fatal("%.200s line %d: Missing argument.",
1160 filename, linenum);
1161 }
1162 len = strspn(s, WHITESPACE "=");
1163 if (parse_jump(s + len, options, *activep) == -1) {
1164 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1165 filename, linenum, s + len);
1166 }
1167 return 0;
1168
Damien Miller95def091999-11-25 00:26:21 +11001169 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001170 arg = strdelim(&s);
1171 if (!arg || *arg == '\0')
1172 fatal("%.200s line %d: Missing argument.",
1173 filename, linenum);
1174 value = a2port(arg);
1175 if (value <= 0)
1176 fatal("%.200s line %d: Bad port '%s'.",
1177 filename, linenum, arg);
1178 if (*activep && options->port == -1)
1179 options->port = value;
1180 break;
1181
1182 case oConnectionAttempts:
1183 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001184parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001185 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001186 if ((errstr = atoi_err(arg, &value)) != NULL)
1187 fatal("%s line %d: integer value %s.",
1188 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001189 if (*activep && *intptr == -1)
1190 *intptr = value;
1191 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
Damien Miller78928792000-04-12 20:17:38 +10001193 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001194 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001195 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001196 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001197 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001198 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001199 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001200 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001201 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001202 break;
1203
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001204 case oMacs:
1205 arg = strdelim(&s);
1206 if (!arg || *arg == '\0')
1207 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001208 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001209 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001210 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001211 if (*activep && options->macs == NULL)
1212 options->macs = xstrdup(arg);
1213 break;
1214
Damien Millerd5f62bf2010-09-24 22:11:14 +10001215 case oKexAlgorithms:
1216 arg = strdelim(&s);
1217 if (!arg || *arg == '\0')
1218 fatal("%.200s line %d: Missing argument.",
1219 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001220 if (*arg != '-' &&
1221 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001222 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1223 filename, linenum, arg ? arg : "<NONE>");
1224 if (*activep && options->kex_algorithms == NULL)
1225 options->kex_algorithms = xstrdup(arg);
1226 break;
1227
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001228 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001229 charptr = &options->hostkeyalgorithms;
1230parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001231 arg = strdelim(&s);
1232 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001233 fatal("%.200s line %d: Missing argument.",
1234 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001235 if (*arg != '-' &&
1236 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001237 fatal("%s line %d: Bad key types '%s'.",
1238 filename, linenum, arg ? arg : "<NONE>");
1239 if (*activep && *charptr == NULL)
1240 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001241 break;
1242
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001243 case oCASignatureAlgorithms:
1244 charptr = &options->ca_sign_algorithms;
1245 goto parse_keytypes;
1246
Damien Miller95def091999-11-25 00:26:21 +11001247 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001248 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001249 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001250 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001251 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001252 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001253 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001254 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1255 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001256 break;
1257
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001258 case oLogFacility:
1259 log_facility_ptr = &options->log_facility;
1260 arg = strdelim(&s);
1261 value = log_facility_number(arg);
1262 if (value == SYSLOG_FACILITY_NOT_SET)
1263 fatal("%.200s line %d: unsupported log facility '%s'",
1264 filename, linenum, arg ? arg : "<NONE>");
1265 if (*log_facility_ptr == -1)
1266 *log_facility_ptr = (SyslogFacility) value;
1267 break;
1268
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001269 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001270 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001271 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001272 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001273 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001274 fatal("%.200s line %d: Missing port argument.",
1275 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001276
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001277 remotefwd = (opcode == oRemoteForward);
1278 dynamicfwd = (opcode == oDynamicForward);
1279
1280 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001281 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001282 if (arg2 == NULL || *arg2 == '\0') {
1283 if (remotefwd)
1284 dynamicfwd = 1;
1285 else
1286 fatal("%.200s line %d: Missing target "
1287 "argument.", filename, linenum);
1288 } else {
1289 /* construct a string for parse_forward */
1290 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1291 arg2);
1292 }
Damien Millera699d952008-11-03 19:27:34 +11001293 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001294 if (dynamicfwd)
1295 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001296
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001297 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001298 fatal("%.200s line %d: Bad forwarding specification.",
1299 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001300
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001301 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001302 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001303 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001304 } else {
1305 add_local_forward(options, &fwd);
1306 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001307 }
Damien Miller95def091999-11-25 00:26:21 +11001308 break;
1309
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001310 case oClearAllForwardings:
1311 intptr = &options->clear_forwardings;
1312 goto parse_flag;
1313
Damien Miller95def091999-11-25 00:26:21 +11001314 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001315 if (cmdline)
1316 fatal("Host directive not supported as a command-line "
1317 "option");
Damien Miller95def091999-11-25 00:26:21 +11001318 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001319 arg2 = NULL;
1320 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001321 if ((flags & SSHCONF_NEVERMATCH) != 0)
1322 break;
Damien Millerfe924212011-05-15 08:44:45 +10001323 negated = *arg == '!';
1324 if (negated)
1325 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001326 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001327 if (negated) {
1328 debug("%.200s line %d: Skipping Host "
1329 "block because of negated match "
1330 "for %.100s", filename, linenum,
1331 arg);
1332 *activep = 0;
1333 break;
1334 }
1335 if (!*activep)
1336 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001337 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001338 }
Damien Millerfe924212011-05-15 08:44:45 +10001339 }
1340 if (*activep)
1341 debug("%.200s line %d: Applying options for %.100s",
1342 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001343 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001344 return 0;
1345
Damien Miller194fd902013-10-15 12:13:05 +11001346 case oMatch:
1347 if (cmdline)
1348 fatal("Host directive not supported as a command-line "
1349 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001350 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001351 flags & SSHCONF_FINAL, want_final_pass,
1352 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001353 if (value < 0)
1354 fatal("%.200s line %d: Bad Match condition", filename,
1355 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001356 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001357 break;
1358
Damien Miller95def091999-11-25 00:26:21 +11001359 case oEscapeChar:
1360 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001361 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001362 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001363 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001364 if (strcmp(arg, "none") == 0)
1365 value = SSH_ESCAPECHAR_NONE;
1366 else if (arg[1] == '\0')
1367 value = (u_char) arg[0];
1368 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001369 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1370 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001371 else {
1372 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001373 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001374 /* NOTREACHED */
1375 value = 0; /* Avoid compiler warning. */
1376 }
1377 if (*activep && *intptr == -1)
1378 *intptr = value;
1379 break;
1380
Damien Miller20a8f972003-05-18 20:50:30 +10001381 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001382 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001383 multistate_ptr = multistate_addressfamily;
1384 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001385
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001386 case oEnableSSHKeysign:
1387 intptr = &options->enable_ssh_keysign;
1388 goto parse_flag;
1389
Damien Millerbd394c32004-03-08 23:12:36 +11001390 case oIdentitiesOnly:
1391 intptr = &options->identities_only;
1392 goto parse_flag;
1393
Damien Miller509b0102003-12-17 16:33:10 +11001394 case oServerAliveInterval:
1395 intptr = &options->server_alive_interval;
1396 goto parse_time;
1397
1398 case oServerAliveCountMax:
1399 intptr = &options->server_alive_count_max;
1400 goto parse_int;
1401
Darren Tucker46bc0752004-05-02 22:11:30 +10001402 case oSendEnv:
1403 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1404 if (strchr(arg, '=') != NULL)
1405 fatal("%s line %d: Invalid environment name.",
1406 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001407 if (!*activep)
1408 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001409 if (*arg == '-') {
1410 /* Removing an env var */
1411 rm_env(options, arg, filename, linenum);
1412 continue;
1413 } else {
1414 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001415 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001416 fatal("%s line %d: too many send env.",
1417 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001418 options->send_env = xrecallocarray(
1419 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001420 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001421 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001422 options->send_env[options->num_send_env++] =
1423 xstrdup(arg);
1424 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001425 }
1426 break;
1427
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001428 case oSetEnv:
1429 value = options->num_setenv;
1430 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1431 if (strchr(arg, '=') == NULL)
1432 fatal("%s line %d: Invalid SetEnv.",
1433 filename, linenum);
1434 if (!*activep || value != 0)
1435 continue;
1436 /* Adding a setenv var */
1437 if (options->num_setenv >= INT_MAX)
1438 fatal("%s line %d: too many SetEnv.",
1439 filename, linenum);
1440 options->setenv = xrecallocarray(
1441 options->setenv, options->num_setenv,
1442 options->num_setenv + 1, sizeof(*options->setenv));
1443 options->setenv[options->num_setenv++] = xstrdup(arg);
1444 }
1445 break;
1446
Damien Miller0e220db2004-06-15 10:34:08 +10001447 case oControlPath:
1448 charptr = &options->control_path;
1449 goto parse_string;
1450
1451 case oControlMaster:
1452 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001453 multistate_ptr = multistate_controlmaster;
1454 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001455
Damien Millere11e1ea2010-08-03 16:04:46 +10001456 case oControlPersist:
1457 /* no/false/yes/true, or a time spec */
1458 intptr = &options->control_persist;
1459 arg = strdelim(&s);
1460 if (!arg || *arg == '\0')
1461 fatal("%.200s line %d: Missing ControlPersist"
1462 " argument.", filename, linenum);
1463 value = 0;
1464 value2 = 0; /* timeout */
1465 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1466 value = 0;
1467 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1468 value = 1;
1469 else if ((value2 = convtime(arg)) >= 0)
1470 value = 1;
1471 else
1472 fatal("%.200s line %d: Bad ControlPersist argument.",
1473 filename, linenum);
1474 if (*activep && *intptr == -1) {
1475 *intptr = value;
1476 options->control_persist_timeout = value2;
1477 }
1478 break;
1479
Damien Millere1776152005-03-01 21:47:37 +11001480 case oHashKnownHosts:
1481 intptr = &options->hash_known_hosts;
1482 goto parse_flag;
1483
Damien Millerd27b9472005-12-13 19:29:02 +11001484 case oTunnel:
1485 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001486 multistate_ptr = multistate_tunnel;
1487 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001488
1489 case oTunnelDevice:
1490 arg = strdelim(&s);
1491 if (!arg || *arg == '\0')
1492 fatal("%.200s line %d: Missing argument.", filename, linenum);
1493 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001494 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001495 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1496 if (*activep) {
1497 options->tun_local = value;
1498 options->tun_remote = value2;
1499 }
1500 break;
1501
1502 case oLocalCommand:
1503 charptr = &options->local_command;
1504 goto parse_command;
1505
1506 case oPermitLocalCommand:
1507 intptr = &options->permit_local_command;
1508 goto parse_flag;
1509
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001510 case oRemoteCommand:
1511 charptr = &options->remote_command;
1512 goto parse_command;
1513
Damien Miller10288242008-06-30 00:04:03 +10001514 case oVisualHostKey:
1515 intptr = &options->visual_host_key;
1516 goto parse_flag;
1517
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001518 case oInclude:
1519 if (cmdline)
1520 fatal("Include directive not supported as a "
1521 "command-line option");
1522 value = 0;
1523 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1524 /*
1525 * Ensure all paths are anchored. User configuration
1526 * files may begin with '~/' but system configurations
1527 * must not. If the path is relative, then treat it
1528 * as living in ~/.ssh for user configurations or
1529 * /etc/ssh for system ones.
1530 */
1531 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1532 fatal("%.200s line %d: bad include path %s.",
1533 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001534 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001535 xasprintf(&arg2, "%s/%s",
1536 (flags & SSHCONF_USERCONF) ?
1537 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1538 } else
1539 arg2 = xstrdup(arg);
1540 memset(&gl, 0, sizeof(gl));
1541 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1542 if (r == GLOB_NOMATCH) {
1543 debug("%.200s line %d: include %s matched no "
1544 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001545 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001546 continue;
1547 } else if (r != 0 || gl.gl_pathc < 0)
1548 fatal("%.200s line %d: glob failed for %s.",
1549 filename, linenum, arg2);
1550 free(arg2);
1551 oactive = *activep;
1552 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1553 debug3("%.200s line %d: Including file %s "
1554 "depth %d%s", filename, linenum,
1555 gl.gl_pathv[i], depth,
1556 oactive ? "" : " (parse only)");
1557 r = read_config_file_depth(gl.gl_pathv[i],
1558 pw, host, original_host, options,
1559 flags | SSHCONF_CHECKPERM |
1560 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001561 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001562 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001563 fatal("Can't open user config file "
1564 "%.100s: %.100s", gl.gl_pathv[i],
1565 strerror(errno));
1566 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001567 /*
1568 * don't let Match in includes clobber the
1569 * containing file's Match state.
1570 */
1571 *activep = oactive;
1572 if (r != 1)
1573 value = -1;
1574 }
1575 globfree(&gl);
1576 }
1577 if (value != 0)
1578 return value;
1579 break;
1580
Damien Miller0dac6fb2010-11-20 15:19:38 +11001581 case oIPQoS:
1582 arg = strdelim(&s);
1583 if ((value = parse_ipqos(arg)) == -1)
1584 fatal("%s line %d: Bad IPQoS value: %s",
1585 filename, linenum, arg);
1586 arg = strdelim(&s);
1587 if (arg == NULL)
1588 value2 = value;
1589 else if ((value2 = parse_ipqos(arg)) == -1)
1590 fatal("%s line %d: Bad IPQoS value: %s",
1591 filename, linenum, arg);
1592 if (*activep) {
1593 options->ip_qos_interactive = value;
1594 options->ip_qos_bulk = value2;
1595 }
1596 break;
1597
Damien Miller21771e22011-05-15 08:45:50 +10001598 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001599 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001600 multistate_ptr = multistate_requesttty;
1601 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001602
Darren Tucker07636982013-05-16 20:30:03 +10001603 case oIgnoreUnknown:
1604 charptr = &options->ignored_unknown;
1605 goto parse_string;
1606
Damien Miller1262b662013-08-21 02:44:24 +10001607 case oProxyUseFdpass:
1608 intptr = &options->proxy_use_fdpass;
1609 goto parse_flag;
1610
Damien Miller0faf7472013-10-17 11:47:23 +11001611 case oCanonicalDomains:
1612 value = options->num_canonical_domains != 0;
1613 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001614 if (!valid_domain(arg, 1, &errstr)) {
1615 fatal("%s line %d: %s", filename, linenum,
1616 errstr);
1617 }
Damien Miller0faf7472013-10-17 11:47:23 +11001618 if (!*activep || value)
1619 continue;
1620 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1621 fatal("%s line %d: too many hostname suffixes.",
1622 filename, linenum);
1623 options->canonical_domains[
1624 options->num_canonical_domains++] = xstrdup(arg);
1625 }
1626 break;
1627
Damien Miller38505592013-10-17 11:48:13 +11001628 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001629 value = options->num_permitted_cnames != 0;
1630 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1631 /* Either '*' for everything or 'list:list' */
1632 if (strcmp(arg, "*") == 0)
1633 arg2 = arg;
1634 else {
1635 lowercase(arg);
1636 if ((arg2 = strchr(arg, ':')) == NULL ||
1637 arg2[1] == '\0') {
1638 fatal("%s line %d: "
1639 "Invalid permitted CNAME \"%s\"",
1640 filename, linenum, arg);
1641 }
1642 *arg2 = '\0';
1643 arg2++;
1644 }
1645 if (!*activep || value)
1646 continue;
1647 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1648 fatal("%s line %d: too many permitted CNAMEs.",
1649 filename, linenum);
1650 cname = options->permitted_cnames +
1651 options->num_permitted_cnames++;
1652 cname->source_list = xstrdup(arg);
1653 cname->target_list = xstrdup(arg2);
1654 }
1655 break;
1656
Damien Miller38505592013-10-17 11:48:13 +11001657 case oCanonicalizeHostname:
1658 intptr = &options->canonicalize_hostname;
1659 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001660 goto parse_multistate;
1661
Damien Miller38505592013-10-17 11:48:13 +11001662 case oCanonicalizeMaxDots:
1663 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001664 goto parse_int;
1665
Damien Miller38505592013-10-17 11:48:13 +11001666 case oCanonicalizeFallbackLocal:
1667 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001668 goto parse_flag;
1669
Damien Miller7acefbb2014-07-18 14:11:24 +10001670 case oStreamLocalBindMask:
1671 arg = strdelim(&s);
1672 if (!arg || *arg == '\0')
1673 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1674 /* Parse mode in octal format */
1675 value = strtol(arg, &endofnumber, 8);
1676 if (arg == endofnumber || value < 0 || value > 0777)
1677 fatal("%.200s line %d: Bad mask.", filename, linenum);
1678 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1679 break;
1680
1681 case oStreamLocalBindUnlink:
1682 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1683 goto parse_flag;
1684
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001685 case oRevokedHostKeys:
1686 charptr = &options->revoked_host_keys;
1687 goto parse_string;
1688
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001689 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001690 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001691 arg = strdelim(&s);
1692 if (!arg || *arg == '\0')
1693 fatal("%.200s line %d: Missing argument.",
1694 filename, linenum);
1695 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1696 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1697 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001698 if (*activep && *intptr == -1)
1699 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001700 break;
1701
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001702 case oUpdateHostkeys:
1703 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001704 multistate_ptr = multistate_yesnoask;
1705 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001706
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001707 case oHostbasedKeyTypes:
1708 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001709 goto parse_keytypes;
1710
1711 case oPubkeyAcceptedKeyTypes:
1712 charptr = &options->pubkey_key_types;
1713 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001714
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001715 case oAddKeysToAgent:
1716 intptr = &options->add_keys_to_agent;
1717 multistate_ptr = multistate_yesnoaskconfirm;
1718 goto parse_multistate;
1719
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001720 case oIdentityAgent:
1721 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001722 arg = strdelim(&s);
1723 if (!arg || *arg == '\0')
1724 fatal("%.200s line %d: Missing argument.",
1725 filename, linenum);
1726 /* Extra validation if the string represents an env var. */
1727 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1728 fatal("%.200s line %d: Invalid environment name %s.",
1729 filename, linenum, arg);
1730 }
1731 if (*activep && *charptr == NULL)
1732 *charptr = xstrdup(arg);
1733 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001734
Ben Lindstrom4daea862002-06-09 20:04:02 +00001735 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001736 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001737 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001738 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001739
Damien Millerf9b3feb2003-05-16 11:38:32 +10001740 case oUnsupported:
1741 error("%s line %d: Unsupported option \"%s\"",
1742 filename, linenum, keyword);
1743 return 0;
1744
Damien Miller95def091999-11-25 00:26:21 +11001745 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001746 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001747 }
1748
1749 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001750 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001751 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001752 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001753 }
Damien Miller95def091999-11-25 00:26:21 +11001754 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001755}
1756
Damien Miller5428f641999-11-25 11:54:57 +11001757/*
1758 * Reads the config file and modifies the options accordingly. Options
1759 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001760 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001761 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001762int
Damien Miller194fd902013-10-15 12:13:05 +11001763read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001764 const char *original_host, Options *options, int flags,
1765 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001766{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001767 int active = 1;
1768
1769 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001770 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001771}
1772
1773#define READCONF_MAX_DEPTH 16
1774static int
1775read_config_file_depth(const char *filename, struct passwd *pw,
1776 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001777 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001778{
Damien Miller95def091999-11-25 00:26:21 +11001779 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001780 char *line = NULL;
1781 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001782 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001783 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001784
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001785 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1786 fatal("Too many recursive configuration includes");
1787
Damien Miller57a44762004-04-20 20:11:57 +10001788 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001789 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001790
Darren Tuckeraefa3682013-04-05 11:18:35 +11001791 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001792 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001793
Damien Miller33793852004-06-15 10:27:55 +10001794 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001795 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001796 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001797 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001798 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001799 }
1800
Damien Miller95def091999-11-25 00:26:21 +11001801 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802
Damien Miller5428f641999-11-25 11:54:57 +11001803 /*
1804 * Mark that we are now processing the options. This flag is turned
1805 * on/off by Host specifications.
1806 */
Damien Miller95def091999-11-25 00:26:21 +11001807 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001808 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001809 /* Update line number counter. */
1810 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001811 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001812 line, filename, linenum, activep, flags, want_final_pass,
1813 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001814 bad_options++;
1815 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001816 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001817 fclose(f);
1818 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001819 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001820 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001821 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822}
1823
Damien Miller13f97b22014-02-24 15:57:55 +11001824/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1825int
1826option_clear_or_none(const char *o)
1827{
1828 return o == NULL || strcasecmp(o, "none") == 0;
1829}
1830
Damien Miller5428f641999-11-25 11:54:57 +11001831/*
1832 * Initializes options to special values that indicate that they have not yet
1833 * been set. Read_config_file will only set options with this value. Options
1834 * are processed in the following order: command line, user config file,
1835 * system config file. Last, fill_default_options is called.
1836 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001837
Damien Miller4af51302000-04-16 11:18:38 +10001838void
Damien Miller95def091999-11-25 00:26:21 +11001839initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840{
Damien Miller95def091999-11-25 00:26:21 +11001841 memset(options, 'X', sizeof(*options));
1842 options->forward_agent = -1;
1843 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001844 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001845 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001846 options->stdio_forward_host = NULL;
1847 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001848 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001849 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001850 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001851 options->fwd_opts.gateway_ports = -1;
1852 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1853 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001854 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001855 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001856 options->gss_authentication = -1;
1857 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001858 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001859 options->kbd_interactive_authentication = -1;
1860 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001861 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001862 options->batch_mode = -1;
1863 options->check_host_ip = -1;
1864 options->strict_host_key_checking = -1;
1865 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001866 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001867 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001868 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001869 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001870 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001871 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001872 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001873 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001874 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001875 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001876 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001877 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001878 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001879 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001880 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001881 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001882 options->jump_user = NULL;
1883 options->jump_host = NULL;
1884 options->jump_port = -1;
1885 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001886 options->user = NULL;
1887 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001888 options->num_system_hostfiles = 0;
1889 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001890 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001891 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001892 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001893 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001894 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001895 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001896 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001897 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001898 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001899 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001900 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001901 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001902 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001903 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001904 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001905 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001906 options->server_alive_interval = -1;
1907 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001908 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001909 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001910 options->setenv = NULL;
1911 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001912 options->control_path = NULL;
1913 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001914 options->control_persist = -1;
1915 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001916 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001917 options->tun_open = -1;
1918 options->tun_local = -1;
1919 options->tun_remote = -1;
1920 options->local_command = NULL;
1921 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001922 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001923 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001924 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001925 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001926 options->ip_qos_interactive = -1;
1927 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001928 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001929 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001930 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001931 options->num_canonical_domains = 0;
1932 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001933 options->canonicalize_max_dots = -1;
1934 options->canonicalize_fallback_local = -1;
1935 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001936 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001937 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001938 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001939 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001940 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941}
1942
Damien Miller5428f641999-11-25 11:54:57 +11001943/*
Damien Miller13f97b22014-02-24 15:57:55 +11001944 * A petite version of fill_default_options() that just fills the options
1945 * needed for hostname canonicalization to proceed.
1946 */
1947void
1948fill_default_options_for_canonicalization(Options *options)
1949{
1950 if (options->canonicalize_max_dots == -1)
1951 options->canonicalize_max_dots = 1;
1952 if (options->canonicalize_fallback_local == -1)
1953 options->canonicalize_fallback_local = 1;
1954 if (options->canonicalize_hostname == -1)
1955 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1956}
1957
1958/*
Damien Miller5428f641999-11-25 11:54:57 +11001959 * Called after processing other sources of option data, this fills those
1960 * options for which no value has been specified with their default values.
1961 */
Damien Miller4af51302000-04-16 11:18:38 +10001962void
Damien Miller95def091999-11-25 00:26:21 +11001963fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001964{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001965 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001966 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001967
Damien Miller95def091999-11-25 00:26:21 +11001968 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001969 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001970 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001971 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001972 if (options->forward_x11_trusted == -1)
1973 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001974 if (options->forward_x11_timeout == -1)
1975 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001976 /*
1977 * stdio forwarding (-W) changes the default for these but we defer
1978 * setting the values so they can be overridden.
1979 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001980 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001981 options->exit_on_forward_failure =
1982 options->stdio_forward_host != NULL ? 1 : 0;
1983 if (options->clear_forwardings == -1)
1984 options->clear_forwardings =
1985 options->stdio_forward_host != NULL ? 1 : 0;
1986 if (options->clear_forwardings == 1)
1987 clear_forwardings(options);
1988
Damien Millerd3a18572000-06-07 19:55:44 +10001989 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001990 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001991 if (options->fwd_opts.gateway_ports == -1)
1992 options->fwd_opts.gateway_ports = 0;
1993 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1994 options->fwd_opts.streamlocal_bind_mask = 0177;
1995 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1996 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001997 if (options->pubkey_authentication == -1)
1998 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001999 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002000 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002001 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002002 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002003 if (options->gss_deleg_creds == -1)
2004 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002005 if (options->password_authentication == -1)
2006 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002007 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002008 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002009 if (options->hostbased_authentication == -1)
2010 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002011 if (options->batch_mode == -1)
2012 options->batch_mode = 0;
2013 if (options->check_host_ip == -1)
2014 options->check_host_ip = 1;
2015 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002016 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002017 if (options->compression == -1)
2018 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002019 if (options->tcp_keep_alive == -1)
2020 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002021 if (options->port == -1)
2022 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002023 if (options->address_family == -1)
2024 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002025 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002026 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002027 if (options->number_of_password_prompts == -1)
2028 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002029 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002030 if (options->add_keys_to_agent == -1)
2031 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002032 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002033 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2034 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002035#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002036 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002037#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002038 add_identity_file(options, "~/",
2039 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002040 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002041 }
Damien Miller95def091999-11-25 00:26:21 +11002042 if (options->escape_char == -1)
2043 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002044 if (options->num_system_hostfiles == 0) {
2045 options->system_hostfiles[options->num_system_hostfiles++] =
2046 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2047 options->system_hostfiles[options->num_system_hostfiles++] =
2048 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2049 }
2050 if (options->num_user_hostfiles == 0) {
2051 options->user_hostfiles[options->num_user_hostfiles++] =
2052 xstrdup(_PATH_SSH_USER_HOSTFILE);
2053 options->user_hostfiles[options->num_user_hostfiles++] =
2054 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2055 }
Damien Millerfcd93202002-02-05 12:26:34 +11002056 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002057 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002058 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2059 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002060 if (options->no_host_authentication_for_localhost == - 1)
2061 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002062 if (options->identities_only == -1)
2063 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002064 if (options->enable_ssh_keysign == -1)
2065 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002066 if (options->rekey_limit == -1)
2067 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002068 if (options->rekey_interval == -1)
2069 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002070 if (options->verify_host_key_dns == -1)
2071 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002072 if (options->server_alive_interval == -1)
2073 options->server_alive_interval = 0;
2074 if (options->server_alive_count_max == -1)
2075 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002076 if (options->control_master == -1)
2077 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002078 if (options->control_persist == -1) {
2079 options->control_persist = 0;
2080 options->control_persist_timeout = 0;
2081 }
Damien Millere1776152005-03-01 21:47:37 +11002082 if (options->hash_known_hosts == -1)
2083 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002084 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002085 options->tun_open = SSH_TUNMODE_NO;
2086 if (options->tun_local == -1)
2087 options->tun_local = SSH_TUNID_ANY;
2088 if (options->tun_remote == -1)
2089 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002090 if (options->permit_local_command == -1)
2091 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002092 if (options->visual_host_key == -1)
2093 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002094 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002095 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002096 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002097 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002098 if (options->request_tty == -1)
2099 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002100 if (options->proxy_use_fdpass == -1)
2101 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002102 if (options->canonicalize_max_dots == -1)
2103 options->canonicalize_max_dots = 1;
2104 if (options->canonicalize_fallback_local == -1)
2105 options->canonicalize_fallback_local = 1;
2106 if (options->canonicalize_hostname == -1)
2107 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002108 if (options->fingerprint_hash == -1)
2109 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002110 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002111 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002112
2113 /* Expand KEX name lists */
2114 all_cipher = cipher_alg_list(',', 0);
2115 all_mac = mac_alg_list(',');
2116 all_kex = kex_alg_list(',');
2117 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002118 all_sig = sshkey_alg_list(0, 1, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002119#define ASSEMBLE(what, defaults, all) \
2120 do { \
2121 if ((r = kex_assemble_names(&options->what, \
2122 defaults, all)) != 0) \
2123 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2124 } while (0)
2125 ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
2126 ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
2127 ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
2128 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
2129 ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002130 ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002131#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002132 free(all_cipher);
2133 free(all_mac);
2134 free(all_kex);
2135 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002136 free(all_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002137
Damien Millere9fc72e2013-10-15 12:14:12 +11002138#define CLEAR_ON_NONE(v) \
2139 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002140 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002141 free(v); \
2142 v = NULL; \
2143 } \
2144 } while(0)
2145 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002146 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002147 CLEAR_ON_NONE(options->proxy_command);
2148 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002149 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002150 if (options->jump_host != NULL &&
2151 strcmp(options->jump_host, "none") == 0 &&
2152 options->jump_port == 0 && options->jump_user == NULL) {
2153 free(options->jump_host);
2154 options->jump_host = NULL;
2155 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002156 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002157 /* options->user will be set in the main program if appropriate */
2158 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002159 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002160 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002162
Damien Miller7acefbb2014-07-18 14:11:24 +10002163struct fwdarg {
2164 char *arg;
2165 int ispath;
2166};
2167
2168/*
2169 * parse_fwd_field
2170 * parses the next field in a port forwarding specification.
2171 * sets fwd to the parsed field and advances p past the colon
2172 * or sets it to NULL at end of string.
2173 * returns 0 on success, else non-zero.
2174 */
2175static int
2176parse_fwd_field(char **p, struct fwdarg *fwd)
2177{
2178 char *ep, *cp = *p;
2179 int ispath = 0;
2180
2181 if (*cp == '\0') {
2182 *p = NULL;
2183 return -1; /* end of string */
2184 }
2185
2186 /*
2187 * A field escaped with square brackets is used literally.
2188 * XXX - allow ']' to be escaped via backslash?
2189 */
2190 if (*cp == '[') {
2191 /* find matching ']' */
2192 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2193 if (*ep == '/')
2194 ispath = 1;
2195 }
2196 /* no matching ']' or not at end of field. */
2197 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2198 return -1;
2199 /* NUL terminate the field and advance p past the colon */
2200 *ep++ = '\0';
2201 if (*ep != '\0')
2202 *ep++ = '\0';
2203 fwd->arg = cp + 1;
2204 fwd->ispath = ispath;
2205 *p = ep;
2206 return 0;
2207 }
2208
2209 for (cp = *p; *cp != '\0'; cp++) {
2210 switch (*cp) {
2211 case '\\':
2212 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002213 if (*cp == '\0')
2214 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002215 break;
2216 case '/':
2217 ispath = 1;
2218 break;
2219 case ':':
2220 *cp++ = '\0';
2221 goto done;
2222 }
2223 }
2224done:
2225 fwd->arg = *p;
2226 fwd->ispath = ispath;
2227 *p = cp;
2228 return 0;
2229}
2230
Damien Millerf91ee4c2005-03-01 21:24:33 +11002231/*
2232 * parse_forward
2233 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002234 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002235 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2236 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002237 * dynamicfwd == 1
2238 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002239 * returns number of arguments parsed or zero on error
2240 */
2241int
Damien Miller7acefbb2014-07-18 14:11:24 +10002242parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002243{
Damien Miller7acefbb2014-07-18 14:11:24 +10002244 struct fwdarg fwdargs[4];
2245 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002246 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002247
Damien Miller7acefbb2014-07-18 14:11:24 +10002248 memset(fwd, 0, sizeof(*fwd));
2249 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002250
2251 cp = p = xstrdup(fwdspec);
2252
2253 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002254 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002255 cp++;
2256
Damien Miller7acefbb2014-07-18 14:11:24 +10002257 for (i = 0; i < 4; ++i) {
2258 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002259 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002260 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002261
Damien Millerf4b39532008-11-03 19:28:21 +11002262 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002263 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002264 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002265 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002266
2267 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002268 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002269 if (fwdargs[0].ispath) {
2270 fwd->listen_path = xstrdup(fwdargs[0].arg);
2271 fwd->listen_port = PORT_STREAMLOCAL;
2272 } else {
2273 fwd->listen_host = NULL;
2274 fwd->listen_port = a2port(fwdargs[0].arg);
2275 }
Damien Millera699d952008-11-03 19:27:34 +11002276 fwd->connect_host = xstrdup("socks");
2277 break;
2278
2279 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002280 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2281 fwd->listen_path = xstrdup(fwdargs[0].arg);
2282 fwd->listen_port = PORT_STREAMLOCAL;
2283 fwd->connect_path = xstrdup(fwdargs[1].arg);
2284 fwd->connect_port = PORT_STREAMLOCAL;
2285 } else if (fwdargs[1].ispath) {
2286 fwd->listen_host = NULL;
2287 fwd->listen_port = a2port(fwdargs[0].arg);
2288 fwd->connect_path = xstrdup(fwdargs[1].arg);
2289 fwd->connect_port = PORT_STREAMLOCAL;
2290 } else {
2291 fwd->listen_host = xstrdup(fwdargs[0].arg);
2292 fwd->listen_port = a2port(fwdargs[1].arg);
2293 fwd->connect_host = xstrdup("socks");
2294 }
Damien Millera699d952008-11-03 19:27:34 +11002295 break;
2296
Damien Millerf91ee4c2005-03-01 21:24:33 +11002297 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002298 if (fwdargs[0].ispath) {
2299 fwd->listen_path = xstrdup(fwdargs[0].arg);
2300 fwd->listen_port = PORT_STREAMLOCAL;
2301 fwd->connect_host = xstrdup(fwdargs[1].arg);
2302 fwd->connect_port = a2port(fwdargs[2].arg);
2303 } else if (fwdargs[2].ispath) {
2304 fwd->listen_host = xstrdup(fwdargs[0].arg);
2305 fwd->listen_port = a2port(fwdargs[1].arg);
2306 fwd->connect_path = xstrdup(fwdargs[2].arg);
2307 fwd->connect_port = PORT_STREAMLOCAL;
2308 } else {
2309 fwd->listen_host = NULL;
2310 fwd->listen_port = a2port(fwdargs[0].arg);
2311 fwd->connect_host = xstrdup(fwdargs[1].arg);
2312 fwd->connect_port = a2port(fwdargs[2].arg);
2313 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002314 break;
2315
2316 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002317 fwd->listen_host = xstrdup(fwdargs[0].arg);
2318 fwd->listen_port = a2port(fwdargs[1].arg);
2319 fwd->connect_host = xstrdup(fwdargs[2].arg);
2320 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002321 break;
2322 default:
2323 i = 0; /* failure */
2324 }
2325
Darren Tuckera627d422013-06-02 07:31:17 +10002326 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002327
Damien Millera699d952008-11-03 19:27:34 +11002328 if (dynamicfwd) {
2329 if (!(i == 1 || i == 2))
2330 goto fail_free;
2331 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002332 if (!(i == 3 || i == 4)) {
2333 if (fwd->connect_path == NULL &&
2334 fwd->listen_path == NULL)
2335 goto fail_free;
2336 }
2337 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002338 goto fail_free;
2339 }
2340
Damien Miller7acefbb2014-07-18 14:11:24 +10002341 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2342 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002343 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344 if (fwd->connect_host != NULL &&
2345 strlen(fwd->connect_host) >= NI_MAXHOST)
2346 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002347 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2348 if (fwd->connect_path != NULL &&
2349 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2350 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002351 if (fwd->listen_host != NULL &&
2352 strlen(fwd->listen_host) >= NI_MAXHOST)
2353 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002354 if (fwd->listen_path != NULL &&
2355 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2356 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002357
2358 return (i);
2359
2360 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002361 free(fwd->connect_host);
2362 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002363 free(fwd->connect_path);
2364 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002365 free(fwd->listen_host);
2366 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002367 free(fwd->listen_path);
2368 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002369 return (0);
2370}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002371
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002372int
2373parse_jump(const char *s, Options *o, int active)
2374{
2375 char *orig, *sdup, *cp;
2376 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002377 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002378
2379 active &= o->proxy_command == NULL && o->jump_host == NULL;
2380
2381 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002382 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002383 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002384 if (strcasecmp(s, "none") == 0)
2385 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002386 if ((cp = strrchr(sdup, ',')) == NULL)
2387 cp = sdup; /* last */
2388 else
2389 *cp++ = '\0';
2390
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002391 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002392 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002393 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2394 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002395 goto out;
2396 } else {
2397 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002398 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2399 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002400 goto out;
2401 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002402 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002403 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002404 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002405 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002406 if (strcasecmp(s, "none") == 0) {
2407 o->jump_host = xstrdup("none");
2408 o->jump_port = 0;
2409 } else {
2410 o->jump_user = user;
2411 o->jump_host = host;
2412 o->jump_port = port;
2413 o->proxy_command = xstrdup("none");
2414 user = host = NULL;
2415 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2416 o->jump_extra = xstrdup(s);
2417 o->jump_extra[cp - s] = '\0';
2418 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002419 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002420 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002421 ret = 0;
2422 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002423 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002424 free(user);
2425 free(host);
2426 return ret;
2427}
2428
millert@openbsd.org887669e2017-10-21 23:06:24 +00002429int
2430parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2431{
2432 char *path;
2433 int r;
2434
2435 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2436 if (r == 0 && path != NULL)
2437 r = -1; /* path not allowed */
2438 return r;
2439}
2440
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002441/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2442static const char *
2443fmt_multistate_int(int val, const struct multistate *m)
2444{
2445 u_int i;
2446
2447 for (i = 0; m[i].key != NULL; i++) {
2448 if (m[i].value == val)
2449 return m[i].key;
2450 }
2451 return "UNKNOWN";
2452}
2453
2454static const char *
2455fmt_intarg(OpCodes code, int val)
2456{
2457 if (val == -1)
2458 return "unset";
2459 switch (code) {
2460 case oAddressFamily:
2461 return fmt_multistate_int(val, multistate_addressfamily);
2462 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002463 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002464 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002465 case oStrictHostKeyChecking:
2466 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002467 case oControlMaster:
2468 return fmt_multistate_int(val, multistate_controlmaster);
2469 case oTunnel:
2470 return fmt_multistate_int(val, multistate_tunnel);
2471 case oRequestTTY:
2472 return fmt_multistate_int(val, multistate_requesttty);
2473 case oCanonicalizeHostname:
2474 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002475 case oAddKeysToAgent:
2476 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002477 case oFingerprintHash:
2478 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002479 default:
2480 switch (val) {
2481 case 0:
2482 return "no";
2483 case 1:
2484 return "yes";
2485 default:
2486 return "UNKNOWN";
2487 }
2488 }
2489}
2490
2491static const char *
2492lookup_opcode_name(OpCodes code)
2493{
2494 u_int i;
2495
2496 for (i = 0; keywords[i].name != NULL; i++)
2497 if (keywords[i].opcode == code)
2498 return(keywords[i].name);
2499 return "UNKNOWN";
2500}
2501
2502static void
2503dump_cfg_int(OpCodes code, int val)
2504{
2505 printf("%s %d\n", lookup_opcode_name(code), val);
2506}
2507
2508static void
2509dump_cfg_fmtint(OpCodes code, int val)
2510{
2511 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2512}
2513
2514static void
2515dump_cfg_string(OpCodes code, const char *val)
2516{
2517 if (val == NULL)
2518 return;
2519 printf("%s %s\n", lookup_opcode_name(code), val);
2520}
2521
2522static void
2523dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2524{
2525 u_int i;
2526
2527 for (i = 0; i < count; i++)
2528 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2529}
2530
2531static void
2532dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2533{
2534 u_int i;
2535
2536 printf("%s", lookup_opcode_name(code));
2537 for (i = 0; i < count; i++)
2538 printf(" %s", vals[i]);
2539 printf("\n");
2540}
2541
2542static void
2543dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2544{
2545 const struct Forward *fwd;
2546 u_int i;
2547
2548 /* oDynamicForward */
2549 for (i = 0; i < count; i++) {
2550 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002551 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002552 strcmp(fwd->connect_host, "socks") != 0)
2553 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002554 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002555 strcmp(fwd->connect_host, "socks") == 0)
2556 continue;
2557 printf("%s", lookup_opcode_name(code));
2558 if (fwd->listen_port == PORT_STREAMLOCAL)
2559 printf(" %s", fwd->listen_path);
2560 else if (fwd->listen_host == NULL)
2561 printf(" %d", fwd->listen_port);
2562 else {
2563 printf(" [%s]:%d",
2564 fwd->listen_host, fwd->listen_port);
2565 }
2566 if (code != oDynamicForward) {
2567 if (fwd->connect_port == PORT_STREAMLOCAL)
2568 printf(" %s", fwd->connect_path);
2569 else if (fwd->connect_host == NULL)
2570 printf(" %d", fwd->connect_port);
2571 else {
2572 printf(" [%s]:%d",
2573 fwd->connect_host, fwd->connect_port);
2574 }
2575 }
2576 printf("\n");
2577 }
2578}
2579
2580void
2581dump_client_config(Options *o, const char *host)
2582{
2583 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002584 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002585
djm@openbsd.org60a92472015-08-21 23:53:08 +00002586 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002587 all_key = sshkey_alg_list(0, 0, 1, ',');
2588 if (kex_assemble_names( &o->hostkeyalgorithms,
2589 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002590 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002591 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002592
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002593 /* Most interesting options first: user, host, port */
2594 dump_cfg_string(oUser, o->user);
2595 dump_cfg_string(oHostName, host);
2596 dump_cfg_int(oPort, o->port);
2597
2598 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002599 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002600 dump_cfg_fmtint(oAddressFamily, o->address_family);
2601 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2602 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2603 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2604 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2605 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2606 dump_cfg_fmtint(oCompression, o->compression);
2607 dump_cfg_fmtint(oControlMaster, o->control_master);
2608 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002609 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002610 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002611 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002612 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2613 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2614 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2615 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2616#ifdef GSSAPI
2617 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2618 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2619#endif /* GSSAPI */
2620 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2621 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2622 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2623 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2624 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2625 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2626 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002627 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2628 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2629 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002630 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2631 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2632 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2633 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002634 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2635 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002636 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002637
2638 /* Integer options */
2639 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002640 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2641 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2642 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2643 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2644 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2645
2646 /* String options */
2647 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002648 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002649 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2650 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002651 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002652 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002653 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002654 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002655 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002656 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2657 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002658 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002659 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002660 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002661 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2662 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002663#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002664 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002665#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002666 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002667 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002668 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002669 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002670
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002671 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002672 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2673 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2674 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2675
2676 /* String array options */
2677 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2678 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002679 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002680 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2681 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2682 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002683 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002684
2685 /* Special cases */
2686
2687 /* oConnectTimeout */
2688 if (o->connection_timeout == -1)
2689 printf("connecttimeout none\n");
2690 else
2691 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2692
2693 /* oTunnelDevice */
2694 printf("tunneldevice");
2695 if (o->tun_local == SSH_TUNID_ANY)
2696 printf(" any");
2697 else
2698 printf(" %d", o->tun_local);
2699 if (o->tun_remote == SSH_TUNID_ANY)
2700 printf(":any");
2701 else
2702 printf(":%d", o->tun_remote);
2703 printf("\n");
2704
2705 /* oCanonicalizePermittedCNAMEs */
2706 if ( o->num_permitted_cnames > 0) {
2707 printf("canonicalizePermittedcnames");
2708 for (i = 0; i < o->num_permitted_cnames; i++) {
2709 printf(" %s:%s", o->permitted_cnames[i].source_list,
2710 o->permitted_cnames[i].target_list);
2711 }
2712 printf("\n");
2713 }
2714
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002715 /* oControlPersist */
2716 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2717 dump_cfg_fmtint(oControlPersist, o->control_persist);
2718 else
2719 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2720
2721 /* oEscapeChar */
2722 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2723 printf("escapechar none\n");
2724 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002725 vis(buf, o->escape_char, VIS_WHITE, 0);
2726 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002727 }
2728
2729 /* oIPQoS */
2730 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2731 printf("%s\n", iptos2str(o->ip_qos_bulk));
2732
2733 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002734 printf("rekeylimit %llu %d\n",
2735 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002736
2737 /* oStreamLocalBindMask */
2738 printf("streamlocalbindmask 0%o\n",
2739 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002740
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002741 /* oLogFacility */
2742 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2743
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002744 /* oProxyCommand / oProxyJump */
2745 if (o->jump_host == NULL)
2746 dump_cfg_string(oProxyCommand, o->proxy_command);
2747 else {
2748 /* Check for numeric addresses */
2749 i = strchr(o->jump_host, ':') != NULL ||
2750 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2751 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2752 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002753 /* optional additional jump spec */
2754 o->jump_extra == NULL ? "" : o->jump_extra,
2755 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002756 /* optional user */
2757 o->jump_user == NULL ? "" : o->jump_user,
2758 o->jump_user == NULL ? "" : "@",
2759 /* opening [ if hostname is numeric */
2760 i ? "[" : "",
2761 /* mandatory hostname */
2762 o->jump_host,
2763 /* closing ] if hostname is numeric */
2764 i ? "]" : "",
2765 /* optional port number */
2766 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002767 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002768 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002769}