blob: f78b4d6fef57c3a03c65b189fbb8682a0bedde63 [file] [log] [blame]
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001/* $OpenBSD: readconf.c,v 1.309 2019/09/06 14:45:34 naddy 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
jmc@openbsd.org73491492019-06-12 11:31:50 +000089 Hostname another.host.name.real.org
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 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,
jmc@openbsd.org73491492019-06-12 11:31:50 +0000151 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 },
jmc@openbsd.org73491492019-06-12 11:31:50 +0000243 { "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
djm@openbsd.org9ab5b942019-08-09 05:05:54 +0000489 if (access(shell, X_OK) == -1) {
490 fatal("Shell \"%s\" is not executable: %s",
491 shell, strerror(errno));
492 }
493
Damien Miller194fd902013-10-15 12:13:05 +1100494 /* Need this to redirect subprocess stdin/out */
495 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
496 fatal("open(/dev/null): %s", strerror(errno));
497
498 debug("Executing command: '%.500s'", cmd);
499
500 /* Fork and execute the command. */
501 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000502 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100503
Damien Miller194fd902013-10-15 12:13:05 +1100504 /* Redirect child stdin and stdout. Leave stderr */
505 if (dup2(devnull, STDIN_FILENO) == -1)
506 fatal("dup2: %s", strerror(errno));
507 if (dup2(devnull, STDOUT_FILENO) == -1)
508 fatal("dup2: %s", strerror(errno));
509 if (devnull > STDERR_FILENO)
510 close(devnull);
511 closefrom(STDERR_FILENO + 1);
512
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000513 argv[0] = shell;
514 argv[1] = "-c";
515 argv[2] = xstrdup(cmd);
516 argv[3] = NULL;
517
Damien Miller194fd902013-10-15 12:13:05 +1100518 execv(argv[0], argv);
519 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
520 /* Die with signal to make this error apparent to parent. */
521 signal(SIGTERM, SIG_DFL);
522 kill(getpid(), SIGTERM);
523 _exit(1);
524 }
525 /* Parent. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000526 if (pid == -1)
Damien Miller194fd902013-10-15 12:13:05 +1100527 fatal("%s: fork: %.100s", __func__, strerror(errno));
528
529 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100530
531 while (waitpid(pid, &status, 0) == -1) {
532 if (errno != EINTR && errno != EAGAIN)
533 fatal("%s: waitpid: %s", __func__, strerror(errno));
534 }
535 if (!WIFEXITED(status)) {
536 error("command '%.100s' exited abnormally", cmd);
537 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000538 }
Damien Miller194fd902013-10-15 12:13:05 +1100539 debug3("command returned status %d", WEXITSTATUS(status));
540 return WEXITSTATUS(status);
541}
542
543/*
544 * Parse and execute a Match directive.
545 */
546static int
547match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000548 const char *host_arg, const char *original_host, int final_pass,
549 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100550{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000551 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100552 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100554 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000555 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100556
557 /*
558 * Configuration is likely to be incomplete at this point so we
559 * must be prepared to use default values.
560 */
561 port = options->port <= 0 ? default_ssh_port() : options->port;
562 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000563 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000564 host = xstrdup(options->hostname);
565 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100566 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = percent_expand(options->hostname,
568 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000569 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100570 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000573 debug2("checking match for '%s' host %s originally %s",
574 cp, host, original_host);
575 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
576 criteria = NULL;
577 this_result = 1;
578 if ((negate = attrib[0] == '!'))
579 attrib++;
580 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100581 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100583 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 error("%.200s line %d: '%s' cannot be combined "
585 "with other Match attributes",
586 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100587 result = -1;
588 goto out;
589 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 if (result)
591 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100592 goto out;
593 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000594 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000595 if (strcasecmp(attrib, "canonical") == 0 ||
596 strcasecmp(attrib, "final") == 0) {
597 /*
598 * If the config requests "Match final" then remember
599 * this so we can perform a second pass later.
600 */
601 if (strcasecmp(attrib, "final") == 0 &&
602 want_final_pass != NULL)
603 *want_final_pass = 1;
604 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000605 if (r == (negate ? 1 : 0))
606 this_result = result = 0;
607 debug3("%.200s line %d: %smatched '%s'",
608 filename, linenum,
609 this_result ? "" : "not ", oattrib);
610 continue;
611 }
612 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100613 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
614 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100615 result = -1;
616 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100617 }
Damien Miller194fd902013-10-15 12:13:05 +1100618 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000620 r = match_hostname(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, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000624 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000625 r = match_hostname(original_host, arg) == 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, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000630 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000631 if (r == (negate ? 1 : 0))
632 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100633 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000634 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000635 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 if (r == (negate ? 1 : 0))
637 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100638 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100639 if (gethostname(thishost, sizeof(thishost)) == -1)
640 fatal("gethostname: %s", strerror(errno));
641 strlcpy(shorthost, thishost, sizeof(shorthost));
642 shorthost[strcspn(thishost, ".")] = '\0';
643 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000644 snprintf(uidstr, sizeof(uidstr), "%llu",
645 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100646
647 cmd = percent_expand(arg,
648 "L", shorthost,
649 "d", pw->pw_dir,
650 "h", host,
651 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000652 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100653 "p", portstr,
654 "r", ruser,
655 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000656 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100657 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100658 if (result != 1) {
659 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000660 debug3("%.200s line %d: skipped exec "
661 "\"%.100s\"", filename, linenum, cmd);
662 free(cmd);
663 continue;
Damien Miller06287802014-02-24 15:56:45 +1100664 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000665 r = execute_in_shell(cmd);
666 if (r == -1) {
667 fatal("%.200s line %d: match exec "
668 "'%.100s' error", filename,
669 linenum, cmd);
670 }
671 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100672 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000673 /* Force exit status to boolean */
674 r = r == 0;
675 if (r == (negate ? 1 : 0))
676 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100677 } else {
678 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100679 result = -1;
680 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100681 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000682 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
683 filename, linenum, this_result ? "": "not ",
684 oattrib, criteria);
685 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100686 }
Damien Millercf31f382013-10-24 21:02:56 +1100687 if (attributes == 0) {
688 error("One or more attributes required for Match");
689 result = -1;
690 goto out;
691 }
Damien Miller084bcd22013-10-23 16:30:51 +1100692 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000693 if (result != -1)
694 debug2("match %sfound", result ? "" : "not ");
695 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100696 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100697 return result;
698}
699
djm@openbsd.org555294a2018-04-06 13:02:39 +0000700/* Remove environment variable by pattern */
701static void
702rm_env(Options *options, const char *arg, const char *filename, int linenum)
703{
704 int i, j;
705 char *cp;
706
707 /* Remove an environment variable */
708 for (i = 0; i < options->num_send_env; ) {
709 cp = xstrdup(options->send_env[i]);
710 if (!match_pattern(cp, arg + 1)) {
711 free(cp);
712 i++;
713 continue;
714 }
715 debug3("%s line %d: removing environment %s",
716 filename, linenum, cp);
717 free(cp);
718 free(options->send_env[i]);
719 options->send_env[i] = NULL;
720 for (j = i; j < options->num_send_env - 1; j++) {
721 options->send_env[j] = options->send_env[j + 1];
722 options->send_env[j + 1] = NULL;
723 }
724 options->num_send_env--;
725 /* NB. don't increment i */
726 }
727}
728
Damien Miller5428f641999-11-25 11:54:57 +1100729/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000730 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100731 */
Damien Miller4af51302000-04-16 11:18:38 +1000732static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000733parse_token(const char *cp, const char *filename, int linenum,
734 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735{
Darren Tucker07636982013-05-16 20:30:03 +1000736 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller95def091999-11-25 00:26:21 +1100738 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000739 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100740 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000741 if (ignored_unknown != NULL &&
742 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000743 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000744 error("%s: line %d: Bad configuration option: %s",
745 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100746 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747}
748
Damien Millere9fc72e2013-10-15 12:14:12 +1100749/* Multistate option parsing */
750struct multistate {
751 char *key;
752 int value;
753};
754static const struct multistate multistate_flag[] = {
755 { "true", 1 },
756 { "false", 0 },
757 { "yes", 1 },
758 { "no", 0 },
759 { NULL, -1 }
760};
761static const struct multistate multistate_yesnoask[] = {
762 { "true", 1 },
763 { "false", 0 },
764 { "yes", 1 },
765 { "no", 0 },
766 { "ask", 2 },
767 { NULL, -1 }
768};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000769static const struct multistate multistate_strict_hostkey[] = {
770 { "true", SSH_STRICT_HOSTKEY_YES },
771 { "false", SSH_STRICT_HOSTKEY_OFF },
772 { "yes", SSH_STRICT_HOSTKEY_YES },
773 { "no", SSH_STRICT_HOSTKEY_OFF },
774 { "ask", SSH_STRICT_HOSTKEY_ASK },
775 { "off", SSH_STRICT_HOSTKEY_OFF },
776 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
777 { NULL, -1 }
778};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000779static const struct multistate multistate_yesnoaskconfirm[] = {
780 { "true", 1 },
781 { "false", 0 },
782 { "yes", 1 },
783 { "no", 0 },
784 { "ask", 2 },
785 { "confirm", 3 },
786 { NULL, -1 }
787};
Damien Millere9fc72e2013-10-15 12:14:12 +1100788static const struct multistate multistate_addressfamily[] = {
789 { "inet", AF_INET },
790 { "inet6", AF_INET6 },
791 { "any", AF_UNSPEC },
792 { NULL, -1 }
793};
794static const struct multistate multistate_controlmaster[] = {
795 { "true", SSHCTL_MASTER_YES },
796 { "yes", SSHCTL_MASTER_YES },
797 { "false", SSHCTL_MASTER_NO },
798 { "no", SSHCTL_MASTER_NO },
799 { "auto", SSHCTL_MASTER_AUTO },
800 { "ask", SSHCTL_MASTER_ASK },
801 { "autoask", SSHCTL_MASTER_AUTO_ASK },
802 { NULL, -1 }
803};
804static const struct multistate multistate_tunnel[] = {
805 { "ethernet", SSH_TUNMODE_ETHERNET },
806 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
807 { "true", SSH_TUNMODE_DEFAULT },
808 { "yes", SSH_TUNMODE_DEFAULT },
809 { "false", SSH_TUNMODE_NO },
810 { "no", SSH_TUNMODE_NO },
811 { NULL, -1 }
812};
813static const struct multistate multistate_requesttty[] = {
814 { "true", REQUEST_TTY_YES },
815 { "yes", REQUEST_TTY_YES },
816 { "false", REQUEST_TTY_NO },
817 { "no", REQUEST_TTY_NO },
818 { "force", REQUEST_TTY_FORCE },
819 { "auto", REQUEST_TTY_AUTO },
820 { NULL, -1 }
821};
Damien Miller38505592013-10-17 11:48:13 +1100822static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100823 { "true", SSH_CANONICALISE_YES },
824 { "false", SSH_CANONICALISE_NO },
825 { "yes", SSH_CANONICALISE_YES },
826 { "no", SSH_CANONICALISE_NO },
827 { "always", SSH_CANONICALISE_ALWAYS },
828 { NULL, -1 }
829};
Damien Millere9fc72e2013-10-15 12:14:12 +1100830
Damien Miller5428f641999-11-25 11:54:57 +1100831/*
832 * Processes a single option line as used in the configuration files. This
833 * only sets those values that have not already been set.
834 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100835int
Damien Miller194fd902013-10-15 12:13:05 +1100836process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000837 const char *original_host, char *line, const char *filename,
838 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000840 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000841 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000842}
843
844#define WHITESPACE " \t\r\n"
845static int
846process_config_line_depth(Options *options, struct passwd *pw, const char *host,
847 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000848 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000849{
Damien Miller295ee632011-05-29 21:42:31 +1000850 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
851 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000852 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000853 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000854 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100855 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000856 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000857 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100858 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000859 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100860 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100861 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000862 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000863 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
Damien Miller194fd902013-10-15 12:13:05 +1100865 if (activep == NULL) { /* We are processing a command line directive */
866 cmdline = 1;
867 activep = &cmdline;
868 }
869
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000870 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000871 if ((len = strlen(line)) == 0)
872 return 0;
873 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000874 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000875 break;
876 line[len] = '\0';
877 }
878
Damien Millerbe484b52000-07-15 14:14:16 +1000879 s = line;
880 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100881 if ((keyword = strdelim(&s)) == NULL)
882 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000883 /* Ignore leading whitespace. */
884 if (*keyword == '\0')
885 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000886 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100887 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000888 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100889 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Darren Tucker07636982013-05-16 20:30:03 +1000891 opcode = parse_token(keyword, filename, linenum,
892 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 switch (opcode) {
895 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100896 /* don't panic, but count bad options */
897 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000898 case oIgnore:
899 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000900 case oIgnoredUnknownOption:
901 debug("%s line %d: Ignored unknown option \"%s\"",
902 filename, linenum, keyword);
903 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000904 case oConnectTimeout:
905 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100906parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000907 arg = strdelim(&s);
908 if (!arg || *arg == '\0')
909 fatal("%s line %d: missing time value.",
910 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000911 if (strcmp(arg, "none") == 0)
912 value = -1;
913 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000914 fatal("%s line %d: invalid time value.",
915 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100916 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000917 *intptr = value;
918 break;
919
Damien Miller95def091999-11-25 00:26:21 +1100920 case oForwardAgent:
921 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100922 parse_flag:
923 multistate_ptr = multistate_flag;
924 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000925 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000926 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100927 fatal("%s line %d: missing argument.",
928 filename, linenum);
929 value = -1;
930 for (i = 0; multistate_ptr[i].key != NULL; i++) {
931 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
932 value = multistate_ptr[i].value;
933 break;
934 }
935 }
936 if (value == -1)
937 fatal("%s line %d: unsupported option \"%s\".",
938 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100939 if (*activep && *intptr == -1)
940 *intptr = value;
941 break;
942
943 case oForwardX11:
944 intptr = &options->forward_x11;
945 goto parse_flag;
946
Darren Tucker0a118da2003-10-15 15:54:32 +1000947 case oForwardX11Trusted:
948 intptr = &options->forward_x11_trusted;
949 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000950
Damien Miller1ab6a512010-06-26 10:02:24 +1000951 case oForwardX11Timeout:
952 intptr = &options->forward_x11_timeout;
953 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000954
Damien Miller95def091999-11-25 00:26:21 +1100955 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000956 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100957 goto parse_flag;
958
Darren Tuckere7d4b192006-07-12 22:17:10 +1000959 case oExitOnForwardFailure:
960 intptr = &options->exit_on_forward_failure;
961 goto parse_flag;
962
Damien Miller95def091999-11-25 00:26:21 +1100963 case oPasswordAuthentication:
964 intptr = &options->password_authentication;
965 goto parse_flag;
966
Damien Miller874d77b2000-10-14 16:23:11 +1100967 case oKbdInteractiveAuthentication:
968 intptr = &options->kbd_interactive_authentication;
969 goto parse_flag;
970
971 case oKbdInteractiveDevices:
972 charptr = &options->kbd_interactive_devices;
973 goto parse_string;
974
Damien Miller0bc1bd82000-11-13 22:57:25 +1100975 case oPubkeyAuthentication:
976 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000977 goto parse_flag;
978
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000979 case oHostbasedAuthentication:
980 intptr = &options->hostbased_authentication;
981 goto parse_flag;
982
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000983 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000984 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100985 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000986
Darren Tucker0efd1552003-08-26 11:49:55 +1000987 case oGssAuthentication:
988 intptr = &options->gss_authentication;
989 goto parse_flag;
990
991 case oGssDelegateCreds:
992 intptr = &options->gss_deleg_creds;
993 goto parse_flag;
994
Damien Miller95def091999-11-25 00:26:21 +1100995 case oBatchMode:
996 intptr = &options->batch_mode;
997 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller95def091999-11-25 00:26:21 +1100999 case oCheckHostIP:
1000 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +10001001 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller37876e92003-05-15 10:19:46 +10001003 case oVerifyHostKeyDNS:
1004 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001005 multistate_ptr = multistate_yesnoask;
1006 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001007
Damien Miller95def091999-11-25 00:26:21 +11001008 case oStrictHostKeyChecking:
1009 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001010 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001011 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 case oCompression:
1014 intptr = &options->compression;
1015 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Damien Miller12c150e2003-12-17 16:31:10 +11001017 case oTCPKeepAlive:
1018 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001019 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001021 case oNoHostAuthenticationForLocalhost:
1022 intptr = &options->no_host_authentication_for_localhost;
1023 goto parse_flag;
1024
Damien Miller95def091999-11-25 00:26:21 +11001025 case oNumberOfPasswordPrompts:
1026 intptr = &options->number_of_password_prompts;
1027 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Damien Millera5539d22003-04-09 20:50:06 +10001029 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001030 arg = strdelim(&s);
1031 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001032 fatal("%.200s line %d: Missing argument.", filename,
1033 linenum);
1034 if (strcmp(arg, "default") == 0) {
1035 val64 = 0;
1036 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001037 if (scan_scaled(arg, &val64) == -1)
1038 fatal("%.200s line %d: Bad number '%s': %s",
1039 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001040 if (val64 != 0 && val64 < 16)
1041 fatal("%.200s line %d: RekeyLimit too small",
1042 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001043 }
Damien Miller3dff1762008-02-10 22:25:52 +11001044 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001045 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001046 if (s != NULL) { /* optional rekey interval present */
1047 if (strcmp(s, "none") == 0) {
1048 (void)strdelim(&s); /* discard */
1049 break;
1050 }
1051 intptr = &options->rekey_interval;
1052 goto parse_time;
1053 }
Damien Millera5539d22003-04-09 20:50:06 +10001054 break;
1055
Damien Miller95def091999-11-25 00:26:21 +11001056 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001057 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001058 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001059 fatal("%.200s line %d: Missing argument.", filename, linenum);
1060 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001061 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001062 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001063 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001064 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001065 add_identity_file(options, NULL,
1066 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001067 }
1068 break;
1069
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001070 case oCertificateFile:
1071 arg = strdelim(&s);
1072 if (!arg || *arg == '\0')
1073 fatal("%.200s line %d: Missing argument.",
1074 filename, linenum);
1075 if (*activep) {
1076 intptr = &options->num_certificate_files;
1077 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1078 fatal("%.200s line %d: Too many certificate "
1079 "files specified (max %d).",
1080 filename, linenum,
1081 SSH_MAX_CERTIFICATE_FILES);
1082 }
1083 add_certificate_file(options, arg,
1084 flags & SSHCONF_USERCONF);
1085 }
1086 break;
1087
Damien Millerd3a18572000-06-07 19:55:44 +10001088 case oXAuthLocation:
1089 charptr=&options->xauth_location;
1090 goto parse_string;
1091
Damien Miller95def091999-11-25 00:26:21 +11001092 case oUser:
1093 charptr = &options->user;
1094parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001095 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001096 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001097 fatal("%.200s line %d: Missing argument.",
1098 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001099 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001100 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001101 break;
1102
1103 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001104 cpptr = (char **)&options->system_hostfiles;
1105 uintptr = &options->num_system_hostfiles;
1106 max_entries = SSH_MAX_HOSTS_FILES;
1107parse_char_array:
1108 if (*activep && *uintptr == 0) {
1109 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1110 if ((*uintptr) >= max_entries)
1111 fatal("%s line %d: "
1112 "too many authorized keys files.",
1113 filename, linenum);
1114 cpptr[(*uintptr)++] = xstrdup(arg);
1115 }
1116 }
1117 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001118
1119 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001120 cpptr = (char **)&options->user_hostfiles;
1121 uintptr = &options->num_user_hostfiles;
1122 max_entries = SSH_MAX_HOSTS_FILES;
1123 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001124
jmc@openbsd.org73491492019-06-12 11:31:50 +00001125 case oHostname:
Damien Miller95def091999-11-25 00:26:21 +11001126 charptr = &options->hostname;
1127 goto parse_string;
1128
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001129 case oHostKeyAlias:
1130 charptr = &options->host_key_alias;
1131 goto parse_string;
1132
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001133 case oPreferredAuthentications:
1134 charptr = &options->preferred_authentications;
1135 goto parse_string;
1136
Ben Lindstrome0f88042001-04-30 13:06:24 +00001137 case oBindAddress:
1138 charptr = &options->bind_address;
1139 goto parse_string;
1140
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001141 case oBindInterface:
1142 charptr = &options->bind_interface;
1143 goto parse_string;
1144
Damien Miller7ea845e2010-02-12 09:21:02 +11001145 case oPKCS11Provider:
1146 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001147 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001148
Damien Miller95def091999-11-25 00:26:21 +11001149 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001150 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001151 /* Ignore ProxyCommand if ProxyJump already specified */
1152 if (options->jump_host != NULL)
1153 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001154parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001155 if (s == NULL)
1156 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001157 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001158 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001159 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001160 return 0;
1161
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001162 case oProxyJump:
1163 if (s == NULL) {
1164 fatal("%.200s line %d: Missing argument.",
1165 filename, linenum);
1166 }
1167 len = strspn(s, WHITESPACE "=");
1168 if (parse_jump(s + len, options, *activep) == -1) {
1169 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1170 filename, linenum, s + len);
1171 }
1172 return 0;
1173
Damien Miller95def091999-11-25 00:26:21 +11001174 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001175 arg = strdelim(&s);
1176 if (!arg || *arg == '\0')
1177 fatal("%.200s line %d: Missing argument.",
1178 filename, linenum);
1179 value = a2port(arg);
1180 if (value <= 0)
1181 fatal("%.200s line %d: Bad port '%s'.",
1182 filename, linenum, arg);
1183 if (*activep && options->port == -1)
1184 options->port = value;
1185 break;
1186
1187 case oConnectionAttempts:
1188 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001189parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001190 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001191 if ((errstr = atoi_err(arg, &value)) != NULL)
1192 fatal("%s line %d: integer value %s.",
1193 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001194 if (*activep && *intptr == -1)
1195 *intptr = value;
1196 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197
Damien Miller78928792000-04-12 20:17:38 +10001198 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001199 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001200 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001201 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001202 if (*arg != '-' &&
1203 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001204 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001205 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001206 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001207 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001208 break;
1209
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001210 case oMacs:
1211 arg = strdelim(&s);
1212 if (!arg || *arg == '\0')
1213 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001214 if (*arg != '-' &&
1215 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001216 fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001217 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001218 if (*activep && options->macs == NULL)
1219 options->macs = xstrdup(arg);
1220 break;
1221
Damien Millerd5f62bf2010-09-24 22:11:14 +10001222 case oKexAlgorithms:
1223 arg = strdelim(&s);
1224 if (!arg || *arg == '\0')
1225 fatal("%.200s line %d: Missing argument.",
1226 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001227 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001228 !kex_names_valid(*arg == '+' || *arg == '^' ?
1229 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001230 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1231 filename, linenum, arg ? arg : "<NONE>");
1232 if (*activep && options->kex_algorithms == NULL)
1233 options->kex_algorithms = xstrdup(arg);
1234 break;
1235
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001236 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001237 charptr = &options->hostkeyalgorithms;
1238parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001239 arg = strdelim(&s);
1240 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001241 fatal("%.200s line %d: Missing argument.",
1242 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001243 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001244 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1245 arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001246 fatal("%s line %d: Bad key types '%s'.",
1247 filename, linenum, arg ? arg : "<NONE>");
1248 if (*activep && *charptr == NULL)
1249 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001250 break;
1251
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001252 case oCASignatureAlgorithms:
1253 charptr = &options->ca_sign_algorithms;
1254 goto parse_keytypes;
1255
Damien Miller95def091999-11-25 00:26:21 +11001256 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001257 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001258 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001259 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001260 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001261 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001262 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001263 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1264 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001265 break;
1266
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001267 case oLogFacility:
1268 log_facility_ptr = &options->log_facility;
1269 arg = strdelim(&s);
1270 value = log_facility_number(arg);
1271 if (value == SYSLOG_FACILITY_NOT_SET)
1272 fatal("%.200s line %d: unsupported log facility '%s'",
1273 filename, linenum, arg ? arg : "<NONE>");
1274 if (*log_facility_ptr == -1)
1275 *log_facility_ptr = (SyslogFacility) value;
1276 break;
1277
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001278 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001279 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001280 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001281 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001282 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001283 fatal("%.200s line %d: Missing port argument.",
1284 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001285
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001286 remotefwd = (opcode == oRemoteForward);
1287 dynamicfwd = (opcode == oDynamicForward);
1288
1289 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001290 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001291 if (arg2 == NULL || *arg2 == '\0') {
1292 if (remotefwd)
1293 dynamicfwd = 1;
1294 else
1295 fatal("%.200s line %d: Missing target "
1296 "argument.", filename, linenum);
1297 } else {
1298 /* construct a string for parse_forward */
1299 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1300 arg2);
1301 }
Damien Millera699d952008-11-03 19:27:34 +11001302 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001303 if (dynamicfwd)
1304 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001305
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001306 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001307 fatal("%.200s line %d: Bad forwarding specification.",
1308 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001309
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001310 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001311 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001312 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001313 } else {
1314 add_local_forward(options, &fwd);
1315 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001316 }
Damien Miller95def091999-11-25 00:26:21 +11001317 break;
1318
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001319 case oClearAllForwardings:
1320 intptr = &options->clear_forwardings;
1321 goto parse_flag;
1322
Damien Miller95def091999-11-25 00:26:21 +11001323 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001324 if (cmdline)
1325 fatal("Host directive not supported as a command-line "
1326 "option");
Damien Miller95def091999-11-25 00:26:21 +11001327 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001328 arg2 = NULL;
1329 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001330 if ((flags & SSHCONF_NEVERMATCH) != 0)
1331 break;
Damien Millerfe924212011-05-15 08:44:45 +10001332 negated = *arg == '!';
1333 if (negated)
1334 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001335 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001336 if (negated) {
1337 debug("%.200s line %d: Skipping Host "
1338 "block because of negated match "
1339 "for %.100s", filename, linenum,
1340 arg);
1341 *activep = 0;
1342 break;
1343 }
1344 if (!*activep)
1345 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001346 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001347 }
Damien Millerfe924212011-05-15 08:44:45 +10001348 }
1349 if (*activep)
1350 debug("%.200s line %d: Applying options for %.100s",
1351 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001352 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001353 return 0;
1354
Damien Miller194fd902013-10-15 12:13:05 +11001355 case oMatch:
1356 if (cmdline)
1357 fatal("Host directive not supported as a command-line "
1358 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001359 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001360 flags & SSHCONF_FINAL, want_final_pass,
1361 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001362 if (value < 0)
1363 fatal("%.200s line %d: Bad Match condition", filename,
1364 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001365 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001366 break;
1367
Damien Miller95def091999-11-25 00:26:21 +11001368 case oEscapeChar:
1369 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001370 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001371 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001372 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001373 if (strcmp(arg, "none") == 0)
1374 value = SSH_ESCAPECHAR_NONE;
1375 else if (arg[1] == '\0')
1376 value = (u_char) arg[0];
1377 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001378 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1379 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001380 else {
1381 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001382 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001383 /* NOTREACHED */
1384 value = 0; /* Avoid compiler warning. */
1385 }
1386 if (*activep && *intptr == -1)
1387 *intptr = value;
1388 break;
1389
Damien Miller20a8f972003-05-18 20:50:30 +10001390 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001391 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001392 multistate_ptr = multistate_addressfamily;
1393 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001394
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001395 case oEnableSSHKeysign:
1396 intptr = &options->enable_ssh_keysign;
1397 goto parse_flag;
1398
Damien Millerbd394c32004-03-08 23:12:36 +11001399 case oIdentitiesOnly:
1400 intptr = &options->identities_only;
1401 goto parse_flag;
1402
Damien Miller509b0102003-12-17 16:33:10 +11001403 case oServerAliveInterval:
1404 intptr = &options->server_alive_interval;
1405 goto parse_time;
1406
1407 case oServerAliveCountMax:
1408 intptr = &options->server_alive_count_max;
1409 goto parse_int;
1410
Darren Tucker46bc0752004-05-02 22:11:30 +10001411 case oSendEnv:
1412 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1413 if (strchr(arg, '=') != NULL)
1414 fatal("%s line %d: Invalid environment name.",
1415 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001416 if (!*activep)
1417 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001418 if (*arg == '-') {
1419 /* Removing an env var */
1420 rm_env(options, arg, filename, linenum);
1421 continue;
1422 } else {
1423 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001424 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001425 fatal("%s line %d: too many send env.",
1426 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001427 options->send_env = xrecallocarray(
1428 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001429 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001430 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001431 options->send_env[options->num_send_env++] =
1432 xstrdup(arg);
1433 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001434 }
1435 break;
1436
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001437 case oSetEnv:
1438 value = options->num_setenv;
1439 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1440 if (strchr(arg, '=') == NULL)
1441 fatal("%s line %d: Invalid SetEnv.",
1442 filename, linenum);
1443 if (!*activep || value != 0)
1444 continue;
1445 /* Adding a setenv var */
1446 if (options->num_setenv >= INT_MAX)
1447 fatal("%s line %d: too many SetEnv.",
1448 filename, linenum);
1449 options->setenv = xrecallocarray(
1450 options->setenv, options->num_setenv,
1451 options->num_setenv + 1, sizeof(*options->setenv));
1452 options->setenv[options->num_setenv++] = xstrdup(arg);
1453 }
1454 break;
1455
Damien Miller0e220db2004-06-15 10:34:08 +10001456 case oControlPath:
1457 charptr = &options->control_path;
1458 goto parse_string;
1459
1460 case oControlMaster:
1461 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001462 multistate_ptr = multistate_controlmaster;
1463 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001464
Damien Millere11e1ea2010-08-03 16:04:46 +10001465 case oControlPersist:
1466 /* no/false/yes/true, or a time spec */
1467 intptr = &options->control_persist;
1468 arg = strdelim(&s);
1469 if (!arg || *arg == '\0')
1470 fatal("%.200s line %d: Missing ControlPersist"
1471 " argument.", filename, linenum);
1472 value = 0;
1473 value2 = 0; /* timeout */
1474 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1475 value = 0;
1476 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1477 value = 1;
1478 else if ((value2 = convtime(arg)) >= 0)
1479 value = 1;
1480 else
1481 fatal("%.200s line %d: Bad ControlPersist argument.",
1482 filename, linenum);
1483 if (*activep && *intptr == -1) {
1484 *intptr = value;
1485 options->control_persist_timeout = value2;
1486 }
1487 break;
1488
Damien Millere1776152005-03-01 21:47:37 +11001489 case oHashKnownHosts:
1490 intptr = &options->hash_known_hosts;
1491 goto parse_flag;
1492
Damien Millerd27b9472005-12-13 19:29:02 +11001493 case oTunnel:
1494 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001495 multistate_ptr = multistate_tunnel;
1496 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001497
1498 case oTunnelDevice:
1499 arg = strdelim(&s);
1500 if (!arg || *arg == '\0')
1501 fatal("%.200s line %d: Missing argument.", filename, linenum);
1502 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001503 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001504 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1505 if (*activep) {
1506 options->tun_local = value;
1507 options->tun_remote = value2;
1508 }
1509 break;
1510
1511 case oLocalCommand:
1512 charptr = &options->local_command;
1513 goto parse_command;
1514
1515 case oPermitLocalCommand:
1516 intptr = &options->permit_local_command;
1517 goto parse_flag;
1518
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001519 case oRemoteCommand:
1520 charptr = &options->remote_command;
1521 goto parse_command;
1522
Damien Miller10288242008-06-30 00:04:03 +10001523 case oVisualHostKey:
1524 intptr = &options->visual_host_key;
1525 goto parse_flag;
1526
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001527 case oInclude:
1528 if (cmdline)
1529 fatal("Include directive not supported as a "
1530 "command-line option");
1531 value = 0;
1532 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1533 /*
1534 * Ensure all paths are anchored. User configuration
1535 * files may begin with '~/' but system configurations
1536 * must not. If the path is relative, then treat it
1537 * as living in ~/.ssh for user configurations or
1538 * /etc/ssh for system ones.
1539 */
1540 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1541 fatal("%.200s line %d: bad include path %s.",
1542 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001543 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001544 xasprintf(&arg2, "%s/%s",
1545 (flags & SSHCONF_USERCONF) ?
1546 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1547 } else
1548 arg2 = xstrdup(arg);
1549 memset(&gl, 0, sizeof(gl));
1550 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1551 if (r == GLOB_NOMATCH) {
1552 debug("%.200s line %d: include %s matched no "
1553 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001554 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001555 continue;
1556 } else if (r != 0 || gl.gl_pathc < 0)
1557 fatal("%.200s line %d: glob failed for %s.",
1558 filename, linenum, arg2);
1559 free(arg2);
1560 oactive = *activep;
1561 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1562 debug3("%.200s line %d: Including file %s "
1563 "depth %d%s", filename, linenum,
1564 gl.gl_pathv[i], depth,
1565 oactive ? "" : " (parse only)");
1566 r = read_config_file_depth(gl.gl_pathv[i],
1567 pw, host, original_host, options,
1568 flags | SSHCONF_CHECKPERM |
1569 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001570 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001571 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001572 fatal("Can't open user config file "
1573 "%.100s: %.100s", gl.gl_pathv[i],
1574 strerror(errno));
1575 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001576 /*
1577 * don't let Match in includes clobber the
1578 * containing file's Match state.
1579 */
1580 *activep = oactive;
1581 if (r != 1)
1582 value = -1;
1583 }
1584 globfree(&gl);
1585 }
1586 if (value != 0)
1587 return value;
1588 break;
1589
Damien Miller0dac6fb2010-11-20 15:19:38 +11001590 case oIPQoS:
1591 arg = strdelim(&s);
1592 if ((value = parse_ipqos(arg)) == -1)
1593 fatal("%s line %d: Bad IPQoS value: %s",
1594 filename, linenum, arg);
1595 arg = strdelim(&s);
1596 if (arg == NULL)
1597 value2 = value;
1598 else if ((value2 = parse_ipqos(arg)) == -1)
1599 fatal("%s line %d: Bad IPQoS value: %s",
1600 filename, linenum, arg);
1601 if (*activep) {
1602 options->ip_qos_interactive = value;
1603 options->ip_qos_bulk = value2;
1604 }
1605 break;
1606
Damien Miller21771e22011-05-15 08:45:50 +10001607 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001608 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001609 multistate_ptr = multistate_requesttty;
1610 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001611
Darren Tucker07636982013-05-16 20:30:03 +10001612 case oIgnoreUnknown:
1613 charptr = &options->ignored_unknown;
1614 goto parse_string;
1615
Damien Miller1262b662013-08-21 02:44:24 +10001616 case oProxyUseFdpass:
1617 intptr = &options->proxy_use_fdpass;
1618 goto parse_flag;
1619
Damien Miller0faf7472013-10-17 11:47:23 +11001620 case oCanonicalDomains:
1621 value = options->num_canonical_domains != 0;
1622 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001623 if (!valid_domain(arg, 1, &errstr)) {
1624 fatal("%s line %d: %s", filename, linenum,
1625 errstr);
1626 }
Damien Miller0faf7472013-10-17 11:47:23 +11001627 if (!*activep || value)
1628 continue;
1629 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1630 fatal("%s line %d: too many hostname suffixes.",
1631 filename, linenum);
1632 options->canonical_domains[
1633 options->num_canonical_domains++] = xstrdup(arg);
1634 }
1635 break;
1636
Damien Miller38505592013-10-17 11:48:13 +11001637 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001638 value = options->num_permitted_cnames != 0;
1639 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1640 /* Either '*' for everything or 'list:list' */
1641 if (strcmp(arg, "*") == 0)
1642 arg2 = arg;
1643 else {
1644 lowercase(arg);
1645 if ((arg2 = strchr(arg, ':')) == NULL ||
1646 arg2[1] == '\0') {
1647 fatal("%s line %d: "
1648 "Invalid permitted CNAME \"%s\"",
1649 filename, linenum, arg);
1650 }
1651 *arg2 = '\0';
1652 arg2++;
1653 }
1654 if (!*activep || value)
1655 continue;
1656 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1657 fatal("%s line %d: too many permitted CNAMEs.",
1658 filename, linenum);
1659 cname = options->permitted_cnames +
1660 options->num_permitted_cnames++;
1661 cname->source_list = xstrdup(arg);
1662 cname->target_list = xstrdup(arg2);
1663 }
1664 break;
1665
Damien Miller38505592013-10-17 11:48:13 +11001666 case oCanonicalizeHostname:
1667 intptr = &options->canonicalize_hostname;
1668 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001669 goto parse_multistate;
1670
Damien Miller38505592013-10-17 11:48:13 +11001671 case oCanonicalizeMaxDots:
1672 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001673 goto parse_int;
1674
Damien Miller38505592013-10-17 11:48:13 +11001675 case oCanonicalizeFallbackLocal:
1676 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001677 goto parse_flag;
1678
Damien Miller7acefbb2014-07-18 14:11:24 +10001679 case oStreamLocalBindMask:
1680 arg = strdelim(&s);
1681 if (!arg || *arg == '\0')
1682 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1683 /* Parse mode in octal format */
1684 value = strtol(arg, &endofnumber, 8);
1685 if (arg == endofnumber || value < 0 || value > 0777)
1686 fatal("%.200s line %d: Bad mask.", filename, linenum);
1687 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1688 break;
1689
1690 case oStreamLocalBindUnlink:
1691 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1692 goto parse_flag;
1693
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001694 case oRevokedHostKeys:
1695 charptr = &options->revoked_host_keys;
1696 goto parse_string;
1697
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001698 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001699 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001700 arg = strdelim(&s);
1701 if (!arg || *arg == '\0')
1702 fatal("%.200s line %d: Missing argument.",
1703 filename, linenum);
1704 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1705 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1706 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001707 if (*activep && *intptr == -1)
1708 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001709 break;
1710
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001711 case oUpdateHostkeys:
1712 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001713 multistate_ptr = multistate_yesnoask;
1714 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001715
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001716 case oHostbasedKeyTypes:
1717 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001718 goto parse_keytypes;
1719
1720 case oPubkeyAcceptedKeyTypes:
1721 charptr = &options->pubkey_key_types;
1722 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001723
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001724 case oAddKeysToAgent:
1725 intptr = &options->add_keys_to_agent;
1726 multistate_ptr = multistate_yesnoaskconfirm;
1727 goto parse_multistate;
1728
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001729 case oIdentityAgent:
1730 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001731 arg = strdelim(&s);
1732 if (!arg || *arg == '\0')
1733 fatal("%.200s line %d: Missing argument.",
1734 filename, linenum);
1735 /* Extra validation if the string represents an env var. */
1736 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1737 fatal("%.200s line %d: Invalid environment name %s.",
1738 filename, linenum, arg);
1739 }
1740 if (*activep && *charptr == NULL)
1741 *charptr = xstrdup(arg);
1742 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001743
Ben Lindstrom4daea862002-06-09 20:04:02 +00001744 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001745 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001746 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001747 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001748
Damien Millerf9b3feb2003-05-16 11:38:32 +10001749 case oUnsupported:
1750 error("%s line %d: Unsupported option \"%s\"",
1751 filename, linenum, keyword);
1752 return 0;
1753
Damien Miller95def091999-11-25 00:26:21 +11001754 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001755 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001756 }
1757
1758 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001759 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001760 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001761 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001762 }
Damien Miller95def091999-11-25 00:26:21 +11001763 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001764}
1765
Damien Miller5428f641999-11-25 11:54:57 +11001766/*
1767 * Reads the config file and modifies the options accordingly. Options
1768 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001769 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001770 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001771int
Damien Miller194fd902013-10-15 12:13:05 +11001772read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001773 const char *original_host, Options *options, int flags,
1774 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001775{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001776 int active = 1;
1777
1778 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001779 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001780}
1781
1782#define READCONF_MAX_DEPTH 16
1783static int
1784read_config_file_depth(const char *filename, struct passwd *pw,
1785 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001786 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001787{
Damien Miller95def091999-11-25 00:26:21 +11001788 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001789 char *line = NULL;
1790 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001791 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001792 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001793
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001794 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1795 fatal("Too many recursive configuration includes");
1796
Damien Miller57a44762004-04-20 20:11:57 +10001797 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001798 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799
Darren Tuckeraefa3682013-04-05 11:18:35 +11001800 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001801 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001802
Damien Miller33793852004-06-15 10:27:55 +10001803 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001804 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001805 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001806 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001807 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001808 }
1809
Damien Miller95def091999-11-25 00:26:21 +11001810 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Miller5428f641999-11-25 11:54:57 +11001812 /*
1813 * Mark that we are now processing the options. This flag is turned
1814 * on/off by Host specifications.
1815 */
Damien Miller95def091999-11-25 00:26:21 +11001816 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001817 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001818 /* Update line number counter. */
1819 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001820 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001821 line, filename, linenum, activep, flags, want_final_pass,
1822 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001823 bad_options++;
1824 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001825 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001826 fclose(f);
1827 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001828 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001829 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001830 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831}
1832
Damien Miller13f97b22014-02-24 15:57:55 +11001833/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1834int
1835option_clear_or_none(const char *o)
1836{
1837 return o == NULL || strcasecmp(o, "none") == 0;
1838}
1839
Damien Miller5428f641999-11-25 11:54:57 +11001840/*
1841 * Initializes options to special values that indicate that they have not yet
1842 * been set. Read_config_file will only set options with this value. Options
1843 * are processed in the following order: command line, user config file,
1844 * system config file. Last, fill_default_options is called.
1845 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846
Damien Miller4af51302000-04-16 11:18:38 +10001847void
Damien Miller95def091999-11-25 00:26:21 +11001848initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001849{
Damien Miller95def091999-11-25 00:26:21 +11001850 memset(options, 'X', sizeof(*options));
1851 options->forward_agent = -1;
1852 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001853 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001854 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001855 options->stdio_forward_host = NULL;
1856 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001857 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001858 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001859 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001860 options->fwd_opts.gateway_ports = -1;
1861 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1862 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001863 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001864 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001865 options->gss_authentication = -1;
1866 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001867 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001868 options->kbd_interactive_authentication = -1;
1869 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001870 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001871 options->batch_mode = -1;
1872 options->check_host_ip = -1;
1873 options->strict_host_key_checking = -1;
1874 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001875 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001876 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001877 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001878 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001879 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001880 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001881 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001882 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001883 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001884 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001885 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001886 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001887 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001888 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001889 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001890 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001891 options->jump_user = NULL;
1892 options->jump_host = NULL;
1893 options->jump_port = -1;
1894 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001895 options->user = NULL;
1896 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001897 options->num_system_hostfiles = 0;
1898 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001899 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001900 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001901 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001902 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001903 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001904 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001905 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001906 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001907 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001908 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001909 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001910 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001911 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001912 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001913 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001914 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001915 options->server_alive_interval = -1;
1916 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001917 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001918 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001919 options->setenv = NULL;
1920 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001921 options->control_path = NULL;
1922 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001923 options->control_persist = -1;
1924 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001925 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001926 options->tun_open = -1;
1927 options->tun_local = -1;
1928 options->tun_remote = -1;
1929 options->local_command = NULL;
1930 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001931 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001932 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001933 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001934 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001935 options->ip_qos_interactive = -1;
1936 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001937 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001938 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001939 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001940 options->num_canonical_domains = 0;
1941 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001942 options->canonicalize_max_dots = -1;
1943 options->canonicalize_fallback_local = -1;
1944 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001945 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001946 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001947 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001948 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001949 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950}
1951
Damien Miller5428f641999-11-25 11:54:57 +11001952/*
Damien Miller13f97b22014-02-24 15:57:55 +11001953 * A petite version of fill_default_options() that just fills the options
1954 * needed for hostname canonicalization to proceed.
1955 */
1956void
1957fill_default_options_for_canonicalization(Options *options)
1958{
1959 if (options->canonicalize_max_dots == -1)
1960 options->canonicalize_max_dots = 1;
1961 if (options->canonicalize_fallback_local == -1)
1962 options->canonicalize_fallback_local = 1;
1963 if (options->canonicalize_hostname == -1)
1964 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1965}
1966
1967/*
Damien Miller5428f641999-11-25 11:54:57 +11001968 * Called after processing other sources of option data, this fills those
1969 * options for which no value has been specified with their default values.
1970 */
Damien Miller4af51302000-04-16 11:18:38 +10001971void
Damien Miller95def091999-11-25 00:26:21 +11001972fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001973{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001974 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001975 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001976
Damien Miller95def091999-11-25 00:26:21 +11001977 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001978 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001979 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001980 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001981 if (options->forward_x11_trusted == -1)
1982 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001983 if (options->forward_x11_timeout == -1)
1984 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001985 /*
1986 * stdio forwarding (-W) changes the default for these but we defer
1987 * setting the values so they can be overridden.
1988 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001989 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001990 options->exit_on_forward_failure =
1991 options->stdio_forward_host != NULL ? 1 : 0;
1992 if (options->clear_forwardings == -1)
1993 options->clear_forwardings =
1994 options->stdio_forward_host != NULL ? 1 : 0;
1995 if (options->clear_forwardings == 1)
1996 clear_forwardings(options);
1997
Damien Millerd3a18572000-06-07 19:55:44 +10001998 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001999 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10002000 if (options->fwd_opts.gateway_ports == -1)
2001 options->fwd_opts.gateway_ports = 0;
2002 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2003 options->fwd_opts.streamlocal_bind_mask = 0177;
2004 if (options->fwd_opts.streamlocal_bind_unlink == -1)
2005 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002006 if (options->pubkey_authentication == -1)
2007 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00002008 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002009 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002010 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002011 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002012 if (options->gss_deleg_creds == -1)
2013 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002014 if (options->password_authentication == -1)
2015 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002016 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002017 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002018 if (options->hostbased_authentication == -1)
2019 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002020 if (options->batch_mode == -1)
2021 options->batch_mode = 0;
2022 if (options->check_host_ip == -1)
2023 options->check_host_ip = 1;
2024 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002025 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002026 if (options->compression == -1)
2027 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002028 if (options->tcp_keep_alive == -1)
2029 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002030 if (options->port == -1)
2031 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002032 if (options->address_family == -1)
2033 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002034 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002035 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002036 if (options->number_of_password_prompts == -1)
2037 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002038 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002039 if (options->add_keys_to_agent == -1)
2040 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002041 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002042 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2043 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002044#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002045 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002046#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002047 add_identity_file(options, "~/",
2048 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002049 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002050 }
Damien Miller95def091999-11-25 00:26:21 +11002051 if (options->escape_char == -1)
2052 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002053 if (options->num_system_hostfiles == 0) {
2054 options->system_hostfiles[options->num_system_hostfiles++] =
2055 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2056 options->system_hostfiles[options->num_system_hostfiles++] =
2057 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2058 }
2059 if (options->num_user_hostfiles == 0) {
2060 options->user_hostfiles[options->num_user_hostfiles++] =
2061 xstrdup(_PATH_SSH_USER_HOSTFILE);
2062 options->user_hostfiles[options->num_user_hostfiles++] =
2063 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2064 }
Damien Millerfcd93202002-02-05 12:26:34 +11002065 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002066 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002067 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2068 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002069 if (options->no_host_authentication_for_localhost == - 1)
2070 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002071 if (options->identities_only == -1)
2072 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002073 if (options->enable_ssh_keysign == -1)
2074 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002075 if (options->rekey_limit == -1)
2076 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002077 if (options->rekey_interval == -1)
2078 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002079 if (options->verify_host_key_dns == -1)
2080 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002081 if (options->server_alive_interval == -1)
2082 options->server_alive_interval = 0;
2083 if (options->server_alive_count_max == -1)
2084 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002085 if (options->control_master == -1)
2086 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002087 if (options->control_persist == -1) {
2088 options->control_persist = 0;
2089 options->control_persist_timeout = 0;
2090 }
Damien Millere1776152005-03-01 21:47:37 +11002091 if (options->hash_known_hosts == -1)
2092 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002093 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002094 options->tun_open = SSH_TUNMODE_NO;
2095 if (options->tun_local == -1)
2096 options->tun_local = SSH_TUNID_ANY;
2097 if (options->tun_remote == -1)
2098 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002099 if (options->permit_local_command == -1)
2100 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002101 if (options->visual_host_key == -1)
2102 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002103 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002104 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002105 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002106 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002107 if (options->request_tty == -1)
2108 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002109 if (options->proxy_use_fdpass == -1)
2110 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002111 if (options->canonicalize_max_dots == -1)
2112 options->canonicalize_max_dots = 1;
2113 if (options->canonicalize_fallback_local == -1)
2114 options->canonicalize_fallback_local = 1;
2115 if (options->canonicalize_hostname == -1)
2116 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002117 if (options->fingerprint_hash == -1)
2118 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002119 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002120 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002121
2122 /* Expand KEX name lists */
2123 all_cipher = cipher_alg_list(',', 0);
2124 all_mac = mac_alg_list(',');
2125 all_kex = kex_alg_list(',');
2126 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002127 all_sig = sshkey_alg_list(0, 1, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002128#define ASSEMBLE(what, defaults, all) \
2129 do { \
2130 if ((r = kex_assemble_names(&options->what, \
2131 defaults, all)) != 0) \
2132 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2133 } while (0)
djm@openbsd.org9b611302019-02-23 08:20:43 +00002134 ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
2135 ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
2136 ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002137 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
2138 ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002139 ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002140#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002141 free(all_cipher);
2142 free(all_mac);
2143 free(all_kex);
2144 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002145 free(all_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002146
Damien Millere9fc72e2013-10-15 12:14:12 +11002147#define CLEAR_ON_NONE(v) \
2148 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002149 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002150 free(v); \
2151 v = NULL; \
2152 } \
2153 } while(0)
2154 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002155 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002156 CLEAR_ON_NONE(options->proxy_command);
2157 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002158 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org95a80582019-03-01 02:08:50 +00002159 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002160 if (options->jump_host != NULL &&
2161 strcmp(options->jump_host, "none") == 0 &&
2162 options->jump_port == 0 && options->jump_user == NULL) {
2163 free(options->jump_host);
2164 options->jump_host = NULL;
2165 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002166 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002167 /* options->user will be set in the main program if appropriate */
2168 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002169 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002170 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002172
Damien Miller7acefbb2014-07-18 14:11:24 +10002173struct fwdarg {
2174 char *arg;
2175 int ispath;
2176};
2177
2178/*
2179 * parse_fwd_field
2180 * parses the next field in a port forwarding specification.
2181 * sets fwd to the parsed field and advances p past the colon
2182 * or sets it to NULL at end of string.
2183 * returns 0 on success, else non-zero.
2184 */
2185static int
2186parse_fwd_field(char **p, struct fwdarg *fwd)
2187{
2188 char *ep, *cp = *p;
2189 int ispath = 0;
2190
2191 if (*cp == '\0') {
2192 *p = NULL;
2193 return -1; /* end of string */
2194 }
2195
2196 /*
2197 * A field escaped with square brackets is used literally.
2198 * XXX - allow ']' to be escaped via backslash?
2199 */
2200 if (*cp == '[') {
2201 /* find matching ']' */
2202 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2203 if (*ep == '/')
2204 ispath = 1;
2205 }
2206 /* no matching ']' or not at end of field. */
2207 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2208 return -1;
2209 /* NUL terminate the field and advance p past the colon */
2210 *ep++ = '\0';
2211 if (*ep != '\0')
2212 *ep++ = '\0';
2213 fwd->arg = cp + 1;
2214 fwd->ispath = ispath;
2215 *p = ep;
2216 return 0;
2217 }
2218
2219 for (cp = *p; *cp != '\0'; cp++) {
2220 switch (*cp) {
2221 case '\\':
2222 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002223 if (*cp == '\0')
2224 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002225 break;
2226 case '/':
2227 ispath = 1;
2228 break;
2229 case ':':
2230 *cp++ = '\0';
2231 goto done;
2232 }
2233 }
2234done:
2235 fwd->arg = *p;
2236 fwd->ispath = ispath;
2237 *p = cp;
2238 return 0;
2239}
2240
Damien Millerf91ee4c2005-03-01 21:24:33 +11002241/*
2242 * parse_forward
2243 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002244 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002245 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2246 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002247 * dynamicfwd == 1
2248 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002249 * returns number of arguments parsed or zero on error
2250 */
2251int
Damien Miller7acefbb2014-07-18 14:11:24 +10002252parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002253{
Damien Miller7acefbb2014-07-18 14:11:24 +10002254 struct fwdarg fwdargs[4];
2255 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002256 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002257
Damien Miller7acefbb2014-07-18 14:11:24 +10002258 memset(fwd, 0, sizeof(*fwd));
2259 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002260
2261 cp = p = xstrdup(fwdspec);
2262
2263 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002264 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002265 cp++;
2266
Damien Miller7acefbb2014-07-18 14:11:24 +10002267 for (i = 0; i < 4; ++i) {
2268 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002269 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002270 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002271
Damien Millerf4b39532008-11-03 19:28:21 +11002272 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002273 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002274 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002275 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002276
2277 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002278 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002279 if (fwdargs[0].ispath) {
2280 fwd->listen_path = xstrdup(fwdargs[0].arg);
2281 fwd->listen_port = PORT_STREAMLOCAL;
2282 } else {
2283 fwd->listen_host = NULL;
2284 fwd->listen_port = a2port(fwdargs[0].arg);
2285 }
Damien Millera699d952008-11-03 19:27:34 +11002286 fwd->connect_host = xstrdup("socks");
2287 break;
2288
2289 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002290 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2291 fwd->listen_path = xstrdup(fwdargs[0].arg);
2292 fwd->listen_port = PORT_STREAMLOCAL;
2293 fwd->connect_path = xstrdup(fwdargs[1].arg);
2294 fwd->connect_port = PORT_STREAMLOCAL;
2295 } else if (fwdargs[1].ispath) {
2296 fwd->listen_host = NULL;
2297 fwd->listen_port = a2port(fwdargs[0].arg);
2298 fwd->connect_path = xstrdup(fwdargs[1].arg);
2299 fwd->connect_port = PORT_STREAMLOCAL;
2300 } else {
2301 fwd->listen_host = xstrdup(fwdargs[0].arg);
2302 fwd->listen_port = a2port(fwdargs[1].arg);
2303 fwd->connect_host = xstrdup("socks");
2304 }
Damien Millera699d952008-11-03 19:27:34 +11002305 break;
2306
Damien Millerf91ee4c2005-03-01 21:24:33 +11002307 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002308 if (fwdargs[0].ispath) {
2309 fwd->listen_path = xstrdup(fwdargs[0].arg);
2310 fwd->listen_port = PORT_STREAMLOCAL;
2311 fwd->connect_host = xstrdup(fwdargs[1].arg);
2312 fwd->connect_port = a2port(fwdargs[2].arg);
2313 } else if (fwdargs[2].ispath) {
2314 fwd->listen_host = xstrdup(fwdargs[0].arg);
2315 fwd->listen_port = a2port(fwdargs[1].arg);
2316 fwd->connect_path = xstrdup(fwdargs[2].arg);
2317 fwd->connect_port = PORT_STREAMLOCAL;
2318 } else {
2319 fwd->listen_host = NULL;
2320 fwd->listen_port = a2port(fwdargs[0].arg);
2321 fwd->connect_host = xstrdup(fwdargs[1].arg);
2322 fwd->connect_port = a2port(fwdargs[2].arg);
2323 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002324 break;
2325
2326 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002327 fwd->listen_host = xstrdup(fwdargs[0].arg);
2328 fwd->listen_port = a2port(fwdargs[1].arg);
2329 fwd->connect_host = xstrdup(fwdargs[2].arg);
2330 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002331 break;
2332 default:
2333 i = 0; /* failure */
2334 }
2335
Darren Tuckera627d422013-06-02 07:31:17 +10002336 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002337
Damien Millera699d952008-11-03 19:27:34 +11002338 if (dynamicfwd) {
2339 if (!(i == 1 || i == 2))
2340 goto fail_free;
2341 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002342 if (!(i == 3 || i == 4)) {
2343 if (fwd->connect_path == NULL &&
2344 fwd->listen_path == NULL)
2345 goto fail_free;
2346 }
2347 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002348 goto fail_free;
2349 }
2350
Damien Miller7acefbb2014-07-18 14:11:24 +10002351 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2352 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002353 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002354 if (fwd->connect_host != NULL &&
2355 strlen(fwd->connect_host) >= NI_MAXHOST)
2356 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002357 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2358 if (fwd->connect_path != NULL &&
2359 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2360 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002361 if (fwd->listen_host != NULL &&
2362 strlen(fwd->listen_host) >= NI_MAXHOST)
2363 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002364 if (fwd->listen_path != NULL &&
2365 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2366 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002367
2368 return (i);
2369
2370 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002371 free(fwd->connect_host);
2372 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002373 free(fwd->connect_path);
2374 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002375 free(fwd->listen_host);
2376 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002377 free(fwd->listen_path);
2378 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002379 return (0);
2380}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002381
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002382int
2383parse_jump(const char *s, Options *o, int active)
2384{
2385 char *orig, *sdup, *cp;
2386 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002387 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002388
2389 active &= o->proxy_command == NULL && o->jump_host == NULL;
2390
2391 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002392 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002393 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002394 if (strcasecmp(s, "none") == 0)
2395 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002396 if ((cp = strrchr(sdup, ',')) == NULL)
2397 cp = sdup; /* last */
2398 else
2399 *cp++ = '\0';
2400
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002401 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002402 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002403 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2404 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002405 goto out;
2406 } else {
2407 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002408 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2409 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002410 goto out;
2411 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002412 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002413 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002414 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002415 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002416 if (strcasecmp(s, "none") == 0) {
2417 o->jump_host = xstrdup("none");
2418 o->jump_port = 0;
2419 } else {
2420 o->jump_user = user;
2421 o->jump_host = host;
2422 o->jump_port = port;
2423 o->proxy_command = xstrdup("none");
2424 user = host = NULL;
2425 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2426 o->jump_extra = xstrdup(s);
2427 o->jump_extra[cp - s] = '\0';
2428 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002429 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002430 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002431 ret = 0;
2432 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002433 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002434 free(user);
2435 free(host);
2436 return ret;
2437}
2438
millert@openbsd.org887669e2017-10-21 23:06:24 +00002439int
2440parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2441{
2442 char *path;
2443 int r;
2444
2445 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2446 if (r == 0 && path != NULL)
2447 r = -1; /* path not allowed */
2448 return r;
2449}
2450
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002451/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2452static const char *
2453fmt_multistate_int(int val, const struct multistate *m)
2454{
2455 u_int i;
2456
2457 for (i = 0; m[i].key != NULL; i++) {
2458 if (m[i].value == val)
2459 return m[i].key;
2460 }
2461 return "UNKNOWN";
2462}
2463
2464static const char *
2465fmt_intarg(OpCodes code, int val)
2466{
2467 if (val == -1)
2468 return "unset";
2469 switch (code) {
2470 case oAddressFamily:
2471 return fmt_multistate_int(val, multistate_addressfamily);
2472 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002473 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002474 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002475 case oStrictHostKeyChecking:
2476 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002477 case oControlMaster:
2478 return fmt_multistate_int(val, multistate_controlmaster);
2479 case oTunnel:
2480 return fmt_multistate_int(val, multistate_tunnel);
2481 case oRequestTTY:
2482 return fmt_multistate_int(val, multistate_requesttty);
2483 case oCanonicalizeHostname:
2484 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002485 case oAddKeysToAgent:
2486 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002487 case oFingerprintHash:
2488 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002489 default:
2490 switch (val) {
2491 case 0:
2492 return "no";
2493 case 1:
2494 return "yes";
2495 default:
2496 return "UNKNOWN";
2497 }
2498 }
2499}
2500
2501static const char *
2502lookup_opcode_name(OpCodes code)
2503{
2504 u_int i;
2505
2506 for (i = 0; keywords[i].name != NULL; i++)
2507 if (keywords[i].opcode == code)
2508 return(keywords[i].name);
2509 return "UNKNOWN";
2510}
2511
2512static void
2513dump_cfg_int(OpCodes code, int val)
2514{
2515 printf("%s %d\n", lookup_opcode_name(code), val);
2516}
2517
2518static void
2519dump_cfg_fmtint(OpCodes code, int val)
2520{
2521 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2522}
2523
2524static void
2525dump_cfg_string(OpCodes code, const char *val)
2526{
2527 if (val == NULL)
2528 return;
2529 printf("%s %s\n", lookup_opcode_name(code), val);
2530}
2531
2532static void
2533dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2534{
2535 u_int i;
2536
2537 for (i = 0; i < count; i++)
2538 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2539}
2540
2541static void
2542dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2543{
2544 u_int i;
2545
2546 printf("%s", lookup_opcode_name(code));
2547 for (i = 0; i < count; i++)
2548 printf(" %s", vals[i]);
2549 printf("\n");
2550}
2551
2552static void
2553dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2554{
2555 const struct Forward *fwd;
2556 u_int i;
2557
2558 /* oDynamicForward */
2559 for (i = 0; i < count; i++) {
2560 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002561 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002562 strcmp(fwd->connect_host, "socks") != 0)
2563 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002564 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002565 strcmp(fwd->connect_host, "socks") == 0)
2566 continue;
2567 printf("%s", lookup_opcode_name(code));
2568 if (fwd->listen_port == PORT_STREAMLOCAL)
2569 printf(" %s", fwd->listen_path);
2570 else if (fwd->listen_host == NULL)
2571 printf(" %d", fwd->listen_port);
2572 else {
2573 printf(" [%s]:%d",
2574 fwd->listen_host, fwd->listen_port);
2575 }
2576 if (code != oDynamicForward) {
2577 if (fwd->connect_port == PORT_STREAMLOCAL)
2578 printf(" %s", fwd->connect_path);
2579 else if (fwd->connect_host == NULL)
2580 printf(" %d", fwd->connect_port);
2581 else {
2582 printf(" [%s]:%d",
2583 fwd->connect_host, fwd->connect_port);
2584 }
2585 }
2586 printf("\n");
2587 }
2588}
2589
2590void
2591dump_client_config(Options *o, const char *host)
2592{
2593 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002594 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002595
djm@openbsd.org60a92472015-08-21 23:53:08 +00002596 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002597 all_key = sshkey_alg_list(0, 0, 1, ',');
2598 if (kex_assemble_names( &o->hostkeyalgorithms,
2599 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002600 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002601 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002602
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002603 /* Most interesting options first: user, host, port */
2604 dump_cfg_string(oUser, o->user);
jmc@openbsd.org73491492019-06-12 11:31:50 +00002605 dump_cfg_string(oHostname, host);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002606 dump_cfg_int(oPort, o->port);
2607
2608 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002609 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002610 dump_cfg_fmtint(oAddressFamily, o->address_family);
2611 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2612 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2613 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2614 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2615 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2616 dump_cfg_fmtint(oCompression, o->compression);
2617 dump_cfg_fmtint(oControlMaster, o->control_master);
2618 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002619 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002620 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002621 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002622 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2623 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2624 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2625 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2626#ifdef GSSAPI
2627 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2628 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2629#endif /* GSSAPI */
2630 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2631 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2632 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2633 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2634 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2635 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2636 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002637 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2638 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2639 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002640 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2641 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2642 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2643 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002644 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2645 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002646 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002647
2648 /* Integer options */
2649 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002650 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2651 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2652 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2653 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2654 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2655
2656 /* String options */
2657 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002658 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002659 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2660 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002661 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002662 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002663 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002664 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002665 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002666 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2667 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002668 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002669 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002670 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002671 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2672 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002673#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002674 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002675#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002676 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002677 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002678 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002679 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002680
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002681 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002682 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2683 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2684 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2685
2686 /* String array options */
2687 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2688 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002689 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002690 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2691 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2692 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002693 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002694
2695 /* Special cases */
2696
2697 /* oConnectTimeout */
2698 if (o->connection_timeout == -1)
2699 printf("connecttimeout none\n");
2700 else
2701 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2702
2703 /* oTunnelDevice */
2704 printf("tunneldevice");
2705 if (o->tun_local == SSH_TUNID_ANY)
2706 printf(" any");
2707 else
2708 printf(" %d", o->tun_local);
2709 if (o->tun_remote == SSH_TUNID_ANY)
2710 printf(":any");
2711 else
2712 printf(":%d", o->tun_remote);
2713 printf("\n");
2714
2715 /* oCanonicalizePermittedCNAMEs */
2716 if ( o->num_permitted_cnames > 0) {
2717 printf("canonicalizePermittedcnames");
2718 for (i = 0; i < o->num_permitted_cnames; i++) {
2719 printf(" %s:%s", o->permitted_cnames[i].source_list,
2720 o->permitted_cnames[i].target_list);
2721 }
2722 printf("\n");
2723 }
2724
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002725 /* oControlPersist */
2726 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2727 dump_cfg_fmtint(oControlPersist, o->control_persist);
2728 else
2729 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2730
2731 /* oEscapeChar */
2732 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2733 printf("escapechar none\n");
2734 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002735 vis(buf, o->escape_char, VIS_WHITE, 0);
2736 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002737 }
2738
2739 /* oIPQoS */
2740 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2741 printf("%s\n", iptos2str(o->ip_qos_bulk));
2742
2743 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002744 printf("rekeylimit %llu %d\n",
2745 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002746
2747 /* oStreamLocalBindMask */
2748 printf("streamlocalbindmask 0%o\n",
2749 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002750
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002751 /* oLogFacility */
2752 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2753
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002754 /* oProxyCommand / oProxyJump */
2755 if (o->jump_host == NULL)
2756 dump_cfg_string(oProxyCommand, o->proxy_command);
2757 else {
2758 /* Check for numeric addresses */
2759 i = strchr(o->jump_host, ':') != NULL ||
2760 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2761 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2762 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002763 /* optional additional jump spec */
2764 o->jump_extra == NULL ? "" : o->jump_extra,
2765 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002766 /* optional user */
2767 o->jump_user == NULL ? "" : o->jump_user,
2768 o->jump_user == NULL ? "" : "@",
2769 /* opening [ if hostname is numeric */
2770 i ? "[" : "",
2771 /* mandatory hostname */
2772 o->jump_host,
2773 /* closing ] if hostname is numeric */
2774 i ? "]" : "",
2775 /* optional port number */
2776 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002777 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002778 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002779}