blob: 2afcbaeca3d648153d18cfb487abcc3a41973217 [file] [log] [blame]
dtucker@openbsd.org2c169012020-04-24 03:33:21 +00001/* $OpenBSD: readconf.c,v 1.329 2020/04/24 03:33:21 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000040#include <stdarg.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
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100125 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 IdentityFile ~/.ssh/identity
127 Port 22
128 EscapeChar ~
129
130*/
131
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000132static int read_config_file_depth(const char *filename, struct passwd *pw,
133 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000134 int flags, int *activep, int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000135static int process_config_line_depth(Options *options, struct passwd *pw,
136 const char *host, const char *original_host, char *line,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000137 const char *filename, int linenum, int *activep, int flags,
138 int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000147 oPasswordAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000149 oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000151 oUser, oEscapeChar, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000154 oTCPKeepAlive, oNumberOfPasswordPrompts,
155 oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000159 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000164 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000165 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000166 oTunnel, oTunnelDevice,
167 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000168 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000169 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100170 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
171 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000172 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000173 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000174 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
djm@openbsd.org884416b2019-10-31 21:18:28 +0000175 oSecurityKeyProvider,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000176 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177} OpCodes;
178
179/* Textual representations of the tokens. */
180
Damien Miller95def091999-11-25 00:26:21 +1100181static struct {
182 const char *name;
183 OpCodes opcode;
184} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000185 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000186 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000187 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000188 { "fallbacktorsh", oDeprecated },
189 { "globalknownhostsfile2", oDeprecated },
190 { "rhostsauthentication", oDeprecated },
191 { "userknownhostsfile2", oDeprecated },
192 { "useroaming", oDeprecated },
193 { "usersh", oDeprecated },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000194 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000195
196 /* Unsupported options */
197 { "afstokenpassing", oUnsupported },
198 { "kerberosauthentication", oUnsupported },
199 { "kerberostgtpassing", oUnsupported },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000200 { "rsaauthentication", oUnsupported },
201 { "rhostsrsaauthentication", oUnsupported },
202 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000203
204 /* Sometimes-unsupported options */
205#if defined(GSSAPI)
206 { "gssapiauthentication", oGssAuthentication },
207 { "gssapidelegatecredentials", oGssDelegateCreds },
208# else
209 { "gssapiauthentication", oUnsupported },
210 { "gssapidelegatecredentials", oUnsupported },
211#endif
212#ifdef ENABLE_PKCS11
djm@openbsd.org381a2612017-01-30 00:38:50 +0000213 { "pkcs11provider", oPKCS11Provider },
djm@openbsd.org95a80582019-03-01 02:08:50 +0000214 { "smartcarddevice", oPKCS11Provider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000215# else
216 { "smartcarddevice", oUnsupported },
217 { "pkcs11provider", oUnsupported },
218#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000219
Damien Miller95def091999-11-25 00:26:21 +1100220 { "forwardagent", oForwardAgent },
221 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000222 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000223 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000224 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000225 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "gatewayports", oGatewayPorts },
Damien Miller95def091999-11-25 00:26:21 +1100227 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100228 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
229 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000231 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000232 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000233 { "challengeresponseauthentication", oChallengeResponseAuthentication },
Damien Miller87f08be2018-07-20 13:18:28 +1000234 { "skeyauthentication", oUnsupported },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000235 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100236 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100237 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100238 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000239 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000240 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000241 { "identityagent", oIdentityAgent },
jmc@openbsd.org73491492019-06-12 11:31:50 +0000242 { "hostname", oHostname },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000243 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100244 { "proxycommand", oProxyCommand },
245 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000246 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000247 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100248 { "remoteforward", oRemoteForward },
249 { "localforward", oLocalForward },
250 { "user", oUser },
251 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100252 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100253 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100254 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100255 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100256 { "connectionattempts", oConnectionAttempts },
257 { "batchmode", oBatchMode },
258 { "checkhostip", oCheckHostIP },
259 { "stricthostkeychecking", oStrictHostKeyChecking },
260 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100261 { "tcpkeepalive", oTCPKeepAlive },
262 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100263 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000264 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100265 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000266 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000267 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000268 { "hostkeyalgorithms", oHostKeyAlgorithms },
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000269 { "casignaturealgorithms", oCASignatureAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000270 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000271 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100272 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000273 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000274 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100275 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000276 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000277 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000278 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100279 { "serveraliveinterval", oServerAliveInterval },
280 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000281 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000282 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000283 { "controlpath", oControlPath },
284 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000285 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100286 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000287 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100288 { "tunnel", oTunnel },
289 { "tunneldevice", oTunnelDevice },
290 { "localcommand", oLocalCommand },
291 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000292 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000293 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000294 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100295 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000296 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000297 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100298 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100299 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
300 { "canonicalizehostname", oCanonicalizeHostname },
301 { "canonicalizemaxdots", oCanonicalizeMaxDots },
302 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000303 { "streamlocalbindmask", oStreamLocalBindMask },
304 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000305 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000306 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000307 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000308 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000309 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000310 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000311 { "proxyjump", oProxyJump },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000312 { "securitykeyprovider", oSecurityKeyProvider },
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
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000317static char *kex_default_pk_alg_filtered;
318
319const char *
320kex_default_pk_alg(void)
321{
322 if (kex_default_pk_alg_filtered == NULL)
323 fatal("kex_default_pk_alg not initialized.");
324 return kex_default_pk_alg_filtered;
325}
326
dtucker@openbsd.orged833da2020-04-03 02:27:12 +0000327char *
328ssh_connection_hash(const char *thishost, const char *host, const char *portstr,
329 const char *user)
330{
331 struct ssh_digest_ctx *md;
332 u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
333
334 if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
335 ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
336 ssh_digest_update(md, host, strlen(host)) < 0 ||
337 ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
338 ssh_digest_update(md, user, strlen(user)) < 0 ||
339 ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
340 fatal("%s: mux digest failed", __func__);
341 ssh_digest_free(md);
342 return tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
343}
344
Damien Miller5428f641999-11-25 11:54:57 +1100345/*
346 * Adds a local TCP/IP port forward to options. Never returns if there is an
347 * error.
348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller4af51302000-04-16 11:18:38 +1000350void
Damien Miller7acefbb2014-07-18 14:11:24 +1000351add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Miller7acefbb2014-07-18 14:11:24 +1000353 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000354 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000355
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000356 /* Don't add duplicates */
357 for (i = 0; i < options->num_local_forwards; i++) {
358 if (forward_equals(newfwd, options->local_forwards + i))
359 return;
360 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000361 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000362 options->num_local_forwards + 1,
363 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100364 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365
Damien Miller1a0442f2008-11-05 16:30:06 +1100366 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000368 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100369 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000371 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
Damien Miller5428f641999-11-25 11:54:57 +1100374/*
375 * Adds a remote TCP/IP port forward to options. Never returns if there is
376 * an error.
377 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378
Damien Miller4af51302000-04-16 11:18:38 +1000379void
Damien Miller7acefbb2014-07-18 14:11:24 +1000380add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381{
Damien Miller7acefbb2014-07-18 14:11:24 +1000382 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000383 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000384
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000385 /* Don't add duplicates */
386 for (i = 0; i < options->num_remote_forwards; i++) {
387 if (forward_equals(newfwd, options->remote_forwards + i))
388 return;
389 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000390 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000391 options->num_remote_forwards + 1,
392 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100393 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100394
Damien Miller1a0442f2008-11-05 16:30:06 +1100395 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100396 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100398 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100399 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000400 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100401 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000402 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403}
404
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405static void
406clear_forwardings(Options *options)
407{
408 int i;
409
Damien Millerf91ee4c2005-03-01 21:24:33 +1100410 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000411 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000412 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000413 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000414 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100415 }
Damien Miller232cfb12010-06-26 09:50:30 +1000416 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000417 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000418 options->local_forwards = NULL;
419 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000420 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100421 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000422 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000423 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000424 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000425 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100426 }
Damien Miller232cfb12010-06-26 09:50:30 +1000427 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000428 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000429 options->remote_forwards = NULL;
430 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000431 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100432 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000433}
434
Darren Tucker19104782013-04-05 11:13:08 +1100435void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000436add_certificate_file(Options *options, const char *path, int userprovided)
437{
438 int i;
439
440 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
441 fatal("Too many certificate files specified (max %d)",
442 SSH_MAX_CERTIFICATE_FILES);
443
444 /* Avoid registering duplicates */
445 for (i = 0; i < options->num_certificate_files; i++) {
446 if (options->certificate_file_userprovided[i] == userprovided &&
447 strcmp(options->certificate_files[i], path) == 0) {
448 debug2("%s: ignoring duplicate key %s", __func__, path);
449 return;
450 }
451 }
452
453 options->certificate_file_userprovided[options->num_certificate_files] =
454 userprovided;
455 options->certificate_files[options->num_certificate_files++] =
456 xstrdup(path);
457}
458
459void
Darren Tucker19104782013-04-05 11:13:08 +1100460add_identity_file(Options *options, const char *dir, const char *filename,
461 int userprovided)
462{
463 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000464 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100465
466 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
467 fatal("Too many identity files specified (max %d)",
468 SSH_MAX_IDENTITY_FILES);
469
470 if (dir == NULL) /* no dir, filename is absolute */
471 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000472 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
473 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100474
Damien Miller15271902014-05-15 13:47:56 +1000475 /* Avoid registering duplicates */
476 for (i = 0; i < options->num_identity_files; i++) {
477 if (options->identity_file_userprovided[i] == userprovided &&
478 strcmp(options->identity_files[i], path) == 0) {
479 debug2("%s: ignoring duplicate key %s", __func__, path);
480 free(path);
481 return;
482 }
483 }
484
Darren Tucker19104782013-04-05 11:13:08 +1100485 options->identity_file_userprovided[options->num_identity_files] =
486 userprovided;
487 options->identity_files[options->num_identity_files++] = path;
488}
489
Damien Miller194fd902013-10-15 12:13:05 +1100490int
491default_ssh_port(void)
492{
493 static int port;
494 struct servent *sp;
495
496 if (port == 0) {
497 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
498 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
499 }
500 return port;
501}
502
503/*
504 * Execute a command in a shell.
505 * Return its exit status or -1 on abnormal exit.
506 */
507static int
508execute_in_shell(const char *cmd)
509{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000510 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100511 pid_t pid;
512 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100513
514 if ((shell = getenv("SHELL")) == NULL)
515 shell = _PATH_BSHELL;
516
djm@openbsd.org9ab5b942019-08-09 05:05:54 +0000517 if (access(shell, X_OK) == -1) {
518 fatal("Shell \"%s\" is not executable: %s",
519 shell, strerror(errno));
520 }
521
Damien Miller194fd902013-10-15 12:13:05 +1100522 /* Need this to redirect subprocess stdin/out */
523 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
524 fatal("open(/dev/null): %s", strerror(errno));
525
526 debug("Executing command: '%.500s'", cmd);
527
528 /* Fork and execute the command. */
529 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000530 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100531
Damien Miller194fd902013-10-15 12:13:05 +1100532 /* Redirect child stdin and stdout. Leave stderr */
533 if (dup2(devnull, STDIN_FILENO) == -1)
534 fatal("dup2: %s", strerror(errno));
535 if (dup2(devnull, STDOUT_FILENO) == -1)
536 fatal("dup2: %s", strerror(errno));
537 if (devnull > STDERR_FILENO)
538 close(devnull);
539 closefrom(STDERR_FILENO + 1);
540
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000541 argv[0] = shell;
542 argv[1] = "-c";
543 argv[2] = xstrdup(cmd);
544 argv[3] = NULL;
545
Damien Miller194fd902013-10-15 12:13:05 +1100546 execv(argv[0], argv);
547 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
548 /* Die with signal to make this error apparent to parent. */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000549 ssh_signal(SIGTERM, SIG_DFL);
Damien Miller194fd902013-10-15 12:13:05 +1100550 kill(getpid(), SIGTERM);
551 _exit(1);
552 }
553 /* Parent. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000554 if (pid == -1)
Damien Miller194fd902013-10-15 12:13:05 +1100555 fatal("%s: fork: %.100s", __func__, strerror(errno));
556
557 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100558
559 while (waitpid(pid, &status, 0) == -1) {
560 if (errno != EINTR && errno != EAGAIN)
561 fatal("%s: waitpid: %s", __func__, strerror(errno));
562 }
563 if (!WIFEXITED(status)) {
564 error("command '%.100s' exited abnormally", cmd);
565 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000566 }
Damien Miller194fd902013-10-15 12:13:05 +1100567 debug3("command returned status %d", WEXITSTATUS(status));
568 return WEXITSTATUS(status);
569}
570
571/*
572 * Parse and execute a Match directive.
573 */
574static int
575match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000576 const char *host_arg, const char *original_host, int final_pass,
577 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100578{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100580 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100582 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000583 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100584
585 /*
586 * Configuration is likely to be incomplete at this point so we
587 * must be prepared to use default values.
588 */
589 port = options->port <= 0 ? default_ssh_port() : options->port;
590 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000591 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000592 host = xstrdup(options->hostname);
593 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100594 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100595 host = percent_expand(options->hostname,
596 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000597 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100598 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000599 }
Damien Miller194fd902013-10-15 12:13:05 +1100600
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000601 debug2("checking match for '%s' host %s originally %s",
602 cp, host, original_host);
603 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
604 criteria = NULL;
605 this_result = 1;
606 if ((negate = attrib[0] == '!'))
607 attrib++;
608 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100609 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100611 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000612 error("%.200s line %d: '%s' cannot be combined "
613 "with other Match attributes",
614 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100615 result = -1;
616 goto out;
617 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 if (result)
619 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100620 goto out;
621 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000622 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000623 if (strcasecmp(attrib, "canonical") == 0 ||
624 strcasecmp(attrib, "final") == 0) {
625 /*
626 * If the config requests "Match final" then remember
627 * this so we can perform a second pass later.
628 */
629 if (strcasecmp(attrib, "final") == 0 &&
630 want_final_pass != NULL)
631 *want_final_pass = 1;
632 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000633 if (r == (negate ? 1 : 0))
634 this_result = result = 0;
635 debug3("%.200s line %d: %smatched '%s'",
636 filename, linenum,
637 this_result ? "" : "not ", oattrib);
638 continue;
639 }
640 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100641 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
642 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100643 result = -1;
644 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100645 }
Damien Miller194fd902013-10-15 12:13:05 +1100646 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000647 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000648 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000649 if (r == (negate ? 1 : 0))
650 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100651 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000652 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000653 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000654 if (r == (negate ? 1 : 0))
655 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100656 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000657 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000658 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000659 if (r == (negate ? 1 : 0))
660 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100661 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000662 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000663 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000664 if (r == (negate ? 1 : 0))
665 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100666 } else if (strcasecmp(attrib, "exec") == 0) {
dtucker@openbsd.orged833da2020-04-03 02:27:12 +0000667 char *conn_hash_hex;
668
Damien Miller194fd902013-10-15 12:13:05 +1100669 if (gethostname(thishost, sizeof(thishost)) == -1)
670 fatal("gethostname: %s", strerror(errno));
671 strlcpy(shorthost, thishost, sizeof(shorthost));
672 shorthost[strcspn(thishost, ".")] = '\0';
673 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000674 snprintf(uidstr, sizeof(uidstr), "%llu",
675 (unsigned long long)pw->pw_uid);
dtucker@openbsd.orged833da2020-04-03 02:27:12 +0000676 conn_hash_hex = ssh_connection_hash(thishost, host,
dtucker@openbsd.orgeece2432020-04-03 03:12:11 +0000677 portstr, ruser);
Damien Miller194fd902013-10-15 12:13:05 +1100678
679 cmd = percent_expand(arg,
dtucker@openbsd.orged833da2020-04-03 02:27:12 +0000680 "C", conn_hash_hex,
Damien Miller194fd902013-10-15 12:13:05 +1100681 "L", shorthost,
682 "d", pw->pw_dir,
683 "h", host,
684 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000685 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100686 "p", portstr,
687 "r", ruser,
688 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000689 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100690 (char *)NULL);
dtucker@openbsd.orged833da2020-04-03 02:27:12 +0000691 free(conn_hash_hex);
Damien Miller06287802014-02-24 15:56:45 +1100692 if (result != 1) {
693 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000694 debug3("%.200s line %d: skipped exec "
695 "\"%.100s\"", filename, linenum, cmd);
696 free(cmd);
697 continue;
Damien Miller06287802014-02-24 15:56:45 +1100698 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000699 r = execute_in_shell(cmd);
700 if (r == -1) {
701 fatal("%.200s line %d: match exec "
702 "'%.100s' error", filename,
703 linenum, cmd);
704 }
705 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100706 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000707 /* Force exit status to boolean */
708 r = r == 0;
709 if (r == (negate ? 1 : 0))
710 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100711 } else {
712 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100713 result = -1;
714 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100715 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000716 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
717 filename, linenum, this_result ? "": "not ",
718 oattrib, criteria);
719 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100720 }
Damien Millercf31f382013-10-24 21:02:56 +1100721 if (attributes == 0) {
722 error("One or more attributes required for Match");
723 result = -1;
724 goto out;
725 }
Damien Miller084bcd22013-10-23 16:30:51 +1100726 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000727 if (result != -1)
728 debug2("match %sfound", result ? "" : "not ");
729 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100730 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100731 return result;
732}
733
djm@openbsd.org555294a2018-04-06 13:02:39 +0000734/* Remove environment variable by pattern */
735static void
736rm_env(Options *options, const char *arg, const char *filename, int linenum)
737{
738 int i, j;
739 char *cp;
740
741 /* Remove an environment variable */
742 for (i = 0; i < options->num_send_env; ) {
743 cp = xstrdup(options->send_env[i]);
744 if (!match_pattern(cp, arg + 1)) {
745 free(cp);
746 i++;
747 continue;
748 }
749 debug3("%s line %d: removing environment %s",
750 filename, linenum, cp);
751 free(cp);
752 free(options->send_env[i]);
753 options->send_env[i] = NULL;
754 for (j = i; j < options->num_send_env - 1; j++) {
755 options->send_env[j] = options->send_env[j + 1];
756 options->send_env[j + 1] = NULL;
757 }
758 options->num_send_env--;
759 /* NB. don't increment i */
760 }
761}
762
Damien Miller5428f641999-11-25 11:54:57 +1100763/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000764 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100765 */
Damien Miller4af51302000-04-16 11:18:38 +1000766static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000767parse_token(const char *cp, const char *filename, int linenum,
768 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769{
Darren Tucker07636982013-05-16 20:30:03 +1000770 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771
Damien Miller95def091999-11-25 00:26:21 +1100772 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000773 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100774 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000775 if (ignored_unknown != NULL &&
776 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000777 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000778 error("%s: line %d: Bad configuration option: %s",
779 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100780 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781}
782
Damien Millere9fc72e2013-10-15 12:14:12 +1100783/* Multistate option parsing */
784struct multistate {
785 char *key;
786 int value;
787};
788static const struct multistate multistate_flag[] = {
789 { "true", 1 },
790 { "false", 0 },
791 { "yes", 1 },
792 { "no", 0 },
793 { NULL, -1 }
794};
795static const struct multistate multistate_yesnoask[] = {
796 { "true", 1 },
797 { "false", 0 },
798 { "yes", 1 },
799 { "no", 0 },
800 { "ask", 2 },
801 { NULL, -1 }
802};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000803static const struct multistate multistate_strict_hostkey[] = {
804 { "true", SSH_STRICT_HOSTKEY_YES },
805 { "false", SSH_STRICT_HOSTKEY_OFF },
806 { "yes", SSH_STRICT_HOSTKEY_YES },
807 { "no", SSH_STRICT_HOSTKEY_OFF },
808 { "ask", SSH_STRICT_HOSTKEY_ASK },
809 { "off", SSH_STRICT_HOSTKEY_OFF },
810 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
811 { NULL, -1 }
812};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000813static const struct multistate multistate_yesnoaskconfirm[] = {
814 { "true", 1 },
815 { "false", 0 },
816 { "yes", 1 },
817 { "no", 0 },
818 { "ask", 2 },
819 { "confirm", 3 },
820 { NULL, -1 }
821};
Damien Millere9fc72e2013-10-15 12:14:12 +1100822static const struct multistate multistate_addressfamily[] = {
823 { "inet", AF_INET },
824 { "inet6", AF_INET6 },
825 { "any", AF_UNSPEC },
826 { NULL, -1 }
827};
828static const struct multistate multistate_controlmaster[] = {
829 { "true", SSHCTL_MASTER_YES },
830 { "yes", SSHCTL_MASTER_YES },
831 { "false", SSHCTL_MASTER_NO },
832 { "no", SSHCTL_MASTER_NO },
833 { "auto", SSHCTL_MASTER_AUTO },
834 { "ask", SSHCTL_MASTER_ASK },
835 { "autoask", SSHCTL_MASTER_AUTO_ASK },
836 { NULL, -1 }
837};
838static const struct multistate multistate_tunnel[] = {
839 { "ethernet", SSH_TUNMODE_ETHERNET },
840 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
841 { "true", SSH_TUNMODE_DEFAULT },
842 { "yes", SSH_TUNMODE_DEFAULT },
843 { "false", SSH_TUNMODE_NO },
844 { "no", SSH_TUNMODE_NO },
845 { NULL, -1 }
846};
847static const struct multistate multistate_requesttty[] = {
848 { "true", REQUEST_TTY_YES },
849 { "yes", REQUEST_TTY_YES },
850 { "false", REQUEST_TTY_NO },
851 { "no", REQUEST_TTY_NO },
852 { "force", REQUEST_TTY_FORCE },
853 { "auto", REQUEST_TTY_AUTO },
854 { NULL, -1 }
855};
Damien Miller38505592013-10-17 11:48:13 +1100856static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100857 { "true", SSH_CANONICALISE_YES },
858 { "false", SSH_CANONICALISE_NO },
859 { "yes", SSH_CANONICALISE_YES },
860 { "no", SSH_CANONICALISE_NO },
861 { "always", SSH_CANONICALISE_ALWAYS },
862 { NULL, -1 }
863};
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000864static const struct multistate multistate_compression[] = {
865#ifdef WITH_ZLIB
866 { "yes", COMP_ZLIB },
867#endif
868 { "no", COMP_NONE },
869 { NULL, -1 }
870};
Damien Millere9fc72e2013-10-15 12:14:12 +1100871
Damien Miller5428f641999-11-25 11:54:57 +1100872/*
873 * Processes a single option line as used in the configuration files. This
874 * only sets those values that have not already been set.
875 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100876int
Damien Miller194fd902013-10-15 12:13:05 +1100877process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000878 const char *original_host, char *line, const char *filename,
879 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000881 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000882 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000883}
884
885#define WHITESPACE " \t\r\n"
886static int
887process_config_line_depth(Options *options, struct passwd *pw, const char *host,
888 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000889 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000890{
Damien Miller295ee632011-05-29 21:42:31 +1000891 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
892 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000893 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000894 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000895 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100896 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000897 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000898 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100899 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000900 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100901 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100902 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000903 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000904 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905
Damien Miller194fd902013-10-15 12:13:05 +1100906 if (activep == NULL) { /* We are processing a command line directive */
907 cmdline = 1;
908 activep = &cmdline;
909 }
910
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000911 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000912 if ((len = strlen(line)) == 0)
913 return 0;
914 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000915 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000916 break;
917 line[len] = '\0';
918 }
919
Damien Millerbe484b52000-07-15 14:14:16 +1000920 s = line;
921 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100922 if ((keyword = strdelim(&s)) == NULL)
923 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000924 /* Ignore leading whitespace. */
925 if (*keyword == '\0')
926 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000927 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100928 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000929 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100930 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
Darren Tucker07636982013-05-16 20:30:03 +1000932 opcode = parse_token(keyword, filename, linenum,
933 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 switch (opcode) {
936 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100937 /* don't panic, but count bad options */
938 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000939 case oIgnore:
940 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000941 case oIgnoredUnknownOption:
942 debug("%s line %d: Ignored unknown option \"%s\"",
943 filename, linenum, keyword);
944 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000945 case oConnectTimeout:
946 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100947parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000948 arg = strdelim(&s);
949 if (!arg || *arg == '\0')
950 fatal("%s line %d: missing time value.",
951 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000952 if (strcmp(arg, "none") == 0)
953 value = -1;
954 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000955 fatal("%s line %d: invalid time value.",
956 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100957 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000958 *intptr = value;
959 break;
960
Damien Miller95def091999-11-25 00:26:21 +1100961 case oForwardAgent:
962 intptr = &options->forward_agent;
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000963
964 arg = strdelim(&s);
965 if (!arg || *arg == '\0')
966 fatal("%s line %d: missing argument.",
967 filename, linenum);
968
969 value = -1;
970 multistate_ptr = multistate_flag;
971 for (i = 0; multistate_ptr[i].key != NULL; i++) {
972 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
973 value = multistate_ptr[i].value;
974 break;
975 }
976 }
977 if (value != -1) {
978 if (*activep && *intptr == -1)
979 *intptr = value;
980 break;
981 }
982 /* ForwardAgent wasn't 'yes' or 'no', assume a path */
983 if (*activep && *intptr == -1)
984 *intptr = 1;
985
986 charptr = &options->forward_agent_sock_path;
987 goto parse_agent_path;
988
989 case oForwardX11:
990 intptr = &options->forward_x11;
Damien Millere9fc72e2013-10-15 12:14:12 +1100991 parse_flag:
992 multistate_ptr = multistate_flag;
993 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000994 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000995 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100996 fatal("%s line %d: missing argument.",
997 filename, linenum);
998 value = -1;
999 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1000 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1001 value = multistate_ptr[i].value;
1002 break;
1003 }
1004 }
1005 if (value == -1)
1006 fatal("%s line %d: unsupported option \"%s\".",
1007 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +11001008 if (*activep && *intptr == -1)
1009 *intptr = value;
1010 break;
1011
Darren Tucker0a118da2003-10-15 15:54:32 +10001012 case oForwardX11Trusted:
1013 intptr = &options->forward_x11_trusted;
1014 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001015
Damien Miller1ab6a512010-06-26 10:02:24 +10001016 case oForwardX11Timeout:
1017 intptr = &options->forward_x11_timeout;
1018 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +10001019
Damien Miller95def091999-11-25 00:26:21 +11001020 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001021 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +11001022 goto parse_flag;
1023
Darren Tuckere7d4b192006-07-12 22:17:10 +10001024 case oExitOnForwardFailure:
1025 intptr = &options->exit_on_forward_failure;
1026 goto parse_flag;
1027
Damien Miller95def091999-11-25 00:26:21 +11001028 case oPasswordAuthentication:
1029 intptr = &options->password_authentication;
1030 goto parse_flag;
1031
Damien Miller874d77b2000-10-14 16:23:11 +11001032 case oKbdInteractiveAuthentication:
1033 intptr = &options->kbd_interactive_authentication;
1034 goto parse_flag;
1035
1036 case oKbdInteractiveDevices:
1037 charptr = &options->kbd_interactive_devices;
1038 goto parse_string;
1039
Damien Miller0bc1bd82000-11-13 22:57:25 +11001040 case oPubkeyAuthentication:
1041 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +10001042 goto parse_flag;
1043
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001044 case oHostbasedAuthentication:
1045 intptr = &options->hostbased_authentication;
1046 goto parse_flag;
1047
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001048 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +00001049 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +11001050 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001051
Darren Tucker0efd1552003-08-26 11:49:55 +10001052 case oGssAuthentication:
1053 intptr = &options->gss_authentication;
1054 goto parse_flag;
1055
1056 case oGssDelegateCreds:
1057 intptr = &options->gss_deleg_creds;
1058 goto parse_flag;
1059
Damien Miller95def091999-11-25 00:26:21 +11001060 case oBatchMode:
1061 intptr = &options->batch_mode;
1062 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 case oCheckHostIP:
1065 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +10001066 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller37876e92003-05-15 10:19:46 +10001068 case oVerifyHostKeyDNS:
1069 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001070 multistate_ptr = multistate_yesnoask;
1071 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001072
Damien Miller95def091999-11-25 00:26:21 +11001073 case oStrictHostKeyChecking:
1074 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001075 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001076 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077
Damien Miller95def091999-11-25 00:26:21 +11001078 case oCompression:
1079 intptr = &options->compression;
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001080 multistate_ptr = multistate_compression;
1081 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082
Damien Miller12c150e2003-12-17 16:31:10 +11001083 case oTCPKeepAlive:
1084 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001085 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001087 case oNoHostAuthenticationForLocalhost:
1088 intptr = &options->no_host_authentication_for_localhost;
1089 goto parse_flag;
1090
Damien Miller95def091999-11-25 00:26:21 +11001091 case oNumberOfPasswordPrompts:
1092 intptr = &options->number_of_password_prompts;
1093 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094
Damien Millera5539d22003-04-09 20:50:06 +10001095 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001096 arg = strdelim(&s);
1097 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001098 fatal("%.200s line %d: Missing argument.", filename,
1099 linenum);
1100 if (strcmp(arg, "default") == 0) {
1101 val64 = 0;
1102 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001103 if (scan_scaled(arg, &val64) == -1)
1104 fatal("%.200s line %d: Bad number '%s': %s",
1105 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001106 if (val64 != 0 && val64 < 16)
1107 fatal("%.200s line %d: RekeyLimit too small",
1108 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001109 }
Damien Miller3dff1762008-02-10 22:25:52 +11001110 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001111 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001112 if (s != NULL) { /* optional rekey interval present */
1113 if (strcmp(s, "none") == 0) {
1114 (void)strdelim(&s); /* discard */
1115 break;
1116 }
1117 intptr = &options->rekey_interval;
1118 goto parse_time;
1119 }
Damien Millera5539d22003-04-09 20:50:06 +10001120 break;
1121
Damien Miller95def091999-11-25 00:26:21 +11001122 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001123 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001124 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001125 fatal("%.200s line %d: Missing argument.", filename, linenum);
1126 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001127 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001128 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001129 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001130 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001131 add_identity_file(options, NULL,
1132 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001133 }
1134 break;
1135
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001136 case oCertificateFile:
1137 arg = strdelim(&s);
1138 if (!arg || *arg == '\0')
1139 fatal("%.200s line %d: Missing argument.",
1140 filename, linenum);
1141 if (*activep) {
1142 intptr = &options->num_certificate_files;
1143 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1144 fatal("%.200s line %d: Too many certificate "
1145 "files specified (max %d).",
1146 filename, linenum,
1147 SSH_MAX_CERTIFICATE_FILES);
1148 }
1149 add_certificate_file(options, arg,
1150 flags & SSHCONF_USERCONF);
1151 }
1152 break;
1153
Damien Millerd3a18572000-06-07 19:55:44 +10001154 case oXAuthLocation:
1155 charptr=&options->xauth_location;
1156 goto parse_string;
1157
Damien Miller95def091999-11-25 00:26:21 +11001158 case oUser:
1159 charptr = &options->user;
1160parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001161 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001162 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001163 fatal("%.200s line %d: Missing argument.",
1164 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001165 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001166 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001167 break;
1168
1169 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001170 cpptr = (char **)&options->system_hostfiles;
1171 uintptr = &options->num_system_hostfiles;
1172 max_entries = SSH_MAX_HOSTS_FILES;
1173parse_char_array:
1174 if (*activep && *uintptr == 0) {
1175 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1176 if ((*uintptr) >= max_entries)
1177 fatal("%s line %d: "
dtucker@openbsd.org2c169012020-04-24 03:33:21 +00001178 "too many known hosts files.",
Damien Miller295ee632011-05-29 21:42:31 +10001179 filename, linenum);
1180 cpptr[(*uintptr)++] = xstrdup(arg);
1181 }
1182 }
1183 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001184
1185 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001186 cpptr = (char **)&options->user_hostfiles;
1187 uintptr = &options->num_user_hostfiles;
1188 max_entries = SSH_MAX_HOSTS_FILES;
1189 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001190
jmc@openbsd.org73491492019-06-12 11:31:50 +00001191 case oHostname:
Damien Miller95def091999-11-25 00:26:21 +11001192 charptr = &options->hostname;
1193 goto parse_string;
1194
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001195 case oHostKeyAlias:
1196 charptr = &options->host_key_alias;
1197 goto parse_string;
1198
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001199 case oPreferredAuthentications:
1200 charptr = &options->preferred_authentications;
1201 goto parse_string;
1202
Ben Lindstrome0f88042001-04-30 13:06:24 +00001203 case oBindAddress:
1204 charptr = &options->bind_address;
1205 goto parse_string;
1206
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001207 case oBindInterface:
1208 charptr = &options->bind_interface;
1209 goto parse_string;
1210
Damien Miller7ea845e2010-02-12 09:21:02 +11001211 case oPKCS11Provider:
1212 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001213 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001214
djm@openbsd.org884416b2019-10-31 21:18:28 +00001215 case oSecurityKeyProvider:
1216 charptr = &options->sk_provider;
1217 goto parse_string;
1218
Damien Miller95def091999-11-25 00:26:21 +11001219 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001220 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001221 /* Ignore ProxyCommand if ProxyJump already specified */
1222 if (options->jump_host != NULL)
1223 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001224parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001225 if (s == NULL)
1226 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001227 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001228 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001229 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001230 return 0;
1231
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001232 case oProxyJump:
1233 if (s == NULL) {
1234 fatal("%.200s line %d: Missing argument.",
1235 filename, linenum);
1236 }
1237 len = strspn(s, WHITESPACE "=");
1238 if (parse_jump(s + len, options, *activep) == -1) {
1239 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1240 filename, linenum, s + len);
1241 }
1242 return 0;
1243
Damien Miller95def091999-11-25 00:26:21 +11001244 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001245 arg = strdelim(&s);
1246 if (!arg || *arg == '\0')
1247 fatal("%.200s line %d: Missing argument.",
1248 filename, linenum);
1249 value = a2port(arg);
1250 if (value <= 0)
1251 fatal("%.200s line %d: Bad port '%s'.",
1252 filename, linenum, arg);
1253 if (*activep && options->port == -1)
1254 options->port = value;
1255 break;
1256
1257 case oConnectionAttempts:
1258 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001259parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001260 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001261 if ((errstr = atoi_err(arg, &value)) != NULL)
1262 fatal("%s line %d: integer value %s.",
1263 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001264 if (*activep && *intptr == -1)
1265 *intptr = value;
1266 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
Damien Miller78928792000-04-12 20:17:38 +10001268 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001269 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001270 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001271 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001272 if (*arg != '-' &&
1273 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001274 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001275 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001276 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001277 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001278 break;
1279
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001280 case oMacs:
1281 arg = strdelim(&s);
1282 if (!arg || *arg == '\0')
1283 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001284 if (*arg != '-' &&
1285 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001286 fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001287 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001288 if (*activep && options->macs == NULL)
1289 options->macs = xstrdup(arg);
1290 break;
1291
Damien Millerd5f62bf2010-09-24 22:11:14 +10001292 case oKexAlgorithms:
1293 arg = strdelim(&s);
1294 if (!arg || *arg == '\0')
1295 fatal("%.200s line %d: Missing argument.",
1296 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001297 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001298 !kex_names_valid(*arg == '+' || *arg == '^' ?
1299 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001300 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1301 filename, linenum, arg ? arg : "<NONE>");
1302 if (*activep && options->kex_algorithms == NULL)
1303 options->kex_algorithms = xstrdup(arg);
1304 break;
1305
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001306 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001307 charptr = &options->hostkeyalgorithms;
1308parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001309 arg = strdelim(&s);
1310 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001311 fatal("%.200s line %d: Missing argument.",
1312 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001313 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001314 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1315 arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001316 fatal("%s line %d: Bad key types '%s'.",
1317 filename, linenum, arg ? arg : "<NONE>");
1318 if (*activep && *charptr == NULL)
1319 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001320 break;
1321
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001322 case oCASignatureAlgorithms:
1323 charptr = &options->ca_sign_algorithms;
1324 goto parse_keytypes;
1325
Damien Miller95def091999-11-25 00:26:21 +11001326 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001327 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001328 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001329 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001330 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001331 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001332 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001333 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1334 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001335 break;
1336
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001337 case oLogFacility:
1338 log_facility_ptr = &options->log_facility;
1339 arg = strdelim(&s);
1340 value = log_facility_number(arg);
1341 if (value == SYSLOG_FACILITY_NOT_SET)
1342 fatal("%.200s line %d: unsupported log facility '%s'",
1343 filename, linenum, arg ? arg : "<NONE>");
1344 if (*log_facility_ptr == -1)
1345 *log_facility_ptr = (SyslogFacility) value;
1346 break;
1347
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001348 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001349 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001350 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001351 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001352 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001353 fatal("%.200s line %d: Missing port argument.",
1354 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001355
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001356 remotefwd = (opcode == oRemoteForward);
1357 dynamicfwd = (opcode == oDynamicForward);
1358
1359 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001360 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001361 if (arg2 == NULL || *arg2 == '\0') {
1362 if (remotefwd)
1363 dynamicfwd = 1;
1364 else
1365 fatal("%.200s line %d: Missing target "
1366 "argument.", filename, linenum);
1367 } else {
1368 /* construct a string for parse_forward */
1369 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1370 arg2);
1371 }
Damien Millera699d952008-11-03 19:27:34 +11001372 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001373 if (dynamicfwd)
1374 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001375
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001376 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001377 fatal("%.200s line %d: Bad forwarding specification.",
1378 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001379
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001380 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001381 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001382 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001383 } else {
1384 add_local_forward(options, &fwd);
1385 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001386 }
Damien Miller95def091999-11-25 00:26:21 +11001387 break;
1388
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001389 case oClearAllForwardings:
1390 intptr = &options->clear_forwardings;
1391 goto parse_flag;
1392
Damien Miller95def091999-11-25 00:26:21 +11001393 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001394 if (cmdline)
1395 fatal("Host directive not supported as a command-line "
1396 "option");
Damien Miller95def091999-11-25 00:26:21 +11001397 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001398 arg2 = NULL;
1399 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001400 if ((flags & SSHCONF_NEVERMATCH) != 0)
1401 break;
Damien Millerfe924212011-05-15 08:44:45 +10001402 negated = *arg == '!';
1403 if (negated)
1404 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001405 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001406 if (negated) {
1407 debug("%.200s line %d: Skipping Host "
1408 "block because of negated match "
1409 "for %.100s", filename, linenum,
1410 arg);
1411 *activep = 0;
1412 break;
1413 }
1414 if (!*activep)
1415 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001416 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001417 }
Damien Millerfe924212011-05-15 08:44:45 +10001418 }
1419 if (*activep)
1420 debug("%.200s line %d: Applying options for %.100s",
1421 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001422 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001423 return 0;
1424
Damien Miller194fd902013-10-15 12:13:05 +11001425 case oMatch:
1426 if (cmdline)
1427 fatal("Host directive not supported as a command-line "
1428 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001429 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001430 flags & SSHCONF_FINAL, want_final_pass,
1431 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001432 if (value < 0)
1433 fatal("%.200s line %d: Bad Match condition", filename,
1434 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001435 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001436 break;
1437
Damien Miller95def091999-11-25 00:26:21 +11001438 case oEscapeChar:
1439 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001440 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001441 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001442 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001443 if (strcmp(arg, "none") == 0)
1444 value = SSH_ESCAPECHAR_NONE;
1445 else if (arg[1] == '\0')
1446 value = (u_char) arg[0];
1447 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001448 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1449 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001450 else {
1451 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001452 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001453 /* NOTREACHED */
1454 value = 0; /* Avoid compiler warning. */
1455 }
1456 if (*activep && *intptr == -1)
1457 *intptr = value;
1458 break;
1459
Damien Miller20a8f972003-05-18 20:50:30 +10001460 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001461 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001462 multistate_ptr = multistate_addressfamily;
1463 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001464
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001465 case oEnableSSHKeysign:
1466 intptr = &options->enable_ssh_keysign;
1467 goto parse_flag;
1468
Damien Millerbd394c32004-03-08 23:12:36 +11001469 case oIdentitiesOnly:
1470 intptr = &options->identities_only;
1471 goto parse_flag;
1472
Damien Miller509b0102003-12-17 16:33:10 +11001473 case oServerAliveInterval:
1474 intptr = &options->server_alive_interval;
1475 goto parse_time;
1476
1477 case oServerAliveCountMax:
1478 intptr = &options->server_alive_count_max;
1479 goto parse_int;
1480
Darren Tucker46bc0752004-05-02 22:11:30 +10001481 case oSendEnv:
1482 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1483 if (strchr(arg, '=') != NULL)
1484 fatal("%s line %d: Invalid environment name.",
1485 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001486 if (!*activep)
1487 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001488 if (*arg == '-') {
1489 /* Removing an env var */
1490 rm_env(options, arg, filename, linenum);
1491 continue;
1492 } else {
1493 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001494 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001495 fatal("%s line %d: too many send env.",
1496 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001497 options->send_env = xrecallocarray(
1498 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001499 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001500 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001501 options->send_env[options->num_send_env++] =
1502 xstrdup(arg);
1503 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001504 }
1505 break;
1506
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001507 case oSetEnv:
1508 value = options->num_setenv;
1509 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1510 if (strchr(arg, '=') == NULL)
1511 fatal("%s line %d: Invalid SetEnv.",
1512 filename, linenum);
1513 if (!*activep || value != 0)
1514 continue;
1515 /* Adding a setenv var */
1516 if (options->num_setenv >= INT_MAX)
1517 fatal("%s line %d: too many SetEnv.",
1518 filename, linenum);
1519 options->setenv = xrecallocarray(
1520 options->setenv, options->num_setenv,
1521 options->num_setenv + 1, sizeof(*options->setenv));
1522 options->setenv[options->num_setenv++] = xstrdup(arg);
1523 }
1524 break;
1525
Damien Miller0e220db2004-06-15 10:34:08 +10001526 case oControlPath:
1527 charptr = &options->control_path;
1528 goto parse_string;
1529
1530 case oControlMaster:
1531 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001532 multistate_ptr = multistate_controlmaster;
1533 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001534
Damien Millere11e1ea2010-08-03 16:04:46 +10001535 case oControlPersist:
1536 /* no/false/yes/true, or a time spec */
1537 intptr = &options->control_persist;
1538 arg = strdelim(&s);
1539 if (!arg || *arg == '\0')
1540 fatal("%.200s line %d: Missing ControlPersist"
1541 " argument.", filename, linenum);
1542 value = 0;
1543 value2 = 0; /* timeout */
1544 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1545 value = 0;
1546 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1547 value = 1;
1548 else if ((value2 = convtime(arg)) >= 0)
1549 value = 1;
1550 else
1551 fatal("%.200s line %d: Bad ControlPersist argument.",
1552 filename, linenum);
1553 if (*activep && *intptr == -1) {
1554 *intptr = value;
1555 options->control_persist_timeout = value2;
1556 }
1557 break;
1558
Damien Millere1776152005-03-01 21:47:37 +11001559 case oHashKnownHosts:
1560 intptr = &options->hash_known_hosts;
1561 goto parse_flag;
1562
Damien Millerd27b9472005-12-13 19:29:02 +11001563 case oTunnel:
1564 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001565 multistate_ptr = multistate_tunnel;
1566 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001567
1568 case oTunnelDevice:
1569 arg = strdelim(&s);
1570 if (!arg || *arg == '\0')
1571 fatal("%.200s line %d: Missing argument.", filename, linenum);
1572 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001573 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001574 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1575 if (*activep) {
1576 options->tun_local = value;
1577 options->tun_remote = value2;
1578 }
1579 break;
1580
1581 case oLocalCommand:
1582 charptr = &options->local_command;
1583 goto parse_command;
1584
1585 case oPermitLocalCommand:
1586 intptr = &options->permit_local_command;
1587 goto parse_flag;
1588
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001589 case oRemoteCommand:
1590 charptr = &options->remote_command;
1591 goto parse_command;
1592
Damien Miller10288242008-06-30 00:04:03 +10001593 case oVisualHostKey:
1594 intptr = &options->visual_host_key;
1595 goto parse_flag;
1596
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001597 case oInclude:
1598 if (cmdline)
1599 fatal("Include directive not supported as a "
1600 "command-line option");
1601 value = 0;
1602 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1603 /*
1604 * Ensure all paths are anchored. User configuration
1605 * files may begin with '~/' but system configurations
1606 * must not. If the path is relative, then treat it
1607 * as living in ~/.ssh for user configurations or
1608 * /etc/ssh for system ones.
1609 */
1610 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1611 fatal("%.200s line %d: bad include path %s.",
1612 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001613 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001614 xasprintf(&arg2, "%s/%s",
1615 (flags & SSHCONF_USERCONF) ?
1616 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1617 } else
1618 arg2 = xstrdup(arg);
1619 memset(&gl, 0, sizeof(gl));
1620 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1621 if (r == GLOB_NOMATCH) {
1622 debug("%.200s line %d: include %s matched no "
1623 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001624 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001625 continue;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001626 } else if (r != 0)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001627 fatal("%.200s line %d: glob failed for %s.",
1628 filename, linenum, arg2);
1629 free(arg2);
1630 oactive = *activep;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001631 for (i = 0; i < gl.gl_pathc; i++) {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001632 debug3("%.200s line %d: Including file %s "
1633 "depth %d%s", filename, linenum,
1634 gl.gl_pathv[i], depth,
1635 oactive ? "" : " (parse only)");
1636 r = read_config_file_depth(gl.gl_pathv[i],
1637 pw, host, original_host, options,
1638 flags | SSHCONF_CHECKPERM |
1639 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001640 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001641 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001642 fatal("Can't open user config file "
1643 "%.100s: %.100s", gl.gl_pathv[i],
1644 strerror(errno));
1645 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001646 /*
1647 * don't let Match in includes clobber the
1648 * containing file's Match state.
1649 */
1650 *activep = oactive;
1651 if (r != 1)
1652 value = -1;
1653 }
1654 globfree(&gl);
1655 }
1656 if (value != 0)
1657 return value;
1658 break;
1659
Damien Miller0dac6fb2010-11-20 15:19:38 +11001660 case oIPQoS:
1661 arg = strdelim(&s);
1662 if ((value = parse_ipqos(arg)) == -1)
1663 fatal("%s line %d: Bad IPQoS value: %s",
1664 filename, linenum, arg);
1665 arg = strdelim(&s);
1666 if (arg == NULL)
1667 value2 = value;
1668 else if ((value2 = parse_ipqos(arg)) == -1)
1669 fatal("%s line %d: Bad IPQoS value: %s",
1670 filename, linenum, arg);
1671 if (*activep) {
1672 options->ip_qos_interactive = value;
1673 options->ip_qos_bulk = value2;
1674 }
1675 break;
1676
Damien Miller21771e22011-05-15 08:45:50 +10001677 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001678 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001679 multistate_ptr = multistate_requesttty;
1680 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001681
Darren Tucker07636982013-05-16 20:30:03 +10001682 case oIgnoreUnknown:
1683 charptr = &options->ignored_unknown;
1684 goto parse_string;
1685
Damien Miller1262b662013-08-21 02:44:24 +10001686 case oProxyUseFdpass:
1687 intptr = &options->proxy_use_fdpass;
1688 goto parse_flag;
1689
Damien Miller0faf7472013-10-17 11:47:23 +11001690 case oCanonicalDomains:
1691 value = options->num_canonical_domains != 0;
1692 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001693 if (!valid_domain(arg, 1, &errstr)) {
1694 fatal("%s line %d: %s", filename, linenum,
1695 errstr);
1696 }
Damien Miller0faf7472013-10-17 11:47:23 +11001697 if (!*activep || value)
1698 continue;
1699 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1700 fatal("%s line %d: too many hostname suffixes.",
1701 filename, linenum);
1702 options->canonical_domains[
1703 options->num_canonical_domains++] = xstrdup(arg);
1704 }
1705 break;
1706
Damien Miller38505592013-10-17 11:48:13 +11001707 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001708 value = options->num_permitted_cnames != 0;
1709 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1710 /* Either '*' for everything or 'list:list' */
1711 if (strcmp(arg, "*") == 0)
1712 arg2 = arg;
1713 else {
1714 lowercase(arg);
1715 if ((arg2 = strchr(arg, ':')) == NULL ||
1716 arg2[1] == '\0') {
1717 fatal("%s line %d: "
1718 "Invalid permitted CNAME \"%s\"",
1719 filename, linenum, arg);
1720 }
1721 *arg2 = '\0';
1722 arg2++;
1723 }
1724 if (!*activep || value)
1725 continue;
1726 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1727 fatal("%s line %d: too many permitted CNAMEs.",
1728 filename, linenum);
1729 cname = options->permitted_cnames +
1730 options->num_permitted_cnames++;
1731 cname->source_list = xstrdup(arg);
1732 cname->target_list = xstrdup(arg2);
1733 }
1734 break;
1735
Damien Miller38505592013-10-17 11:48:13 +11001736 case oCanonicalizeHostname:
1737 intptr = &options->canonicalize_hostname;
1738 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001739 goto parse_multistate;
1740
Damien Miller38505592013-10-17 11:48:13 +11001741 case oCanonicalizeMaxDots:
1742 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001743 goto parse_int;
1744
Damien Miller38505592013-10-17 11:48:13 +11001745 case oCanonicalizeFallbackLocal:
1746 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001747 goto parse_flag;
1748
Damien Miller7acefbb2014-07-18 14:11:24 +10001749 case oStreamLocalBindMask:
1750 arg = strdelim(&s);
1751 if (!arg || *arg == '\0')
1752 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1753 /* Parse mode in octal format */
1754 value = strtol(arg, &endofnumber, 8);
1755 if (arg == endofnumber || value < 0 || value > 0777)
1756 fatal("%.200s line %d: Bad mask.", filename, linenum);
1757 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1758 break;
1759
1760 case oStreamLocalBindUnlink:
1761 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1762 goto parse_flag;
1763
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001764 case oRevokedHostKeys:
1765 charptr = &options->revoked_host_keys;
1766 goto parse_string;
1767
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001768 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001769 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001770 arg = strdelim(&s);
1771 if (!arg || *arg == '\0')
1772 fatal("%.200s line %d: Missing argument.",
1773 filename, linenum);
1774 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1775 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1776 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001777 if (*activep && *intptr == -1)
1778 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001779 break;
1780
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001781 case oUpdateHostkeys:
1782 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001783 multistate_ptr = multistate_yesnoask;
1784 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001785
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001786 case oHostbasedKeyTypes:
1787 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001788 goto parse_keytypes;
1789
1790 case oPubkeyAcceptedKeyTypes:
1791 charptr = &options->pubkey_key_types;
1792 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001793
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001794 case oAddKeysToAgent:
1795 intptr = &options->add_keys_to_agent;
1796 multistate_ptr = multistate_yesnoaskconfirm;
1797 goto parse_multistate;
1798
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001799 case oIdentityAgent:
1800 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001801 arg = strdelim(&s);
1802 if (!arg || *arg == '\0')
1803 fatal("%.200s line %d: Missing argument.",
1804 filename, linenum);
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001805 parse_agent_path:
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001806 /* Extra validation if the string represents an env var. */
1807 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1808 fatal("%.200s line %d: Invalid environment name %s.",
1809 filename, linenum, arg);
1810 }
1811 if (*activep && *charptr == NULL)
1812 *charptr = xstrdup(arg);
1813 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001814
Ben Lindstrom4daea862002-06-09 20:04:02 +00001815 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001816 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001817 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001818 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001819
Damien Millerf9b3feb2003-05-16 11:38:32 +10001820 case oUnsupported:
1821 error("%s line %d: Unsupported option \"%s\"",
1822 filename, linenum, keyword);
1823 return 0;
1824
Damien Miller95def091999-11-25 00:26:21 +11001825 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001826 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001827 }
1828
1829 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001830 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001831 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001832 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001833 }
Damien Miller95def091999-11-25 00:26:21 +11001834 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001835}
1836
Damien Miller5428f641999-11-25 11:54:57 +11001837/*
1838 * Reads the config file and modifies the options accordingly. Options
1839 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001840 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001841 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001842int
Damien Miller194fd902013-10-15 12:13:05 +11001843read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001844 const char *original_host, Options *options, int flags,
1845 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001847 int active = 1;
1848
1849 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001850 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001851}
1852
1853#define READCONF_MAX_DEPTH 16
1854static int
1855read_config_file_depth(const char *filename, struct passwd *pw,
1856 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001857 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001858{
Damien Miller95def091999-11-25 00:26:21 +11001859 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001860 char *line = NULL;
1861 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001862 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001863 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001865 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1866 fatal("Too many recursive configuration includes");
1867
Damien Miller57a44762004-04-20 20:11:57 +10001868 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001869 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870
Darren Tuckeraefa3682013-04-05 11:18:35 +11001871 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001872 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001873
Damien Miller33793852004-06-15 10:27:55 +10001874 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001875 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001876 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001877 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001878 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001879 }
1880
Damien Miller95def091999-11-25 00:26:21 +11001881 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001882
Damien Miller5428f641999-11-25 11:54:57 +11001883 /*
1884 * Mark that we are now processing the options. This flag is turned
1885 * on/off by Host specifications.
1886 */
Damien Miller95def091999-11-25 00:26:21 +11001887 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001888 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001889 /* Update line number counter. */
1890 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001891 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001892 line, filename, linenum, activep, flags, want_final_pass,
1893 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001894 bad_options++;
1895 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001896 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001897 fclose(f);
1898 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001899 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001900 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001901 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001902}
1903
Damien Miller13f97b22014-02-24 15:57:55 +11001904/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1905int
1906option_clear_or_none(const char *o)
1907{
1908 return o == NULL || strcasecmp(o, "none") == 0;
1909}
1910
Damien Miller5428f641999-11-25 11:54:57 +11001911/*
1912 * Initializes options to special values that indicate that they have not yet
1913 * been set. Read_config_file will only set options with this value. Options
1914 * are processed in the following order: command line, user config file,
1915 * system config file. Last, fill_default_options is called.
1916 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001917
Damien Miller4af51302000-04-16 11:18:38 +10001918void
Damien Miller95def091999-11-25 00:26:21 +11001919initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920{
Damien Miller95def091999-11-25 00:26:21 +11001921 memset(options, 'X', sizeof(*options));
1922 options->forward_agent = -1;
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001923 options->forward_agent_sock_path = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001924 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001925 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001926 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001927 options->stdio_forward_host = NULL;
1928 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001929 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001930 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001931 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001932 options->fwd_opts.gateway_ports = -1;
1933 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1934 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001935 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001936 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001937 options->gss_authentication = -1;
1938 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001939 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001940 options->kbd_interactive_authentication = -1;
1941 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001942 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001943 options->batch_mode = -1;
1944 options->check_host_ip = -1;
1945 options->strict_host_key_checking = -1;
1946 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001947 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001948 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001949 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001950 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001951 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001952 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001953 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001954 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001955 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001956 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001957 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001958 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001959 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001960 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001961 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001962 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001963 options->jump_user = NULL;
1964 options->jump_host = NULL;
1965 options->jump_port = -1;
1966 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001967 options->user = NULL;
1968 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001969 options->num_system_hostfiles = 0;
1970 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001971 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001972 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001973 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001974 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001975 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001976 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001977 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001978 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001979 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001980 options->pkcs11_provider = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001981 options->sk_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001982 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001983 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001984 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001985 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001986 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001987 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001988 options->server_alive_interval = -1;
1989 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001990 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001991 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001992 options->setenv = NULL;
1993 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001994 options->control_path = NULL;
1995 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001996 options->control_persist = -1;
1997 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001998 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001999 options->tun_open = -1;
2000 options->tun_local = -1;
2001 options->tun_remote = -1;
2002 options->local_command = NULL;
2003 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002004 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002005 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002006 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10002007 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002008 options->ip_qos_interactive = -1;
2009 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10002010 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10002011 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10002012 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11002013 options->num_canonical_domains = 0;
2014 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11002015 options->canonicalize_max_dots = -1;
2016 options->canonicalize_fallback_local = -1;
2017 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002018 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002019 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002020 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002021 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002022 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002023}
2024
Damien Miller5428f641999-11-25 11:54:57 +11002025/*
Damien Miller13f97b22014-02-24 15:57:55 +11002026 * A petite version of fill_default_options() that just fills the options
2027 * needed for hostname canonicalization to proceed.
2028 */
2029void
2030fill_default_options_for_canonicalization(Options *options)
2031{
2032 if (options->canonicalize_max_dots == -1)
2033 options->canonicalize_max_dots = 1;
2034 if (options->canonicalize_fallback_local == -1)
2035 options->canonicalize_fallback_local = 1;
2036 if (options->canonicalize_hostname == -1)
2037 options->canonicalize_hostname = SSH_CANONICALISE_NO;
2038}
2039
2040/*
Damien Miller5428f641999-11-25 11:54:57 +11002041 * Called after processing other sources of option data, this fills those
2042 * options for which no value has been specified with their default values.
2043 */
Damien Miller4af51302000-04-16 11:18:38 +10002044void
Damien Miller95def091999-11-25 00:26:21 +11002045fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002047 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002048 char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002049 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002050
Damien Miller95def091999-11-25 00:26:21 +11002051 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10002052 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002053 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11002054 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10002055 if (options->forward_x11_trusted == -1)
2056 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10002057 if (options->forward_x11_timeout == -1)
2058 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002059 /*
2060 * stdio forwarding (-W) changes the default for these but we defer
2061 * setting the values so they can be overridden.
2062 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002063 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002064 options->exit_on_forward_failure =
2065 options->stdio_forward_host != NULL ? 1 : 0;
2066 if (options->clear_forwardings == -1)
2067 options->clear_forwardings =
2068 options->stdio_forward_host != NULL ? 1 : 0;
2069 if (options->clear_forwardings == 1)
2070 clear_forwardings(options);
2071
Damien Millerd3a18572000-06-07 19:55:44 +10002072 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00002073 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10002074 if (options->fwd_opts.gateway_ports == -1)
2075 options->fwd_opts.gateway_ports = 0;
2076 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2077 options->fwd_opts.streamlocal_bind_mask = 0177;
2078 if (options->fwd_opts.streamlocal_bind_unlink == -1)
2079 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002080 if (options->pubkey_authentication == -1)
2081 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00002082 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002083 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002084 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002085 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002086 if (options->gss_deleg_creds == -1)
2087 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002088 if (options->password_authentication == -1)
2089 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002090 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002091 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002092 if (options->hostbased_authentication == -1)
2093 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002094 if (options->batch_mode == -1)
2095 options->batch_mode = 0;
2096 if (options->check_host_ip == -1)
2097 options->check_host_ip = 1;
2098 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002099 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002100 if (options->compression == -1)
2101 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002102 if (options->tcp_keep_alive == -1)
2103 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002104 if (options->port == -1)
2105 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002106 if (options->address_family == -1)
2107 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002108 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002109 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002110 if (options->number_of_password_prompts == -1)
2111 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002112 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002113 if (options->add_keys_to_agent == -1)
2114 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002115 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002116 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2117 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002118#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002119 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002120 add_identity_file(options, "~/",
2121 _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002122#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002123 add_identity_file(options, "~/",
2124 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org2c557442019-11-12 19:33:08 +00002125 add_identity_file(options, "~/",
2126 _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002127 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002128 }
Damien Miller95def091999-11-25 00:26:21 +11002129 if (options->escape_char == -1)
2130 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002131 if (options->num_system_hostfiles == 0) {
2132 options->system_hostfiles[options->num_system_hostfiles++] =
2133 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2134 options->system_hostfiles[options->num_system_hostfiles++] =
2135 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2136 }
djm@openbsd.org501f3582020-02-03 23:47:57 +00002137 if (options->update_hostkeys == -1)
2138 options->update_hostkeys = SSH_UPDATE_HOSTKEYS_NO;
Damien Miller295ee632011-05-29 21:42:31 +10002139 if (options->num_user_hostfiles == 0) {
2140 options->user_hostfiles[options->num_user_hostfiles++] =
2141 xstrdup(_PATH_SSH_USER_HOSTFILE);
2142 options->user_hostfiles[options->num_user_hostfiles++] =
2143 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2144 }
Damien Millerfcd93202002-02-05 12:26:34 +11002145 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002146 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002147 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2148 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002149 if (options->no_host_authentication_for_localhost == - 1)
2150 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002151 if (options->identities_only == -1)
2152 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002153 if (options->enable_ssh_keysign == -1)
2154 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002155 if (options->rekey_limit == -1)
2156 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002157 if (options->rekey_interval == -1)
2158 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002159 if (options->verify_host_key_dns == -1)
2160 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002161 if (options->server_alive_interval == -1)
2162 options->server_alive_interval = 0;
2163 if (options->server_alive_count_max == -1)
2164 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002165 if (options->control_master == -1)
2166 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002167 if (options->control_persist == -1) {
2168 options->control_persist = 0;
2169 options->control_persist_timeout = 0;
2170 }
Damien Millere1776152005-03-01 21:47:37 +11002171 if (options->hash_known_hosts == -1)
2172 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002173 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002174 options->tun_open = SSH_TUNMODE_NO;
2175 if (options->tun_local == -1)
2176 options->tun_local = SSH_TUNID_ANY;
2177 if (options->tun_remote == -1)
2178 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002179 if (options->permit_local_command == -1)
2180 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002181 if (options->visual_host_key == -1)
2182 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002183 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002184 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002185 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002186 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002187 if (options->request_tty == -1)
2188 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002189 if (options->proxy_use_fdpass == -1)
2190 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002191 if (options->canonicalize_max_dots == -1)
2192 options->canonicalize_max_dots = 1;
2193 if (options->canonicalize_fallback_local == -1)
2194 options->canonicalize_fallback_local = 1;
2195 if (options->canonicalize_hostname == -1)
2196 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002197 if (options->fingerprint_hash == -1)
2198 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002199#ifdef ENABLE_SK_INTERNAL
2200 if (options->sk_provider == NULL)
2201 options->sk_provider = xstrdup("internal");
2202#else
djm@openbsd.org884416b2019-10-31 21:18:28 +00002203 if (options->sk_provider == NULL)
2204 options->sk_provider = xstrdup("$SSH_SK_PROVIDER");
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002205#endif
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002206
2207 /* Expand KEX name lists */
2208 all_cipher = cipher_alg_list(',', 0);
2209 all_mac = mac_alg_list(',');
2210 all_kex = kex_alg_list(',');
2211 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002212 all_sig = sshkey_alg_list(0, 1, 1, ',');
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002213 /* remove unsupported algos from default lists */
2214 def_cipher = match_filter_whitelist(KEX_CLIENT_ENCRYPT, all_cipher);
2215 def_mac = match_filter_whitelist(KEX_CLIENT_MAC, all_mac);
2216 def_kex = match_filter_whitelist(KEX_CLIENT_KEX, all_kex);
2217 def_key = match_filter_whitelist(KEX_DEFAULT_PK_ALG, all_key);
2218 def_sig = match_filter_whitelist(SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002219#define ASSEMBLE(what, defaults, all) \
2220 do { \
2221 if ((r = kex_assemble_names(&options->what, \
2222 defaults, all)) != 0) \
2223 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2224 } while (0)
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002225 ASSEMBLE(ciphers, def_cipher, all_cipher);
2226 ASSEMBLE(macs, def_mac, all_mac);
2227 ASSEMBLE(kex_algorithms, def_kex, all_kex);
2228 ASSEMBLE(hostbased_key_types, def_key, all_key);
2229 ASSEMBLE(pubkey_key_types, def_key, all_key);
2230 ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002231#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002232 free(all_cipher);
2233 free(all_mac);
2234 free(all_kex);
2235 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002236 free(all_sig);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002237 free(def_cipher);
2238 free(def_mac);
2239 free(def_kex);
2240 kex_default_pk_alg_filtered = def_key; /* save for later use */
2241 free(def_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002242
Damien Millere9fc72e2013-10-15 12:14:12 +11002243#define CLEAR_ON_NONE(v) \
2244 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002245 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002246 free(v); \
2247 v = NULL; \
2248 } \
2249 } while(0)
2250 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002251 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002252 CLEAR_ON_NONE(options->proxy_command);
2253 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002254 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org95a80582019-03-01 02:08:50 +00002255 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002256 CLEAR_ON_NONE(options->sk_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002257 if (options->jump_host != NULL &&
2258 strcmp(options->jump_host, "none") == 0 &&
2259 options->jump_port == 0 && options->jump_user == NULL) {
2260 free(options->jump_host);
2261 options->jump_host = NULL;
2262 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002263 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002264 /* options->user will be set in the main program if appropriate */
2265 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002266 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002267 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002269
Damien Miller7acefbb2014-07-18 14:11:24 +10002270struct fwdarg {
2271 char *arg;
2272 int ispath;
2273};
2274
2275/*
2276 * parse_fwd_field
2277 * parses the next field in a port forwarding specification.
2278 * sets fwd to the parsed field and advances p past the colon
2279 * or sets it to NULL at end of string.
2280 * returns 0 on success, else non-zero.
2281 */
2282static int
2283parse_fwd_field(char **p, struct fwdarg *fwd)
2284{
2285 char *ep, *cp = *p;
2286 int ispath = 0;
2287
2288 if (*cp == '\0') {
2289 *p = NULL;
2290 return -1; /* end of string */
2291 }
2292
2293 /*
2294 * A field escaped with square brackets is used literally.
2295 * XXX - allow ']' to be escaped via backslash?
2296 */
2297 if (*cp == '[') {
2298 /* find matching ']' */
2299 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2300 if (*ep == '/')
2301 ispath = 1;
2302 }
2303 /* no matching ']' or not at end of field. */
2304 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2305 return -1;
2306 /* NUL terminate the field and advance p past the colon */
2307 *ep++ = '\0';
2308 if (*ep != '\0')
2309 *ep++ = '\0';
2310 fwd->arg = cp + 1;
2311 fwd->ispath = ispath;
2312 *p = ep;
2313 return 0;
2314 }
2315
2316 for (cp = *p; *cp != '\0'; cp++) {
2317 switch (*cp) {
2318 case '\\':
2319 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002320 if (*cp == '\0')
2321 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002322 break;
2323 case '/':
2324 ispath = 1;
2325 break;
2326 case ':':
2327 *cp++ = '\0';
2328 goto done;
2329 }
2330 }
2331done:
2332 fwd->arg = *p;
2333 fwd->ispath = ispath;
2334 *p = cp;
2335 return 0;
2336}
2337
Damien Millerf91ee4c2005-03-01 21:24:33 +11002338/*
2339 * parse_forward
2340 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002341 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002342 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2343 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002344 * dynamicfwd == 1
2345 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002346 * returns number of arguments parsed or zero on error
2347 */
2348int
Damien Miller7acefbb2014-07-18 14:11:24 +10002349parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002350{
Damien Miller7acefbb2014-07-18 14:11:24 +10002351 struct fwdarg fwdargs[4];
2352 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002353 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002354
Damien Miller7acefbb2014-07-18 14:11:24 +10002355 memset(fwd, 0, sizeof(*fwd));
2356 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002357
2358 cp = p = xstrdup(fwdspec);
2359
2360 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002361 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002362 cp++;
2363
Damien Miller7acefbb2014-07-18 14:11:24 +10002364 for (i = 0; i < 4; ++i) {
2365 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002366 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002367 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002368
Damien Millerf4b39532008-11-03 19:28:21 +11002369 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002370 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002371 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002372 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002373
2374 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002375 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002376 if (fwdargs[0].ispath) {
2377 fwd->listen_path = xstrdup(fwdargs[0].arg);
2378 fwd->listen_port = PORT_STREAMLOCAL;
2379 } else {
2380 fwd->listen_host = NULL;
2381 fwd->listen_port = a2port(fwdargs[0].arg);
2382 }
Damien Millera699d952008-11-03 19:27:34 +11002383 fwd->connect_host = xstrdup("socks");
2384 break;
2385
2386 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002387 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2388 fwd->listen_path = xstrdup(fwdargs[0].arg);
2389 fwd->listen_port = PORT_STREAMLOCAL;
2390 fwd->connect_path = xstrdup(fwdargs[1].arg);
2391 fwd->connect_port = PORT_STREAMLOCAL;
2392 } else if (fwdargs[1].ispath) {
2393 fwd->listen_host = NULL;
2394 fwd->listen_port = a2port(fwdargs[0].arg);
2395 fwd->connect_path = xstrdup(fwdargs[1].arg);
2396 fwd->connect_port = PORT_STREAMLOCAL;
2397 } else {
2398 fwd->listen_host = xstrdup(fwdargs[0].arg);
2399 fwd->listen_port = a2port(fwdargs[1].arg);
2400 fwd->connect_host = xstrdup("socks");
2401 }
Damien Millera699d952008-11-03 19:27:34 +11002402 break;
2403
Damien Millerf91ee4c2005-03-01 21:24:33 +11002404 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002405 if (fwdargs[0].ispath) {
2406 fwd->listen_path = xstrdup(fwdargs[0].arg);
2407 fwd->listen_port = PORT_STREAMLOCAL;
2408 fwd->connect_host = xstrdup(fwdargs[1].arg);
2409 fwd->connect_port = a2port(fwdargs[2].arg);
2410 } else if (fwdargs[2].ispath) {
2411 fwd->listen_host = xstrdup(fwdargs[0].arg);
2412 fwd->listen_port = a2port(fwdargs[1].arg);
2413 fwd->connect_path = xstrdup(fwdargs[2].arg);
2414 fwd->connect_port = PORT_STREAMLOCAL;
2415 } else {
2416 fwd->listen_host = NULL;
2417 fwd->listen_port = a2port(fwdargs[0].arg);
2418 fwd->connect_host = xstrdup(fwdargs[1].arg);
2419 fwd->connect_port = a2port(fwdargs[2].arg);
2420 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002421 break;
2422
2423 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002424 fwd->listen_host = xstrdup(fwdargs[0].arg);
2425 fwd->listen_port = a2port(fwdargs[1].arg);
2426 fwd->connect_host = xstrdup(fwdargs[2].arg);
2427 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002428 break;
2429 default:
2430 i = 0; /* failure */
2431 }
2432
Darren Tuckera627d422013-06-02 07:31:17 +10002433 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002434
Damien Millera699d952008-11-03 19:27:34 +11002435 if (dynamicfwd) {
2436 if (!(i == 1 || i == 2))
2437 goto fail_free;
2438 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002439 if (!(i == 3 || i == 4)) {
2440 if (fwd->connect_path == NULL &&
2441 fwd->listen_path == NULL)
2442 goto fail_free;
2443 }
2444 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002445 goto fail_free;
2446 }
2447
Damien Miller7acefbb2014-07-18 14:11:24 +10002448 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2449 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002450 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002451 if (fwd->connect_host != NULL &&
2452 strlen(fwd->connect_host) >= NI_MAXHOST)
2453 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002454 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2455 if (fwd->connect_path != NULL &&
2456 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2457 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002458 if (fwd->listen_host != NULL &&
2459 strlen(fwd->listen_host) >= NI_MAXHOST)
2460 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002461 if (fwd->listen_path != NULL &&
2462 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2463 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002464
2465 return (i);
2466
2467 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002468 free(fwd->connect_host);
2469 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002470 free(fwd->connect_path);
2471 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002472 free(fwd->listen_host);
2473 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002474 free(fwd->listen_path);
2475 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002476 return (0);
2477}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002478
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002479int
2480parse_jump(const char *s, Options *o, int active)
2481{
2482 char *orig, *sdup, *cp;
2483 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002484 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002485
2486 active &= o->proxy_command == NULL && o->jump_host == NULL;
2487
2488 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002489 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002490 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002491 if (strcasecmp(s, "none") == 0)
2492 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002493 if ((cp = strrchr(sdup, ',')) == NULL)
2494 cp = sdup; /* last */
2495 else
2496 *cp++ = '\0';
2497
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002498 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002499 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002500 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2501 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002502 goto out;
2503 } else {
2504 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002505 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2506 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002507 goto out;
2508 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002509 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002510 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002511 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002512 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002513 if (strcasecmp(s, "none") == 0) {
2514 o->jump_host = xstrdup("none");
2515 o->jump_port = 0;
2516 } else {
2517 o->jump_user = user;
2518 o->jump_host = host;
2519 o->jump_port = port;
2520 o->proxy_command = xstrdup("none");
2521 user = host = NULL;
2522 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2523 o->jump_extra = xstrdup(s);
2524 o->jump_extra[cp - s] = '\0';
2525 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002526 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002527 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002528 ret = 0;
2529 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002530 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002531 free(user);
2532 free(host);
2533 return ret;
2534}
2535
millert@openbsd.org887669e2017-10-21 23:06:24 +00002536int
2537parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2538{
2539 char *path;
2540 int r;
2541
2542 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2543 if (r == 0 && path != NULL)
2544 r = -1; /* path not allowed */
2545 return r;
2546}
2547
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002548/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2549static const char *
2550fmt_multistate_int(int val, const struct multistate *m)
2551{
2552 u_int i;
2553
2554 for (i = 0; m[i].key != NULL; i++) {
2555 if (m[i].value == val)
2556 return m[i].key;
2557 }
2558 return "UNKNOWN";
2559}
2560
2561static const char *
2562fmt_intarg(OpCodes code, int val)
2563{
2564 if (val == -1)
2565 return "unset";
2566 switch (code) {
2567 case oAddressFamily:
2568 return fmt_multistate_int(val, multistate_addressfamily);
2569 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002570 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002571 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002572 case oStrictHostKeyChecking:
2573 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002574 case oControlMaster:
2575 return fmt_multistate_int(val, multistate_controlmaster);
2576 case oTunnel:
2577 return fmt_multistate_int(val, multistate_tunnel);
2578 case oRequestTTY:
2579 return fmt_multistate_int(val, multistate_requesttty);
2580 case oCanonicalizeHostname:
2581 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002582 case oAddKeysToAgent:
2583 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002584 case oFingerprintHash:
2585 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002586 default:
2587 switch (val) {
2588 case 0:
2589 return "no";
2590 case 1:
2591 return "yes";
2592 default:
2593 return "UNKNOWN";
2594 }
2595 }
2596}
2597
2598static const char *
2599lookup_opcode_name(OpCodes code)
2600{
2601 u_int i;
2602
2603 for (i = 0; keywords[i].name != NULL; i++)
2604 if (keywords[i].opcode == code)
2605 return(keywords[i].name);
2606 return "UNKNOWN";
2607}
2608
2609static void
2610dump_cfg_int(OpCodes code, int val)
2611{
2612 printf("%s %d\n", lookup_opcode_name(code), val);
2613}
2614
2615static void
2616dump_cfg_fmtint(OpCodes code, int val)
2617{
2618 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2619}
2620
2621static void
2622dump_cfg_string(OpCodes code, const char *val)
2623{
2624 if (val == NULL)
2625 return;
2626 printf("%s %s\n", lookup_opcode_name(code), val);
2627}
2628
2629static void
2630dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2631{
2632 u_int i;
2633
2634 for (i = 0; i < count; i++)
2635 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2636}
2637
2638static void
2639dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2640{
2641 u_int i;
2642
2643 printf("%s", lookup_opcode_name(code));
2644 for (i = 0; i < count; i++)
2645 printf(" %s", vals[i]);
2646 printf("\n");
2647}
2648
2649static void
2650dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2651{
2652 const struct Forward *fwd;
2653 u_int i;
2654
2655 /* oDynamicForward */
2656 for (i = 0; i < count; i++) {
2657 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002658 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002659 strcmp(fwd->connect_host, "socks") != 0)
2660 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002661 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002662 strcmp(fwd->connect_host, "socks") == 0)
2663 continue;
2664 printf("%s", lookup_opcode_name(code));
2665 if (fwd->listen_port == PORT_STREAMLOCAL)
2666 printf(" %s", fwd->listen_path);
2667 else if (fwd->listen_host == NULL)
2668 printf(" %d", fwd->listen_port);
2669 else {
2670 printf(" [%s]:%d",
2671 fwd->listen_host, fwd->listen_port);
2672 }
2673 if (code != oDynamicForward) {
2674 if (fwd->connect_port == PORT_STREAMLOCAL)
2675 printf(" %s", fwd->connect_path);
2676 else if (fwd->connect_host == NULL)
2677 printf(" %d", fwd->connect_port);
2678 else {
2679 printf(" [%s]:%d",
2680 fwd->connect_host, fwd->connect_port);
2681 }
2682 }
2683 printf("\n");
2684 }
2685}
2686
2687void
2688dump_client_config(Options *o, const char *host)
2689{
djm@openbsd.orgc7288482020-02-06 22:46:31 +00002690 int i, r;
2691 char buf[8], *all_key;
2692
2693 /*
2694 * Expand HostKeyAlgorithms name lists. This isn't handled in
2695 * fill_default_options() like the other algorithm lists because
2696 * the host key algorithms are by default dynamically chosen based
2697 * on the host's keys found in known_hosts.
2698 */
2699 all_key = sshkey_alg_list(0, 0, 1, ',');
2700 if ((r = kex_assemble_names(&o->hostkeyalgorithms, kex_default_pk_alg(),
2701 all_key)) != 0)
2702 fatal("%s: expand HostKeyAlgorithms: %s", __func__, ssh_err(r));
2703 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002704
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002705 /* Most interesting options first: user, host, port */
2706 dump_cfg_string(oUser, o->user);
jmc@openbsd.org73491492019-06-12 11:31:50 +00002707 dump_cfg_string(oHostname, host);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002708 dump_cfg_int(oPort, o->port);
2709
2710 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002711 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002712 dump_cfg_fmtint(oAddressFamily, o->address_family);
2713 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2714 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2715 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2716 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2717 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2718 dump_cfg_fmtint(oCompression, o->compression);
2719 dump_cfg_fmtint(oControlMaster, o->control_master);
2720 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002721 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002722 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002723 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002724 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2725 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2726 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2727#ifdef GSSAPI
2728 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2729 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2730#endif /* GSSAPI */
2731 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2732 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2733 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2734 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2735 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2736 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2737 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002738 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2739 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2740 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002741 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2742 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2743 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2744 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002745 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2746 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002747 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002748
2749 /* Integer options */
2750 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002751 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2752 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2753 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2754 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2755 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2756
2757 /* String options */
2758 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002759 dump_cfg_string(oBindInterface, o->bind_interface);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002760 dump_cfg_string(oCiphers, o->ciphers);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002761 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002762 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002763 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002764 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002765 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002766 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002767 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002768 dump_cfg_string(oKexAlgorithms, o->kex_algorithms);
2769 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002770 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002771 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002772 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002773 dump_cfg_string(oMacs, o->macs);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002774#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002775 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002776#endif
djm@openbsd.org884416b2019-10-31 21:18:28 +00002777 dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002778 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002779 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002780 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002781 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002782
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002783 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002784 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2785 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2786 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2787
2788 /* String array options */
2789 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2790 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002791 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002792 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2793 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2794 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002795 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002796
2797 /* Special cases */
2798
djm@openbsd.org40be78f2019-12-21 02:19:13 +00002799 /* oForwardAgent */
2800 if (o->forward_agent_sock_path == NULL)
2801 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2802 else
2803 dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
2804
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002805 /* oConnectTimeout */
2806 if (o->connection_timeout == -1)
2807 printf("connecttimeout none\n");
2808 else
2809 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2810
2811 /* oTunnelDevice */
2812 printf("tunneldevice");
2813 if (o->tun_local == SSH_TUNID_ANY)
2814 printf(" any");
2815 else
2816 printf(" %d", o->tun_local);
2817 if (o->tun_remote == SSH_TUNID_ANY)
2818 printf(":any");
2819 else
2820 printf(":%d", o->tun_remote);
2821 printf("\n");
2822
2823 /* oCanonicalizePermittedCNAMEs */
2824 if ( o->num_permitted_cnames > 0) {
2825 printf("canonicalizePermittedcnames");
2826 for (i = 0; i < o->num_permitted_cnames; i++) {
2827 printf(" %s:%s", o->permitted_cnames[i].source_list,
2828 o->permitted_cnames[i].target_list);
2829 }
2830 printf("\n");
2831 }
2832
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002833 /* oControlPersist */
2834 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2835 dump_cfg_fmtint(oControlPersist, o->control_persist);
2836 else
2837 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2838
2839 /* oEscapeChar */
2840 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2841 printf("escapechar none\n");
2842 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002843 vis(buf, o->escape_char, VIS_WHITE, 0);
2844 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002845 }
2846
2847 /* oIPQoS */
2848 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2849 printf("%s\n", iptos2str(o->ip_qos_bulk));
2850
2851 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002852 printf("rekeylimit %llu %d\n",
2853 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002854
2855 /* oStreamLocalBindMask */
2856 printf("streamlocalbindmask 0%o\n",
2857 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002858
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002859 /* oLogFacility */
2860 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2861
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002862 /* oProxyCommand / oProxyJump */
2863 if (o->jump_host == NULL)
2864 dump_cfg_string(oProxyCommand, o->proxy_command);
2865 else {
2866 /* Check for numeric addresses */
2867 i = strchr(o->jump_host, ':') != NULL ||
2868 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2869 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2870 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002871 /* optional additional jump spec */
2872 o->jump_extra == NULL ? "" : o->jump_extra,
2873 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002874 /* optional user */
2875 o->jump_user == NULL ? "" : o->jump_user,
2876 o->jump_user == NULL ? "" : "@",
2877 /* opening [ if hostname is numeric */
2878 i ? "[" : "",
2879 /* mandatory hostname */
2880 o->jump_host,
2881 /* closing ] if hostname is numeric */
2882 i ? "]" : "",
2883 /* optional port number */
2884 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002885 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002886 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002887}