blob: cb3ae6dc7ea73461d994b14c27c9f28d4dcfdbb8 [file] [log] [blame]
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001/* $OpenBSD: readconf.c,v 1.319 2019/12/21 02:19:13 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000040#include <stdarg.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +000056#include "ssherr.h"
Damien Miller78928792000-04-12 20:17:38 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
59#include "pathnames.h"
60#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000061#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100062#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "readconf.h"
64#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "kex.h"
66#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110067#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000068#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000069#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Format of the configuration file:
72
73 # Configuration data is parsed as follows:
74 # 1. command line options
75 # 2. user-specific file
76 # 3. system-wide file
77 # Any configuration value is only changed the first time it is set.
78 # Thus, host-specific definitions should be at the beginning of the
79 # configuration file, and defaults at the end.
80
81 # Host-specific declarations. These may override anything above. A single
82 # host may match multiple declarations; these are processed in the order
83 # that they are given in.
84
85 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host fake.com
jmc@openbsd.org73491492019-06-12 11:31:50 +000089 Hostname another.host.name.real.org
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 User blaah
91 Port 34289
92 ForwardX11 no
93 ForwardAgent no
94
95 Host books.com
96 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000097 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host fascist.blob.com
100 Port 23123
101 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 PasswordAuthentication no
103
104 Host puukko.hut.fi
105 User t35124p
106 ProxyCommand ssh-proxy %h %p
107
108 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000112 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 PasswordAuthentication no
114
Damien Millerd27b9472005-12-13 19:29:02 +1100115 Host vpn.fake.com
116 Tunnel yes
117 TunnelDevice 3
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 # Defaults for various options
120 Host *
121 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 PasswordAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100125 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 IdentityFile ~/.ssh/identity
127 Port 22
128 EscapeChar ~
129
130*/
131
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000132static int read_config_file_depth(const char *filename, struct passwd *pw,
133 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000134 int flags, int *activep, int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000135static int process_config_line_depth(Options *options, struct passwd *pw,
136 const char *host, const char *original_host, char *line,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000137 const char *filename, int linenum, int *activep, int flags,
138 int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000147 oPasswordAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000149 oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000151 oUser, oEscapeChar, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000154 oTCPKeepAlive, oNumberOfPasswordPrompts,
155 oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000159 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000164 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000165 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000166 oTunnel, oTunnelDevice,
167 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000168 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000169 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100170 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
171 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000172 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000173 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000174 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
djm@openbsd.org884416b2019-10-31 21:18:28 +0000175 oSecurityKeyProvider,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000176 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177} OpCodes;
178
179/* Textual representations of the tokens. */
180
Damien Miller95def091999-11-25 00:26:21 +1100181static struct {
182 const char *name;
183 OpCodes opcode;
184} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000185 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000186 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000187 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000188 { "fallbacktorsh", oDeprecated },
189 { "globalknownhostsfile2", oDeprecated },
190 { "rhostsauthentication", oDeprecated },
191 { "userknownhostsfile2", oDeprecated },
192 { "useroaming", oDeprecated },
193 { "usersh", oDeprecated },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000194 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000195
196 /* Unsupported options */
197 { "afstokenpassing", oUnsupported },
198 { "kerberosauthentication", oUnsupported },
199 { "kerberostgtpassing", oUnsupported },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000200 { "rsaauthentication", oUnsupported },
201 { "rhostsrsaauthentication", oUnsupported },
202 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000203
204 /* Sometimes-unsupported options */
205#if defined(GSSAPI)
206 { "gssapiauthentication", oGssAuthentication },
207 { "gssapidelegatecredentials", oGssDelegateCreds },
208# else
209 { "gssapiauthentication", oUnsupported },
210 { "gssapidelegatecredentials", oUnsupported },
211#endif
212#ifdef ENABLE_PKCS11
djm@openbsd.org381a2612017-01-30 00:38:50 +0000213 { "pkcs11provider", oPKCS11Provider },
djm@openbsd.org95a80582019-03-01 02:08:50 +0000214 { "smartcarddevice", oPKCS11Provider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000215# else
216 { "smartcarddevice", oUnsupported },
217 { "pkcs11provider", oUnsupported },
218#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000219
Damien Miller95def091999-11-25 00:26:21 +1100220 { "forwardagent", oForwardAgent },
221 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000222 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000223 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000224 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000225 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "gatewayports", oGatewayPorts },
Damien Miller95def091999-11-25 00:26:21 +1100227 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100228 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
229 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000231 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000232 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000233 { "challengeresponseauthentication", oChallengeResponseAuthentication },
Damien Miller87f08be2018-07-20 13:18:28 +1000234 { "skeyauthentication", oUnsupported },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000235 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100236 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100237 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100238 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000239 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000240 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000241 { "identityagent", oIdentityAgent },
jmc@openbsd.org73491492019-06-12 11:31:50 +0000242 { "hostname", oHostname },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000243 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100244 { "proxycommand", oProxyCommand },
245 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000246 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000247 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100248 { "remoteforward", oRemoteForward },
249 { "localforward", oLocalForward },
250 { "user", oUser },
251 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100252 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100253 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100254 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100255 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100256 { "connectionattempts", oConnectionAttempts },
257 { "batchmode", oBatchMode },
258 { "checkhostip", oCheckHostIP },
259 { "stricthostkeychecking", oStrictHostKeyChecking },
260 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100261 { "tcpkeepalive", oTCPKeepAlive },
262 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100263 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000264 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100265 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000266 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000267 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000268 { "hostkeyalgorithms", oHostKeyAlgorithms },
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000269 { "casignaturealgorithms", oCASignatureAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000270 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000271 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100272 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000273 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000274 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100275 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000276 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000277 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000278 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100279 { "serveraliveinterval", oServerAliveInterval },
280 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000281 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000282 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000283 { "controlpath", oControlPath },
284 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000285 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100286 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000287 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100288 { "tunnel", oTunnel },
289 { "tunneldevice", oTunnelDevice },
290 { "localcommand", oLocalCommand },
291 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000292 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000293 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000294 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100295 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000296 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000297 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100298 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100299 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
300 { "canonicalizehostname", oCanonicalizeHostname },
301 { "canonicalizemaxdots", oCanonicalizeMaxDots },
302 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000303 { "streamlocalbindmask", oStreamLocalBindMask },
304 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000305 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000306 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000307 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000308 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000309 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000310 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000311 { "proxyjump", oProxyJump },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000312 { "securitykeyprovider", oSecurityKeyProvider },
Damien Miller01ed2272008-11-05 16:20:46 +1100313
Ben Lindstrom65366a82001-12-06 16:32:47 +0000314 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100315};
316
Damien Miller5428f641999-11-25 11:54:57 +1100317/*
318 * Adds a local TCP/IP port forward to options. Never returns if there is an
319 * error.
320 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Miller4af51302000-04-16 11:18:38 +1000322void
Damien Miller7acefbb2014-07-18 14:11:24 +1000323add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Damien Miller7acefbb2014-07-18 14:11:24 +1000325 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000326 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000327
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000328 /* Don't add duplicates */
329 for (i = 0; i < options->num_local_forwards; i++) {
330 if (forward_equals(newfwd, options->local_forwards + i))
331 return;
332 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000333 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000334 options->num_local_forwards + 1,
335 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100336 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337
Damien Miller1a0442f2008-11-05 16:30:06 +1100338 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100341 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100342 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000343 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344}
345
Damien Miller5428f641999-11-25 11:54:57 +1100346/*
347 * Adds a remote TCP/IP port forward to options. Never returns if there is
348 * an error.
349 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller4af51302000-04-16 11:18:38 +1000351void
Damien Miller7acefbb2014-07-18 14:11:24 +1000352add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller7acefbb2014-07-18 14:11:24 +1000354 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000355 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000356
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000357 /* Don't add duplicates */
358 for (i = 0; i < options->num_remote_forwards; i++) {
359 if (forward_equals(newfwd, options->remote_forwards + i))
360 return;
361 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000362 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000363 options->num_remote_forwards + 1,
364 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100365 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100366
Damien Miller1a0442f2008-11-05 16:30:06 +1100367 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000369 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100370 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100371 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000372 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100373 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000374 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375}
376
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000377static void
378clear_forwardings(Options *options)
379{
380 int i;
381
Damien Millerf91ee4c2005-03-01 21:24:33 +1100382 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000384 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000386 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100387 }
Damien Miller232cfb12010-06-26 09:50:30 +1000388 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000390 options->local_forwards = NULL;
391 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000392 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100393 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000394 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000395 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100398 }
Damien Miller232cfb12010-06-26 09:50:30 +1000399 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000400 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000401 options->remote_forwards = NULL;
402 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000403 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100404 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405}
406
Darren Tucker19104782013-04-05 11:13:08 +1100407void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000408add_certificate_file(Options *options, const char *path, int userprovided)
409{
410 int i;
411
412 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
413 fatal("Too many certificate files specified (max %d)",
414 SSH_MAX_CERTIFICATE_FILES);
415
416 /* Avoid registering duplicates */
417 for (i = 0; i < options->num_certificate_files; i++) {
418 if (options->certificate_file_userprovided[i] == userprovided &&
419 strcmp(options->certificate_files[i], path) == 0) {
420 debug2("%s: ignoring duplicate key %s", __func__, path);
421 return;
422 }
423 }
424
425 options->certificate_file_userprovided[options->num_certificate_files] =
426 userprovided;
427 options->certificate_files[options->num_certificate_files++] =
428 xstrdup(path);
429}
430
431void
Darren Tucker19104782013-04-05 11:13:08 +1100432add_identity_file(Options *options, const char *dir, const char *filename,
433 int userprovided)
434{
435 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000436 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100437
438 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
439 fatal("Too many identity files specified (max %d)",
440 SSH_MAX_IDENTITY_FILES);
441
442 if (dir == NULL) /* no dir, filename is absolute */
443 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000444 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
445 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100446
Damien Miller15271902014-05-15 13:47:56 +1000447 /* Avoid registering duplicates */
448 for (i = 0; i < options->num_identity_files; i++) {
449 if (options->identity_file_userprovided[i] == userprovided &&
450 strcmp(options->identity_files[i], path) == 0) {
451 debug2("%s: ignoring duplicate key %s", __func__, path);
452 free(path);
453 return;
454 }
455 }
456
Darren Tucker19104782013-04-05 11:13:08 +1100457 options->identity_file_userprovided[options->num_identity_files] =
458 userprovided;
459 options->identity_files[options->num_identity_files++] = path;
460}
461
Damien Miller194fd902013-10-15 12:13:05 +1100462int
463default_ssh_port(void)
464{
465 static int port;
466 struct servent *sp;
467
468 if (port == 0) {
469 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
470 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
471 }
472 return port;
473}
474
475/*
476 * Execute a command in a shell.
477 * Return its exit status or -1 on abnormal exit.
478 */
479static int
480execute_in_shell(const char *cmd)
481{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000482 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100483 pid_t pid;
484 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100485
486 if ((shell = getenv("SHELL")) == NULL)
487 shell = _PATH_BSHELL;
488
djm@openbsd.org9ab5b942019-08-09 05:05:54 +0000489 if (access(shell, X_OK) == -1) {
490 fatal("Shell \"%s\" is not executable: %s",
491 shell, strerror(errno));
492 }
493
Damien Miller194fd902013-10-15 12:13:05 +1100494 /* Need this to redirect subprocess stdin/out */
495 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
496 fatal("open(/dev/null): %s", strerror(errno));
497
498 debug("Executing command: '%.500s'", cmd);
499
500 /* Fork and execute the command. */
501 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000502 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100503
Damien Miller194fd902013-10-15 12:13:05 +1100504 /* Redirect child stdin and stdout. Leave stderr */
505 if (dup2(devnull, STDIN_FILENO) == -1)
506 fatal("dup2: %s", strerror(errno));
507 if (dup2(devnull, STDOUT_FILENO) == -1)
508 fatal("dup2: %s", strerror(errno));
509 if (devnull > STDERR_FILENO)
510 close(devnull);
511 closefrom(STDERR_FILENO + 1);
512
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000513 argv[0] = shell;
514 argv[1] = "-c";
515 argv[2] = xstrdup(cmd);
516 argv[3] = NULL;
517
Damien Miller194fd902013-10-15 12:13:05 +1100518 execv(argv[0], argv);
519 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
520 /* Die with signal to make this error apparent to parent. */
521 signal(SIGTERM, SIG_DFL);
522 kill(getpid(), SIGTERM);
523 _exit(1);
524 }
525 /* Parent. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000526 if (pid == -1)
Damien Miller194fd902013-10-15 12:13:05 +1100527 fatal("%s: fork: %.100s", __func__, strerror(errno));
528
529 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100530
531 while (waitpid(pid, &status, 0) == -1) {
532 if (errno != EINTR && errno != EAGAIN)
533 fatal("%s: waitpid: %s", __func__, strerror(errno));
534 }
535 if (!WIFEXITED(status)) {
536 error("command '%.100s' exited abnormally", cmd);
537 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000538 }
Damien Miller194fd902013-10-15 12:13:05 +1100539 debug3("command returned status %d", WEXITSTATUS(status));
540 return WEXITSTATUS(status);
541}
542
543/*
544 * Parse and execute a Match directive.
545 */
546static int
547match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000548 const char *host_arg, const char *original_host, int final_pass,
549 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100550{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000551 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100552 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100554 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000555 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100556
557 /*
558 * Configuration is likely to be incomplete at this point so we
559 * must be prepared to use default values.
560 */
561 port = options->port <= 0 ? default_ssh_port() : options->port;
562 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000563 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000564 host = xstrdup(options->hostname);
565 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100566 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = percent_expand(options->hostname,
568 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000569 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100570 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000573 debug2("checking match for '%s' host %s originally %s",
574 cp, host, original_host);
575 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
576 criteria = NULL;
577 this_result = 1;
578 if ((negate = attrib[0] == '!'))
579 attrib++;
580 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100581 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100583 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 error("%.200s line %d: '%s' cannot be combined "
585 "with other Match attributes",
586 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100587 result = -1;
588 goto out;
589 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 if (result)
591 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100592 goto out;
593 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000594 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000595 if (strcasecmp(attrib, "canonical") == 0 ||
596 strcasecmp(attrib, "final") == 0) {
597 /*
598 * If the config requests "Match final" then remember
599 * this so we can perform a second pass later.
600 */
601 if (strcasecmp(attrib, "final") == 0 &&
602 want_final_pass != NULL)
603 *want_final_pass = 1;
604 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000605 if (r == (negate ? 1 : 0))
606 this_result = result = 0;
607 debug3("%.200s line %d: %smatched '%s'",
608 filename, linenum,
609 this_result ? "" : "not ", oattrib);
610 continue;
611 }
612 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100613 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
614 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100615 result = -1;
616 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100617 }
Damien Miller194fd902013-10-15 12:13:05 +1100618 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000620 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 if (r == (negate ? 1 : 0))
622 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100623 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000624 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000625 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000626 if (r == (negate ? 1 : 0))
627 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100628 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000630 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000631 if (r == (negate ? 1 : 0))
632 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100633 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000634 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000635 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 if (r == (negate ? 1 : 0))
637 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100638 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100639 if (gethostname(thishost, sizeof(thishost)) == -1)
640 fatal("gethostname: %s", strerror(errno));
641 strlcpy(shorthost, thishost, sizeof(shorthost));
642 shorthost[strcspn(thishost, ".")] = '\0';
643 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000644 snprintf(uidstr, sizeof(uidstr), "%llu",
645 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100646
647 cmd = percent_expand(arg,
648 "L", shorthost,
649 "d", pw->pw_dir,
650 "h", host,
651 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000652 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100653 "p", portstr,
654 "r", ruser,
655 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000656 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100657 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100658 if (result != 1) {
659 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000660 debug3("%.200s line %d: skipped exec "
661 "\"%.100s\"", filename, linenum, cmd);
662 free(cmd);
663 continue;
Damien Miller06287802014-02-24 15:56:45 +1100664 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000665 r = execute_in_shell(cmd);
666 if (r == -1) {
667 fatal("%.200s line %d: match exec "
668 "'%.100s' error", filename,
669 linenum, cmd);
670 }
671 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100672 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000673 /* Force exit status to boolean */
674 r = r == 0;
675 if (r == (negate ? 1 : 0))
676 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100677 } else {
678 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100679 result = -1;
680 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100681 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000682 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
683 filename, linenum, this_result ? "": "not ",
684 oattrib, criteria);
685 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100686 }
Damien Millercf31f382013-10-24 21:02:56 +1100687 if (attributes == 0) {
688 error("One or more attributes required for Match");
689 result = -1;
690 goto out;
691 }
Damien Miller084bcd22013-10-23 16:30:51 +1100692 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000693 if (result != -1)
694 debug2("match %sfound", result ? "" : "not ");
695 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100696 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100697 return result;
698}
699
djm@openbsd.org555294a2018-04-06 13:02:39 +0000700/* Remove environment variable by pattern */
701static void
702rm_env(Options *options, const char *arg, const char *filename, int linenum)
703{
704 int i, j;
705 char *cp;
706
707 /* Remove an environment variable */
708 for (i = 0; i < options->num_send_env; ) {
709 cp = xstrdup(options->send_env[i]);
710 if (!match_pattern(cp, arg + 1)) {
711 free(cp);
712 i++;
713 continue;
714 }
715 debug3("%s line %d: removing environment %s",
716 filename, linenum, cp);
717 free(cp);
718 free(options->send_env[i]);
719 options->send_env[i] = NULL;
720 for (j = i; j < options->num_send_env - 1; j++) {
721 options->send_env[j] = options->send_env[j + 1];
722 options->send_env[j + 1] = NULL;
723 }
724 options->num_send_env--;
725 /* NB. don't increment i */
726 }
727}
728
Damien Miller5428f641999-11-25 11:54:57 +1100729/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000730 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100731 */
Damien Miller4af51302000-04-16 11:18:38 +1000732static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000733parse_token(const char *cp, const char *filename, int linenum,
734 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735{
Darren Tucker07636982013-05-16 20:30:03 +1000736 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller95def091999-11-25 00:26:21 +1100738 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000739 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100740 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000741 if (ignored_unknown != NULL &&
742 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000743 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000744 error("%s: line %d: Bad configuration option: %s",
745 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100746 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747}
748
Damien Millere9fc72e2013-10-15 12:14:12 +1100749/* Multistate option parsing */
750struct multistate {
751 char *key;
752 int value;
753};
754static const struct multistate multistate_flag[] = {
755 { "true", 1 },
756 { "false", 0 },
757 { "yes", 1 },
758 { "no", 0 },
759 { NULL, -1 }
760};
761static const struct multistate multistate_yesnoask[] = {
762 { "true", 1 },
763 { "false", 0 },
764 { "yes", 1 },
765 { "no", 0 },
766 { "ask", 2 },
767 { NULL, -1 }
768};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000769static const struct multistate multistate_strict_hostkey[] = {
770 { "true", SSH_STRICT_HOSTKEY_YES },
771 { "false", SSH_STRICT_HOSTKEY_OFF },
772 { "yes", SSH_STRICT_HOSTKEY_YES },
773 { "no", SSH_STRICT_HOSTKEY_OFF },
774 { "ask", SSH_STRICT_HOSTKEY_ASK },
775 { "off", SSH_STRICT_HOSTKEY_OFF },
776 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
777 { NULL, -1 }
778};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000779static const struct multistate multistate_yesnoaskconfirm[] = {
780 { "true", 1 },
781 { "false", 0 },
782 { "yes", 1 },
783 { "no", 0 },
784 { "ask", 2 },
785 { "confirm", 3 },
786 { NULL, -1 }
787};
Damien Millere9fc72e2013-10-15 12:14:12 +1100788static const struct multistate multistate_addressfamily[] = {
789 { "inet", AF_INET },
790 { "inet6", AF_INET6 },
791 { "any", AF_UNSPEC },
792 { NULL, -1 }
793};
794static const struct multistate multistate_controlmaster[] = {
795 { "true", SSHCTL_MASTER_YES },
796 { "yes", SSHCTL_MASTER_YES },
797 { "false", SSHCTL_MASTER_NO },
798 { "no", SSHCTL_MASTER_NO },
799 { "auto", SSHCTL_MASTER_AUTO },
800 { "ask", SSHCTL_MASTER_ASK },
801 { "autoask", SSHCTL_MASTER_AUTO_ASK },
802 { NULL, -1 }
803};
804static const struct multistate multistate_tunnel[] = {
805 { "ethernet", SSH_TUNMODE_ETHERNET },
806 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
807 { "true", SSH_TUNMODE_DEFAULT },
808 { "yes", SSH_TUNMODE_DEFAULT },
809 { "false", SSH_TUNMODE_NO },
810 { "no", SSH_TUNMODE_NO },
811 { NULL, -1 }
812};
813static const struct multistate multistate_requesttty[] = {
814 { "true", REQUEST_TTY_YES },
815 { "yes", REQUEST_TTY_YES },
816 { "false", REQUEST_TTY_NO },
817 { "no", REQUEST_TTY_NO },
818 { "force", REQUEST_TTY_FORCE },
819 { "auto", REQUEST_TTY_AUTO },
820 { NULL, -1 }
821};
Damien Miller38505592013-10-17 11:48:13 +1100822static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100823 { "true", SSH_CANONICALISE_YES },
824 { "false", SSH_CANONICALISE_NO },
825 { "yes", SSH_CANONICALISE_YES },
826 { "no", SSH_CANONICALISE_NO },
827 { "always", SSH_CANONICALISE_ALWAYS },
828 { NULL, -1 }
829};
Damien Millere9fc72e2013-10-15 12:14:12 +1100830
Damien Miller5428f641999-11-25 11:54:57 +1100831/*
832 * Processes a single option line as used in the configuration files. This
833 * only sets those values that have not already been set.
834 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100835int
Damien Miller194fd902013-10-15 12:13:05 +1100836process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000837 const char *original_host, char *line, const char *filename,
838 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000840 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000841 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000842}
843
844#define WHITESPACE " \t\r\n"
845static int
846process_config_line_depth(Options *options, struct passwd *pw, const char *host,
847 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000848 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000849{
Damien Miller295ee632011-05-29 21:42:31 +1000850 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
851 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000852 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000853 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000854 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100855 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000856 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000857 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100858 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000859 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100860 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100861 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000862 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000863 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
Damien Miller194fd902013-10-15 12:13:05 +1100865 if (activep == NULL) { /* We are processing a command line directive */
866 cmdline = 1;
867 activep = &cmdline;
868 }
869
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000870 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000871 if ((len = strlen(line)) == 0)
872 return 0;
873 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000874 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000875 break;
876 line[len] = '\0';
877 }
878
Damien Millerbe484b52000-07-15 14:14:16 +1000879 s = line;
880 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100881 if ((keyword = strdelim(&s)) == NULL)
882 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000883 /* Ignore leading whitespace. */
884 if (*keyword == '\0')
885 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000886 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100887 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000888 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100889 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Darren Tucker07636982013-05-16 20:30:03 +1000891 opcode = parse_token(keyword, filename, linenum,
892 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 switch (opcode) {
895 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100896 /* don't panic, but count bad options */
897 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000898 case oIgnore:
899 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000900 case oIgnoredUnknownOption:
901 debug("%s line %d: Ignored unknown option \"%s\"",
902 filename, linenum, keyword);
903 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000904 case oConnectTimeout:
905 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100906parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000907 arg = strdelim(&s);
908 if (!arg || *arg == '\0')
909 fatal("%s line %d: missing time value.",
910 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000911 if (strcmp(arg, "none") == 0)
912 value = -1;
913 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000914 fatal("%s line %d: invalid time value.",
915 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100916 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000917 *intptr = value;
918 break;
919
Damien Miller95def091999-11-25 00:26:21 +1100920 case oForwardAgent:
921 intptr = &options->forward_agent;
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000922
923 arg = strdelim(&s);
924 if (!arg || *arg == '\0')
925 fatal("%s line %d: missing argument.",
926 filename, linenum);
927
928 value = -1;
929 multistate_ptr = multistate_flag;
930 for (i = 0; multistate_ptr[i].key != NULL; i++) {
931 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
932 value = multistate_ptr[i].value;
933 break;
934 }
935 }
936 if (value != -1) {
937 if (*activep && *intptr == -1)
938 *intptr = value;
939 break;
940 }
941 /* ForwardAgent wasn't 'yes' or 'no', assume a path */
942 if (*activep && *intptr == -1)
943 *intptr = 1;
944
945 charptr = &options->forward_agent_sock_path;
946 goto parse_agent_path;
947
948 case oForwardX11:
949 intptr = &options->forward_x11;
Damien Millere9fc72e2013-10-15 12:14:12 +1100950 parse_flag:
951 multistate_ptr = multistate_flag;
952 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000953 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000954 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100955 fatal("%s line %d: missing argument.",
956 filename, linenum);
957 value = -1;
958 for (i = 0; multistate_ptr[i].key != NULL; i++) {
959 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
960 value = multistate_ptr[i].value;
961 break;
962 }
963 }
964 if (value == -1)
965 fatal("%s line %d: unsupported option \"%s\".",
966 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100967 if (*activep && *intptr == -1)
968 *intptr = value;
969 break;
970
Darren Tucker0a118da2003-10-15 15:54:32 +1000971 case oForwardX11Trusted:
972 intptr = &options->forward_x11_trusted;
973 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000974
Damien Miller1ab6a512010-06-26 10:02:24 +1000975 case oForwardX11Timeout:
976 intptr = &options->forward_x11_timeout;
977 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000980 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100981 goto parse_flag;
982
Darren Tuckere7d4b192006-07-12 22:17:10 +1000983 case oExitOnForwardFailure:
984 intptr = &options->exit_on_forward_failure;
985 goto parse_flag;
986
Damien Miller95def091999-11-25 00:26:21 +1100987 case oPasswordAuthentication:
988 intptr = &options->password_authentication;
989 goto parse_flag;
990
Damien Miller874d77b2000-10-14 16:23:11 +1100991 case oKbdInteractiveAuthentication:
992 intptr = &options->kbd_interactive_authentication;
993 goto parse_flag;
994
995 case oKbdInteractiveDevices:
996 charptr = &options->kbd_interactive_devices;
997 goto parse_string;
998
Damien Miller0bc1bd82000-11-13 22:57:25 +1100999 case oPubkeyAuthentication:
1000 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +10001001 goto parse_flag;
1002
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001003 case oHostbasedAuthentication:
1004 intptr = &options->hostbased_authentication;
1005 goto parse_flag;
1006
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001007 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +00001008 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +11001009 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001010
Darren Tucker0efd1552003-08-26 11:49:55 +10001011 case oGssAuthentication:
1012 intptr = &options->gss_authentication;
1013 goto parse_flag;
1014
1015 case oGssDelegateCreds:
1016 intptr = &options->gss_deleg_creds;
1017 goto parse_flag;
1018
Damien Miller95def091999-11-25 00:26:21 +11001019 case oBatchMode:
1020 intptr = &options->batch_mode;
1021 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Damien Miller95def091999-11-25 00:26:21 +11001023 case oCheckHostIP:
1024 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +10001025 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026
Damien Miller37876e92003-05-15 10:19:46 +10001027 case oVerifyHostKeyDNS:
1028 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001029 multistate_ptr = multistate_yesnoask;
1030 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001031
Damien Miller95def091999-11-25 00:26:21 +11001032 case oStrictHostKeyChecking:
1033 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001034 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001035 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 case oCompression:
1038 intptr = &options->compression;
1039 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
Damien Miller12c150e2003-12-17 16:31:10 +11001041 case oTCPKeepAlive:
1042 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001043 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001045 case oNoHostAuthenticationForLocalhost:
1046 intptr = &options->no_host_authentication_for_localhost;
1047 goto parse_flag;
1048
Damien Miller95def091999-11-25 00:26:21 +11001049 case oNumberOfPasswordPrompts:
1050 intptr = &options->number_of_password_prompts;
1051 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
Damien Millera5539d22003-04-09 20:50:06 +10001053 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001054 arg = strdelim(&s);
1055 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001056 fatal("%.200s line %d: Missing argument.", filename,
1057 linenum);
1058 if (strcmp(arg, "default") == 0) {
1059 val64 = 0;
1060 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001061 if (scan_scaled(arg, &val64) == -1)
1062 fatal("%.200s line %d: Bad number '%s': %s",
1063 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001064 if (val64 != 0 && val64 < 16)
1065 fatal("%.200s line %d: RekeyLimit too small",
1066 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001067 }
Damien Miller3dff1762008-02-10 22:25:52 +11001068 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001069 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001070 if (s != NULL) { /* optional rekey interval present */
1071 if (strcmp(s, "none") == 0) {
1072 (void)strdelim(&s); /* discard */
1073 break;
1074 }
1075 intptr = &options->rekey_interval;
1076 goto parse_time;
1077 }
Damien Millera5539d22003-04-09 20:50:06 +10001078 break;
1079
Damien Miller95def091999-11-25 00:26:21 +11001080 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001081 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001082 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001083 fatal("%.200s line %d: Missing argument.", filename, linenum);
1084 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001085 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001086 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001087 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001088 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001089 add_identity_file(options, NULL,
1090 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001091 }
1092 break;
1093
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001094 case oCertificateFile:
1095 arg = strdelim(&s);
1096 if (!arg || *arg == '\0')
1097 fatal("%.200s line %d: Missing argument.",
1098 filename, linenum);
1099 if (*activep) {
1100 intptr = &options->num_certificate_files;
1101 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1102 fatal("%.200s line %d: Too many certificate "
1103 "files specified (max %d).",
1104 filename, linenum,
1105 SSH_MAX_CERTIFICATE_FILES);
1106 }
1107 add_certificate_file(options, arg,
1108 flags & SSHCONF_USERCONF);
1109 }
1110 break;
1111
Damien Millerd3a18572000-06-07 19:55:44 +10001112 case oXAuthLocation:
1113 charptr=&options->xauth_location;
1114 goto parse_string;
1115
Damien Miller95def091999-11-25 00:26:21 +11001116 case oUser:
1117 charptr = &options->user;
1118parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001119 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001120 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001121 fatal("%.200s line %d: Missing argument.",
1122 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001123 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001124 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001125 break;
1126
1127 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001128 cpptr = (char **)&options->system_hostfiles;
1129 uintptr = &options->num_system_hostfiles;
1130 max_entries = SSH_MAX_HOSTS_FILES;
1131parse_char_array:
1132 if (*activep && *uintptr == 0) {
1133 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1134 if ((*uintptr) >= max_entries)
1135 fatal("%s line %d: "
1136 "too many authorized keys files.",
1137 filename, linenum);
1138 cpptr[(*uintptr)++] = xstrdup(arg);
1139 }
1140 }
1141 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001142
1143 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001144 cpptr = (char **)&options->user_hostfiles;
1145 uintptr = &options->num_user_hostfiles;
1146 max_entries = SSH_MAX_HOSTS_FILES;
1147 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001148
jmc@openbsd.org73491492019-06-12 11:31:50 +00001149 case oHostname:
Damien Miller95def091999-11-25 00:26:21 +11001150 charptr = &options->hostname;
1151 goto parse_string;
1152
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001153 case oHostKeyAlias:
1154 charptr = &options->host_key_alias;
1155 goto parse_string;
1156
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001157 case oPreferredAuthentications:
1158 charptr = &options->preferred_authentications;
1159 goto parse_string;
1160
Ben Lindstrome0f88042001-04-30 13:06:24 +00001161 case oBindAddress:
1162 charptr = &options->bind_address;
1163 goto parse_string;
1164
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001165 case oBindInterface:
1166 charptr = &options->bind_interface;
1167 goto parse_string;
1168
Damien Miller7ea845e2010-02-12 09:21:02 +11001169 case oPKCS11Provider:
1170 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001171 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001172
djm@openbsd.org884416b2019-10-31 21:18:28 +00001173 case oSecurityKeyProvider:
1174 charptr = &options->sk_provider;
1175 goto parse_string;
1176
Damien Miller95def091999-11-25 00:26:21 +11001177 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001178 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001179 /* Ignore ProxyCommand if ProxyJump already specified */
1180 if (options->jump_host != NULL)
1181 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001182parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001183 if (s == NULL)
1184 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001185 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001186 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001187 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001188 return 0;
1189
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001190 case oProxyJump:
1191 if (s == NULL) {
1192 fatal("%.200s line %d: Missing argument.",
1193 filename, linenum);
1194 }
1195 len = strspn(s, WHITESPACE "=");
1196 if (parse_jump(s + len, options, *activep) == -1) {
1197 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1198 filename, linenum, s + len);
1199 }
1200 return 0;
1201
Damien Miller95def091999-11-25 00:26:21 +11001202 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001203 arg = strdelim(&s);
1204 if (!arg || *arg == '\0')
1205 fatal("%.200s line %d: Missing argument.",
1206 filename, linenum);
1207 value = a2port(arg);
1208 if (value <= 0)
1209 fatal("%.200s line %d: Bad port '%s'.",
1210 filename, linenum, arg);
1211 if (*activep && options->port == -1)
1212 options->port = value;
1213 break;
1214
1215 case oConnectionAttempts:
1216 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001217parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001218 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001219 if ((errstr = atoi_err(arg, &value)) != NULL)
1220 fatal("%s line %d: integer value %s.",
1221 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001222 if (*activep && *intptr == -1)
1223 *intptr = value;
1224 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225
Damien Miller78928792000-04-12 20:17:38 +10001226 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001227 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001228 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001229 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001230 if (*arg != '-' &&
1231 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001232 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001233 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001234 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001235 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001236 break;
1237
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001238 case oMacs:
1239 arg = strdelim(&s);
1240 if (!arg || *arg == '\0')
1241 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001242 if (*arg != '-' &&
1243 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001244 fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001245 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001246 if (*activep && options->macs == NULL)
1247 options->macs = xstrdup(arg);
1248 break;
1249
Damien Millerd5f62bf2010-09-24 22:11:14 +10001250 case oKexAlgorithms:
1251 arg = strdelim(&s);
1252 if (!arg || *arg == '\0')
1253 fatal("%.200s line %d: Missing argument.",
1254 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001255 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001256 !kex_names_valid(*arg == '+' || *arg == '^' ?
1257 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001258 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1259 filename, linenum, arg ? arg : "<NONE>");
1260 if (*activep && options->kex_algorithms == NULL)
1261 options->kex_algorithms = xstrdup(arg);
1262 break;
1263
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001264 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001265 charptr = &options->hostkeyalgorithms;
1266parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001267 arg = strdelim(&s);
1268 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001269 fatal("%.200s line %d: Missing argument.",
1270 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001271 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001272 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1273 arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001274 fatal("%s line %d: Bad key types '%s'.",
1275 filename, linenum, arg ? arg : "<NONE>");
1276 if (*activep && *charptr == NULL)
1277 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001278 break;
1279
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001280 case oCASignatureAlgorithms:
1281 charptr = &options->ca_sign_algorithms;
1282 goto parse_keytypes;
1283
Damien Miller95def091999-11-25 00:26:21 +11001284 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001285 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001286 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001287 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001288 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001289 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001290 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001291 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1292 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001293 break;
1294
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001295 case oLogFacility:
1296 log_facility_ptr = &options->log_facility;
1297 arg = strdelim(&s);
1298 value = log_facility_number(arg);
1299 if (value == SYSLOG_FACILITY_NOT_SET)
1300 fatal("%.200s line %d: unsupported log facility '%s'",
1301 filename, linenum, arg ? arg : "<NONE>");
1302 if (*log_facility_ptr == -1)
1303 *log_facility_ptr = (SyslogFacility) value;
1304 break;
1305
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001306 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001307 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001308 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001309 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001310 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001311 fatal("%.200s line %d: Missing port argument.",
1312 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001313
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001314 remotefwd = (opcode == oRemoteForward);
1315 dynamicfwd = (opcode == oDynamicForward);
1316
1317 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001318 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001319 if (arg2 == NULL || *arg2 == '\0') {
1320 if (remotefwd)
1321 dynamicfwd = 1;
1322 else
1323 fatal("%.200s line %d: Missing target "
1324 "argument.", filename, linenum);
1325 } else {
1326 /* construct a string for parse_forward */
1327 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1328 arg2);
1329 }
Damien Millera699d952008-11-03 19:27:34 +11001330 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001331 if (dynamicfwd)
1332 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001333
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001334 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001335 fatal("%.200s line %d: Bad forwarding specification.",
1336 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001337
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001338 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001339 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001340 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001341 } else {
1342 add_local_forward(options, &fwd);
1343 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001344 }
Damien Miller95def091999-11-25 00:26:21 +11001345 break;
1346
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001347 case oClearAllForwardings:
1348 intptr = &options->clear_forwardings;
1349 goto parse_flag;
1350
Damien Miller95def091999-11-25 00:26:21 +11001351 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001352 if (cmdline)
1353 fatal("Host directive not supported as a command-line "
1354 "option");
Damien Miller95def091999-11-25 00:26:21 +11001355 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001356 arg2 = NULL;
1357 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001358 if ((flags & SSHCONF_NEVERMATCH) != 0)
1359 break;
Damien Millerfe924212011-05-15 08:44:45 +10001360 negated = *arg == '!';
1361 if (negated)
1362 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001363 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001364 if (negated) {
1365 debug("%.200s line %d: Skipping Host "
1366 "block because of negated match "
1367 "for %.100s", filename, linenum,
1368 arg);
1369 *activep = 0;
1370 break;
1371 }
1372 if (!*activep)
1373 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001374 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001375 }
Damien Millerfe924212011-05-15 08:44:45 +10001376 }
1377 if (*activep)
1378 debug("%.200s line %d: Applying options for %.100s",
1379 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001380 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001381 return 0;
1382
Damien Miller194fd902013-10-15 12:13:05 +11001383 case oMatch:
1384 if (cmdline)
1385 fatal("Host directive not supported as a command-line "
1386 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001387 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001388 flags & SSHCONF_FINAL, want_final_pass,
1389 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001390 if (value < 0)
1391 fatal("%.200s line %d: Bad Match condition", filename,
1392 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001393 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001394 break;
1395
Damien Miller95def091999-11-25 00:26:21 +11001396 case oEscapeChar:
1397 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001398 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001399 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001400 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001401 if (strcmp(arg, "none") == 0)
1402 value = SSH_ESCAPECHAR_NONE;
1403 else if (arg[1] == '\0')
1404 value = (u_char) arg[0];
1405 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001406 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1407 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001408 else {
1409 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001410 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001411 /* NOTREACHED */
1412 value = 0; /* Avoid compiler warning. */
1413 }
1414 if (*activep && *intptr == -1)
1415 *intptr = value;
1416 break;
1417
Damien Miller20a8f972003-05-18 20:50:30 +10001418 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001419 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001420 multistate_ptr = multistate_addressfamily;
1421 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001422
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001423 case oEnableSSHKeysign:
1424 intptr = &options->enable_ssh_keysign;
1425 goto parse_flag;
1426
Damien Millerbd394c32004-03-08 23:12:36 +11001427 case oIdentitiesOnly:
1428 intptr = &options->identities_only;
1429 goto parse_flag;
1430
Damien Miller509b0102003-12-17 16:33:10 +11001431 case oServerAliveInterval:
1432 intptr = &options->server_alive_interval;
1433 goto parse_time;
1434
1435 case oServerAliveCountMax:
1436 intptr = &options->server_alive_count_max;
1437 goto parse_int;
1438
Darren Tucker46bc0752004-05-02 22:11:30 +10001439 case oSendEnv:
1440 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1441 if (strchr(arg, '=') != NULL)
1442 fatal("%s line %d: Invalid environment name.",
1443 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001444 if (!*activep)
1445 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001446 if (*arg == '-') {
1447 /* Removing an env var */
1448 rm_env(options, arg, filename, linenum);
1449 continue;
1450 } else {
1451 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001452 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001453 fatal("%s line %d: too many send env.",
1454 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001455 options->send_env = xrecallocarray(
1456 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001457 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001458 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001459 options->send_env[options->num_send_env++] =
1460 xstrdup(arg);
1461 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001462 }
1463 break;
1464
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001465 case oSetEnv:
1466 value = options->num_setenv;
1467 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1468 if (strchr(arg, '=') == NULL)
1469 fatal("%s line %d: Invalid SetEnv.",
1470 filename, linenum);
1471 if (!*activep || value != 0)
1472 continue;
1473 /* Adding a setenv var */
1474 if (options->num_setenv >= INT_MAX)
1475 fatal("%s line %d: too many SetEnv.",
1476 filename, linenum);
1477 options->setenv = xrecallocarray(
1478 options->setenv, options->num_setenv,
1479 options->num_setenv + 1, sizeof(*options->setenv));
1480 options->setenv[options->num_setenv++] = xstrdup(arg);
1481 }
1482 break;
1483
Damien Miller0e220db2004-06-15 10:34:08 +10001484 case oControlPath:
1485 charptr = &options->control_path;
1486 goto parse_string;
1487
1488 case oControlMaster:
1489 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001490 multistate_ptr = multistate_controlmaster;
1491 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001492
Damien Millere11e1ea2010-08-03 16:04:46 +10001493 case oControlPersist:
1494 /* no/false/yes/true, or a time spec */
1495 intptr = &options->control_persist;
1496 arg = strdelim(&s);
1497 if (!arg || *arg == '\0')
1498 fatal("%.200s line %d: Missing ControlPersist"
1499 " argument.", filename, linenum);
1500 value = 0;
1501 value2 = 0; /* timeout */
1502 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1503 value = 0;
1504 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1505 value = 1;
1506 else if ((value2 = convtime(arg)) >= 0)
1507 value = 1;
1508 else
1509 fatal("%.200s line %d: Bad ControlPersist argument.",
1510 filename, linenum);
1511 if (*activep && *intptr == -1) {
1512 *intptr = value;
1513 options->control_persist_timeout = value2;
1514 }
1515 break;
1516
Damien Millere1776152005-03-01 21:47:37 +11001517 case oHashKnownHosts:
1518 intptr = &options->hash_known_hosts;
1519 goto parse_flag;
1520
Damien Millerd27b9472005-12-13 19:29:02 +11001521 case oTunnel:
1522 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001523 multistate_ptr = multistate_tunnel;
1524 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001525
1526 case oTunnelDevice:
1527 arg = strdelim(&s);
1528 if (!arg || *arg == '\0')
1529 fatal("%.200s line %d: Missing argument.", filename, linenum);
1530 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001531 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001532 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1533 if (*activep) {
1534 options->tun_local = value;
1535 options->tun_remote = value2;
1536 }
1537 break;
1538
1539 case oLocalCommand:
1540 charptr = &options->local_command;
1541 goto parse_command;
1542
1543 case oPermitLocalCommand:
1544 intptr = &options->permit_local_command;
1545 goto parse_flag;
1546
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001547 case oRemoteCommand:
1548 charptr = &options->remote_command;
1549 goto parse_command;
1550
Damien Miller10288242008-06-30 00:04:03 +10001551 case oVisualHostKey:
1552 intptr = &options->visual_host_key;
1553 goto parse_flag;
1554
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001555 case oInclude:
1556 if (cmdline)
1557 fatal("Include directive not supported as a "
1558 "command-line option");
1559 value = 0;
1560 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1561 /*
1562 * Ensure all paths are anchored. User configuration
1563 * files may begin with '~/' but system configurations
1564 * must not. If the path is relative, then treat it
1565 * as living in ~/.ssh for user configurations or
1566 * /etc/ssh for system ones.
1567 */
1568 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1569 fatal("%.200s line %d: bad include path %s.",
1570 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001571 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001572 xasprintf(&arg2, "%s/%s",
1573 (flags & SSHCONF_USERCONF) ?
1574 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1575 } else
1576 arg2 = xstrdup(arg);
1577 memset(&gl, 0, sizeof(gl));
1578 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1579 if (r == GLOB_NOMATCH) {
1580 debug("%.200s line %d: include %s matched no "
1581 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001582 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001583 continue;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001584 } else if (r != 0)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001585 fatal("%.200s line %d: glob failed for %s.",
1586 filename, linenum, arg2);
1587 free(arg2);
1588 oactive = *activep;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001589 for (i = 0; i < gl.gl_pathc; i++) {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001590 debug3("%.200s line %d: Including file %s "
1591 "depth %d%s", filename, linenum,
1592 gl.gl_pathv[i], depth,
1593 oactive ? "" : " (parse only)");
1594 r = read_config_file_depth(gl.gl_pathv[i],
1595 pw, host, original_host, options,
1596 flags | SSHCONF_CHECKPERM |
1597 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001598 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001599 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001600 fatal("Can't open user config file "
1601 "%.100s: %.100s", gl.gl_pathv[i],
1602 strerror(errno));
1603 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001604 /*
1605 * don't let Match in includes clobber the
1606 * containing file's Match state.
1607 */
1608 *activep = oactive;
1609 if (r != 1)
1610 value = -1;
1611 }
1612 globfree(&gl);
1613 }
1614 if (value != 0)
1615 return value;
1616 break;
1617
Damien Miller0dac6fb2010-11-20 15:19:38 +11001618 case oIPQoS:
1619 arg = strdelim(&s);
1620 if ((value = parse_ipqos(arg)) == -1)
1621 fatal("%s line %d: Bad IPQoS value: %s",
1622 filename, linenum, arg);
1623 arg = strdelim(&s);
1624 if (arg == NULL)
1625 value2 = value;
1626 else if ((value2 = parse_ipqos(arg)) == -1)
1627 fatal("%s line %d: Bad IPQoS value: %s",
1628 filename, linenum, arg);
1629 if (*activep) {
1630 options->ip_qos_interactive = value;
1631 options->ip_qos_bulk = value2;
1632 }
1633 break;
1634
Damien Miller21771e22011-05-15 08:45:50 +10001635 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001636 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001637 multistate_ptr = multistate_requesttty;
1638 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001639
Darren Tucker07636982013-05-16 20:30:03 +10001640 case oIgnoreUnknown:
1641 charptr = &options->ignored_unknown;
1642 goto parse_string;
1643
Damien Miller1262b662013-08-21 02:44:24 +10001644 case oProxyUseFdpass:
1645 intptr = &options->proxy_use_fdpass;
1646 goto parse_flag;
1647
Damien Miller0faf7472013-10-17 11:47:23 +11001648 case oCanonicalDomains:
1649 value = options->num_canonical_domains != 0;
1650 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001651 if (!valid_domain(arg, 1, &errstr)) {
1652 fatal("%s line %d: %s", filename, linenum,
1653 errstr);
1654 }
Damien Miller0faf7472013-10-17 11:47:23 +11001655 if (!*activep || value)
1656 continue;
1657 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1658 fatal("%s line %d: too many hostname suffixes.",
1659 filename, linenum);
1660 options->canonical_domains[
1661 options->num_canonical_domains++] = xstrdup(arg);
1662 }
1663 break;
1664
Damien Miller38505592013-10-17 11:48:13 +11001665 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001666 value = options->num_permitted_cnames != 0;
1667 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1668 /* Either '*' for everything or 'list:list' */
1669 if (strcmp(arg, "*") == 0)
1670 arg2 = arg;
1671 else {
1672 lowercase(arg);
1673 if ((arg2 = strchr(arg, ':')) == NULL ||
1674 arg2[1] == '\0') {
1675 fatal("%s line %d: "
1676 "Invalid permitted CNAME \"%s\"",
1677 filename, linenum, arg);
1678 }
1679 *arg2 = '\0';
1680 arg2++;
1681 }
1682 if (!*activep || value)
1683 continue;
1684 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1685 fatal("%s line %d: too many permitted CNAMEs.",
1686 filename, linenum);
1687 cname = options->permitted_cnames +
1688 options->num_permitted_cnames++;
1689 cname->source_list = xstrdup(arg);
1690 cname->target_list = xstrdup(arg2);
1691 }
1692 break;
1693
Damien Miller38505592013-10-17 11:48:13 +11001694 case oCanonicalizeHostname:
1695 intptr = &options->canonicalize_hostname;
1696 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001697 goto parse_multistate;
1698
Damien Miller38505592013-10-17 11:48:13 +11001699 case oCanonicalizeMaxDots:
1700 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001701 goto parse_int;
1702
Damien Miller38505592013-10-17 11:48:13 +11001703 case oCanonicalizeFallbackLocal:
1704 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001705 goto parse_flag;
1706
Damien Miller7acefbb2014-07-18 14:11:24 +10001707 case oStreamLocalBindMask:
1708 arg = strdelim(&s);
1709 if (!arg || *arg == '\0')
1710 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1711 /* Parse mode in octal format */
1712 value = strtol(arg, &endofnumber, 8);
1713 if (arg == endofnumber || value < 0 || value > 0777)
1714 fatal("%.200s line %d: Bad mask.", filename, linenum);
1715 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1716 break;
1717
1718 case oStreamLocalBindUnlink:
1719 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1720 goto parse_flag;
1721
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001722 case oRevokedHostKeys:
1723 charptr = &options->revoked_host_keys;
1724 goto parse_string;
1725
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001726 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001727 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001728 arg = strdelim(&s);
1729 if (!arg || *arg == '\0')
1730 fatal("%.200s line %d: Missing argument.",
1731 filename, linenum);
1732 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1733 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1734 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001735 if (*activep && *intptr == -1)
1736 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001737 break;
1738
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001739 case oUpdateHostkeys:
1740 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001741 multistate_ptr = multistate_yesnoask;
1742 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001743
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001744 case oHostbasedKeyTypes:
1745 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001746 goto parse_keytypes;
1747
1748 case oPubkeyAcceptedKeyTypes:
1749 charptr = &options->pubkey_key_types;
1750 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001751
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001752 case oAddKeysToAgent:
1753 intptr = &options->add_keys_to_agent;
1754 multistate_ptr = multistate_yesnoaskconfirm;
1755 goto parse_multistate;
1756
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001757 case oIdentityAgent:
1758 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001759 arg = strdelim(&s);
1760 if (!arg || *arg == '\0')
1761 fatal("%.200s line %d: Missing argument.",
1762 filename, linenum);
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001763 parse_agent_path:
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001764 /* Extra validation if the string represents an env var. */
1765 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1766 fatal("%.200s line %d: Invalid environment name %s.",
1767 filename, linenum, arg);
1768 }
1769 if (*activep && *charptr == NULL)
1770 *charptr = xstrdup(arg);
1771 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001772
Ben Lindstrom4daea862002-06-09 20:04:02 +00001773 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001774 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001775 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001776 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001777
Damien Millerf9b3feb2003-05-16 11:38:32 +10001778 case oUnsupported:
1779 error("%s line %d: Unsupported option \"%s\"",
1780 filename, linenum, keyword);
1781 return 0;
1782
Damien Miller95def091999-11-25 00:26:21 +11001783 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001784 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001785 }
1786
1787 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001788 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001789 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001790 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001791 }
Damien Miller95def091999-11-25 00:26:21 +11001792 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001793}
1794
Damien Miller5428f641999-11-25 11:54:57 +11001795/*
1796 * Reads the config file and modifies the options accordingly. Options
1797 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001798 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001799 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001800int
Damien Miller194fd902013-10-15 12:13:05 +11001801read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001802 const char *original_host, Options *options, int flags,
1803 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001805 int active = 1;
1806
1807 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001808 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001809}
1810
1811#define READCONF_MAX_DEPTH 16
1812static int
1813read_config_file_depth(const char *filename, struct passwd *pw,
1814 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001815 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001816{
Damien Miller95def091999-11-25 00:26:21 +11001817 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001818 char *line = NULL;
1819 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001820 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001821 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001823 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1824 fatal("Too many recursive configuration includes");
1825
Damien Miller57a44762004-04-20 20:11:57 +10001826 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001827 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828
Darren Tuckeraefa3682013-04-05 11:18:35 +11001829 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001830 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001831
Damien Miller33793852004-06-15 10:27:55 +10001832 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001833 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001834 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001835 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001836 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001837 }
1838
Damien Miller95def091999-11-25 00:26:21 +11001839 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840
Damien Miller5428f641999-11-25 11:54:57 +11001841 /*
1842 * Mark that we are now processing the options. This flag is turned
1843 * on/off by Host specifications.
1844 */
Damien Miller95def091999-11-25 00:26:21 +11001845 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001846 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001847 /* Update line number counter. */
1848 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001849 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001850 line, filename, linenum, activep, flags, want_final_pass,
1851 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001852 bad_options++;
1853 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001854 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001855 fclose(f);
1856 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001857 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001858 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001859 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860}
1861
Damien Miller13f97b22014-02-24 15:57:55 +11001862/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1863int
1864option_clear_or_none(const char *o)
1865{
1866 return o == NULL || strcasecmp(o, "none") == 0;
1867}
1868
Damien Miller5428f641999-11-25 11:54:57 +11001869/*
1870 * Initializes options to special values that indicate that they have not yet
1871 * been set. Read_config_file will only set options with this value. Options
1872 * are processed in the following order: command line, user config file,
1873 * system config file. Last, fill_default_options is called.
1874 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001875
Damien Miller4af51302000-04-16 11:18:38 +10001876void
Damien Miller95def091999-11-25 00:26:21 +11001877initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878{
Damien Miller95def091999-11-25 00:26:21 +11001879 memset(options, 'X', sizeof(*options));
1880 options->forward_agent = -1;
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001881 options->forward_agent_sock_path = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001882 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001883 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001884 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001885 options->stdio_forward_host = NULL;
1886 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001887 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001888 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001889 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001890 options->fwd_opts.gateway_ports = -1;
1891 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1892 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001893 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001894 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001895 options->gss_authentication = -1;
1896 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001897 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001898 options->kbd_interactive_authentication = -1;
1899 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001900 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001901 options->batch_mode = -1;
1902 options->check_host_ip = -1;
1903 options->strict_host_key_checking = -1;
1904 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001905 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001906 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001907 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001908 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001909 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001910 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001911 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001912 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001913 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001914 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001915 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001916 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001917 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001918 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001919 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001920 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001921 options->jump_user = NULL;
1922 options->jump_host = NULL;
1923 options->jump_port = -1;
1924 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001925 options->user = NULL;
1926 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001927 options->num_system_hostfiles = 0;
1928 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001929 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001930 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001931 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001932 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001933 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001934 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001935 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001936 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001937 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001938 options->pkcs11_provider = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001939 options->sk_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001940 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001941 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001942 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001943 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001944 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001945 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001946 options->server_alive_interval = -1;
1947 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001948 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001949 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001950 options->setenv = NULL;
1951 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001952 options->control_path = NULL;
1953 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001954 options->control_persist = -1;
1955 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001956 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001957 options->tun_open = -1;
1958 options->tun_local = -1;
1959 options->tun_remote = -1;
1960 options->local_command = NULL;
1961 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001962 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001963 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001964 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001965 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001966 options->ip_qos_interactive = -1;
1967 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001968 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001969 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001970 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001971 options->num_canonical_domains = 0;
1972 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001973 options->canonicalize_max_dots = -1;
1974 options->canonicalize_fallback_local = -1;
1975 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001976 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001977 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001978 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001979 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001980 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001981}
1982
Damien Miller5428f641999-11-25 11:54:57 +11001983/*
Damien Miller13f97b22014-02-24 15:57:55 +11001984 * A petite version of fill_default_options() that just fills the options
1985 * needed for hostname canonicalization to proceed.
1986 */
1987void
1988fill_default_options_for_canonicalization(Options *options)
1989{
1990 if (options->canonicalize_max_dots == -1)
1991 options->canonicalize_max_dots = 1;
1992 if (options->canonicalize_fallback_local == -1)
1993 options->canonicalize_fallback_local = 1;
1994 if (options->canonicalize_hostname == -1)
1995 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1996}
1997
1998/*
Damien Miller5428f641999-11-25 11:54:57 +11001999 * Called after processing other sources of option data, this fills those
2000 * options for which no value has been specified with their default values.
2001 */
Damien Miller4af51302000-04-16 11:18:38 +10002002void
Damien Miller95def091999-11-25 00:26:21 +11002003fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002004{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002005 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002006 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002007
Damien Miller95def091999-11-25 00:26:21 +11002008 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10002009 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002010 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11002011 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10002012 if (options->forward_x11_trusted == -1)
2013 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10002014 if (options->forward_x11_timeout == -1)
2015 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002016 /*
2017 * stdio forwarding (-W) changes the default for these but we defer
2018 * setting the values so they can be overridden.
2019 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002020 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002021 options->exit_on_forward_failure =
2022 options->stdio_forward_host != NULL ? 1 : 0;
2023 if (options->clear_forwardings == -1)
2024 options->clear_forwardings =
2025 options->stdio_forward_host != NULL ? 1 : 0;
2026 if (options->clear_forwardings == 1)
2027 clear_forwardings(options);
2028
Damien Millerd3a18572000-06-07 19:55:44 +10002029 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00002030 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10002031 if (options->fwd_opts.gateway_ports == -1)
2032 options->fwd_opts.gateway_ports = 0;
2033 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2034 options->fwd_opts.streamlocal_bind_mask = 0177;
2035 if (options->fwd_opts.streamlocal_bind_unlink == -1)
2036 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002037 if (options->pubkey_authentication == -1)
2038 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00002039 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002040 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002041 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002042 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002043 if (options->gss_deleg_creds == -1)
2044 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002045 if (options->password_authentication == -1)
2046 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002047 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002048 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002049 if (options->hostbased_authentication == -1)
2050 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002051 if (options->batch_mode == -1)
2052 options->batch_mode = 0;
2053 if (options->check_host_ip == -1)
2054 options->check_host_ip = 1;
2055 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002056 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002057 if (options->compression == -1)
2058 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002059 if (options->tcp_keep_alive == -1)
2060 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002061 if (options->port == -1)
2062 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002063 if (options->address_family == -1)
2064 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002065 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002066 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002067 if (options->number_of_password_prompts == -1)
2068 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002069 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002070 if (options->add_keys_to_agent == -1)
2071 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002072 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002073 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2074 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002075#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002076 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002077 add_identity_file(options, "~/",
2078 _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002079#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002080 add_identity_file(options, "~/",
2081 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org2c557442019-11-12 19:33:08 +00002082 add_identity_file(options, "~/",
2083 _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002084 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002085 }
Damien Miller95def091999-11-25 00:26:21 +11002086 if (options->escape_char == -1)
2087 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002088 if (options->num_system_hostfiles == 0) {
2089 options->system_hostfiles[options->num_system_hostfiles++] =
2090 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2091 options->system_hostfiles[options->num_system_hostfiles++] =
2092 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2093 }
2094 if (options->num_user_hostfiles == 0) {
2095 options->user_hostfiles[options->num_user_hostfiles++] =
2096 xstrdup(_PATH_SSH_USER_HOSTFILE);
2097 options->user_hostfiles[options->num_user_hostfiles++] =
2098 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2099 }
Damien Millerfcd93202002-02-05 12:26:34 +11002100 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002101 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002102 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2103 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002104 if (options->no_host_authentication_for_localhost == - 1)
2105 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002106 if (options->identities_only == -1)
2107 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002108 if (options->enable_ssh_keysign == -1)
2109 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002110 if (options->rekey_limit == -1)
2111 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002112 if (options->rekey_interval == -1)
2113 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002114 if (options->verify_host_key_dns == -1)
2115 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002116 if (options->server_alive_interval == -1)
2117 options->server_alive_interval = 0;
2118 if (options->server_alive_count_max == -1)
2119 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002120 if (options->control_master == -1)
2121 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002122 if (options->control_persist == -1) {
2123 options->control_persist = 0;
2124 options->control_persist_timeout = 0;
2125 }
Damien Millere1776152005-03-01 21:47:37 +11002126 if (options->hash_known_hosts == -1)
2127 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002128 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002129 options->tun_open = SSH_TUNMODE_NO;
2130 if (options->tun_local == -1)
2131 options->tun_local = SSH_TUNID_ANY;
2132 if (options->tun_remote == -1)
2133 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002134 if (options->permit_local_command == -1)
2135 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002136 if (options->visual_host_key == -1)
2137 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002138 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002139 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002140 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002141 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002142 if (options->request_tty == -1)
2143 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002144 if (options->proxy_use_fdpass == -1)
2145 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002146 if (options->canonicalize_max_dots == -1)
2147 options->canonicalize_max_dots = 1;
2148 if (options->canonicalize_fallback_local == -1)
2149 options->canonicalize_fallback_local = 1;
2150 if (options->canonicalize_hostname == -1)
2151 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002152 if (options->fingerprint_hash == -1)
2153 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002154 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002155 options->update_hostkeys = 0;
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002156#ifdef ENABLE_SK_INTERNAL
2157 if (options->sk_provider == NULL)
2158 options->sk_provider = xstrdup("internal");
2159#else
djm@openbsd.org884416b2019-10-31 21:18:28 +00002160 if (options->sk_provider == NULL)
2161 options->sk_provider = xstrdup("$SSH_SK_PROVIDER");
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002162#endif
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002163
2164 /* Expand KEX name lists */
2165 all_cipher = cipher_alg_list(',', 0);
2166 all_mac = mac_alg_list(',');
2167 all_kex = kex_alg_list(',');
2168 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002169 all_sig = sshkey_alg_list(0, 1, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002170#define ASSEMBLE(what, defaults, all) \
2171 do { \
2172 if ((r = kex_assemble_names(&options->what, \
2173 defaults, all)) != 0) \
2174 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2175 } while (0)
djm@openbsd.org9b611302019-02-23 08:20:43 +00002176 ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
2177 ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
2178 ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002179 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.org56584cc2019-12-15 18:57:30 +00002180 ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002181 ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002182#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002183 free(all_cipher);
2184 free(all_mac);
2185 free(all_kex);
2186 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002187 free(all_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002188
Damien Millere9fc72e2013-10-15 12:14:12 +11002189#define CLEAR_ON_NONE(v) \
2190 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002191 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002192 free(v); \
2193 v = NULL; \
2194 } \
2195 } while(0)
2196 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002197 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002198 CLEAR_ON_NONE(options->proxy_command);
2199 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002200 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org95a80582019-03-01 02:08:50 +00002201 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002202 CLEAR_ON_NONE(options->sk_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002203 if (options->jump_host != NULL &&
2204 strcmp(options->jump_host, "none") == 0 &&
2205 options->jump_port == 0 && options->jump_user == NULL) {
2206 free(options->jump_host);
2207 options->jump_host = NULL;
2208 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002209 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002210 /* options->user will be set in the main program if appropriate */
2211 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002212 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002213 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002214}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002215
Damien Miller7acefbb2014-07-18 14:11:24 +10002216struct fwdarg {
2217 char *arg;
2218 int ispath;
2219};
2220
2221/*
2222 * parse_fwd_field
2223 * parses the next field in a port forwarding specification.
2224 * sets fwd to the parsed field and advances p past the colon
2225 * or sets it to NULL at end of string.
2226 * returns 0 on success, else non-zero.
2227 */
2228static int
2229parse_fwd_field(char **p, struct fwdarg *fwd)
2230{
2231 char *ep, *cp = *p;
2232 int ispath = 0;
2233
2234 if (*cp == '\0') {
2235 *p = NULL;
2236 return -1; /* end of string */
2237 }
2238
2239 /*
2240 * A field escaped with square brackets is used literally.
2241 * XXX - allow ']' to be escaped via backslash?
2242 */
2243 if (*cp == '[') {
2244 /* find matching ']' */
2245 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2246 if (*ep == '/')
2247 ispath = 1;
2248 }
2249 /* no matching ']' or not at end of field. */
2250 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2251 return -1;
2252 /* NUL terminate the field and advance p past the colon */
2253 *ep++ = '\0';
2254 if (*ep != '\0')
2255 *ep++ = '\0';
2256 fwd->arg = cp + 1;
2257 fwd->ispath = ispath;
2258 *p = ep;
2259 return 0;
2260 }
2261
2262 for (cp = *p; *cp != '\0'; cp++) {
2263 switch (*cp) {
2264 case '\\':
2265 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002266 if (*cp == '\0')
2267 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002268 break;
2269 case '/':
2270 ispath = 1;
2271 break;
2272 case ':':
2273 *cp++ = '\0';
2274 goto done;
2275 }
2276 }
2277done:
2278 fwd->arg = *p;
2279 fwd->ispath = ispath;
2280 *p = cp;
2281 return 0;
2282}
2283
Damien Millerf91ee4c2005-03-01 21:24:33 +11002284/*
2285 * parse_forward
2286 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002287 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002288 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2289 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002290 * dynamicfwd == 1
2291 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002292 * returns number of arguments parsed or zero on error
2293 */
2294int
Damien Miller7acefbb2014-07-18 14:11:24 +10002295parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002296{
Damien Miller7acefbb2014-07-18 14:11:24 +10002297 struct fwdarg fwdargs[4];
2298 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002299 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002300
Damien Miller7acefbb2014-07-18 14:11:24 +10002301 memset(fwd, 0, sizeof(*fwd));
2302 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002303
2304 cp = p = xstrdup(fwdspec);
2305
2306 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002307 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002308 cp++;
2309
Damien Miller7acefbb2014-07-18 14:11:24 +10002310 for (i = 0; i < 4; ++i) {
2311 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002312 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002313 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002314
Damien Millerf4b39532008-11-03 19:28:21 +11002315 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002316 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002317 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002318 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002319
2320 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002321 case 1:
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 } else {
2326 fwd->listen_host = NULL;
2327 fwd->listen_port = a2port(fwdargs[0].arg);
2328 }
Damien Millera699d952008-11-03 19:27:34 +11002329 fwd->connect_host = xstrdup("socks");
2330 break;
2331
2332 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002333 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2334 fwd->listen_path = xstrdup(fwdargs[0].arg);
2335 fwd->listen_port = PORT_STREAMLOCAL;
2336 fwd->connect_path = xstrdup(fwdargs[1].arg);
2337 fwd->connect_port = PORT_STREAMLOCAL;
2338 } else if (fwdargs[1].ispath) {
2339 fwd->listen_host = NULL;
2340 fwd->listen_port = a2port(fwdargs[0].arg);
2341 fwd->connect_path = xstrdup(fwdargs[1].arg);
2342 fwd->connect_port = PORT_STREAMLOCAL;
2343 } else {
2344 fwd->listen_host = xstrdup(fwdargs[0].arg);
2345 fwd->listen_port = a2port(fwdargs[1].arg);
2346 fwd->connect_host = xstrdup("socks");
2347 }
Damien Millera699d952008-11-03 19:27:34 +11002348 break;
2349
Damien Millerf91ee4c2005-03-01 21:24:33 +11002350 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002351 if (fwdargs[0].ispath) {
2352 fwd->listen_path = xstrdup(fwdargs[0].arg);
2353 fwd->listen_port = PORT_STREAMLOCAL;
2354 fwd->connect_host = xstrdup(fwdargs[1].arg);
2355 fwd->connect_port = a2port(fwdargs[2].arg);
2356 } else if (fwdargs[2].ispath) {
2357 fwd->listen_host = xstrdup(fwdargs[0].arg);
2358 fwd->listen_port = a2port(fwdargs[1].arg);
2359 fwd->connect_path = xstrdup(fwdargs[2].arg);
2360 fwd->connect_port = PORT_STREAMLOCAL;
2361 } else {
2362 fwd->listen_host = NULL;
2363 fwd->listen_port = a2port(fwdargs[0].arg);
2364 fwd->connect_host = xstrdup(fwdargs[1].arg);
2365 fwd->connect_port = a2port(fwdargs[2].arg);
2366 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002367 break;
2368
2369 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002370 fwd->listen_host = xstrdup(fwdargs[0].arg);
2371 fwd->listen_port = a2port(fwdargs[1].arg);
2372 fwd->connect_host = xstrdup(fwdargs[2].arg);
2373 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002374 break;
2375 default:
2376 i = 0; /* failure */
2377 }
2378
Darren Tuckera627d422013-06-02 07:31:17 +10002379 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002380
Damien Millera699d952008-11-03 19:27:34 +11002381 if (dynamicfwd) {
2382 if (!(i == 1 || i == 2))
2383 goto fail_free;
2384 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002385 if (!(i == 3 || i == 4)) {
2386 if (fwd->connect_path == NULL &&
2387 fwd->listen_path == NULL)
2388 goto fail_free;
2389 }
2390 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002391 goto fail_free;
2392 }
2393
Damien Miller7acefbb2014-07-18 14:11:24 +10002394 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2395 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002396 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002397 if (fwd->connect_host != NULL &&
2398 strlen(fwd->connect_host) >= NI_MAXHOST)
2399 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002400 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2401 if (fwd->connect_path != NULL &&
2402 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2403 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002404 if (fwd->listen_host != NULL &&
2405 strlen(fwd->listen_host) >= NI_MAXHOST)
2406 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002407 if (fwd->listen_path != NULL &&
2408 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2409 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002410
2411 return (i);
2412
2413 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002414 free(fwd->connect_host);
2415 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002416 free(fwd->connect_path);
2417 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002418 free(fwd->listen_host);
2419 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002420 free(fwd->listen_path);
2421 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002422 return (0);
2423}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002424
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002425int
2426parse_jump(const char *s, Options *o, int active)
2427{
2428 char *orig, *sdup, *cp;
2429 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002430 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002431
2432 active &= o->proxy_command == NULL && o->jump_host == NULL;
2433
2434 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002435 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002436 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002437 if (strcasecmp(s, "none") == 0)
2438 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002439 if ((cp = strrchr(sdup, ',')) == NULL)
2440 cp = sdup; /* last */
2441 else
2442 *cp++ = '\0';
2443
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002444 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002445 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002446 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2447 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002448 goto out;
2449 } else {
2450 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002451 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2452 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002453 goto out;
2454 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002455 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002456 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002457 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002458 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002459 if (strcasecmp(s, "none") == 0) {
2460 o->jump_host = xstrdup("none");
2461 o->jump_port = 0;
2462 } else {
2463 o->jump_user = user;
2464 o->jump_host = host;
2465 o->jump_port = port;
2466 o->proxy_command = xstrdup("none");
2467 user = host = NULL;
2468 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2469 o->jump_extra = xstrdup(s);
2470 o->jump_extra[cp - s] = '\0';
2471 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002472 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002473 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002474 ret = 0;
2475 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002476 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002477 free(user);
2478 free(host);
2479 return ret;
2480}
2481
millert@openbsd.org887669e2017-10-21 23:06:24 +00002482int
2483parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2484{
2485 char *path;
2486 int r;
2487
2488 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2489 if (r == 0 && path != NULL)
2490 r = -1; /* path not allowed */
2491 return r;
2492}
2493
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002494/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2495static const char *
2496fmt_multistate_int(int val, const struct multistate *m)
2497{
2498 u_int i;
2499
2500 for (i = 0; m[i].key != NULL; i++) {
2501 if (m[i].value == val)
2502 return m[i].key;
2503 }
2504 return "UNKNOWN";
2505}
2506
2507static const char *
2508fmt_intarg(OpCodes code, int val)
2509{
2510 if (val == -1)
2511 return "unset";
2512 switch (code) {
2513 case oAddressFamily:
2514 return fmt_multistate_int(val, multistate_addressfamily);
2515 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002516 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002517 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002518 case oStrictHostKeyChecking:
2519 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002520 case oControlMaster:
2521 return fmt_multistate_int(val, multistate_controlmaster);
2522 case oTunnel:
2523 return fmt_multistate_int(val, multistate_tunnel);
2524 case oRequestTTY:
2525 return fmt_multistate_int(val, multistate_requesttty);
2526 case oCanonicalizeHostname:
2527 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002528 case oAddKeysToAgent:
2529 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002530 case oFingerprintHash:
2531 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002532 default:
2533 switch (val) {
2534 case 0:
2535 return "no";
2536 case 1:
2537 return "yes";
2538 default:
2539 return "UNKNOWN";
2540 }
2541 }
2542}
2543
2544static const char *
2545lookup_opcode_name(OpCodes code)
2546{
2547 u_int i;
2548
2549 for (i = 0; keywords[i].name != NULL; i++)
2550 if (keywords[i].opcode == code)
2551 return(keywords[i].name);
2552 return "UNKNOWN";
2553}
2554
2555static void
2556dump_cfg_int(OpCodes code, int val)
2557{
2558 printf("%s %d\n", lookup_opcode_name(code), val);
2559}
2560
2561static void
2562dump_cfg_fmtint(OpCodes code, int val)
2563{
2564 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2565}
2566
2567static void
2568dump_cfg_string(OpCodes code, const char *val)
2569{
2570 if (val == NULL)
2571 return;
2572 printf("%s %s\n", lookup_opcode_name(code), val);
2573}
2574
2575static void
2576dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2577{
2578 u_int i;
2579
2580 for (i = 0; i < count; i++)
2581 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2582}
2583
2584static void
2585dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2586{
2587 u_int i;
2588
2589 printf("%s", lookup_opcode_name(code));
2590 for (i = 0; i < count; i++)
2591 printf(" %s", vals[i]);
2592 printf("\n");
2593}
2594
2595static void
2596dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2597{
2598 const struct Forward *fwd;
2599 u_int i;
2600
2601 /* oDynamicForward */
2602 for (i = 0; i < count; i++) {
2603 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002604 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002605 strcmp(fwd->connect_host, "socks") != 0)
2606 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002607 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002608 strcmp(fwd->connect_host, "socks") == 0)
2609 continue;
2610 printf("%s", lookup_opcode_name(code));
2611 if (fwd->listen_port == PORT_STREAMLOCAL)
2612 printf(" %s", fwd->listen_path);
2613 else if (fwd->listen_host == NULL)
2614 printf(" %d", fwd->listen_port);
2615 else {
2616 printf(" [%s]:%d",
2617 fwd->listen_host, fwd->listen_port);
2618 }
2619 if (code != oDynamicForward) {
2620 if (fwd->connect_port == PORT_STREAMLOCAL)
2621 printf(" %s", fwd->connect_path);
2622 else if (fwd->connect_host == NULL)
2623 printf(" %d", fwd->connect_port);
2624 else {
2625 printf(" [%s]:%d",
2626 fwd->connect_host, fwd->connect_port);
2627 }
2628 }
2629 printf("\n");
2630 }
2631}
2632
2633void
2634dump_client_config(Options *o, const char *host)
2635{
2636 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002637 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002638
djm@openbsd.org60a92472015-08-21 23:53:08 +00002639 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002640 all_key = sshkey_alg_list(0, 0, 1, ',');
2641 if (kex_assemble_names( &o->hostkeyalgorithms,
2642 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002643 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002644 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002645
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002646 /* Most interesting options first: user, host, port */
2647 dump_cfg_string(oUser, o->user);
jmc@openbsd.org73491492019-06-12 11:31:50 +00002648 dump_cfg_string(oHostname, host);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002649 dump_cfg_int(oPort, o->port);
2650
2651 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002652 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002653 dump_cfg_fmtint(oAddressFamily, o->address_family);
2654 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2655 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2656 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2657 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2658 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2659 dump_cfg_fmtint(oCompression, o->compression);
2660 dump_cfg_fmtint(oControlMaster, o->control_master);
2661 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002662 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002663 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002664 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002665 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2666 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2667 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2668#ifdef GSSAPI
2669 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2670 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2671#endif /* GSSAPI */
2672 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2673 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2674 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2675 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2676 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2677 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2678 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002679 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2680 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2681 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002682 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2683 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2684 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2685 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002686 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2687 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002688 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002689
2690 /* Integer options */
2691 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002692 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2693 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2694 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2695 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2696 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2697
2698 /* String options */
2699 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002700 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002701 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2702 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002703 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002704 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002705 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002706 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002707 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002708 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2709 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002710 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002711 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002712 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002713 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2714 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002715#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002716 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002717#endif
djm@openbsd.org884416b2019-10-31 21:18:28 +00002718 dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002719 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002720 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002721 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002722 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002723
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002724 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002725 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2726 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2727 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2728
2729 /* String array options */
2730 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2731 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002732 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002733 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2734 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2735 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002736 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002737
2738 /* Special cases */
2739
djm@openbsd.org40be78f2019-12-21 02:19:13 +00002740 /* oForwardAgent */
2741 if (o->forward_agent_sock_path == NULL)
2742 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2743 else
2744 dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
2745
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002746 /* oConnectTimeout */
2747 if (o->connection_timeout == -1)
2748 printf("connecttimeout none\n");
2749 else
2750 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2751
2752 /* oTunnelDevice */
2753 printf("tunneldevice");
2754 if (o->tun_local == SSH_TUNID_ANY)
2755 printf(" any");
2756 else
2757 printf(" %d", o->tun_local);
2758 if (o->tun_remote == SSH_TUNID_ANY)
2759 printf(":any");
2760 else
2761 printf(":%d", o->tun_remote);
2762 printf("\n");
2763
2764 /* oCanonicalizePermittedCNAMEs */
2765 if ( o->num_permitted_cnames > 0) {
2766 printf("canonicalizePermittedcnames");
2767 for (i = 0; i < o->num_permitted_cnames; i++) {
2768 printf(" %s:%s", o->permitted_cnames[i].source_list,
2769 o->permitted_cnames[i].target_list);
2770 }
2771 printf("\n");
2772 }
2773
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002774 /* oControlPersist */
2775 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2776 dump_cfg_fmtint(oControlPersist, o->control_persist);
2777 else
2778 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2779
2780 /* oEscapeChar */
2781 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2782 printf("escapechar none\n");
2783 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002784 vis(buf, o->escape_char, VIS_WHITE, 0);
2785 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002786 }
2787
2788 /* oIPQoS */
2789 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2790 printf("%s\n", iptos2str(o->ip_qos_bulk));
2791
2792 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002793 printf("rekeylimit %llu %d\n",
2794 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002795
2796 /* oStreamLocalBindMask */
2797 printf("streamlocalbindmask 0%o\n",
2798 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002799
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002800 /* oLogFacility */
2801 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2802
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002803 /* oProxyCommand / oProxyJump */
2804 if (o->jump_host == NULL)
2805 dump_cfg_string(oProxyCommand, o->proxy_command);
2806 else {
2807 /* Check for numeric addresses */
2808 i = strchr(o->jump_host, ':') != NULL ||
2809 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2810 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2811 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002812 /* optional additional jump spec */
2813 o->jump_extra == NULL ? "" : o->jump_extra,
2814 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002815 /* optional user */
2816 o->jump_user == NULL ? "" : o->jump_user,
2817 o->jump_user == NULL ? "" : "@",
2818 /* opening [ if hostname is numeric */
2819 i ? "[" : "",
2820 /* mandatory hostname */
2821 o->jump_host,
2822 /* closing ] if hostname is numeric */
2823 i ? "]" : "",
2824 /* optional port number */
2825 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002826 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002827 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002828}