blob: 3d99367c35845a70609990479914f95be6884813 [file] [log] [blame]
markus@openbsd.org2c557442019-11-12 19:33:08 +00001/* $OpenBSD: readconf.c,v 1.311 2019/11/12 19:33:08 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +000056#include "ssherr.h"
Damien Miller78928792000-04-12 20:17:38 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
59#include "pathnames.h"
60#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000061#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100062#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "readconf.h"
64#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "kex.h"
66#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110067#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000068#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000069#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Format of the configuration file:
72
73 # Configuration data is parsed as follows:
74 # 1. command line options
75 # 2. user-specific file
76 # 3. system-wide file
77 # Any configuration value is only changed the first time it is set.
78 # Thus, host-specific definitions should be at the beginning of the
79 # configuration file, and defaults at the end.
80
81 # Host-specific declarations. These may override anything above. A single
82 # host may match multiple declarations; these are processed in the order
83 # that they are given in.
84
85 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host fake.com
jmc@openbsd.org73491492019-06-12 11:31:50 +000089 Hostname another.host.name.real.org
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 User blaah
91 Port 34289
92 ForwardX11 no
93 ForwardAgent no
94
95 Host books.com
96 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000097 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host fascist.blob.com
100 Port 23123
101 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 PasswordAuthentication no
103
104 Host puukko.hut.fi
105 User t35124p
106 ProxyCommand ssh-proxy %h %p
107
108 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000112 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 PasswordAuthentication no
114
Damien Millerd27b9472005-12-13 19:29:02 +1100115 Host vpn.fake.com
116 Tunnel yes
117 TunnelDevice 3
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 # Defaults for various options
120 Host *
121 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 PasswordAuthentication yes
124 RSAAuthentication yes
125 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100127 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 IdentityFile ~/.ssh/identity
129 Port 22
130 EscapeChar ~
131
132*/
133
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000134static int read_config_file_depth(const char *filename, struct passwd *pw,
135 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000136 int flags, int *activep, int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000137static int process_config_line_depth(Options *options, struct passwd *pw,
138 const char *host, const char *original_host, char *line,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000139 const char *filename, int linenum, int *activep, int flags,
140 int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000141
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142/* Keyword tokens. */
143
Damien Miller95def091999-11-25 00:26:21 +1100144typedef enum {
145 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000146 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000147 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
148 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000149 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000150 oChallengeResponseAuthentication, oXAuthLocation,
jmc@openbsd.org73491492019-06-12 11:31:50 +0000151 oIdentityFile, oHostname, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000152 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100153 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100154 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
155 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100156 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000157 oUsePrivilegedPort, oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000158 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000159 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000160 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000161 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000162 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000163 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000164 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100165 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000166 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000167 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000168 oTunnel, oTunnelDevice,
169 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000170 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000171 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100172 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
173 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000174 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000175 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000176 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
djm@openbsd.org884416b2019-10-31 21:18:28 +0000177 oSecurityKeyProvider,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000178 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179} OpCodes;
180
181/* Textual representations of the tokens. */
182
Damien Miller95def091999-11-25 00:26:21 +1100183static struct {
184 const char *name;
185 OpCodes opcode;
186} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000187 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000188 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000189 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000190 { "fallbacktorsh", oDeprecated },
191 { "globalknownhostsfile2", oDeprecated },
192 { "rhostsauthentication", oDeprecated },
193 { "userknownhostsfile2", oDeprecated },
194 { "useroaming", oDeprecated },
195 { "usersh", oDeprecated },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000196 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000197
198 /* Unsupported options */
199 { "afstokenpassing", oUnsupported },
200 { "kerberosauthentication", oUnsupported },
201 { "kerberostgtpassing", oUnsupported },
202
203 /* Sometimes-unsupported options */
204#if defined(GSSAPI)
205 { "gssapiauthentication", oGssAuthentication },
206 { "gssapidelegatecredentials", oGssDelegateCreds },
207# else
208 { "gssapiauthentication", oUnsupported },
209 { "gssapidelegatecredentials", oUnsupported },
210#endif
211#ifdef ENABLE_PKCS11
djm@openbsd.org381a2612017-01-30 00:38:50 +0000212 { "pkcs11provider", oPKCS11Provider },
djm@openbsd.org95a80582019-03-01 02:08:50 +0000213 { "smartcarddevice", oPKCS11Provider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000214# else
215 { "smartcarddevice", oUnsupported },
216 { "pkcs11provider", oUnsupported },
217#endif
djm@openbsd.org884416b2019-10-31 21:18:28 +0000218 { "securitykeyprovider", oSecurityKeyProvider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000219 { "rsaauthentication", oUnsupported },
220 { "rhostsrsaauthentication", oUnsupported },
221 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000222
Damien Miller95def091999-11-25 00:26:21 +1100223 { "forwardagent", oForwardAgent },
224 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000225 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000226 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000227 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000228 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100229 { "gatewayports", oGatewayPorts },
Damien Miller95def091999-11-25 00:26:21 +1100230 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100231 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
232 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100233 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000234 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000235 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000236 { "challengeresponseauthentication", oChallengeResponseAuthentication },
Damien Miller87f08be2018-07-20 13:18:28 +1000237 { "skeyauthentication", oUnsupported },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000238 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100239 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100240 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100241 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000242 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000243 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000244 { "identityagent", oIdentityAgent },
jmc@openbsd.org73491492019-06-12 11:31:50 +0000245 { "hostname", oHostname },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000246 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100247 { "proxycommand", oProxyCommand },
248 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000249 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000250 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100251 { "remoteforward", oRemoteForward },
252 { "localforward", oLocalForward },
253 { "user", oUser },
254 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100255 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100256 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100257 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100258 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100259 { "connectionattempts", oConnectionAttempts },
260 { "batchmode", oBatchMode },
261 { "checkhostip", oCheckHostIP },
262 { "stricthostkeychecking", oStrictHostKeyChecking },
263 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100264 { "tcpkeepalive", oTCPKeepAlive },
265 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100266 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000267 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100268 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000269 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000270 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000271 { "hostkeyalgorithms", oHostKeyAlgorithms },
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000272 { "casignaturealgorithms", oCASignatureAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000273 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000274 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100275 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000276 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000277 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100278 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000279 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000280 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000281 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100282 { "serveraliveinterval", oServerAliveInterval },
283 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000284 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000285 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000286 { "controlpath", oControlPath },
287 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000288 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100289 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000290 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100291 { "tunnel", oTunnel },
292 { "tunneldevice", oTunnelDevice },
293 { "localcommand", oLocalCommand },
294 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000295 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000296 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000297 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100298 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000299 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000300 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100301 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100302 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
303 { "canonicalizehostname", oCanonicalizeHostname },
304 { "canonicalizemaxdots", oCanonicalizeMaxDots },
305 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000306 { "streamlocalbindmask", oStreamLocalBindMask },
307 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000308 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000309 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000310 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000311 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000312 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000313 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000314 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100315
Ben Lindstrom65366a82001-12-06 16:32:47 +0000316 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100317};
318
Damien Miller5428f641999-11-25 11:54:57 +1100319/*
320 * Adds a local TCP/IP port forward to options. Never returns if there is an
321 * error.
322 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller4af51302000-04-16 11:18:38 +1000324void
Damien Miller7acefbb2014-07-18 14:11:24 +1000325add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Miller7acefbb2014-07-18 14:11:24 +1000327 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000328 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000329
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000330 /* Don't add duplicates */
331 for (i = 0; i < options->num_local_forwards; i++) {
332 if (forward_equals(newfwd, options->local_forwards + i))
333 return;
334 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000335 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000336 options->num_local_forwards + 1,
337 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100338 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339
Damien Miller1a0442f2008-11-05 16:30:06 +1100340 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100343 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100344 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000345 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
Damien Miller5428f641999-11-25 11:54:57 +1100348/*
349 * Adds a remote TCP/IP port forward to options. Never returns if there is
350 * an error.
351 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller4af51302000-04-16 11:18:38 +1000353void
Damien Miller7acefbb2014-07-18 14:11:24 +1000354add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller7acefbb2014-07-18 14:11:24 +1000356 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000357 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000358
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000359 /* Don't add duplicates */
360 for (i = 0; i < options->num_remote_forwards; i++) {
361 if (forward_equals(newfwd, options->remote_forwards + i))
362 return;
363 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000364 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000365 options->num_remote_forwards + 1,
366 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100367 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368
Damien Miller1a0442f2008-11-05 16:30:06 +1100369 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000371 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100372 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100373 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000374 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100375 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000376 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000379static void
380clear_forwardings(Options *options)
381{
382 int i;
383
Damien Millerf91ee4c2005-03-01 21:24:33 +1100384 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000386 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000387 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000388 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100389 }
Damien Miller232cfb12010-06-26 09:50:30 +1000390 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000392 options->local_forwards = NULL;
393 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000394 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100395 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000399 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100400 }
Damien Miller232cfb12010-06-26 09:50:30 +1000401 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000402 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000403 options->remote_forwards = NULL;
404 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100406 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000407}
408
Darren Tucker19104782013-04-05 11:13:08 +1100409void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000410add_certificate_file(Options *options, const char *path, int userprovided)
411{
412 int i;
413
414 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
415 fatal("Too many certificate files specified (max %d)",
416 SSH_MAX_CERTIFICATE_FILES);
417
418 /* Avoid registering duplicates */
419 for (i = 0; i < options->num_certificate_files; i++) {
420 if (options->certificate_file_userprovided[i] == userprovided &&
421 strcmp(options->certificate_files[i], path) == 0) {
422 debug2("%s: ignoring duplicate key %s", __func__, path);
423 return;
424 }
425 }
426
427 options->certificate_file_userprovided[options->num_certificate_files] =
428 userprovided;
429 options->certificate_files[options->num_certificate_files++] =
430 xstrdup(path);
431}
432
433void
Darren Tucker19104782013-04-05 11:13:08 +1100434add_identity_file(Options *options, const char *dir, const char *filename,
435 int userprovided)
436{
437 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000438 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100439
440 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
441 fatal("Too many identity files specified (max %d)",
442 SSH_MAX_IDENTITY_FILES);
443
444 if (dir == NULL) /* no dir, filename is absolute */
445 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000446 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
447 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100448
Damien Miller15271902014-05-15 13:47:56 +1000449 /* Avoid registering duplicates */
450 for (i = 0; i < options->num_identity_files; i++) {
451 if (options->identity_file_userprovided[i] == userprovided &&
452 strcmp(options->identity_files[i], path) == 0) {
453 debug2("%s: ignoring duplicate key %s", __func__, path);
454 free(path);
455 return;
456 }
457 }
458
Darren Tucker19104782013-04-05 11:13:08 +1100459 options->identity_file_userprovided[options->num_identity_files] =
460 userprovided;
461 options->identity_files[options->num_identity_files++] = path;
462}
463
Damien Miller194fd902013-10-15 12:13:05 +1100464int
465default_ssh_port(void)
466{
467 static int port;
468 struct servent *sp;
469
470 if (port == 0) {
471 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
472 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
473 }
474 return port;
475}
476
477/*
478 * Execute a command in a shell.
479 * Return its exit status or -1 on abnormal exit.
480 */
481static int
482execute_in_shell(const char *cmd)
483{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000484 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100485 pid_t pid;
486 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100487
488 if ((shell = getenv("SHELL")) == NULL)
489 shell = _PATH_BSHELL;
490
djm@openbsd.org9ab5b942019-08-09 05:05:54 +0000491 if (access(shell, X_OK) == -1) {
492 fatal("Shell \"%s\" is not executable: %s",
493 shell, strerror(errno));
494 }
495
Damien Miller194fd902013-10-15 12:13:05 +1100496 /* Need this to redirect subprocess stdin/out */
497 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
498 fatal("open(/dev/null): %s", strerror(errno));
499
500 debug("Executing command: '%.500s'", cmd);
501
502 /* Fork and execute the command. */
503 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000504 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100505
Damien Miller194fd902013-10-15 12:13:05 +1100506 /* Redirect child stdin and stdout. Leave stderr */
507 if (dup2(devnull, STDIN_FILENO) == -1)
508 fatal("dup2: %s", strerror(errno));
509 if (dup2(devnull, STDOUT_FILENO) == -1)
510 fatal("dup2: %s", strerror(errno));
511 if (devnull > STDERR_FILENO)
512 close(devnull);
513 closefrom(STDERR_FILENO + 1);
514
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000515 argv[0] = shell;
516 argv[1] = "-c";
517 argv[2] = xstrdup(cmd);
518 argv[3] = NULL;
519
Damien Miller194fd902013-10-15 12:13:05 +1100520 execv(argv[0], argv);
521 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
522 /* Die with signal to make this error apparent to parent. */
523 signal(SIGTERM, SIG_DFL);
524 kill(getpid(), SIGTERM);
525 _exit(1);
526 }
527 /* Parent. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000528 if (pid == -1)
Damien Miller194fd902013-10-15 12:13:05 +1100529 fatal("%s: fork: %.100s", __func__, strerror(errno));
530
531 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100532
533 while (waitpid(pid, &status, 0) == -1) {
534 if (errno != EINTR && errno != EAGAIN)
535 fatal("%s: waitpid: %s", __func__, strerror(errno));
536 }
537 if (!WIFEXITED(status)) {
538 error("command '%.100s' exited abnormally", cmd);
539 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000540 }
Damien Miller194fd902013-10-15 12:13:05 +1100541 debug3("command returned status %d", WEXITSTATUS(status));
542 return WEXITSTATUS(status);
543}
544
545/*
546 * Parse and execute a Match directive.
547 */
548static int
549match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000550 const char *host_arg, const char *original_host, int final_pass,
551 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100552{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100554 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000555 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100556 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000557 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100558
559 /*
560 * Configuration is likely to be incomplete at this point so we
561 * must be prepared to use default values.
562 */
563 port = options->port <= 0 ? default_ssh_port() : options->port;
564 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000565 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000566 host = xstrdup(options->hostname);
567 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100568 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100569 host = percent_expand(options->hostname,
570 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000571 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100572 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000573 }
Damien Miller194fd902013-10-15 12:13:05 +1100574
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000575 debug2("checking match for '%s' host %s originally %s",
576 cp, host, original_host);
577 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
578 criteria = NULL;
579 this_result = 1;
580 if ((negate = attrib[0] == '!'))
581 attrib++;
582 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100583 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100585 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 error("%.200s line %d: '%s' cannot be combined "
587 "with other Match attributes",
588 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100589 result = -1;
590 goto out;
591 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000592 if (result)
593 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100594 goto out;
595 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000596 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000597 if (strcasecmp(attrib, "canonical") == 0 ||
598 strcasecmp(attrib, "final") == 0) {
599 /*
600 * If the config requests "Match final" then remember
601 * this so we can perform a second pass later.
602 */
603 if (strcasecmp(attrib, "final") == 0 &&
604 want_final_pass != NULL)
605 *want_final_pass = 1;
606 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000607 if (r == (negate ? 1 : 0))
608 this_result = result = 0;
609 debug3("%.200s line %d: %smatched '%s'",
610 filename, linenum,
611 this_result ? "" : "not ", oattrib);
612 continue;
613 }
614 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100615 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
616 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100617 result = -1;
618 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100619 }
Damien Miller194fd902013-10-15 12:13:05 +1100620 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000622 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 if (r == (negate ? 1 : 0))
624 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100625 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000626 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000627 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000628 if (r == (negate ? 1 : 0))
629 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100630 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000631 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000632 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000633 if (r == (negate ? 1 : 0))
634 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100635 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000637 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000638 if (r == (negate ? 1 : 0))
639 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100640 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100641 if (gethostname(thishost, sizeof(thishost)) == -1)
642 fatal("gethostname: %s", strerror(errno));
643 strlcpy(shorthost, thishost, sizeof(shorthost));
644 shorthost[strcspn(thishost, ".")] = '\0';
645 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000646 snprintf(uidstr, sizeof(uidstr), "%llu",
647 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100648
649 cmd = percent_expand(arg,
650 "L", shorthost,
651 "d", pw->pw_dir,
652 "h", host,
653 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000654 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100655 "p", portstr,
656 "r", ruser,
657 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000658 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100659 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100660 if (result != 1) {
661 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000662 debug3("%.200s line %d: skipped exec "
663 "\"%.100s\"", filename, linenum, cmd);
664 free(cmd);
665 continue;
Damien Miller06287802014-02-24 15:56:45 +1100666 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000667 r = execute_in_shell(cmd);
668 if (r == -1) {
669 fatal("%.200s line %d: match exec "
670 "'%.100s' error", filename,
671 linenum, cmd);
672 }
673 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100674 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000675 /* Force exit status to boolean */
676 r = r == 0;
677 if (r == (negate ? 1 : 0))
678 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100679 } else {
680 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100681 result = -1;
682 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100683 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000684 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
685 filename, linenum, this_result ? "": "not ",
686 oattrib, criteria);
687 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100688 }
Damien Millercf31f382013-10-24 21:02:56 +1100689 if (attributes == 0) {
690 error("One or more attributes required for Match");
691 result = -1;
692 goto out;
693 }
Damien Miller084bcd22013-10-23 16:30:51 +1100694 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000695 if (result != -1)
696 debug2("match %sfound", result ? "" : "not ");
697 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100698 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100699 return result;
700}
701
djm@openbsd.org555294a2018-04-06 13:02:39 +0000702/* Remove environment variable by pattern */
703static void
704rm_env(Options *options, const char *arg, const char *filename, int linenum)
705{
706 int i, j;
707 char *cp;
708
709 /* Remove an environment variable */
710 for (i = 0; i < options->num_send_env; ) {
711 cp = xstrdup(options->send_env[i]);
712 if (!match_pattern(cp, arg + 1)) {
713 free(cp);
714 i++;
715 continue;
716 }
717 debug3("%s line %d: removing environment %s",
718 filename, linenum, cp);
719 free(cp);
720 free(options->send_env[i]);
721 options->send_env[i] = NULL;
722 for (j = i; j < options->num_send_env - 1; j++) {
723 options->send_env[j] = options->send_env[j + 1];
724 options->send_env[j + 1] = NULL;
725 }
726 options->num_send_env--;
727 /* NB. don't increment i */
728 }
729}
730
Damien Miller5428f641999-11-25 11:54:57 +1100731/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000732 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100733 */
Damien Miller4af51302000-04-16 11:18:38 +1000734static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000735parse_token(const char *cp, const char *filename, int linenum,
736 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737{
Darren Tucker07636982013-05-16 20:30:03 +1000738 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739
Damien Miller95def091999-11-25 00:26:21 +1100740 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000741 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100742 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000743 if (ignored_unknown != NULL &&
744 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000745 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000746 error("%s: line %d: Bad configuration option: %s",
747 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100748 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749}
750
Damien Millere9fc72e2013-10-15 12:14:12 +1100751/* Multistate option parsing */
752struct multistate {
753 char *key;
754 int value;
755};
756static const struct multistate multistate_flag[] = {
757 { "true", 1 },
758 { "false", 0 },
759 { "yes", 1 },
760 { "no", 0 },
761 { NULL, -1 }
762};
763static const struct multistate multistate_yesnoask[] = {
764 { "true", 1 },
765 { "false", 0 },
766 { "yes", 1 },
767 { "no", 0 },
768 { "ask", 2 },
769 { NULL, -1 }
770};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000771static const struct multistate multistate_strict_hostkey[] = {
772 { "true", SSH_STRICT_HOSTKEY_YES },
773 { "false", SSH_STRICT_HOSTKEY_OFF },
774 { "yes", SSH_STRICT_HOSTKEY_YES },
775 { "no", SSH_STRICT_HOSTKEY_OFF },
776 { "ask", SSH_STRICT_HOSTKEY_ASK },
777 { "off", SSH_STRICT_HOSTKEY_OFF },
778 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
779 { NULL, -1 }
780};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000781static const struct multistate multistate_yesnoaskconfirm[] = {
782 { "true", 1 },
783 { "false", 0 },
784 { "yes", 1 },
785 { "no", 0 },
786 { "ask", 2 },
787 { "confirm", 3 },
788 { NULL, -1 }
789};
Damien Millere9fc72e2013-10-15 12:14:12 +1100790static const struct multistate multistate_addressfamily[] = {
791 { "inet", AF_INET },
792 { "inet6", AF_INET6 },
793 { "any", AF_UNSPEC },
794 { NULL, -1 }
795};
796static const struct multistate multistate_controlmaster[] = {
797 { "true", SSHCTL_MASTER_YES },
798 { "yes", SSHCTL_MASTER_YES },
799 { "false", SSHCTL_MASTER_NO },
800 { "no", SSHCTL_MASTER_NO },
801 { "auto", SSHCTL_MASTER_AUTO },
802 { "ask", SSHCTL_MASTER_ASK },
803 { "autoask", SSHCTL_MASTER_AUTO_ASK },
804 { NULL, -1 }
805};
806static const struct multistate multistate_tunnel[] = {
807 { "ethernet", SSH_TUNMODE_ETHERNET },
808 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
809 { "true", SSH_TUNMODE_DEFAULT },
810 { "yes", SSH_TUNMODE_DEFAULT },
811 { "false", SSH_TUNMODE_NO },
812 { "no", SSH_TUNMODE_NO },
813 { NULL, -1 }
814};
815static const struct multistate multistate_requesttty[] = {
816 { "true", REQUEST_TTY_YES },
817 { "yes", REQUEST_TTY_YES },
818 { "false", REQUEST_TTY_NO },
819 { "no", REQUEST_TTY_NO },
820 { "force", REQUEST_TTY_FORCE },
821 { "auto", REQUEST_TTY_AUTO },
822 { NULL, -1 }
823};
Damien Miller38505592013-10-17 11:48:13 +1100824static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100825 { "true", SSH_CANONICALISE_YES },
826 { "false", SSH_CANONICALISE_NO },
827 { "yes", SSH_CANONICALISE_YES },
828 { "no", SSH_CANONICALISE_NO },
829 { "always", SSH_CANONICALISE_ALWAYS },
830 { NULL, -1 }
831};
Damien Millere9fc72e2013-10-15 12:14:12 +1100832
Damien Miller5428f641999-11-25 11:54:57 +1100833/*
834 * Processes a single option line as used in the configuration files. This
835 * only sets those values that have not already been set.
836 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100837int
Damien Miller194fd902013-10-15 12:13:05 +1100838process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000839 const char *original_host, char *line, const char *filename,
840 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000842 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000843 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000844}
845
846#define WHITESPACE " \t\r\n"
847static int
848process_config_line_depth(Options *options, struct passwd *pw, const char *host,
849 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000850 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000851{
Damien Miller295ee632011-05-29 21:42:31 +1000852 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
853 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000854 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000855 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000856 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100857 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000858 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000859 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100860 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000861 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100862 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100863 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000864 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000865 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Damien Miller194fd902013-10-15 12:13:05 +1100867 if (activep == NULL) { /* We are processing a command line directive */
868 cmdline = 1;
869 activep = &cmdline;
870 }
871
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000872 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000873 if ((len = strlen(line)) == 0)
874 return 0;
875 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000876 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000877 break;
878 line[len] = '\0';
879 }
880
Damien Millerbe484b52000-07-15 14:14:16 +1000881 s = line;
882 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100883 if ((keyword = strdelim(&s)) == NULL)
884 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000885 /* Ignore leading whitespace. */
886 if (*keyword == '\0')
887 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000888 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100889 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000890 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100891 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Darren Tucker07636982013-05-16 20:30:03 +1000893 opcode = parse_token(keyword, filename, linenum,
894 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895
Damien Miller95def091999-11-25 00:26:21 +1100896 switch (opcode) {
897 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100898 /* don't panic, but count bad options */
899 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000900 case oIgnore:
901 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000902 case oIgnoredUnknownOption:
903 debug("%s line %d: Ignored unknown option \"%s\"",
904 filename, linenum, keyword);
905 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000906 case oConnectTimeout:
907 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100908parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000909 arg = strdelim(&s);
910 if (!arg || *arg == '\0')
911 fatal("%s line %d: missing time value.",
912 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000913 if (strcmp(arg, "none") == 0)
914 value = -1;
915 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000916 fatal("%s line %d: invalid time value.",
917 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100918 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000919 *intptr = value;
920 break;
921
Damien Miller95def091999-11-25 00:26:21 +1100922 case oForwardAgent:
923 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100924 parse_flag:
925 multistate_ptr = multistate_flag;
926 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000927 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000928 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100929 fatal("%s line %d: missing argument.",
930 filename, linenum);
931 value = -1;
932 for (i = 0; multistate_ptr[i].key != NULL; i++) {
933 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
934 value = multistate_ptr[i].value;
935 break;
936 }
937 }
938 if (value == -1)
939 fatal("%s line %d: unsupported option \"%s\".",
940 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100941 if (*activep && *intptr == -1)
942 *intptr = value;
943 break;
944
945 case oForwardX11:
946 intptr = &options->forward_x11;
947 goto parse_flag;
948
Darren Tucker0a118da2003-10-15 15:54:32 +1000949 case oForwardX11Trusted:
950 intptr = &options->forward_x11_trusted;
951 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000952
Damien Miller1ab6a512010-06-26 10:02:24 +1000953 case oForwardX11Timeout:
954 intptr = &options->forward_x11_timeout;
955 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000956
Damien Miller95def091999-11-25 00:26:21 +1100957 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000958 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100959 goto parse_flag;
960
Darren Tuckere7d4b192006-07-12 22:17:10 +1000961 case oExitOnForwardFailure:
962 intptr = &options->exit_on_forward_failure;
963 goto parse_flag;
964
Damien Miller95def091999-11-25 00:26:21 +1100965 case oPasswordAuthentication:
966 intptr = &options->password_authentication;
967 goto parse_flag;
968
Damien Miller874d77b2000-10-14 16:23:11 +1100969 case oKbdInteractiveAuthentication:
970 intptr = &options->kbd_interactive_authentication;
971 goto parse_flag;
972
973 case oKbdInteractiveDevices:
974 charptr = &options->kbd_interactive_devices;
975 goto parse_string;
976
Damien Miller0bc1bd82000-11-13 22:57:25 +1100977 case oPubkeyAuthentication:
978 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000979 goto parse_flag;
980
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000981 case oHostbasedAuthentication:
982 intptr = &options->hostbased_authentication;
983 goto parse_flag;
984
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000985 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000986 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100987 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000988
Darren Tucker0efd1552003-08-26 11:49:55 +1000989 case oGssAuthentication:
990 intptr = &options->gss_authentication;
991 goto parse_flag;
992
993 case oGssDelegateCreds:
994 intptr = &options->gss_deleg_creds;
995 goto parse_flag;
996
Damien Miller95def091999-11-25 00:26:21 +1100997 case oBatchMode:
998 intptr = &options->batch_mode;
999 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001 case oCheckHostIP:
1002 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +10001003 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Damien Miller37876e92003-05-15 10:19:46 +10001005 case oVerifyHostKeyDNS:
1006 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001007 multistate_ptr = multistate_yesnoask;
1008 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001009
Damien Miller95def091999-11-25 00:26:21 +11001010 case oStrictHostKeyChecking:
1011 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001012 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001013 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Miller95def091999-11-25 00:26:21 +11001015 case oCompression:
1016 intptr = &options->compression;
1017 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller12c150e2003-12-17 16:31:10 +11001019 case oTCPKeepAlive:
1020 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001021 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001023 case oNoHostAuthenticationForLocalhost:
1024 intptr = &options->no_host_authentication_for_localhost;
1025 goto parse_flag;
1026
Damien Miller95def091999-11-25 00:26:21 +11001027 case oNumberOfPasswordPrompts:
1028 intptr = &options->number_of_password_prompts;
1029 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Millera5539d22003-04-09 20:50:06 +10001031 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001032 arg = strdelim(&s);
1033 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001034 fatal("%.200s line %d: Missing argument.", filename,
1035 linenum);
1036 if (strcmp(arg, "default") == 0) {
1037 val64 = 0;
1038 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001039 if (scan_scaled(arg, &val64) == -1)
1040 fatal("%.200s line %d: Bad number '%s': %s",
1041 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001042 if (val64 != 0 && val64 < 16)
1043 fatal("%.200s line %d: RekeyLimit too small",
1044 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001045 }
Damien Miller3dff1762008-02-10 22:25:52 +11001046 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001047 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001048 if (s != NULL) { /* optional rekey interval present */
1049 if (strcmp(s, "none") == 0) {
1050 (void)strdelim(&s); /* discard */
1051 break;
1052 }
1053 intptr = &options->rekey_interval;
1054 goto parse_time;
1055 }
Damien Millera5539d22003-04-09 20:50:06 +10001056 break;
1057
Damien Miller95def091999-11-25 00:26:21 +11001058 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001059 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001060 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001061 fatal("%.200s line %d: Missing argument.", filename, linenum);
1062 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001064 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001065 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001066 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001067 add_identity_file(options, NULL,
1068 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001069 }
1070 break;
1071
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001072 case oCertificateFile:
1073 arg = strdelim(&s);
1074 if (!arg || *arg == '\0')
1075 fatal("%.200s line %d: Missing argument.",
1076 filename, linenum);
1077 if (*activep) {
1078 intptr = &options->num_certificate_files;
1079 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1080 fatal("%.200s line %d: Too many certificate "
1081 "files specified (max %d).",
1082 filename, linenum,
1083 SSH_MAX_CERTIFICATE_FILES);
1084 }
1085 add_certificate_file(options, arg,
1086 flags & SSHCONF_USERCONF);
1087 }
1088 break;
1089
Damien Millerd3a18572000-06-07 19:55:44 +10001090 case oXAuthLocation:
1091 charptr=&options->xauth_location;
1092 goto parse_string;
1093
Damien Miller95def091999-11-25 00:26:21 +11001094 case oUser:
1095 charptr = &options->user;
1096parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001097 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001098 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001099 fatal("%.200s line %d: Missing argument.",
1100 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001101 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001102 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001103 break;
1104
1105 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001106 cpptr = (char **)&options->system_hostfiles;
1107 uintptr = &options->num_system_hostfiles;
1108 max_entries = SSH_MAX_HOSTS_FILES;
1109parse_char_array:
1110 if (*activep && *uintptr == 0) {
1111 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1112 if ((*uintptr) >= max_entries)
1113 fatal("%s line %d: "
1114 "too many authorized keys files.",
1115 filename, linenum);
1116 cpptr[(*uintptr)++] = xstrdup(arg);
1117 }
1118 }
1119 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001120
1121 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001122 cpptr = (char **)&options->user_hostfiles;
1123 uintptr = &options->num_user_hostfiles;
1124 max_entries = SSH_MAX_HOSTS_FILES;
1125 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001126
jmc@openbsd.org73491492019-06-12 11:31:50 +00001127 case oHostname:
Damien Miller95def091999-11-25 00:26:21 +11001128 charptr = &options->hostname;
1129 goto parse_string;
1130
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001131 case oHostKeyAlias:
1132 charptr = &options->host_key_alias;
1133 goto parse_string;
1134
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001135 case oPreferredAuthentications:
1136 charptr = &options->preferred_authentications;
1137 goto parse_string;
1138
Ben Lindstrome0f88042001-04-30 13:06:24 +00001139 case oBindAddress:
1140 charptr = &options->bind_address;
1141 goto parse_string;
1142
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001143 case oBindInterface:
1144 charptr = &options->bind_interface;
1145 goto parse_string;
1146
Damien Miller7ea845e2010-02-12 09:21:02 +11001147 case oPKCS11Provider:
1148 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001149 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001150
djm@openbsd.org884416b2019-10-31 21:18:28 +00001151 case oSecurityKeyProvider:
1152 charptr = &options->sk_provider;
1153 goto parse_string;
1154
Damien Miller95def091999-11-25 00:26:21 +11001155 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001156 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001157 /* Ignore ProxyCommand if ProxyJump already specified */
1158 if (options->jump_host != NULL)
1159 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001160parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001161 if (s == NULL)
1162 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001163 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001164 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001165 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001166 return 0;
1167
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001168 case oProxyJump:
1169 if (s == NULL) {
1170 fatal("%.200s line %d: Missing argument.",
1171 filename, linenum);
1172 }
1173 len = strspn(s, WHITESPACE "=");
1174 if (parse_jump(s + len, options, *activep) == -1) {
1175 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1176 filename, linenum, s + len);
1177 }
1178 return 0;
1179
Damien Miller95def091999-11-25 00:26:21 +11001180 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001181 arg = strdelim(&s);
1182 if (!arg || *arg == '\0')
1183 fatal("%.200s line %d: Missing argument.",
1184 filename, linenum);
1185 value = a2port(arg);
1186 if (value <= 0)
1187 fatal("%.200s line %d: Bad port '%s'.",
1188 filename, linenum, arg);
1189 if (*activep && options->port == -1)
1190 options->port = value;
1191 break;
1192
1193 case oConnectionAttempts:
1194 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001195parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001196 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001197 if ((errstr = atoi_err(arg, &value)) != NULL)
1198 fatal("%s line %d: integer value %s.",
1199 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001200 if (*activep && *intptr == -1)
1201 *intptr = value;
1202 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203
Damien Miller78928792000-04-12 20:17:38 +10001204 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001205 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001206 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001207 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001208 if (*arg != '-' &&
1209 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001210 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001211 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001212 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001213 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001214 break;
1215
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001216 case oMacs:
1217 arg = strdelim(&s);
1218 if (!arg || *arg == '\0')
1219 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001220 if (*arg != '-' &&
1221 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001222 fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001223 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001224 if (*activep && options->macs == NULL)
1225 options->macs = xstrdup(arg);
1226 break;
1227
Damien Millerd5f62bf2010-09-24 22:11:14 +10001228 case oKexAlgorithms:
1229 arg = strdelim(&s);
1230 if (!arg || *arg == '\0')
1231 fatal("%.200s line %d: Missing argument.",
1232 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001233 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001234 !kex_names_valid(*arg == '+' || *arg == '^' ?
1235 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001236 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1237 filename, linenum, arg ? arg : "<NONE>");
1238 if (*activep && options->kex_algorithms == NULL)
1239 options->kex_algorithms = xstrdup(arg);
1240 break;
1241
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001242 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001243 charptr = &options->hostkeyalgorithms;
1244parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001245 arg = strdelim(&s);
1246 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001247 fatal("%.200s line %d: Missing argument.",
1248 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001249 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001250 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1251 arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001252 fatal("%s line %d: Bad key types '%s'.",
1253 filename, linenum, arg ? arg : "<NONE>");
1254 if (*activep && *charptr == NULL)
1255 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001256 break;
1257
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001258 case oCASignatureAlgorithms:
1259 charptr = &options->ca_sign_algorithms;
1260 goto parse_keytypes;
1261
Damien Miller95def091999-11-25 00:26:21 +11001262 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001263 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001264 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001265 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001266 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001267 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001268 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001269 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1270 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001271 break;
1272
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001273 case oLogFacility:
1274 log_facility_ptr = &options->log_facility;
1275 arg = strdelim(&s);
1276 value = log_facility_number(arg);
1277 if (value == SYSLOG_FACILITY_NOT_SET)
1278 fatal("%.200s line %d: unsupported log facility '%s'",
1279 filename, linenum, arg ? arg : "<NONE>");
1280 if (*log_facility_ptr == -1)
1281 *log_facility_ptr = (SyslogFacility) value;
1282 break;
1283
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001284 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001285 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001286 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001287 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001288 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001289 fatal("%.200s line %d: Missing port argument.",
1290 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001291
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001292 remotefwd = (opcode == oRemoteForward);
1293 dynamicfwd = (opcode == oDynamicForward);
1294
1295 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001296 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001297 if (arg2 == NULL || *arg2 == '\0') {
1298 if (remotefwd)
1299 dynamicfwd = 1;
1300 else
1301 fatal("%.200s line %d: Missing target "
1302 "argument.", filename, linenum);
1303 } else {
1304 /* construct a string for parse_forward */
1305 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1306 arg2);
1307 }
Damien Millera699d952008-11-03 19:27:34 +11001308 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001309 if (dynamicfwd)
1310 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001311
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001312 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001313 fatal("%.200s line %d: Bad forwarding specification.",
1314 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001315
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001316 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001317 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001318 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001319 } else {
1320 add_local_forward(options, &fwd);
1321 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001322 }
Damien Miller95def091999-11-25 00:26:21 +11001323 break;
1324
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001325 case oClearAllForwardings:
1326 intptr = &options->clear_forwardings;
1327 goto parse_flag;
1328
Damien Miller95def091999-11-25 00:26:21 +11001329 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001330 if (cmdline)
1331 fatal("Host directive not supported as a command-line "
1332 "option");
Damien Miller95def091999-11-25 00:26:21 +11001333 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001334 arg2 = NULL;
1335 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001336 if ((flags & SSHCONF_NEVERMATCH) != 0)
1337 break;
Damien Millerfe924212011-05-15 08:44:45 +10001338 negated = *arg == '!';
1339 if (negated)
1340 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001341 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001342 if (negated) {
1343 debug("%.200s line %d: Skipping Host "
1344 "block because of negated match "
1345 "for %.100s", filename, linenum,
1346 arg);
1347 *activep = 0;
1348 break;
1349 }
1350 if (!*activep)
1351 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001352 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001353 }
Damien Millerfe924212011-05-15 08:44:45 +10001354 }
1355 if (*activep)
1356 debug("%.200s line %d: Applying options for %.100s",
1357 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001358 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001359 return 0;
1360
Damien Miller194fd902013-10-15 12:13:05 +11001361 case oMatch:
1362 if (cmdline)
1363 fatal("Host directive not supported as a command-line "
1364 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001365 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001366 flags & SSHCONF_FINAL, want_final_pass,
1367 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001368 if (value < 0)
1369 fatal("%.200s line %d: Bad Match condition", filename,
1370 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001371 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001372 break;
1373
Damien Miller95def091999-11-25 00:26:21 +11001374 case oEscapeChar:
1375 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001376 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001377 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001378 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001379 if (strcmp(arg, "none") == 0)
1380 value = SSH_ESCAPECHAR_NONE;
1381 else if (arg[1] == '\0')
1382 value = (u_char) arg[0];
1383 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001384 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1385 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001386 else {
1387 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001388 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001389 /* NOTREACHED */
1390 value = 0; /* Avoid compiler warning. */
1391 }
1392 if (*activep && *intptr == -1)
1393 *intptr = value;
1394 break;
1395
Damien Miller20a8f972003-05-18 20:50:30 +10001396 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001397 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001398 multistate_ptr = multistate_addressfamily;
1399 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001400
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001401 case oEnableSSHKeysign:
1402 intptr = &options->enable_ssh_keysign;
1403 goto parse_flag;
1404
Damien Millerbd394c32004-03-08 23:12:36 +11001405 case oIdentitiesOnly:
1406 intptr = &options->identities_only;
1407 goto parse_flag;
1408
Damien Miller509b0102003-12-17 16:33:10 +11001409 case oServerAliveInterval:
1410 intptr = &options->server_alive_interval;
1411 goto parse_time;
1412
1413 case oServerAliveCountMax:
1414 intptr = &options->server_alive_count_max;
1415 goto parse_int;
1416
Darren Tucker46bc0752004-05-02 22:11:30 +10001417 case oSendEnv:
1418 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1419 if (strchr(arg, '=') != NULL)
1420 fatal("%s line %d: Invalid environment name.",
1421 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001422 if (!*activep)
1423 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001424 if (*arg == '-') {
1425 /* Removing an env var */
1426 rm_env(options, arg, filename, linenum);
1427 continue;
1428 } else {
1429 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001430 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001431 fatal("%s line %d: too many send env.",
1432 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001433 options->send_env = xrecallocarray(
1434 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001435 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001436 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001437 options->send_env[options->num_send_env++] =
1438 xstrdup(arg);
1439 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001440 }
1441 break;
1442
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001443 case oSetEnv:
1444 value = options->num_setenv;
1445 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1446 if (strchr(arg, '=') == NULL)
1447 fatal("%s line %d: Invalid SetEnv.",
1448 filename, linenum);
1449 if (!*activep || value != 0)
1450 continue;
1451 /* Adding a setenv var */
1452 if (options->num_setenv >= INT_MAX)
1453 fatal("%s line %d: too many SetEnv.",
1454 filename, linenum);
1455 options->setenv = xrecallocarray(
1456 options->setenv, options->num_setenv,
1457 options->num_setenv + 1, sizeof(*options->setenv));
1458 options->setenv[options->num_setenv++] = xstrdup(arg);
1459 }
1460 break;
1461
Damien Miller0e220db2004-06-15 10:34:08 +10001462 case oControlPath:
1463 charptr = &options->control_path;
1464 goto parse_string;
1465
1466 case oControlMaster:
1467 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001468 multistate_ptr = multistate_controlmaster;
1469 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001470
Damien Millere11e1ea2010-08-03 16:04:46 +10001471 case oControlPersist:
1472 /* no/false/yes/true, or a time spec */
1473 intptr = &options->control_persist;
1474 arg = strdelim(&s);
1475 if (!arg || *arg == '\0')
1476 fatal("%.200s line %d: Missing ControlPersist"
1477 " argument.", filename, linenum);
1478 value = 0;
1479 value2 = 0; /* timeout */
1480 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1481 value = 0;
1482 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1483 value = 1;
1484 else if ((value2 = convtime(arg)) >= 0)
1485 value = 1;
1486 else
1487 fatal("%.200s line %d: Bad ControlPersist argument.",
1488 filename, linenum);
1489 if (*activep && *intptr == -1) {
1490 *intptr = value;
1491 options->control_persist_timeout = value2;
1492 }
1493 break;
1494
Damien Millere1776152005-03-01 21:47:37 +11001495 case oHashKnownHosts:
1496 intptr = &options->hash_known_hosts;
1497 goto parse_flag;
1498
Damien Millerd27b9472005-12-13 19:29:02 +11001499 case oTunnel:
1500 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001501 multistate_ptr = multistate_tunnel;
1502 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001503
1504 case oTunnelDevice:
1505 arg = strdelim(&s);
1506 if (!arg || *arg == '\0')
1507 fatal("%.200s line %d: Missing argument.", filename, linenum);
1508 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001509 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001510 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1511 if (*activep) {
1512 options->tun_local = value;
1513 options->tun_remote = value2;
1514 }
1515 break;
1516
1517 case oLocalCommand:
1518 charptr = &options->local_command;
1519 goto parse_command;
1520
1521 case oPermitLocalCommand:
1522 intptr = &options->permit_local_command;
1523 goto parse_flag;
1524
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001525 case oRemoteCommand:
1526 charptr = &options->remote_command;
1527 goto parse_command;
1528
Damien Miller10288242008-06-30 00:04:03 +10001529 case oVisualHostKey:
1530 intptr = &options->visual_host_key;
1531 goto parse_flag;
1532
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001533 case oInclude:
1534 if (cmdline)
1535 fatal("Include directive not supported as a "
1536 "command-line option");
1537 value = 0;
1538 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1539 /*
1540 * Ensure all paths are anchored. User configuration
1541 * files may begin with '~/' but system configurations
1542 * must not. If the path is relative, then treat it
1543 * as living in ~/.ssh for user configurations or
1544 * /etc/ssh for system ones.
1545 */
1546 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1547 fatal("%.200s line %d: bad include path %s.",
1548 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001549 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001550 xasprintf(&arg2, "%s/%s",
1551 (flags & SSHCONF_USERCONF) ?
1552 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1553 } else
1554 arg2 = xstrdup(arg);
1555 memset(&gl, 0, sizeof(gl));
1556 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1557 if (r == GLOB_NOMATCH) {
1558 debug("%.200s line %d: include %s matched no "
1559 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001560 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001561 continue;
1562 } else if (r != 0 || gl.gl_pathc < 0)
1563 fatal("%.200s line %d: glob failed for %s.",
1564 filename, linenum, arg2);
1565 free(arg2);
1566 oactive = *activep;
1567 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1568 debug3("%.200s line %d: Including file %s "
1569 "depth %d%s", filename, linenum,
1570 gl.gl_pathv[i], depth,
1571 oactive ? "" : " (parse only)");
1572 r = read_config_file_depth(gl.gl_pathv[i],
1573 pw, host, original_host, options,
1574 flags | SSHCONF_CHECKPERM |
1575 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001576 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001577 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001578 fatal("Can't open user config file "
1579 "%.100s: %.100s", gl.gl_pathv[i],
1580 strerror(errno));
1581 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001582 /*
1583 * don't let Match in includes clobber the
1584 * containing file's Match state.
1585 */
1586 *activep = oactive;
1587 if (r != 1)
1588 value = -1;
1589 }
1590 globfree(&gl);
1591 }
1592 if (value != 0)
1593 return value;
1594 break;
1595
Damien Miller0dac6fb2010-11-20 15:19:38 +11001596 case oIPQoS:
1597 arg = strdelim(&s);
1598 if ((value = parse_ipqos(arg)) == -1)
1599 fatal("%s line %d: Bad IPQoS value: %s",
1600 filename, linenum, arg);
1601 arg = strdelim(&s);
1602 if (arg == NULL)
1603 value2 = value;
1604 else if ((value2 = parse_ipqos(arg)) == -1)
1605 fatal("%s line %d: Bad IPQoS value: %s",
1606 filename, linenum, arg);
1607 if (*activep) {
1608 options->ip_qos_interactive = value;
1609 options->ip_qos_bulk = value2;
1610 }
1611 break;
1612
Damien Miller21771e22011-05-15 08:45:50 +10001613 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001614 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001615 multistate_ptr = multistate_requesttty;
1616 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001617
Darren Tucker07636982013-05-16 20:30:03 +10001618 case oIgnoreUnknown:
1619 charptr = &options->ignored_unknown;
1620 goto parse_string;
1621
Damien Miller1262b662013-08-21 02:44:24 +10001622 case oProxyUseFdpass:
1623 intptr = &options->proxy_use_fdpass;
1624 goto parse_flag;
1625
Damien Miller0faf7472013-10-17 11:47:23 +11001626 case oCanonicalDomains:
1627 value = options->num_canonical_domains != 0;
1628 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001629 if (!valid_domain(arg, 1, &errstr)) {
1630 fatal("%s line %d: %s", filename, linenum,
1631 errstr);
1632 }
Damien Miller0faf7472013-10-17 11:47:23 +11001633 if (!*activep || value)
1634 continue;
1635 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1636 fatal("%s line %d: too many hostname suffixes.",
1637 filename, linenum);
1638 options->canonical_domains[
1639 options->num_canonical_domains++] = xstrdup(arg);
1640 }
1641 break;
1642
Damien Miller38505592013-10-17 11:48:13 +11001643 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001644 value = options->num_permitted_cnames != 0;
1645 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1646 /* Either '*' for everything or 'list:list' */
1647 if (strcmp(arg, "*") == 0)
1648 arg2 = arg;
1649 else {
1650 lowercase(arg);
1651 if ((arg2 = strchr(arg, ':')) == NULL ||
1652 arg2[1] == '\0') {
1653 fatal("%s line %d: "
1654 "Invalid permitted CNAME \"%s\"",
1655 filename, linenum, arg);
1656 }
1657 *arg2 = '\0';
1658 arg2++;
1659 }
1660 if (!*activep || value)
1661 continue;
1662 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1663 fatal("%s line %d: too many permitted CNAMEs.",
1664 filename, linenum);
1665 cname = options->permitted_cnames +
1666 options->num_permitted_cnames++;
1667 cname->source_list = xstrdup(arg);
1668 cname->target_list = xstrdup(arg2);
1669 }
1670 break;
1671
Damien Miller38505592013-10-17 11:48:13 +11001672 case oCanonicalizeHostname:
1673 intptr = &options->canonicalize_hostname;
1674 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001675 goto parse_multistate;
1676
Damien Miller38505592013-10-17 11:48:13 +11001677 case oCanonicalizeMaxDots:
1678 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001679 goto parse_int;
1680
Damien Miller38505592013-10-17 11:48:13 +11001681 case oCanonicalizeFallbackLocal:
1682 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001683 goto parse_flag;
1684
Damien Miller7acefbb2014-07-18 14:11:24 +10001685 case oStreamLocalBindMask:
1686 arg = strdelim(&s);
1687 if (!arg || *arg == '\0')
1688 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1689 /* Parse mode in octal format */
1690 value = strtol(arg, &endofnumber, 8);
1691 if (arg == endofnumber || value < 0 || value > 0777)
1692 fatal("%.200s line %d: Bad mask.", filename, linenum);
1693 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1694 break;
1695
1696 case oStreamLocalBindUnlink:
1697 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1698 goto parse_flag;
1699
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001700 case oRevokedHostKeys:
1701 charptr = &options->revoked_host_keys;
1702 goto parse_string;
1703
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001704 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001705 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001706 arg = strdelim(&s);
1707 if (!arg || *arg == '\0')
1708 fatal("%.200s line %d: Missing argument.",
1709 filename, linenum);
1710 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1711 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1712 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001713 if (*activep && *intptr == -1)
1714 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001715 break;
1716
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001717 case oUpdateHostkeys:
1718 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001719 multistate_ptr = multistate_yesnoask;
1720 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001721
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001722 case oHostbasedKeyTypes:
1723 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001724 goto parse_keytypes;
1725
1726 case oPubkeyAcceptedKeyTypes:
1727 charptr = &options->pubkey_key_types;
1728 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001729
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001730 case oAddKeysToAgent:
1731 intptr = &options->add_keys_to_agent;
1732 multistate_ptr = multistate_yesnoaskconfirm;
1733 goto parse_multistate;
1734
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001735 case oIdentityAgent:
1736 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001737 arg = strdelim(&s);
1738 if (!arg || *arg == '\0')
1739 fatal("%.200s line %d: Missing argument.",
1740 filename, linenum);
1741 /* Extra validation if the string represents an env var. */
1742 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1743 fatal("%.200s line %d: Invalid environment name %s.",
1744 filename, linenum, arg);
1745 }
1746 if (*activep && *charptr == NULL)
1747 *charptr = xstrdup(arg);
1748 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001749
Ben Lindstrom4daea862002-06-09 20:04:02 +00001750 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001751 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001752 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001753 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001754
Damien Millerf9b3feb2003-05-16 11:38:32 +10001755 case oUnsupported:
1756 error("%s line %d: Unsupported option \"%s\"",
1757 filename, linenum, keyword);
1758 return 0;
1759
Damien Miller95def091999-11-25 00:26:21 +11001760 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001761 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001762 }
1763
1764 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001765 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001766 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001767 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001768 }
Damien Miller95def091999-11-25 00:26:21 +11001769 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001770}
1771
Damien Miller5428f641999-11-25 11:54:57 +11001772/*
1773 * Reads the config file and modifies the options accordingly. Options
1774 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001775 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001776 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001777int
Damien Miller194fd902013-10-15 12:13:05 +11001778read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001779 const char *original_host, Options *options, int flags,
1780 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001781{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001782 int active = 1;
1783
1784 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001785 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001786}
1787
1788#define READCONF_MAX_DEPTH 16
1789static int
1790read_config_file_depth(const char *filename, struct passwd *pw,
1791 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001792 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001793{
Damien Miller95def091999-11-25 00:26:21 +11001794 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001795 char *line = NULL;
1796 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001797 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001798 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001800 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1801 fatal("Too many recursive configuration includes");
1802
Damien Miller57a44762004-04-20 20:11:57 +10001803 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001804 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001805
Darren Tuckeraefa3682013-04-05 11:18:35 +11001806 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001807 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001808
Damien Miller33793852004-06-15 10:27:55 +10001809 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001810 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001811 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001812 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001813 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001814 }
1815
Damien Miller95def091999-11-25 00:26:21 +11001816 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001817
Damien Miller5428f641999-11-25 11:54:57 +11001818 /*
1819 * Mark that we are now processing the options. This flag is turned
1820 * on/off by Host specifications.
1821 */
Damien Miller95def091999-11-25 00:26:21 +11001822 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001823 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001824 /* Update line number counter. */
1825 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001826 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001827 line, filename, linenum, activep, flags, want_final_pass,
1828 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001829 bad_options++;
1830 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001831 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001832 fclose(f);
1833 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001834 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001835 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001836 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001837}
1838
Damien Miller13f97b22014-02-24 15:57:55 +11001839/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1840int
1841option_clear_or_none(const char *o)
1842{
1843 return o == NULL || strcasecmp(o, "none") == 0;
1844}
1845
Damien Miller5428f641999-11-25 11:54:57 +11001846/*
1847 * Initializes options to special values that indicate that they have not yet
1848 * been set. Read_config_file will only set options with this value. Options
1849 * are processed in the following order: command line, user config file,
1850 * system config file. Last, fill_default_options is called.
1851 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852
Damien Miller4af51302000-04-16 11:18:38 +10001853void
Damien Miller95def091999-11-25 00:26:21 +11001854initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001855{
Damien Miller95def091999-11-25 00:26:21 +11001856 memset(options, 'X', sizeof(*options));
1857 options->forward_agent = -1;
1858 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001859 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001860 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001861 options->stdio_forward_host = NULL;
1862 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001863 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001864 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001865 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001866 options->fwd_opts.gateway_ports = -1;
1867 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1868 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001869 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001870 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001871 options->gss_authentication = -1;
1872 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001873 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001874 options->kbd_interactive_authentication = -1;
1875 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001876 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001877 options->batch_mode = -1;
1878 options->check_host_ip = -1;
1879 options->strict_host_key_checking = -1;
1880 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001881 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001882 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001883 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001884 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001885 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001886 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001887 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001888 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001889 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001890 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001891 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001892 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001893 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001894 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001895 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001896 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001897 options->jump_user = NULL;
1898 options->jump_host = NULL;
1899 options->jump_port = -1;
1900 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001901 options->user = NULL;
1902 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001903 options->num_system_hostfiles = 0;
1904 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001905 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001906 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001907 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001908 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001909 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001910 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001911 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001912 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001913 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001914 options->pkcs11_provider = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001915 options->sk_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001916 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001917 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001918 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001919 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001920 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001921 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001922 options->server_alive_interval = -1;
1923 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001924 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001925 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001926 options->setenv = NULL;
1927 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001928 options->control_path = NULL;
1929 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001930 options->control_persist = -1;
1931 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001932 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001933 options->tun_open = -1;
1934 options->tun_local = -1;
1935 options->tun_remote = -1;
1936 options->local_command = NULL;
1937 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001938 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001939 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001940 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001941 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001942 options->ip_qos_interactive = -1;
1943 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001944 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001945 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001946 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001947 options->num_canonical_domains = 0;
1948 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001949 options->canonicalize_max_dots = -1;
1950 options->canonicalize_fallback_local = -1;
1951 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001952 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001953 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001954 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001955 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001956 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001957}
1958
Damien Miller5428f641999-11-25 11:54:57 +11001959/*
Damien Miller13f97b22014-02-24 15:57:55 +11001960 * A petite version of fill_default_options() that just fills the options
1961 * needed for hostname canonicalization to proceed.
1962 */
1963void
1964fill_default_options_for_canonicalization(Options *options)
1965{
1966 if (options->canonicalize_max_dots == -1)
1967 options->canonicalize_max_dots = 1;
1968 if (options->canonicalize_fallback_local == -1)
1969 options->canonicalize_fallback_local = 1;
1970 if (options->canonicalize_hostname == -1)
1971 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1972}
1973
1974/*
Damien Miller5428f641999-11-25 11:54:57 +11001975 * Called after processing other sources of option data, this fills those
1976 * options for which no value has been specified with their default values.
1977 */
Damien Miller4af51302000-04-16 11:18:38 +10001978void
Damien Miller95def091999-11-25 00:26:21 +11001979fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001980{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001981 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001982 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001983
Damien Miller95def091999-11-25 00:26:21 +11001984 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001985 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001986 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001987 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001988 if (options->forward_x11_trusted == -1)
1989 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001990 if (options->forward_x11_timeout == -1)
1991 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001992 /*
1993 * stdio forwarding (-W) changes the default for these but we defer
1994 * setting the values so they can be overridden.
1995 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001996 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001997 options->exit_on_forward_failure =
1998 options->stdio_forward_host != NULL ? 1 : 0;
1999 if (options->clear_forwardings == -1)
2000 options->clear_forwardings =
2001 options->stdio_forward_host != NULL ? 1 : 0;
2002 if (options->clear_forwardings == 1)
2003 clear_forwardings(options);
2004
Damien Millerd3a18572000-06-07 19:55:44 +10002005 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00002006 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10002007 if (options->fwd_opts.gateway_ports == -1)
2008 options->fwd_opts.gateway_ports = 0;
2009 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2010 options->fwd_opts.streamlocal_bind_mask = 0177;
2011 if (options->fwd_opts.streamlocal_bind_unlink == -1)
2012 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002013 if (options->pubkey_authentication == -1)
2014 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00002015 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002016 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002017 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002018 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002019 if (options->gss_deleg_creds == -1)
2020 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002021 if (options->password_authentication == -1)
2022 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002023 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002024 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002025 if (options->hostbased_authentication == -1)
2026 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002027 if (options->batch_mode == -1)
2028 options->batch_mode = 0;
2029 if (options->check_host_ip == -1)
2030 options->check_host_ip = 1;
2031 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002032 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002033 if (options->compression == -1)
2034 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002035 if (options->tcp_keep_alive == -1)
2036 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002037 if (options->port == -1)
2038 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002039 if (options->address_family == -1)
2040 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002041 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002042 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002043 if (options->number_of_password_prompts == -1)
2044 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002045 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002046 if (options->add_keys_to_agent == -1)
2047 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002048 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002049 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2050 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002051#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002052 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002053 add_identity_file(options, "~/",
2054 _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002055#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002056 add_identity_file(options, "~/",
2057 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org2c557442019-11-12 19:33:08 +00002058 add_identity_file(options, "~/",
2059 _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002060 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002061 }
Damien Miller95def091999-11-25 00:26:21 +11002062 if (options->escape_char == -1)
2063 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002064 if (options->num_system_hostfiles == 0) {
2065 options->system_hostfiles[options->num_system_hostfiles++] =
2066 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2067 options->system_hostfiles[options->num_system_hostfiles++] =
2068 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2069 }
2070 if (options->num_user_hostfiles == 0) {
2071 options->user_hostfiles[options->num_user_hostfiles++] =
2072 xstrdup(_PATH_SSH_USER_HOSTFILE);
2073 options->user_hostfiles[options->num_user_hostfiles++] =
2074 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2075 }
Damien Millerfcd93202002-02-05 12:26:34 +11002076 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002077 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002078 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2079 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002080 if (options->no_host_authentication_for_localhost == - 1)
2081 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002082 if (options->identities_only == -1)
2083 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002084 if (options->enable_ssh_keysign == -1)
2085 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002086 if (options->rekey_limit == -1)
2087 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002088 if (options->rekey_interval == -1)
2089 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002090 if (options->verify_host_key_dns == -1)
2091 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002092 if (options->server_alive_interval == -1)
2093 options->server_alive_interval = 0;
2094 if (options->server_alive_count_max == -1)
2095 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002096 if (options->control_master == -1)
2097 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002098 if (options->control_persist == -1) {
2099 options->control_persist = 0;
2100 options->control_persist_timeout = 0;
2101 }
Damien Millere1776152005-03-01 21:47:37 +11002102 if (options->hash_known_hosts == -1)
2103 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002104 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002105 options->tun_open = SSH_TUNMODE_NO;
2106 if (options->tun_local == -1)
2107 options->tun_local = SSH_TUNID_ANY;
2108 if (options->tun_remote == -1)
2109 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002110 if (options->permit_local_command == -1)
2111 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002112 if (options->visual_host_key == -1)
2113 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002114 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002115 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002116 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002117 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002118 if (options->request_tty == -1)
2119 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002120 if (options->proxy_use_fdpass == -1)
2121 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002122 if (options->canonicalize_max_dots == -1)
2123 options->canonicalize_max_dots = 1;
2124 if (options->canonicalize_fallback_local == -1)
2125 options->canonicalize_fallback_local = 1;
2126 if (options->canonicalize_hostname == -1)
2127 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002128 if (options->fingerprint_hash == -1)
2129 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002130 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002131 options->update_hostkeys = 0;
djm@openbsd.org884416b2019-10-31 21:18:28 +00002132 if (options->sk_provider == NULL)
2133 options->sk_provider = xstrdup("$SSH_SK_PROVIDER");
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002134
2135 /* Expand KEX name lists */
2136 all_cipher = cipher_alg_list(',', 0);
2137 all_mac = mac_alg_list(',');
2138 all_kex = kex_alg_list(',');
2139 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002140 all_sig = sshkey_alg_list(0, 1, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002141#define ASSEMBLE(what, defaults, all) \
2142 do { \
2143 if ((r = kex_assemble_names(&options->what, \
2144 defaults, all)) != 0) \
2145 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2146 } while (0)
djm@openbsd.org9b611302019-02-23 08:20:43 +00002147 ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
2148 ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
2149 ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002150 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002151 ASSEMBLE(pubkey_key_types, PUBKEY_DEFAULT_PK_ALG, all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002152 ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002153#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002154 free(all_cipher);
2155 free(all_mac);
2156 free(all_kex);
2157 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002158 free(all_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002159
Damien Millere9fc72e2013-10-15 12:14:12 +11002160#define CLEAR_ON_NONE(v) \
2161 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002162 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002163 free(v); \
2164 v = NULL; \
2165 } \
2166 } while(0)
2167 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002168 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002169 CLEAR_ON_NONE(options->proxy_command);
2170 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002171 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org95a80582019-03-01 02:08:50 +00002172 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002173 CLEAR_ON_NONE(options->sk_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002174 if (options->jump_host != NULL &&
2175 strcmp(options->jump_host, "none") == 0 &&
2176 options->jump_port == 0 && options->jump_user == NULL) {
2177 free(options->jump_host);
2178 options->jump_host = NULL;
2179 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002180 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002181 /* options->user will be set in the main program if appropriate */
2182 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002183 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002184 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002185}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002186
Damien Miller7acefbb2014-07-18 14:11:24 +10002187struct fwdarg {
2188 char *arg;
2189 int ispath;
2190};
2191
2192/*
2193 * parse_fwd_field
2194 * parses the next field in a port forwarding specification.
2195 * sets fwd to the parsed field and advances p past the colon
2196 * or sets it to NULL at end of string.
2197 * returns 0 on success, else non-zero.
2198 */
2199static int
2200parse_fwd_field(char **p, struct fwdarg *fwd)
2201{
2202 char *ep, *cp = *p;
2203 int ispath = 0;
2204
2205 if (*cp == '\0') {
2206 *p = NULL;
2207 return -1; /* end of string */
2208 }
2209
2210 /*
2211 * A field escaped with square brackets is used literally.
2212 * XXX - allow ']' to be escaped via backslash?
2213 */
2214 if (*cp == '[') {
2215 /* find matching ']' */
2216 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2217 if (*ep == '/')
2218 ispath = 1;
2219 }
2220 /* no matching ']' or not at end of field. */
2221 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2222 return -1;
2223 /* NUL terminate the field and advance p past the colon */
2224 *ep++ = '\0';
2225 if (*ep != '\0')
2226 *ep++ = '\0';
2227 fwd->arg = cp + 1;
2228 fwd->ispath = ispath;
2229 *p = ep;
2230 return 0;
2231 }
2232
2233 for (cp = *p; *cp != '\0'; cp++) {
2234 switch (*cp) {
2235 case '\\':
2236 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002237 if (*cp == '\0')
2238 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002239 break;
2240 case '/':
2241 ispath = 1;
2242 break;
2243 case ':':
2244 *cp++ = '\0';
2245 goto done;
2246 }
2247 }
2248done:
2249 fwd->arg = *p;
2250 fwd->ispath = ispath;
2251 *p = cp;
2252 return 0;
2253}
2254
Damien Millerf91ee4c2005-03-01 21:24:33 +11002255/*
2256 * parse_forward
2257 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002258 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002259 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2260 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002261 * dynamicfwd == 1
2262 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002263 * returns number of arguments parsed or zero on error
2264 */
2265int
Damien Miller7acefbb2014-07-18 14:11:24 +10002266parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002267{
Damien Miller7acefbb2014-07-18 14:11:24 +10002268 struct fwdarg fwdargs[4];
2269 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002270 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002271
Damien Miller7acefbb2014-07-18 14:11:24 +10002272 memset(fwd, 0, sizeof(*fwd));
2273 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002274
2275 cp = p = xstrdup(fwdspec);
2276
2277 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002278 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002279 cp++;
2280
Damien Miller7acefbb2014-07-18 14:11:24 +10002281 for (i = 0; i < 4; ++i) {
2282 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002283 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002284 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002285
Damien Millerf4b39532008-11-03 19:28:21 +11002286 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002287 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002288 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002289 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002290
2291 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002292 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002293 if (fwdargs[0].ispath) {
2294 fwd->listen_path = xstrdup(fwdargs[0].arg);
2295 fwd->listen_port = PORT_STREAMLOCAL;
2296 } else {
2297 fwd->listen_host = NULL;
2298 fwd->listen_port = a2port(fwdargs[0].arg);
2299 }
Damien Millera699d952008-11-03 19:27:34 +11002300 fwd->connect_host = xstrdup("socks");
2301 break;
2302
2303 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002304 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2305 fwd->listen_path = xstrdup(fwdargs[0].arg);
2306 fwd->listen_port = PORT_STREAMLOCAL;
2307 fwd->connect_path = xstrdup(fwdargs[1].arg);
2308 fwd->connect_port = PORT_STREAMLOCAL;
2309 } else if (fwdargs[1].ispath) {
2310 fwd->listen_host = NULL;
2311 fwd->listen_port = a2port(fwdargs[0].arg);
2312 fwd->connect_path = xstrdup(fwdargs[1].arg);
2313 fwd->connect_port = PORT_STREAMLOCAL;
2314 } else {
2315 fwd->listen_host = xstrdup(fwdargs[0].arg);
2316 fwd->listen_port = a2port(fwdargs[1].arg);
2317 fwd->connect_host = xstrdup("socks");
2318 }
Damien Millera699d952008-11-03 19:27:34 +11002319 break;
2320
Damien Millerf91ee4c2005-03-01 21:24:33 +11002321 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002322 if (fwdargs[0].ispath) {
2323 fwd->listen_path = xstrdup(fwdargs[0].arg);
2324 fwd->listen_port = PORT_STREAMLOCAL;
2325 fwd->connect_host = xstrdup(fwdargs[1].arg);
2326 fwd->connect_port = a2port(fwdargs[2].arg);
2327 } else if (fwdargs[2].ispath) {
2328 fwd->listen_host = xstrdup(fwdargs[0].arg);
2329 fwd->listen_port = a2port(fwdargs[1].arg);
2330 fwd->connect_path = xstrdup(fwdargs[2].arg);
2331 fwd->connect_port = PORT_STREAMLOCAL;
2332 } else {
2333 fwd->listen_host = NULL;
2334 fwd->listen_port = a2port(fwdargs[0].arg);
2335 fwd->connect_host = xstrdup(fwdargs[1].arg);
2336 fwd->connect_port = a2port(fwdargs[2].arg);
2337 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002338 break;
2339
2340 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002341 fwd->listen_host = xstrdup(fwdargs[0].arg);
2342 fwd->listen_port = a2port(fwdargs[1].arg);
2343 fwd->connect_host = xstrdup(fwdargs[2].arg);
2344 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002345 break;
2346 default:
2347 i = 0; /* failure */
2348 }
2349
Darren Tuckera627d422013-06-02 07:31:17 +10002350 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002351
Damien Millera699d952008-11-03 19:27:34 +11002352 if (dynamicfwd) {
2353 if (!(i == 1 || i == 2))
2354 goto fail_free;
2355 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002356 if (!(i == 3 || i == 4)) {
2357 if (fwd->connect_path == NULL &&
2358 fwd->listen_path == NULL)
2359 goto fail_free;
2360 }
2361 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002362 goto fail_free;
2363 }
2364
Damien Miller7acefbb2014-07-18 14:11:24 +10002365 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2366 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002367 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002368 if (fwd->connect_host != NULL &&
2369 strlen(fwd->connect_host) >= NI_MAXHOST)
2370 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002371 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2372 if (fwd->connect_path != NULL &&
2373 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2374 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002375 if (fwd->listen_host != NULL &&
2376 strlen(fwd->listen_host) >= NI_MAXHOST)
2377 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002378 if (fwd->listen_path != NULL &&
2379 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2380 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002381
2382 return (i);
2383
2384 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002385 free(fwd->connect_host);
2386 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002387 free(fwd->connect_path);
2388 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002389 free(fwd->listen_host);
2390 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002391 free(fwd->listen_path);
2392 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002393 return (0);
2394}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002395
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002396int
2397parse_jump(const char *s, Options *o, int active)
2398{
2399 char *orig, *sdup, *cp;
2400 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002401 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002402
2403 active &= o->proxy_command == NULL && o->jump_host == NULL;
2404
2405 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002406 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002407 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002408 if (strcasecmp(s, "none") == 0)
2409 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002410 if ((cp = strrchr(sdup, ',')) == NULL)
2411 cp = sdup; /* last */
2412 else
2413 *cp++ = '\0';
2414
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002415 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002416 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002417 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2418 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002419 goto out;
2420 } else {
2421 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002422 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2423 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002424 goto out;
2425 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002426 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002427 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002428 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002429 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002430 if (strcasecmp(s, "none") == 0) {
2431 o->jump_host = xstrdup("none");
2432 o->jump_port = 0;
2433 } else {
2434 o->jump_user = user;
2435 o->jump_host = host;
2436 o->jump_port = port;
2437 o->proxy_command = xstrdup("none");
2438 user = host = NULL;
2439 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2440 o->jump_extra = xstrdup(s);
2441 o->jump_extra[cp - s] = '\0';
2442 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002443 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002444 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002445 ret = 0;
2446 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002447 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002448 free(user);
2449 free(host);
2450 return ret;
2451}
2452
millert@openbsd.org887669e2017-10-21 23:06:24 +00002453int
2454parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2455{
2456 char *path;
2457 int r;
2458
2459 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2460 if (r == 0 && path != NULL)
2461 r = -1; /* path not allowed */
2462 return r;
2463}
2464
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002465/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2466static const char *
2467fmt_multistate_int(int val, const struct multistate *m)
2468{
2469 u_int i;
2470
2471 for (i = 0; m[i].key != NULL; i++) {
2472 if (m[i].value == val)
2473 return m[i].key;
2474 }
2475 return "UNKNOWN";
2476}
2477
2478static const char *
2479fmt_intarg(OpCodes code, int val)
2480{
2481 if (val == -1)
2482 return "unset";
2483 switch (code) {
2484 case oAddressFamily:
2485 return fmt_multistate_int(val, multistate_addressfamily);
2486 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002487 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002488 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002489 case oStrictHostKeyChecking:
2490 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002491 case oControlMaster:
2492 return fmt_multistate_int(val, multistate_controlmaster);
2493 case oTunnel:
2494 return fmt_multistate_int(val, multistate_tunnel);
2495 case oRequestTTY:
2496 return fmt_multistate_int(val, multistate_requesttty);
2497 case oCanonicalizeHostname:
2498 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002499 case oAddKeysToAgent:
2500 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002501 case oFingerprintHash:
2502 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002503 default:
2504 switch (val) {
2505 case 0:
2506 return "no";
2507 case 1:
2508 return "yes";
2509 default:
2510 return "UNKNOWN";
2511 }
2512 }
2513}
2514
2515static const char *
2516lookup_opcode_name(OpCodes code)
2517{
2518 u_int i;
2519
2520 for (i = 0; keywords[i].name != NULL; i++)
2521 if (keywords[i].opcode == code)
2522 return(keywords[i].name);
2523 return "UNKNOWN";
2524}
2525
2526static void
2527dump_cfg_int(OpCodes code, int val)
2528{
2529 printf("%s %d\n", lookup_opcode_name(code), val);
2530}
2531
2532static void
2533dump_cfg_fmtint(OpCodes code, int val)
2534{
2535 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2536}
2537
2538static void
2539dump_cfg_string(OpCodes code, const char *val)
2540{
2541 if (val == NULL)
2542 return;
2543 printf("%s %s\n", lookup_opcode_name(code), val);
2544}
2545
2546static void
2547dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2548{
2549 u_int i;
2550
2551 for (i = 0; i < count; i++)
2552 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2553}
2554
2555static void
2556dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2557{
2558 u_int i;
2559
2560 printf("%s", lookup_opcode_name(code));
2561 for (i = 0; i < count; i++)
2562 printf(" %s", vals[i]);
2563 printf("\n");
2564}
2565
2566static void
2567dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2568{
2569 const struct Forward *fwd;
2570 u_int i;
2571
2572 /* oDynamicForward */
2573 for (i = 0; i < count; i++) {
2574 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002575 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002576 strcmp(fwd->connect_host, "socks") != 0)
2577 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002578 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002579 strcmp(fwd->connect_host, "socks") == 0)
2580 continue;
2581 printf("%s", lookup_opcode_name(code));
2582 if (fwd->listen_port == PORT_STREAMLOCAL)
2583 printf(" %s", fwd->listen_path);
2584 else if (fwd->listen_host == NULL)
2585 printf(" %d", fwd->listen_port);
2586 else {
2587 printf(" [%s]:%d",
2588 fwd->listen_host, fwd->listen_port);
2589 }
2590 if (code != oDynamicForward) {
2591 if (fwd->connect_port == PORT_STREAMLOCAL)
2592 printf(" %s", fwd->connect_path);
2593 else if (fwd->connect_host == NULL)
2594 printf(" %d", fwd->connect_port);
2595 else {
2596 printf(" [%s]:%d",
2597 fwd->connect_host, fwd->connect_port);
2598 }
2599 }
2600 printf("\n");
2601 }
2602}
2603
2604void
2605dump_client_config(Options *o, const char *host)
2606{
2607 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002608 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002609
djm@openbsd.org60a92472015-08-21 23:53:08 +00002610 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002611 all_key = sshkey_alg_list(0, 0, 1, ',');
2612 if (kex_assemble_names( &o->hostkeyalgorithms,
2613 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002614 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002615 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002616
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002617 /* Most interesting options first: user, host, port */
2618 dump_cfg_string(oUser, o->user);
jmc@openbsd.org73491492019-06-12 11:31:50 +00002619 dump_cfg_string(oHostname, host);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002620 dump_cfg_int(oPort, o->port);
2621
2622 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002623 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002624 dump_cfg_fmtint(oAddressFamily, o->address_family);
2625 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2626 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2627 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2628 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2629 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2630 dump_cfg_fmtint(oCompression, o->compression);
2631 dump_cfg_fmtint(oControlMaster, o->control_master);
2632 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002633 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002634 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002635 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002636 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2637 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2638 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2639 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2640#ifdef GSSAPI
2641 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2642 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2643#endif /* GSSAPI */
2644 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2645 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2646 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2647 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2648 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2649 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2650 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002651 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2652 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2653 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002654 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2655 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2656 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2657 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002658 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2659 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002660 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002661
2662 /* Integer options */
2663 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002664 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2665 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2666 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2667 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2668 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2669
2670 /* String options */
2671 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002672 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002673 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2674 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002675 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002676 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002677 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002678 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002679 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002680 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2681 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002682 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002683 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002684 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002685 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2686 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002687#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002688 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002689#endif
djm@openbsd.org884416b2019-10-31 21:18:28 +00002690 dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002691 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002692 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002693 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002694 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002695
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002696 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002697 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2698 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2699 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2700
2701 /* String array options */
2702 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2703 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002704 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002705 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2706 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2707 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002708 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002709
2710 /* Special cases */
2711
2712 /* oConnectTimeout */
2713 if (o->connection_timeout == -1)
2714 printf("connecttimeout none\n");
2715 else
2716 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2717
2718 /* oTunnelDevice */
2719 printf("tunneldevice");
2720 if (o->tun_local == SSH_TUNID_ANY)
2721 printf(" any");
2722 else
2723 printf(" %d", o->tun_local);
2724 if (o->tun_remote == SSH_TUNID_ANY)
2725 printf(":any");
2726 else
2727 printf(":%d", o->tun_remote);
2728 printf("\n");
2729
2730 /* oCanonicalizePermittedCNAMEs */
2731 if ( o->num_permitted_cnames > 0) {
2732 printf("canonicalizePermittedcnames");
2733 for (i = 0; i < o->num_permitted_cnames; i++) {
2734 printf(" %s:%s", o->permitted_cnames[i].source_list,
2735 o->permitted_cnames[i].target_list);
2736 }
2737 printf("\n");
2738 }
2739
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002740 /* oControlPersist */
2741 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2742 dump_cfg_fmtint(oControlPersist, o->control_persist);
2743 else
2744 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2745
2746 /* oEscapeChar */
2747 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2748 printf("escapechar none\n");
2749 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002750 vis(buf, o->escape_char, VIS_WHITE, 0);
2751 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002752 }
2753
2754 /* oIPQoS */
2755 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2756 printf("%s\n", iptos2str(o->ip_qos_bulk));
2757
2758 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002759 printf("rekeylimit %llu %d\n",
2760 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002761
2762 /* oStreamLocalBindMask */
2763 printf("streamlocalbindmask 0%o\n",
2764 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002765
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002766 /* oLogFacility */
2767 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2768
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002769 /* oProxyCommand / oProxyJump */
2770 if (o->jump_host == NULL)
2771 dump_cfg_string(oProxyCommand, o->proxy_command);
2772 else {
2773 /* Check for numeric addresses */
2774 i = strchr(o->jump_host, ':') != NULL ||
2775 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2776 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2777 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002778 /* optional additional jump spec */
2779 o->jump_extra == NULL ? "" : o->jump_extra,
2780 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002781 /* optional user */
2782 o->jump_user == NULL ? "" : o->jump_user,
2783 o->jump_user == NULL ? "" : "@",
2784 /* opening [ if hostname is numeric */
2785 i ? "[" : "",
2786 /* mandatory hostname */
2787 o->jump_host,
2788 /* closing ] if hostname is numeric */
2789 i ? "]" : "",
2790 /* optional port number */
2791 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002792 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002793 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002794}