blob: c143fa2e231bc02dfc1b59252e137078b635868f [file] [log] [blame]
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001/* $OpenBSD: readconf.c,v 1.305 2019/06/07 14:18:48 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
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
djm@openbsd.org381a2612017-01-30 00:38:50 +0000211 { "pkcs11provider", oPKCS11Provider },
djm@openbsd.org95a80582019-03-01 02:08:50 +0000212 { "smartcarddevice", oPKCS11Provider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000213# 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))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +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)
djm@openbsd.org9b611302019-02-23 08:20:43 +00002125 ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
2126 ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
2127 ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002128 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.org95a80582019-03-01 02:08:50 +00002150 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002151 if (options->jump_host != NULL &&
2152 strcmp(options->jump_host, "none") == 0 &&
2153 options->jump_port == 0 && options->jump_user == NULL) {
2154 free(options->jump_host);
2155 options->jump_host = NULL;
2156 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002157 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002158 /* options->user will be set in the main program if appropriate */
2159 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002160 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002161 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002162}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002163
Damien Miller7acefbb2014-07-18 14:11:24 +10002164struct fwdarg {
2165 char *arg;
2166 int ispath;
2167};
2168
2169/*
2170 * parse_fwd_field
2171 * parses the next field in a port forwarding specification.
2172 * sets fwd to the parsed field and advances p past the colon
2173 * or sets it to NULL at end of string.
2174 * returns 0 on success, else non-zero.
2175 */
2176static int
2177parse_fwd_field(char **p, struct fwdarg *fwd)
2178{
2179 char *ep, *cp = *p;
2180 int ispath = 0;
2181
2182 if (*cp == '\0') {
2183 *p = NULL;
2184 return -1; /* end of string */
2185 }
2186
2187 /*
2188 * A field escaped with square brackets is used literally.
2189 * XXX - allow ']' to be escaped via backslash?
2190 */
2191 if (*cp == '[') {
2192 /* find matching ']' */
2193 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2194 if (*ep == '/')
2195 ispath = 1;
2196 }
2197 /* no matching ']' or not at end of field. */
2198 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2199 return -1;
2200 /* NUL terminate the field and advance p past the colon */
2201 *ep++ = '\0';
2202 if (*ep != '\0')
2203 *ep++ = '\0';
2204 fwd->arg = cp + 1;
2205 fwd->ispath = ispath;
2206 *p = ep;
2207 return 0;
2208 }
2209
2210 for (cp = *p; *cp != '\0'; cp++) {
2211 switch (*cp) {
2212 case '\\':
2213 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002214 if (*cp == '\0')
2215 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002216 break;
2217 case '/':
2218 ispath = 1;
2219 break;
2220 case ':':
2221 *cp++ = '\0';
2222 goto done;
2223 }
2224 }
2225done:
2226 fwd->arg = *p;
2227 fwd->ispath = ispath;
2228 *p = cp;
2229 return 0;
2230}
2231
Damien Millerf91ee4c2005-03-01 21:24:33 +11002232/*
2233 * parse_forward
2234 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002235 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002236 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2237 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002238 * dynamicfwd == 1
2239 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002240 * returns number of arguments parsed or zero on error
2241 */
2242int
Damien Miller7acefbb2014-07-18 14:11:24 +10002243parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002244{
Damien Miller7acefbb2014-07-18 14:11:24 +10002245 struct fwdarg fwdargs[4];
2246 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002247 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002248
Damien Miller7acefbb2014-07-18 14:11:24 +10002249 memset(fwd, 0, sizeof(*fwd));
2250 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002251
2252 cp = p = xstrdup(fwdspec);
2253
2254 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002255 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002256 cp++;
2257
Damien Miller7acefbb2014-07-18 14:11:24 +10002258 for (i = 0; i < 4; ++i) {
2259 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002260 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002261 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002262
Damien Millerf4b39532008-11-03 19:28:21 +11002263 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002264 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002265 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002266 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002267
2268 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002269 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002270 if (fwdargs[0].ispath) {
2271 fwd->listen_path = xstrdup(fwdargs[0].arg);
2272 fwd->listen_port = PORT_STREAMLOCAL;
2273 } else {
2274 fwd->listen_host = NULL;
2275 fwd->listen_port = a2port(fwdargs[0].arg);
2276 }
Damien Millera699d952008-11-03 19:27:34 +11002277 fwd->connect_host = xstrdup("socks");
2278 break;
2279
2280 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002281 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2282 fwd->listen_path = xstrdup(fwdargs[0].arg);
2283 fwd->listen_port = PORT_STREAMLOCAL;
2284 fwd->connect_path = xstrdup(fwdargs[1].arg);
2285 fwd->connect_port = PORT_STREAMLOCAL;
2286 } else if (fwdargs[1].ispath) {
2287 fwd->listen_host = NULL;
2288 fwd->listen_port = a2port(fwdargs[0].arg);
2289 fwd->connect_path = xstrdup(fwdargs[1].arg);
2290 fwd->connect_port = PORT_STREAMLOCAL;
2291 } else {
2292 fwd->listen_host = xstrdup(fwdargs[0].arg);
2293 fwd->listen_port = a2port(fwdargs[1].arg);
2294 fwd->connect_host = xstrdup("socks");
2295 }
Damien Millera699d952008-11-03 19:27:34 +11002296 break;
2297
Damien Millerf91ee4c2005-03-01 21:24:33 +11002298 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002299 if (fwdargs[0].ispath) {
2300 fwd->listen_path = xstrdup(fwdargs[0].arg);
2301 fwd->listen_port = PORT_STREAMLOCAL;
2302 fwd->connect_host = xstrdup(fwdargs[1].arg);
2303 fwd->connect_port = a2port(fwdargs[2].arg);
2304 } else if (fwdargs[2].ispath) {
2305 fwd->listen_host = xstrdup(fwdargs[0].arg);
2306 fwd->listen_port = a2port(fwdargs[1].arg);
2307 fwd->connect_path = xstrdup(fwdargs[2].arg);
2308 fwd->connect_port = PORT_STREAMLOCAL;
2309 } else {
2310 fwd->listen_host = NULL;
2311 fwd->listen_port = a2port(fwdargs[0].arg);
2312 fwd->connect_host = xstrdup(fwdargs[1].arg);
2313 fwd->connect_port = a2port(fwdargs[2].arg);
2314 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002315 break;
2316
2317 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002318 fwd->listen_host = xstrdup(fwdargs[0].arg);
2319 fwd->listen_port = a2port(fwdargs[1].arg);
2320 fwd->connect_host = xstrdup(fwdargs[2].arg);
2321 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002322 break;
2323 default:
2324 i = 0; /* failure */
2325 }
2326
Darren Tuckera627d422013-06-02 07:31:17 +10002327 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002328
Damien Millera699d952008-11-03 19:27:34 +11002329 if (dynamicfwd) {
2330 if (!(i == 1 || i == 2))
2331 goto fail_free;
2332 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002333 if (!(i == 3 || i == 4)) {
2334 if (fwd->connect_path == NULL &&
2335 fwd->listen_path == NULL)
2336 goto fail_free;
2337 }
2338 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002339 goto fail_free;
2340 }
2341
Damien Miller7acefbb2014-07-18 14:11:24 +10002342 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2343 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002345 if (fwd->connect_host != NULL &&
2346 strlen(fwd->connect_host) >= NI_MAXHOST)
2347 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002348 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2349 if (fwd->connect_path != NULL &&
2350 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2351 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002352 if (fwd->listen_host != NULL &&
2353 strlen(fwd->listen_host) >= NI_MAXHOST)
2354 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002355 if (fwd->listen_path != NULL &&
2356 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2357 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002358
2359 return (i);
2360
2361 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002362 free(fwd->connect_host);
2363 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002364 free(fwd->connect_path);
2365 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002366 free(fwd->listen_host);
2367 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002368 free(fwd->listen_path);
2369 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002370 return (0);
2371}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002372
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002373int
2374parse_jump(const char *s, Options *o, int active)
2375{
2376 char *orig, *sdup, *cp;
2377 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002378 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002379
2380 active &= o->proxy_command == NULL && o->jump_host == NULL;
2381
2382 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002383 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002384 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002385 if (strcasecmp(s, "none") == 0)
2386 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002387 if ((cp = strrchr(sdup, ',')) == NULL)
2388 cp = sdup; /* last */
2389 else
2390 *cp++ = '\0';
2391
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002392 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002393 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002394 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2395 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002396 goto out;
2397 } else {
2398 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002399 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2400 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002401 goto out;
2402 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002403 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002404 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002405 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002406 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002407 if (strcasecmp(s, "none") == 0) {
2408 o->jump_host = xstrdup("none");
2409 o->jump_port = 0;
2410 } else {
2411 o->jump_user = user;
2412 o->jump_host = host;
2413 o->jump_port = port;
2414 o->proxy_command = xstrdup("none");
2415 user = host = NULL;
2416 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2417 o->jump_extra = xstrdup(s);
2418 o->jump_extra[cp - s] = '\0';
2419 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002420 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002421 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002422 ret = 0;
2423 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002424 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002425 free(user);
2426 free(host);
2427 return ret;
2428}
2429
millert@openbsd.org887669e2017-10-21 23:06:24 +00002430int
2431parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2432{
2433 char *path;
2434 int r;
2435
2436 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2437 if (r == 0 && path != NULL)
2438 r = -1; /* path not allowed */
2439 return r;
2440}
2441
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002442/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2443static const char *
2444fmt_multistate_int(int val, const struct multistate *m)
2445{
2446 u_int i;
2447
2448 for (i = 0; m[i].key != NULL; i++) {
2449 if (m[i].value == val)
2450 return m[i].key;
2451 }
2452 return "UNKNOWN";
2453}
2454
2455static const char *
2456fmt_intarg(OpCodes code, int val)
2457{
2458 if (val == -1)
2459 return "unset";
2460 switch (code) {
2461 case oAddressFamily:
2462 return fmt_multistate_int(val, multistate_addressfamily);
2463 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002464 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002465 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002466 case oStrictHostKeyChecking:
2467 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002468 case oControlMaster:
2469 return fmt_multistate_int(val, multistate_controlmaster);
2470 case oTunnel:
2471 return fmt_multistate_int(val, multistate_tunnel);
2472 case oRequestTTY:
2473 return fmt_multistate_int(val, multistate_requesttty);
2474 case oCanonicalizeHostname:
2475 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002476 case oAddKeysToAgent:
2477 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002478 case oFingerprintHash:
2479 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002480 default:
2481 switch (val) {
2482 case 0:
2483 return "no";
2484 case 1:
2485 return "yes";
2486 default:
2487 return "UNKNOWN";
2488 }
2489 }
2490}
2491
2492static const char *
2493lookup_opcode_name(OpCodes code)
2494{
2495 u_int i;
2496
2497 for (i = 0; keywords[i].name != NULL; i++)
2498 if (keywords[i].opcode == code)
2499 return(keywords[i].name);
2500 return "UNKNOWN";
2501}
2502
2503static void
2504dump_cfg_int(OpCodes code, int val)
2505{
2506 printf("%s %d\n", lookup_opcode_name(code), val);
2507}
2508
2509static void
2510dump_cfg_fmtint(OpCodes code, int val)
2511{
2512 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2513}
2514
2515static void
2516dump_cfg_string(OpCodes code, const char *val)
2517{
2518 if (val == NULL)
2519 return;
2520 printf("%s %s\n", lookup_opcode_name(code), val);
2521}
2522
2523static void
2524dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2525{
2526 u_int i;
2527
2528 for (i = 0; i < count; i++)
2529 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2530}
2531
2532static void
2533dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2534{
2535 u_int i;
2536
2537 printf("%s", lookup_opcode_name(code));
2538 for (i = 0; i < count; i++)
2539 printf(" %s", vals[i]);
2540 printf("\n");
2541}
2542
2543static void
2544dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2545{
2546 const struct Forward *fwd;
2547 u_int i;
2548
2549 /* oDynamicForward */
2550 for (i = 0; i < count; i++) {
2551 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002552 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002553 strcmp(fwd->connect_host, "socks") != 0)
2554 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002555 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002556 strcmp(fwd->connect_host, "socks") == 0)
2557 continue;
2558 printf("%s", lookup_opcode_name(code));
2559 if (fwd->listen_port == PORT_STREAMLOCAL)
2560 printf(" %s", fwd->listen_path);
2561 else if (fwd->listen_host == NULL)
2562 printf(" %d", fwd->listen_port);
2563 else {
2564 printf(" [%s]:%d",
2565 fwd->listen_host, fwd->listen_port);
2566 }
2567 if (code != oDynamicForward) {
2568 if (fwd->connect_port == PORT_STREAMLOCAL)
2569 printf(" %s", fwd->connect_path);
2570 else if (fwd->connect_host == NULL)
2571 printf(" %d", fwd->connect_port);
2572 else {
2573 printf(" [%s]:%d",
2574 fwd->connect_host, fwd->connect_port);
2575 }
2576 }
2577 printf("\n");
2578 }
2579}
2580
2581void
2582dump_client_config(Options *o, const char *host)
2583{
2584 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002585 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002586
djm@openbsd.org60a92472015-08-21 23:53:08 +00002587 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002588 all_key = sshkey_alg_list(0, 0, 1, ',');
2589 if (kex_assemble_names( &o->hostkeyalgorithms,
2590 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002591 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002592 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002593
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002594 /* Most interesting options first: user, host, port */
2595 dump_cfg_string(oUser, o->user);
2596 dump_cfg_string(oHostName, host);
2597 dump_cfg_int(oPort, o->port);
2598
2599 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002600 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002601 dump_cfg_fmtint(oAddressFamily, o->address_family);
2602 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2603 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2604 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2605 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2606 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2607 dump_cfg_fmtint(oCompression, o->compression);
2608 dump_cfg_fmtint(oControlMaster, o->control_master);
2609 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002610 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002611 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002612 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002613 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2614 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2615 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2616 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2617#ifdef GSSAPI
2618 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2619 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2620#endif /* GSSAPI */
2621 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2622 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2623 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2624 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2625 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2626 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2627 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002628 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2629 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2630 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002631 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2632 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2633 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2634 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002635 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2636 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002637 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002638
2639 /* Integer options */
2640 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002641 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2642 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2643 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2644 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2645 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2646
2647 /* String options */
2648 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002649 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002650 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2651 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002652 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002653 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002654 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002655 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002656 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002657 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2658 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002659 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002660 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002661 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002662 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2663 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002664#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002665 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002666#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002667 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002668 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002669 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002670 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002671
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002672 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002673 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2674 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2675 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2676
2677 /* String array options */
2678 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2679 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002680 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002681 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2682 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2683 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002684 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002685
2686 /* Special cases */
2687
2688 /* oConnectTimeout */
2689 if (o->connection_timeout == -1)
2690 printf("connecttimeout none\n");
2691 else
2692 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2693
2694 /* oTunnelDevice */
2695 printf("tunneldevice");
2696 if (o->tun_local == SSH_TUNID_ANY)
2697 printf(" any");
2698 else
2699 printf(" %d", o->tun_local);
2700 if (o->tun_remote == SSH_TUNID_ANY)
2701 printf(":any");
2702 else
2703 printf(":%d", o->tun_remote);
2704 printf("\n");
2705
2706 /* oCanonicalizePermittedCNAMEs */
2707 if ( o->num_permitted_cnames > 0) {
2708 printf("canonicalizePermittedcnames");
2709 for (i = 0; i < o->num_permitted_cnames; i++) {
2710 printf(" %s:%s", o->permitted_cnames[i].source_list,
2711 o->permitted_cnames[i].target_list);
2712 }
2713 printf("\n");
2714 }
2715
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002716 /* oControlPersist */
2717 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2718 dump_cfg_fmtint(oControlPersist, o->control_persist);
2719 else
2720 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2721
2722 /* oEscapeChar */
2723 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2724 printf("escapechar none\n");
2725 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002726 vis(buf, o->escape_char, VIS_WHITE, 0);
2727 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002728 }
2729
2730 /* oIPQoS */
2731 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2732 printf("%s\n", iptos2str(o->ip_qos_bulk));
2733
2734 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002735 printf("rekeylimit %llu %d\n",
2736 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002737
2738 /* oStreamLocalBindMask */
2739 printf("streamlocalbindmask 0%o\n",
2740 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002741
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002742 /* oLogFacility */
2743 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2744
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002745 /* oProxyCommand / oProxyJump */
2746 if (o->jump_host == NULL)
2747 dump_cfg_string(oProxyCommand, o->proxy_command);
2748 else {
2749 /* Check for numeric addresses */
2750 i = strchr(o->jump_host, ':') != NULL ||
2751 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2752 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2753 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002754 /* optional additional jump spec */
2755 o->jump_extra == NULL ? "" : o->jump_extra,
2756 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002757 /* optional user */
2758 o->jump_user == NULL ? "" : o->jump_user,
2759 o->jump_user == NULL ? "" : "@",
2760 /* opening [ if hostname is numeric */
2761 i ? "[" : "",
2762 /* mandatory hostname */
2763 o->jump_host,
2764 /* closing ] if hostname is numeric */
2765 i ? "]" : "",
2766 /* optional port number */
2767 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002768 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002769 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002770}