blob: b259845480c62f510584c2b1015ff8c16892d98a [file] [log] [blame]
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001/* $OpenBSD: readconf.c,v 1.322 2020/01/23 10:24:29 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000040#include <stdarg.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +000056#include "ssherr.h"
Damien Miller78928792000-04-12 20:17:38 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
59#include "pathnames.h"
60#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000061#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100062#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "readconf.h"
64#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "kex.h"
66#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110067#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000068#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000069#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Format of the configuration file:
72
73 # Configuration data is parsed as follows:
74 # 1. command line options
75 # 2. user-specific file
76 # 3. system-wide file
77 # Any configuration value is only changed the first time it is set.
78 # Thus, host-specific definitions should be at the beginning of the
79 # configuration file, and defaults at the end.
80
81 # Host-specific declarations. These may override anything above. A single
82 # host may match multiple declarations; these are processed in the order
83 # that they are given in.
84
85 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host fake.com
jmc@openbsd.org73491492019-06-12 11:31:50 +000089 Hostname another.host.name.real.org
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 User blaah
91 Port 34289
92 ForwardX11 no
93 ForwardAgent no
94
95 Host books.com
96 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000097 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host fascist.blob.com
100 Port 23123
101 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 PasswordAuthentication no
103
104 Host puukko.hut.fi
105 User t35124p
106 ProxyCommand ssh-proxy %h %p
107
108 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000112 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 PasswordAuthentication no
114
Damien Millerd27b9472005-12-13 19:29:02 +1100115 Host vpn.fake.com
116 Tunnel yes
117 TunnelDevice 3
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 # Defaults for various options
120 Host *
121 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 PasswordAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100125 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 IdentityFile ~/.ssh/identity
127 Port 22
128 EscapeChar ~
129
130*/
131
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000132static int read_config_file_depth(const char *filename, struct passwd *pw,
133 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000134 int flags, int *activep, int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000135static int process_config_line_depth(Options *options, struct passwd *pw,
136 const char *host, const char *original_host, char *line,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000137 const char *filename, int linenum, int *activep, int flags,
138 int *want_final_pass, int depth);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000147 oPasswordAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000149 oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000151 oUser, oEscapeChar, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
dtucker@openbsd.org8784b022019-12-20 02:29:21 +0000154 oTCPKeepAlive, oNumberOfPasswordPrompts,
155 oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000159 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000164 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000165 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000166 oTunnel, oTunnelDevice,
167 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000168 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000169 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100170 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
171 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000172 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000173 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000174 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
djm@openbsd.org884416b2019-10-31 21:18:28 +0000175 oSecurityKeyProvider,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000176 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177} OpCodes;
178
179/* Textual representations of the tokens. */
180
Damien Miller95def091999-11-25 00:26:21 +1100181static struct {
182 const char *name;
183 OpCodes opcode;
184} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000185 /* Deprecated options */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000186 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000187 { "cipher", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000188 { "fallbacktorsh", oDeprecated },
189 { "globalknownhostsfile2", oDeprecated },
190 { "rhostsauthentication", oDeprecated },
191 { "userknownhostsfile2", oDeprecated },
192 { "useroaming", oDeprecated },
193 { "usersh", oDeprecated },
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +0000194 { "useprivilegedport", oDeprecated },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000195
196 /* Unsupported options */
197 { "afstokenpassing", oUnsupported },
198 { "kerberosauthentication", oUnsupported },
199 { "kerberostgtpassing", oUnsupported },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000200 { "rsaauthentication", oUnsupported },
201 { "rhostsrsaauthentication", oUnsupported },
202 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000203
204 /* Sometimes-unsupported options */
205#if defined(GSSAPI)
206 { "gssapiauthentication", oGssAuthentication },
207 { "gssapidelegatecredentials", oGssDelegateCreds },
208# else
209 { "gssapiauthentication", oUnsupported },
210 { "gssapidelegatecredentials", oUnsupported },
211#endif
212#ifdef ENABLE_PKCS11
djm@openbsd.org381a2612017-01-30 00:38:50 +0000213 { "pkcs11provider", oPKCS11Provider },
djm@openbsd.org95a80582019-03-01 02:08:50 +0000214 { "smartcarddevice", oPKCS11Provider },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000215# else
216 { "smartcarddevice", oUnsupported },
217 { "pkcs11provider", oUnsupported },
218#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000219
Damien Miller95def091999-11-25 00:26:21 +1100220 { "forwardagent", oForwardAgent },
221 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000222 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000223 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000224 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000225 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "gatewayports", oGatewayPorts },
Damien Miller95def091999-11-25 00:26:21 +1100227 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100228 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
229 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000231 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000232 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000233 { "challengeresponseauthentication", oChallengeResponseAuthentication },
Damien Miller87f08be2018-07-20 13:18:28 +1000234 { "skeyauthentication", oUnsupported },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000235 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100236 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100237 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100238 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000239 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000240 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000241 { "identityagent", oIdentityAgent },
jmc@openbsd.org73491492019-06-12 11:31:50 +0000242 { "hostname", oHostname },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000243 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100244 { "proxycommand", oProxyCommand },
245 { "port", oPort },
Damien Miller78928792000-04-12 20:17:38 +1000246 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000247 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100248 { "remoteforward", oRemoteForward },
249 { "localforward", oLocalForward },
250 { "user", oUser },
251 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100252 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100253 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100254 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100255 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100256 { "connectionattempts", oConnectionAttempts },
257 { "batchmode", oBatchMode },
258 { "checkhostip", oCheckHostIP },
259 { "stricthostkeychecking", oStrictHostKeyChecking },
260 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100261 { "tcpkeepalive", oTCPKeepAlive },
262 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100263 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000264 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100265 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000266 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000267 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000268 { "hostkeyalgorithms", oHostKeyAlgorithms },
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000269 { "casignaturealgorithms", oCASignatureAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000270 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000271 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100272 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000273 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000274 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100275 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000276 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000277 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000278 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100279 { "serveraliveinterval", oServerAliveInterval },
280 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000281 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000282 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000283 { "controlpath", oControlPath },
284 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000285 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100286 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000287 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100288 { "tunnel", oTunnel },
289 { "tunneldevice", oTunnelDevice },
290 { "localcommand", oLocalCommand },
291 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000292 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000293 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000294 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100295 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000296 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000297 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100298 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100299 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
300 { "canonicalizehostname", oCanonicalizeHostname },
301 { "canonicalizemaxdots", oCanonicalizeMaxDots },
302 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000303 { "streamlocalbindmask", oStreamLocalBindMask },
304 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000305 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000306 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000307 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000308 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000309 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000310 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000311 { "proxyjump", oProxyJump },
dtucker@openbsd.org68010ac2019-12-20 02:42:42 +0000312 { "securitykeyprovider", oSecurityKeyProvider },
Damien Miller01ed2272008-11-05 16:20:46 +1100313
Ben Lindstrom65366a82001-12-06 16:32:47 +0000314 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100315};
316
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000317static char *kex_default_pk_alg_filtered;
318
319const char *
320kex_default_pk_alg(void)
321{
322 if (kex_default_pk_alg_filtered == NULL)
323 fatal("kex_default_pk_alg not initialized.");
324 return kex_default_pk_alg_filtered;
325}
326
Damien Miller5428f641999-11-25 11:54:57 +1100327/*
328 * Adds a local TCP/IP port forward to options. Never returns if there is an
329 * error.
330 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller4af51302000-04-16 11:18:38 +1000332void
Damien Miller7acefbb2014-07-18 14:11:24 +1000333add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Miller7acefbb2014-07-18 14:11:24 +1000335 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000336 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000337
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000338 /* Don't add duplicates */
339 for (i = 0; i < options->num_local_forwards; i++) {
340 if (forward_equals(newfwd, options->local_forwards + i))
341 return;
342 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000343 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000344 options->num_local_forwards + 1,
345 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100346 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100347
Damien Miller1a0442f2008-11-05 16:30:06 +1100348 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100349 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000350 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100351 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100352 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000353 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354}
355
Damien Miller5428f641999-11-25 11:54:57 +1100356/*
357 * Adds a remote TCP/IP port forward to options. Never returns if there is
358 * an error.
359 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller4af51302000-04-16 11:18:38 +1000361void
Damien Miller7acefbb2014-07-18 14:11:24 +1000362add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363{
Damien Miller7acefbb2014-07-18 14:11:24 +1000364 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000365 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000366
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000367 /* Don't add duplicates */
368 for (i = 0; i < options->num_remote_forwards; i++) {
369 if (forward_equals(newfwd, options->remote_forwards + i))
370 return;
371 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000372 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000373 options->num_remote_forwards + 1,
374 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100375 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100376
Damien Miller1a0442f2008-11-05 16:30:06 +1100377 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100378 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000379 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100380 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100381 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000382 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100383 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000384 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000387static void
388clear_forwardings(Options *options)
389{
390 int i;
391
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000394 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000396 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100397 }
Damien Miller232cfb12010-06-26 09:50:30 +1000398 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000399 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000400 options->local_forwards = NULL;
401 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100403 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000404 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000405 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000406 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000407 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100408 }
Damien Miller232cfb12010-06-26 09:50:30 +1000409 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000410 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000411 options->remote_forwards = NULL;
412 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000413 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100414 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000415}
416
Darren Tucker19104782013-04-05 11:13:08 +1100417void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000418add_certificate_file(Options *options, const char *path, int userprovided)
419{
420 int i;
421
422 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
423 fatal("Too many certificate files specified (max %d)",
424 SSH_MAX_CERTIFICATE_FILES);
425
426 /* Avoid registering duplicates */
427 for (i = 0; i < options->num_certificate_files; i++) {
428 if (options->certificate_file_userprovided[i] == userprovided &&
429 strcmp(options->certificate_files[i], path) == 0) {
430 debug2("%s: ignoring duplicate key %s", __func__, path);
431 return;
432 }
433 }
434
435 options->certificate_file_userprovided[options->num_certificate_files] =
436 userprovided;
437 options->certificate_files[options->num_certificate_files++] =
438 xstrdup(path);
439}
440
441void
Darren Tucker19104782013-04-05 11:13:08 +1100442add_identity_file(Options *options, const char *dir, const char *filename,
443 int userprovided)
444{
445 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000446 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100447
448 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
449 fatal("Too many identity files specified (max %d)",
450 SSH_MAX_IDENTITY_FILES);
451
452 if (dir == NULL) /* no dir, filename is absolute */
453 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000454 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
455 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100456
Damien Miller15271902014-05-15 13:47:56 +1000457 /* Avoid registering duplicates */
458 for (i = 0; i < options->num_identity_files; i++) {
459 if (options->identity_file_userprovided[i] == userprovided &&
460 strcmp(options->identity_files[i], path) == 0) {
461 debug2("%s: ignoring duplicate key %s", __func__, path);
462 free(path);
463 return;
464 }
465 }
466
Darren Tucker19104782013-04-05 11:13:08 +1100467 options->identity_file_userprovided[options->num_identity_files] =
468 userprovided;
469 options->identity_files[options->num_identity_files++] = path;
470}
471
Damien Miller194fd902013-10-15 12:13:05 +1100472int
473default_ssh_port(void)
474{
475 static int port;
476 struct servent *sp;
477
478 if (port == 0) {
479 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
480 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
481 }
482 return port;
483}
484
485/*
486 * Execute a command in a shell.
487 * Return its exit status or -1 on abnormal exit.
488 */
489static int
490execute_in_shell(const char *cmd)
491{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000492 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100493 pid_t pid;
494 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100495
496 if ((shell = getenv("SHELL")) == NULL)
497 shell = _PATH_BSHELL;
498
djm@openbsd.org9ab5b942019-08-09 05:05:54 +0000499 if (access(shell, X_OK) == -1) {
500 fatal("Shell \"%s\" is not executable: %s",
501 shell, strerror(errno));
502 }
503
Damien Miller194fd902013-10-15 12:13:05 +1100504 /* Need this to redirect subprocess stdin/out */
505 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
506 fatal("open(/dev/null): %s", strerror(errno));
507
508 debug("Executing command: '%.500s'", cmd);
509
510 /* Fork and execute the command. */
511 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000512 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100513
Damien Miller194fd902013-10-15 12:13:05 +1100514 /* Redirect child stdin and stdout. Leave stderr */
515 if (dup2(devnull, STDIN_FILENO) == -1)
516 fatal("dup2: %s", strerror(errno));
517 if (dup2(devnull, STDOUT_FILENO) == -1)
518 fatal("dup2: %s", strerror(errno));
519 if (devnull > STDERR_FILENO)
520 close(devnull);
521 closefrom(STDERR_FILENO + 1);
522
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000523 argv[0] = shell;
524 argv[1] = "-c";
525 argv[2] = xstrdup(cmd);
526 argv[3] = NULL;
527
Damien Miller194fd902013-10-15 12:13:05 +1100528 execv(argv[0], argv);
529 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
530 /* Die with signal to make this error apparent to parent. */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000531 ssh_signal(SIGTERM, SIG_DFL);
Damien Miller194fd902013-10-15 12:13:05 +1100532 kill(getpid(), SIGTERM);
533 _exit(1);
534 }
535 /* Parent. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000536 if (pid == -1)
Damien Miller194fd902013-10-15 12:13:05 +1100537 fatal("%s: fork: %.100s", __func__, strerror(errno));
538
539 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100540
541 while (waitpid(pid, &status, 0) == -1) {
542 if (errno != EINTR && errno != EAGAIN)
543 fatal("%s: waitpid: %s", __func__, strerror(errno));
544 }
545 if (!WIFEXITED(status)) {
546 error("command '%.100s' exited abnormally", cmd);
547 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 }
Damien Miller194fd902013-10-15 12:13:05 +1100549 debug3("command returned status %d", WEXITSTATUS(status));
550 return WEXITSTATUS(status);
551}
552
553/*
554 * Parse and execute a Match directive.
555 */
556static int
557match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000558 const char *host_arg, const char *original_host, int final_pass,
559 int *want_final_pass, const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100560{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000561 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100562 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000563 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100564 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000565 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100566
567 /*
568 * Configuration is likely to be incomplete at this point so we
569 * must be prepared to use default values.
570 */
571 port = options->port <= 0 ? default_ssh_port() : options->port;
572 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000573 if (final_pass) {
djm@openbsd.org5a622842016-02-08 23:40:12 +0000574 host = xstrdup(options->hostname);
575 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100576 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100577 host = percent_expand(options->hostname,
578 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000579 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100580 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000581 }
Damien Miller194fd902013-10-15 12:13:05 +1100582
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 debug2("checking match for '%s' host %s originally %s",
584 cp, host, original_host);
585 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
586 criteria = NULL;
587 this_result = 1;
588 if ((negate = attrib[0] == '!'))
589 attrib++;
590 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100591 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000592 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100593 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000594 error("%.200s line %d: '%s' cannot be combined "
595 "with other Match attributes",
596 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100597 result = -1;
598 goto out;
599 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000600 if (result)
601 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100602 goto out;
603 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000604 attributes++;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000605 if (strcasecmp(attrib, "canonical") == 0 ||
606 strcasecmp(attrib, "final") == 0) {
607 /*
608 * If the config requests "Match final" then remember
609 * this so we can perform a second pass later.
610 */
611 if (strcasecmp(attrib, "final") == 0 &&
612 want_final_pass != NULL)
613 *want_final_pass = 1;
614 r = !!final_pass; /* force bitmask member to boolean */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 if (r == (negate ? 1 : 0))
616 this_result = result = 0;
617 debug3("%.200s line %d: %smatched '%s'",
618 filename, linenum,
619 this_result ? "" : "not ", oattrib);
620 continue;
621 }
622 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100623 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
624 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100625 result = -1;
626 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100627 }
Damien Miller194fd902013-10-15 12:13:05 +1100628 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000630 r = match_hostname(host, arg) == 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, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000634 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000635 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 if (r == (negate ? 1 : 0))
637 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100638 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000639 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000640 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000641 if (r == (negate ? 1 : 0))
642 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100643 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000644 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000645 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000646 if (r == (negate ? 1 : 0))
647 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100648 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100649 if (gethostname(thishost, sizeof(thishost)) == -1)
650 fatal("gethostname: %s", strerror(errno));
651 strlcpy(shorthost, thishost, sizeof(shorthost));
652 shorthost[strcspn(thishost, ".")] = '\0';
653 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000654 snprintf(uidstr, sizeof(uidstr), "%llu",
655 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100656
657 cmd = percent_expand(arg,
658 "L", shorthost,
659 "d", pw->pw_dir,
660 "h", host,
661 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000662 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100663 "p", portstr,
664 "r", ruser,
665 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000666 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100667 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100668 if (result != 1) {
669 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000670 debug3("%.200s line %d: skipped exec "
671 "\"%.100s\"", filename, linenum, cmd);
672 free(cmd);
673 continue;
Damien Miller06287802014-02-24 15:56:45 +1100674 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000675 r = execute_in_shell(cmd);
676 if (r == -1) {
677 fatal("%.200s line %d: match exec "
678 "'%.100s' error", filename,
679 linenum, cmd);
680 }
681 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100682 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000683 /* Force exit status to boolean */
684 r = r == 0;
685 if (r == (negate ? 1 : 0))
686 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100687 } else {
688 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100689 result = -1;
690 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100691 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000692 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
693 filename, linenum, this_result ? "": "not ",
694 oattrib, criteria);
695 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100696 }
Damien Millercf31f382013-10-24 21:02:56 +1100697 if (attributes == 0) {
698 error("One or more attributes required for Match");
699 result = -1;
700 goto out;
701 }
Damien Miller084bcd22013-10-23 16:30:51 +1100702 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000703 if (result != -1)
704 debug2("match %sfound", result ? "" : "not ");
705 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100706 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100707 return result;
708}
709
djm@openbsd.org555294a2018-04-06 13:02:39 +0000710/* Remove environment variable by pattern */
711static void
712rm_env(Options *options, const char *arg, const char *filename, int linenum)
713{
714 int i, j;
715 char *cp;
716
717 /* Remove an environment variable */
718 for (i = 0; i < options->num_send_env; ) {
719 cp = xstrdup(options->send_env[i]);
720 if (!match_pattern(cp, arg + 1)) {
721 free(cp);
722 i++;
723 continue;
724 }
725 debug3("%s line %d: removing environment %s",
726 filename, linenum, cp);
727 free(cp);
728 free(options->send_env[i]);
729 options->send_env[i] = NULL;
730 for (j = i; j < options->num_send_env - 1; j++) {
731 options->send_env[j] = options->send_env[j + 1];
732 options->send_env[j + 1] = NULL;
733 }
734 options->num_send_env--;
735 /* NB. don't increment i */
736 }
737}
738
Damien Miller5428f641999-11-25 11:54:57 +1100739/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000740 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100741 */
Damien Miller4af51302000-04-16 11:18:38 +1000742static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000743parse_token(const char *cp, const char *filename, int linenum,
744 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745{
Darren Tucker07636982013-05-16 20:30:03 +1000746 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Damien Miller95def091999-11-25 00:26:21 +1100748 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000749 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100750 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000751 if (ignored_unknown != NULL &&
752 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000753 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000754 error("%s: line %d: Bad configuration option: %s",
755 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100756 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757}
758
Damien Millere9fc72e2013-10-15 12:14:12 +1100759/* Multistate option parsing */
760struct multistate {
761 char *key;
762 int value;
763};
764static const struct multistate multistate_flag[] = {
765 { "true", 1 },
766 { "false", 0 },
767 { "yes", 1 },
768 { "no", 0 },
769 { NULL, -1 }
770};
771static const struct multistate multistate_yesnoask[] = {
772 { "true", 1 },
773 { "false", 0 },
774 { "yes", 1 },
775 { "no", 0 },
776 { "ask", 2 },
777 { NULL, -1 }
778};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000779static const struct multistate multistate_strict_hostkey[] = {
780 { "true", SSH_STRICT_HOSTKEY_YES },
781 { "false", SSH_STRICT_HOSTKEY_OFF },
782 { "yes", SSH_STRICT_HOSTKEY_YES },
783 { "no", SSH_STRICT_HOSTKEY_OFF },
784 { "ask", SSH_STRICT_HOSTKEY_ASK },
785 { "off", SSH_STRICT_HOSTKEY_OFF },
786 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
787 { NULL, -1 }
788};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000789static const struct multistate multistate_yesnoaskconfirm[] = {
790 { "true", 1 },
791 { "false", 0 },
792 { "yes", 1 },
793 { "no", 0 },
794 { "ask", 2 },
795 { "confirm", 3 },
796 { NULL, -1 }
797};
Damien Millere9fc72e2013-10-15 12:14:12 +1100798static const struct multistate multistate_addressfamily[] = {
799 { "inet", AF_INET },
800 { "inet6", AF_INET6 },
801 { "any", AF_UNSPEC },
802 { NULL, -1 }
803};
804static const struct multistate multistate_controlmaster[] = {
805 { "true", SSHCTL_MASTER_YES },
806 { "yes", SSHCTL_MASTER_YES },
807 { "false", SSHCTL_MASTER_NO },
808 { "no", SSHCTL_MASTER_NO },
809 { "auto", SSHCTL_MASTER_AUTO },
810 { "ask", SSHCTL_MASTER_ASK },
811 { "autoask", SSHCTL_MASTER_AUTO_ASK },
812 { NULL, -1 }
813};
814static const struct multistate multistate_tunnel[] = {
815 { "ethernet", SSH_TUNMODE_ETHERNET },
816 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
817 { "true", SSH_TUNMODE_DEFAULT },
818 { "yes", SSH_TUNMODE_DEFAULT },
819 { "false", SSH_TUNMODE_NO },
820 { "no", SSH_TUNMODE_NO },
821 { NULL, -1 }
822};
823static const struct multistate multistate_requesttty[] = {
824 { "true", REQUEST_TTY_YES },
825 { "yes", REQUEST_TTY_YES },
826 { "false", REQUEST_TTY_NO },
827 { "no", REQUEST_TTY_NO },
828 { "force", REQUEST_TTY_FORCE },
829 { "auto", REQUEST_TTY_AUTO },
830 { NULL, -1 }
831};
Damien Miller38505592013-10-17 11:48:13 +1100832static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100833 { "true", SSH_CANONICALISE_YES },
834 { "false", SSH_CANONICALISE_NO },
835 { "yes", SSH_CANONICALISE_YES },
836 { "no", SSH_CANONICALISE_NO },
837 { "always", SSH_CANONICALISE_ALWAYS },
838 { NULL, -1 }
839};
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000840static const struct multistate multistate_compression[] = {
841#ifdef WITH_ZLIB
842 { "yes", COMP_ZLIB },
843#endif
844 { "no", COMP_NONE },
845 { NULL, -1 }
846};
Damien Millere9fc72e2013-10-15 12:14:12 +1100847
Damien Miller5428f641999-11-25 11:54:57 +1100848/*
849 * Processes a single option line as used in the configuration files. This
850 * only sets those values that have not already been set.
851 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100852int
Damien Miller194fd902013-10-15 12:13:05 +1100853process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000854 const char *original_host, char *line, const char *filename,
855 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000857 return process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000858 line, filename, linenum, activep, flags, NULL, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000859}
860
861#define WHITESPACE " \t\r\n"
862static int
863process_config_line_depth(Options *options, struct passwd *pw, const char *host,
864 const char *original_host, char *line, const char *filename,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000865 int linenum, int *activep, int flags, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000866{
Damien Miller295ee632011-05-29 21:42:31 +1000867 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
868 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000869 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000870 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000871 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100872 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000873 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000874 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100875 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000876 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100877 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100878 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000879 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000880 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
Damien Miller194fd902013-10-15 12:13:05 +1100882 if (activep == NULL) { /* We are processing a command line directive */
883 cmdline = 1;
884 activep = &cmdline;
885 }
886
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000887 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000888 if ((len = strlen(line)) == 0)
889 return 0;
890 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000891 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000892 break;
893 line[len] = '\0';
894 }
895
Damien Millerbe484b52000-07-15 14:14:16 +1000896 s = line;
897 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100898 if ((keyword = strdelim(&s)) == NULL)
899 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000900 /* Ignore leading whitespace. */
901 if (*keyword == '\0')
902 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000903 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100904 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000905 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100906 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907
Darren Tucker07636982013-05-16 20:30:03 +1000908 opcode = parse_token(keyword, filename, linenum,
909 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Damien Miller95def091999-11-25 00:26:21 +1100911 switch (opcode) {
912 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100913 /* don't panic, but count bad options */
914 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000915 case oIgnore:
916 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000917 case oIgnoredUnknownOption:
918 debug("%s line %d: Ignored unknown option \"%s\"",
919 filename, linenum, keyword);
920 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000921 case oConnectTimeout:
922 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100923parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000924 arg = strdelim(&s);
925 if (!arg || *arg == '\0')
926 fatal("%s line %d: missing time value.",
927 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000928 if (strcmp(arg, "none") == 0)
929 value = -1;
930 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000931 fatal("%s line %d: invalid time value.",
932 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100933 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000934 *intptr = value;
935 break;
936
Damien Miller95def091999-11-25 00:26:21 +1100937 case oForwardAgent:
938 intptr = &options->forward_agent;
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000939
940 arg = strdelim(&s);
941 if (!arg || *arg == '\0')
942 fatal("%s line %d: missing argument.",
943 filename, linenum);
944
945 value = -1;
946 multistate_ptr = multistate_flag;
947 for (i = 0; multistate_ptr[i].key != NULL; i++) {
948 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
949 value = multistate_ptr[i].value;
950 break;
951 }
952 }
953 if (value != -1) {
954 if (*activep && *intptr == -1)
955 *intptr = value;
956 break;
957 }
958 /* ForwardAgent wasn't 'yes' or 'no', assume a path */
959 if (*activep && *intptr == -1)
960 *intptr = 1;
961
962 charptr = &options->forward_agent_sock_path;
963 goto parse_agent_path;
964
965 case oForwardX11:
966 intptr = &options->forward_x11;
Damien Millere9fc72e2013-10-15 12:14:12 +1100967 parse_flag:
968 multistate_ptr = multistate_flag;
969 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000970 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000971 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100972 fatal("%s line %d: missing argument.",
973 filename, linenum);
974 value = -1;
975 for (i = 0; multistate_ptr[i].key != NULL; i++) {
976 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
977 value = multistate_ptr[i].value;
978 break;
979 }
980 }
981 if (value == -1)
982 fatal("%s line %d: unsupported option \"%s\".",
983 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100984 if (*activep && *intptr == -1)
985 *intptr = value;
986 break;
987
Darren Tucker0a118da2003-10-15 15:54:32 +1000988 case oForwardX11Trusted:
989 intptr = &options->forward_x11_trusted;
990 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000991
Damien Miller1ab6a512010-06-26 10:02:24 +1000992 case oForwardX11Timeout:
993 intptr = &options->forward_x11_timeout;
994 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000995
Damien Miller95def091999-11-25 00:26:21 +1100996 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000997 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100998 goto parse_flag;
999
Darren Tuckere7d4b192006-07-12 22:17:10 +10001000 case oExitOnForwardFailure:
1001 intptr = &options->exit_on_forward_failure;
1002 goto parse_flag;
1003
Damien Miller95def091999-11-25 00:26:21 +11001004 case oPasswordAuthentication:
1005 intptr = &options->password_authentication;
1006 goto parse_flag;
1007
Damien Miller874d77b2000-10-14 16:23:11 +11001008 case oKbdInteractiveAuthentication:
1009 intptr = &options->kbd_interactive_authentication;
1010 goto parse_flag;
1011
1012 case oKbdInteractiveDevices:
1013 charptr = &options->kbd_interactive_devices;
1014 goto parse_string;
1015
Damien Miller0bc1bd82000-11-13 22:57:25 +11001016 case oPubkeyAuthentication:
1017 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +10001018 goto parse_flag;
1019
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001020 case oHostbasedAuthentication:
1021 intptr = &options->hostbased_authentication;
1022 goto parse_flag;
1023
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001024 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +00001025 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +11001026 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001027
Darren Tucker0efd1552003-08-26 11:49:55 +10001028 case oGssAuthentication:
1029 intptr = &options->gss_authentication;
1030 goto parse_flag;
1031
1032 case oGssDelegateCreds:
1033 intptr = &options->gss_deleg_creds;
1034 goto parse_flag;
1035
Damien Miller95def091999-11-25 00:26:21 +11001036 case oBatchMode:
1037 intptr = &options->batch_mode;
1038 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039
Damien Miller95def091999-11-25 00:26:21 +11001040 case oCheckHostIP:
1041 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +10001042 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
Damien Miller37876e92003-05-15 10:19:46 +10001044 case oVerifyHostKeyDNS:
1045 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +11001046 multistate_ptr = multistate_yesnoask;
1047 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +10001048
Damien Miller95def091999-11-25 00:26:21 +11001049 case oStrictHostKeyChecking:
1050 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001051 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +11001052 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053
Damien Miller95def091999-11-25 00:26:21 +11001054 case oCompression:
1055 intptr = &options->compression;
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001056 multistate_ptr = multistate_compression;
1057 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller12c150e2003-12-17 16:31:10 +11001059 case oTCPKeepAlive:
1060 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001061 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001062
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001063 case oNoHostAuthenticationForLocalhost:
1064 intptr = &options->no_host_authentication_for_localhost;
1065 goto parse_flag;
1066
Damien Miller95def091999-11-25 00:26:21 +11001067 case oNumberOfPasswordPrompts:
1068 intptr = &options->number_of_password_prompts;
1069 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070
Damien Millera5539d22003-04-09 20:50:06 +10001071 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001072 arg = strdelim(&s);
1073 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001074 fatal("%.200s line %d: Missing argument.", filename,
1075 linenum);
1076 if (strcmp(arg, "default") == 0) {
1077 val64 = 0;
1078 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001079 if (scan_scaled(arg, &val64) == -1)
1080 fatal("%.200s line %d: Bad number '%s': %s",
1081 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001082 if (val64 != 0 && val64 < 16)
1083 fatal("%.200s line %d: RekeyLimit too small",
1084 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001085 }
Damien Miller3dff1762008-02-10 22:25:52 +11001086 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001087 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001088 if (s != NULL) { /* optional rekey interval present */
1089 if (strcmp(s, "none") == 0) {
1090 (void)strdelim(&s); /* discard */
1091 break;
1092 }
1093 intptr = &options->rekey_interval;
1094 goto parse_time;
1095 }
Damien Millera5539d22003-04-09 20:50:06 +10001096 break;
1097
Damien Miller95def091999-11-25 00:26:21 +11001098 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001099 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001100 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001101 fatal("%.200s line %d: Missing argument.", filename, linenum);
1102 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001103 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001104 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001105 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001106 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001107 add_identity_file(options, NULL,
1108 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001109 }
1110 break;
1111
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001112 case oCertificateFile:
1113 arg = strdelim(&s);
1114 if (!arg || *arg == '\0')
1115 fatal("%.200s line %d: Missing argument.",
1116 filename, linenum);
1117 if (*activep) {
1118 intptr = &options->num_certificate_files;
1119 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1120 fatal("%.200s line %d: Too many certificate "
1121 "files specified (max %d).",
1122 filename, linenum,
1123 SSH_MAX_CERTIFICATE_FILES);
1124 }
1125 add_certificate_file(options, arg,
1126 flags & SSHCONF_USERCONF);
1127 }
1128 break;
1129
Damien Millerd3a18572000-06-07 19:55:44 +10001130 case oXAuthLocation:
1131 charptr=&options->xauth_location;
1132 goto parse_string;
1133
Damien Miller95def091999-11-25 00:26:21 +11001134 case oUser:
1135 charptr = &options->user;
1136parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001137 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001138 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001139 fatal("%.200s line %d: Missing argument.",
1140 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001141 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001142 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001143 break;
1144
1145 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001146 cpptr = (char **)&options->system_hostfiles;
1147 uintptr = &options->num_system_hostfiles;
1148 max_entries = SSH_MAX_HOSTS_FILES;
1149parse_char_array:
1150 if (*activep && *uintptr == 0) {
1151 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1152 if ((*uintptr) >= max_entries)
1153 fatal("%s line %d: "
1154 "too many authorized keys files.",
1155 filename, linenum);
1156 cpptr[(*uintptr)++] = xstrdup(arg);
1157 }
1158 }
1159 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001160
1161 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001162 cpptr = (char **)&options->user_hostfiles;
1163 uintptr = &options->num_user_hostfiles;
1164 max_entries = SSH_MAX_HOSTS_FILES;
1165 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001166
jmc@openbsd.org73491492019-06-12 11:31:50 +00001167 case oHostname:
Damien Miller95def091999-11-25 00:26:21 +11001168 charptr = &options->hostname;
1169 goto parse_string;
1170
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001171 case oHostKeyAlias:
1172 charptr = &options->host_key_alias;
1173 goto parse_string;
1174
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001175 case oPreferredAuthentications:
1176 charptr = &options->preferred_authentications;
1177 goto parse_string;
1178
Ben Lindstrome0f88042001-04-30 13:06:24 +00001179 case oBindAddress:
1180 charptr = &options->bind_address;
1181 goto parse_string;
1182
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001183 case oBindInterface:
1184 charptr = &options->bind_interface;
1185 goto parse_string;
1186
Damien Miller7ea845e2010-02-12 09:21:02 +11001187 case oPKCS11Provider:
1188 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001189 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001190
djm@openbsd.org884416b2019-10-31 21:18:28 +00001191 case oSecurityKeyProvider:
1192 charptr = &options->sk_provider;
1193 goto parse_string;
1194
Damien Miller95def091999-11-25 00:26:21 +11001195 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001196 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001197 /* Ignore ProxyCommand if ProxyJump already specified */
1198 if (options->jump_host != NULL)
1199 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001200parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001201 if (s == NULL)
1202 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001203 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001204 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001205 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001206 return 0;
1207
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001208 case oProxyJump:
1209 if (s == NULL) {
1210 fatal("%.200s line %d: Missing argument.",
1211 filename, linenum);
1212 }
1213 len = strspn(s, WHITESPACE "=");
1214 if (parse_jump(s + len, options, *activep) == -1) {
1215 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1216 filename, linenum, s + len);
1217 }
1218 return 0;
1219
Damien Miller95def091999-11-25 00:26:21 +11001220 case oPort:
naddy@openbsd.org25813332018-10-05 14:26:09 +00001221 arg = strdelim(&s);
1222 if (!arg || *arg == '\0')
1223 fatal("%.200s line %d: Missing argument.",
1224 filename, linenum);
1225 value = a2port(arg);
1226 if (value <= 0)
1227 fatal("%.200s line %d: Bad port '%s'.",
1228 filename, linenum, arg);
1229 if (*activep && options->port == -1)
1230 options->port = value;
1231 break;
1232
1233 case oConnectionAttempts:
1234 intptr = &options->connection_attempts;
Damien Miller95def091999-11-25 00:26:21 +11001235parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001236 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001237 if ((errstr = atoi_err(arg, &value)) != NULL)
1238 fatal("%s line %d: integer value %s.",
1239 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001240 if (*activep && *intptr == -1)
1241 *intptr = value;
1242 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
Damien Miller78928792000-04-12 20:17:38 +10001244 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001245 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001246 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001247 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001248 if (*arg != '-' &&
1249 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001250 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001251 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001252 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001253 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001254 break;
1255
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001256 case oMacs:
1257 arg = strdelim(&s);
1258 if (!arg || *arg == '\0')
1259 fatal("%.200s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001260 if (*arg != '-' &&
1261 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +00001262 fatal("%.200s line %d: Bad SSH2 MAC spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001263 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001264 if (*activep && options->macs == NULL)
1265 options->macs = xstrdup(arg);
1266 break;
1267
Damien Millerd5f62bf2010-09-24 22:11:14 +10001268 case oKexAlgorithms:
1269 arg = strdelim(&s);
1270 if (!arg || *arg == '\0')
1271 fatal("%.200s line %d: Missing argument.",
1272 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001273 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001274 !kex_names_valid(*arg == '+' || *arg == '^' ?
1275 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001276 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1277 filename, linenum, arg ? arg : "<NONE>");
1278 if (*activep && options->kex_algorithms == NULL)
1279 options->kex_algorithms = xstrdup(arg);
1280 break;
1281
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001282 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001283 charptr = &options->hostkeyalgorithms;
1284parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001285 arg = strdelim(&s);
1286 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001287 fatal("%.200s line %d: Missing argument.",
1288 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001289 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001290 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1291 arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001292 fatal("%s line %d: Bad key types '%s'.",
1293 filename, linenum, arg ? arg : "<NONE>");
1294 if (*activep && *charptr == NULL)
1295 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001296 break;
1297
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001298 case oCASignatureAlgorithms:
1299 charptr = &options->ca_sign_algorithms;
1300 goto parse_keytypes;
1301
Damien Miller95def091999-11-25 00:26:21 +11001302 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001303 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001304 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001305 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001306 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001307 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001308 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001309 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1310 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001311 break;
1312
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001313 case oLogFacility:
1314 log_facility_ptr = &options->log_facility;
1315 arg = strdelim(&s);
1316 value = log_facility_number(arg);
1317 if (value == SYSLOG_FACILITY_NOT_SET)
1318 fatal("%.200s line %d: unsupported log facility '%s'",
1319 filename, linenum, arg ? arg : "<NONE>");
1320 if (*log_facility_ptr == -1)
1321 *log_facility_ptr = (SyslogFacility) value;
1322 break;
1323
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001324 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001325 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001326 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001327 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001328 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001329 fatal("%.200s line %d: Missing port argument.",
1330 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001331
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001332 remotefwd = (opcode == oRemoteForward);
1333 dynamicfwd = (opcode == oDynamicForward);
1334
1335 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001336 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001337 if (arg2 == NULL || *arg2 == '\0') {
1338 if (remotefwd)
1339 dynamicfwd = 1;
1340 else
1341 fatal("%.200s line %d: Missing target "
1342 "argument.", filename, linenum);
1343 } else {
1344 /* construct a string for parse_forward */
1345 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1346 arg2);
1347 }
Damien Millera699d952008-11-03 19:27:34 +11001348 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001349 if (dynamicfwd)
1350 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001351
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001352 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001353 fatal("%.200s line %d: Bad forwarding specification.",
1354 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001355
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001356 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001357 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001358 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001359 } else {
1360 add_local_forward(options, &fwd);
1361 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001362 }
Damien Miller95def091999-11-25 00:26:21 +11001363 break;
1364
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001365 case oClearAllForwardings:
1366 intptr = &options->clear_forwardings;
1367 goto parse_flag;
1368
Damien Miller95def091999-11-25 00:26:21 +11001369 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001370 if (cmdline)
1371 fatal("Host directive not supported as a command-line "
1372 "option");
Damien Miller95def091999-11-25 00:26:21 +11001373 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001374 arg2 = NULL;
1375 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001376 if ((flags & SSHCONF_NEVERMATCH) != 0)
1377 break;
Damien Millerfe924212011-05-15 08:44:45 +10001378 negated = *arg == '!';
1379 if (negated)
1380 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001381 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001382 if (negated) {
1383 debug("%.200s line %d: Skipping Host "
1384 "block because of negated match "
1385 "for %.100s", filename, linenum,
1386 arg);
1387 *activep = 0;
1388 break;
1389 }
1390 if (!*activep)
1391 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001392 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001393 }
Damien Millerfe924212011-05-15 08:44:45 +10001394 }
1395 if (*activep)
1396 debug("%.200s line %d: Applying options for %.100s",
1397 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001398 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001399 return 0;
1400
Damien Miller194fd902013-10-15 12:13:05 +11001401 case oMatch:
1402 if (cmdline)
1403 fatal("Host directive not supported as a command-line "
1404 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001405 value = match_cfg_line(options, &s, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001406 flags & SSHCONF_FINAL, want_final_pass,
1407 filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001408 if (value < 0)
1409 fatal("%.200s line %d: Bad Match condition", filename,
1410 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001411 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001412 break;
1413
Damien Miller95def091999-11-25 00:26:21 +11001414 case oEscapeChar:
1415 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001416 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001417 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001418 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001419 if (strcmp(arg, "none") == 0)
1420 value = SSH_ESCAPECHAR_NONE;
1421 else if (arg[1] == '\0')
1422 value = (u_char) arg[0];
1423 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001424 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1425 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001426 else {
1427 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001428 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001429 /* NOTREACHED */
1430 value = 0; /* Avoid compiler warning. */
1431 }
1432 if (*activep && *intptr == -1)
1433 *intptr = value;
1434 break;
1435
Damien Miller20a8f972003-05-18 20:50:30 +10001436 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001437 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001438 multistate_ptr = multistate_addressfamily;
1439 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001440
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001441 case oEnableSSHKeysign:
1442 intptr = &options->enable_ssh_keysign;
1443 goto parse_flag;
1444
Damien Millerbd394c32004-03-08 23:12:36 +11001445 case oIdentitiesOnly:
1446 intptr = &options->identities_only;
1447 goto parse_flag;
1448
Damien Miller509b0102003-12-17 16:33:10 +11001449 case oServerAliveInterval:
1450 intptr = &options->server_alive_interval;
1451 goto parse_time;
1452
1453 case oServerAliveCountMax:
1454 intptr = &options->server_alive_count_max;
1455 goto parse_int;
1456
Darren Tucker46bc0752004-05-02 22:11:30 +10001457 case oSendEnv:
1458 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1459 if (strchr(arg, '=') != NULL)
1460 fatal("%s line %d: Invalid environment name.",
1461 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001462 if (!*activep)
1463 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001464 if (*arg == '-') {
1465 /* Removing an env var */
1466 rm_env(options, arg, filename, linenum);
1467 continue;
1468 } else {
1469 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001470 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001471 fatal("%s line %d: too many send env.",
1472 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001473 options->send_env = xrecallocarray(
1474 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001475 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001476 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001477 options->send_env[options->num_send_env++] =
1478 xstrdup(arg);
1479 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001480 }
1481 break;
1482
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001483 case oSetEnv:
1484 value = options->num_setenv;
1485 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1486 if (strchr(arg, '=') == NULL)
1487 fatal("%s line %d: Invalid SetEnv.",
1488 filename, linenum);
1489 if (!*activep || value != 0)
1490 continue;
1491 /* Adding a setenv var */
1492 if (options->num_setenv >= INT_MAX)
1493 fatal("%s line %d: too many SetEnv.",
1494 filename, linenum);
1495 options->setenv = xrecallocarray(
1496 options->setenv, options->num_setenv,
1497 options->num_setenv + 1, sizeof(*options->setenv));
1498 options->setenv[options->num_setenv++] = xstrdup(arg);
1499 }
1500 break;
1501
Damien Miller0e220db2004-06-15 10:34:08 +10001502 case oControlPath:
1503 charptr = &options->control_path;
1504 goto parse_string;
1505
1506 case oControlMaster:
1507 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001508 multistate_ptr = multistate_controlmaster;
1509 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001510
Damien Millere11e1ea2010-08-03 16:04:46 +10001511 case oControlPersist:
1512 /* no/false/yes/true, or a time spec */
1513 intptr = &options->control_persist;
1514 arg = strdelim(&s);
1515 if (!arg || *arg == '\0')
1516 fatal("%.200s line %d: Missing ControlPersist"
1517 " argument.", filename, linenum);
1518 value = 0;
1519 value2 = 0; /* timeout */
1520 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1521 value = 0;
1522 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1523 value = 1;
1524 else if ((value2 = convtime(arg)) >= 0)
1525 value = 1;
1526 else
1527 fatal("%.200s line %d: Bad ControlPersist argument.",
1528 filename, linenum);
1529 if (*activep && *intptr == -1) {
1530 *intptr = value;
1531 options->control_persist_timeout = value2;
1532 }
1533 break;
1534
Damien Millere1776152005-03-01 21:47:37 +11001535 case oHashKnownHosts:
1536 intptr = &options->hash_known_hosts;
1537 goto parse_flag;
1538
Damien Millerd27b9472005-12-13 19:29:02 +11001539 case oTunnel:
1540 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001541 multistate_ptr = multistate_tunnel;
1542 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001543
1544 case oTunnelDevice:
1545 arg = strdelim(&s);
1546 if (!arg || *arg == '\0')
1547 fatal("%.200s line %d: Missing argument.", filename, linenum);
1548 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001549 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001550 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1551 if (*activep) {
1552 options->tun_local = value;
1553 options->tun_remote = value2;
1554 }
1555 break;
1556
1557 case oLocalCommand:
1558 charptr = &options->local_command;
1559 goto parse_command;
1560
1561 case oPermitLocalCommand:
1562 intptr = &options->permit_local_command;
1563 goto parse_flag;
1564
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001565 case oRemoteCommand:
1566 charptr = &options->remote_command;
1567 goto parse_command;
1568
Damien Miller10288242008-06-30 00:04:03 +10001569 case oVisualHostKey:
1570 intptr = &options->visual_host_key;
1571 goto parse_flag;
1572
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001573 case oInclude:
1574 if (cmdline)
1575 fatal("Include directive not supported as a "
1576 "command-line option");
1577 value = 0;
1578 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1579 /*
1580 * Ensure all paths are anchored. User configuration
1581 * files may begin with '~/' but system configurations
1582 * must not. If the path is relative, then treat it
1583 * as living in ~/.ssh for user configurations or
1584 * /etc/ssh for system ones.
1585 */
1586 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1587 fatal("%.200s line %d: bad include path %s.",
1588 filename, linenum, arg);
djm@openbsd.org2a358622018-11-16 03:26:01 +00001589 if (!path_absolute(arg) && *arg != '~') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001590 xasprintf(&arg2, "%s/%s",
1591 (flags & SSHCONF_USERCONF) ?
1592 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1593 } else
1594 arg2 = xstrdup(arg);
1595 memset(&gl, 0, sizeof(gl));
1596 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1597 if (r == GLOB_NOMATCH) {
1598 debug("%.200s line %d: include %s matched no "
1599 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001600 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001601 continue;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001602 } else if (r != 0)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001603 fatal("%.200s line %d: glob failed for %s.",
1604 filename, linenum, arg2);
1605 free(arg2);
1606 oactive = *activep;
deraadt@openbsd.org40598b82019-11-13 05:42:26 +00001607 for (i = 0; i < gl.gl_pathc; i++) {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001608 debug3("%.200s line %d: Including file %s "
1609 "depth %d%s", filename, linenum,
1610 gl.gl_pathv[i], depth,
1611 oactive ? "" : " (parse only)");
1612 r = read_config_file_depth(gl.gl_pathv[i],
1613 pw, host, original_host, options,
1614 flags | SSHCONF_CHECKPERM |
1615 (oactive ? 0 : SSHCONF_NEVERMATCH),
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001616 activep, want_final_pass, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001617 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001618 fatal("Can't open user config file "
1619 "%.100s: %.100s", gl.gl_pathv[i],
1620 strerror(errno));
1621 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001622 /*
1623 * don't let Match in includes clobber the
1624 * containing file's Match state.
1625 */
1626 *activep = oactive;
1627 if (r != 1)
1628 value = -1;
1629 }
1630 globfree(&gl);
1631 }
1632 if (value != 0)
1633 return value;
1634 break;
1635
Damien Miller0dac6fb2010-11-20 15:19:38 +11001636 case oIPQoS:
1637 arg = strdelim(&s);
1638 if ((value = parse_ipqos(arg)) == -1)
1639 fatal("%s line %d: Bad IPQoS value: %s",
1640 filename, linenum, arg);
1641 arg = strdelim(&s);
1642 if (arg == NULL)
1643 value2 = value;
1644 else if ((value2 = parse_ipqos(arg)) == -1)
1645 fatal("%s line %d: Bad IPQoS value: %s",
1646 filename, linenum, arg);
1647 if (*activep) {
1648 options->ip_qos_interactive = value;
1649 options->ip_qos_bulk = value2;
1650 }
1651 break;
1652
Damien Miller21771e22011-05-15 08:45:50 +10001653 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001654 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001655 multistate_ptr = multistate_requesttty;
1656 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001657
Darren Tucker07636982013-05-16 20:30:03 +10001658 case oIgnoreUnknown:
1659 charptr = &options->ignored_unknown;
1660 goto parse_string;
1661
Damien Miller1262b662013-08-21 02:44:24 +10001662 case oProxyUseFdpass:
1663 intptr = &options->proxy_use_fdpass;
1664 goto parse_flag;
1665
Damien Miller0faf7472013-10-17 11:47:23 +11001666 case oCanonicalDomains:
1667 value = options->num_canonical_domains != 0;
1668 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001669 if (!valid_domain(arg, 1, &errstr)) {
1670 fatal("%s line %d: %s", filename, linenum,
1671 errstr);
1672 }
Damien Miller0faf7472013-10-17 11:47:23 +11001673 if (!*activep || value)
1674 continue;
1675 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1676 fatal("%s line %d: too many hostname suffixes.",
1677 filename, linenum);
1678 options->canonical_domains[
1679 options->num_canonical_domains++] = xstrdup(arg);
1680 }
1681 break;
1682
Damien Miller38505592013-10-17 11:48:13 +11001683 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001684 value = options->num_permitted_cnames != 0;
1685 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1686 /* Either '*' for everything or 'list:list' */
1687 if (strcmp(arg, "*") == 0)
1688 arg2 = arg;
1689 else {
1690 lowercase(arg);
1691 if ((arg2 = strchr(arg, ':')) == NULL ||
1692 arg2[1] == '\0') {
1693 fatal("%s line %d: "
1694 "Invalid permitted CNAME \"%s\"",
1695 filename, linenum, arg);
1696 }
1697 *arg2 = '\0';
1698 arg2++;
1699 }
1700 if (!*activep || value)
1701 continue;
1702 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1703 fatal("%s line %d: too many permitted CNAMEs.",
1704 filename, linenum);
1705 cname = options->permitted_cnames +
1706 options->num_permitted_cnames++;
1707 cname->source_list = xstrdup(arg);
1708 cname->target_list = xstrdup(arg2);
1709 }
1710 break;
1711
Damien Miller38505592013-10-17 11:48:13 +11001712 case oCanonicalizeHostname:
1713 intptr = &options->canonicalize_hostname;
1714 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001715 goto parse_multistate;
1716
Damien Miller38505592013-10-17 11:48:13 +11001717 case oCanonicalizeMaxDots:
1718 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001719 goto parse_int;
1720
Damien Miller38505592013-10-17 11:48:13 +11001721 case oCanonicalizeFallbackLocal:
1722 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001723 goto parse_flag;
1724
Damien Miller7acefbb2014-07-18 14:11:24 +10001725 case oStreamLocalBindMask:
1726 arg = strdelim(&s);
1727 if (!arg || *arg == '\0')
1728 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1729 /* Parse mode in octal format */
1730 value = strtol(arg, &endofnumber, 8);
1731 if (arg == endofnumber || value < 0 || value > 0777)
1732 fatal("%.200s line %d: Bad mask.", filename, linenum);
1733 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1734 break;
1735
1736 case oStreamLocalBindUnlink:
1737 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1738 goto parse_flag;
1739
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001740 case oRevokedHostKeys:
1741 charptr = &options->revoked_host_keys;
1742 goto parse_string;
1743
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001744 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001745 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001746 arg = strdelim(&s);
1747 if (!arg || *arg == '\0')
1748 fatal("%.200s line %d: Missing argument.",
1749 filename, linenum);
1750 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1751 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1752 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001753 if (*activep && *intptr == -1)
1754 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001755 break;
1756
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001757 case oUpdateHostkeys:
1758 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001759 multistate_ptr = multistate_yesnoask;
1760 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001761
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001762 case oHostbasedKeyTypes:
1763 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001764 goto parse_keytypes;
1765
1766 case oPubkeyAcceptedKeyTypes:
1767 charptr = &options->pubkey_key_types;
1768 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001769
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001770 case oAddKeysToAgent:
1771 intptr = &options->add_keys_to_agent;
1772 multistate_ptr = multistate_yesnoaskconfirm;
1773 goto parse_multistate;
1774
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001775 case oIdentityAgent:
1776 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001777 arg = strdelim(&s);
1778 if (!arg || *arg == '\0')
1779 fatal("%.200s line %d: Missing argument.",
1780 filename, linenum);
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001781 parse_agent_path:
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001782 /* Extra validation if the string represents an env var. */
1783 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1784 fatal("%.200s line %d: Invalid environment name %s.",
1785 filename, linenum, arg);
1786 }
1787 if (*activep && *charptr == NULL)
1788 *charptr = xstrdup(arg);
1789 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001790
Ben Lindstrom4daea862002-06-09 20:04:02 +00001791 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001792 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001793 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001794 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001795
Damien Millerf9b3feb2003-05-16 11:38:32 +10001796 case oUnsupported:
1797 error("%s line %d: Unsupported option \"%s\"",
1798 filename, linenum, keyword);
1799 return 0;
1800
Damien Miller95def091999-11-25 00:26:21 +11001801 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001802 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001803 }
1804
1805 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001806 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001807 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001808 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001809 }
Damien Miller95def091999-11-25 00:26:21 +11001810 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811}
1812
Damien Miller5428f641999-11-25 11:54:57 +11001813/*
1814 * Reads the config file and modifies the options accordingly. Options
1815 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001816 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001817 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001818int
Damien Miller194fd902013-10-15 12:13:05 +11001819read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001820 const char *original_host, Options *options, int flags,
1821 int *want_final_pass)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001823 int active = 1;
1824
1825 return read_config_file_depth(filename, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001826 options, flags, &active, want_final_pass, 0);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001827}
1828
1829#define READCONF_MAX_DEPTH 16
1830static int
1831read_config_file_depth(const char *filename, struct passwd *pw,
1832 const char *host, const char *original_host, Options *options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001833 int flags, int *activep, int *want_final_pass, int depth)
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001834{
Damien Miller95def091999-11-25 00:26:21 +11001835 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001836 char *line = NULL;
1837 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001838 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001839 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001841 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1842 fatal("Too many recursive configuration includes");
1843
Damien Miller57a44762004-04-20 20:11:57 +10001844 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001845 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846
Darren Tuckeraefa3682013-04-05 11:18:35 +11001847 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001848 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001849
Damien Miller33793852004-06-15 10:27:55 +10001850 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001851 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001852 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001853 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001854 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001855 }
1856
Damien Miller95def091999-11-25 00:26:21 +11001857 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001858
Damien Miller5428f641999-11-25 11:54:57 +11001859 /*
1860 * Mark that we are now processing the options. This flag is turned
1861 * on/off by Host specifications.
1862 */
Damien Miller95def091999-11-25 00:26:21 +11001863 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001864 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001865 /* Update line number counter. */
1866 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001867 if (process_config_line_depth(options, pw, host, original_host,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001868 line, filename, linenum, activep, flags, want_final_pass,
1869 depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001870 bad_options++;
1871 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001872 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001873 fclose(f);
1874 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001875 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001876 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001877 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878}
1879
Damien Miller13f97b22014-02-24 15:57:55 +11001880/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1881int
1882option_clear_or_none(const char *o)
1883{
1884 return o == NULL || strcasecmp(o, "none") == 0;
1885}
1886
Damien Miller5428f641999-11-25 11:54:57 +11001887/*
1888 * Initializes options to special values that indicate that they have not yet
1889 * been set. Read_config_file will only set options with this value. Options
1890 * are processed in the following order: command line, user config file,
1891 * system config file. Last, fill_default_options is called.
1892 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893
Damien Miller4af51302000-04-16 11:18:38 +10001894void
Damien Miller95def091999-11-25 00:26:21 +11001895initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001896{
Damien Miller95def091999-11-25 00:26:21 +11001897 memset(options, 'X', sizeof(*options));
1898 options->forward_agent = -1;
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001899 options->forward_agent_sock_path = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001900 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001901 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001902 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001903 options->stdio_forward_host = NULL;
1904 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001905 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001906 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001907 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001908 options->fwd_opts.gateway_ports = -1;
1909 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1910 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001911 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001912 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001913 options->gss_authentication = -1;
1914 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001915 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001916 options->kbd_interactive_authentication = -1;
1917 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001918 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001919 options->batch_mode = -1;
1920 options->check_host_ip = -1;
1921 options->strict_host_key_checking = -1;
1922 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001923 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001924 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001925 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001926 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001927 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001928 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001929 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001930 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001931 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001932 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001933 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001934 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001935 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001936 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001937 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001938 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001939 options->jump_user = NULL;
1940 options->jump_host = NULL;
1941 options->jump_port = -1;
1942 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001943 options->user = NULL;
1944 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001945 options->num_system_hostfiles = 0;
1946 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001947 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001948 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001949 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001950 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001951 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001952 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001953 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001954 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001955 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001956 options->pkcs11_provider = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001957 options->sk_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001958 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001959 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001960 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001961 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001962 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001963 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001964 options->server_alive_interval = -1;
1965 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001966 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001967 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001968 options->setenv = NULL;
1969 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001970 options->control_path = NULL;
1971 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001972 options->control_persist = -1;
1973 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001974 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001975 options->tun_open = -1;
1976 options->tun_local = -1;
1977 options->tun_remote = -1;
1978 options->local_command = NULL;
1979 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001980 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001981 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001982 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001983 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001984 options->ip_qos_interactive = -1;
1985 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001986 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001987 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001988 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001989 options->num_canonical_domains = 0;
1990 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001991 options->canonicalize_max_dots = -1;
1992 options->canonicalize_fallback_local = -1;
1993 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001994 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001995 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001996 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001997 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001998 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999}
2000
Damien Miller5428f641999-11-25 11:54:57 +11002001/*
Damien Miller13f97b22014-02-24 15:57:55 +11002002 * A petite version of fill_default_options() that just fills the options
2003 * needed for hostname canonicalization to proceed.
2004 */
2005void
2006fill_default_options_for_canonicalization(Options *options)
2007{
2008 if (options->canonicalize_max_dots == -1)
2009 options->canonicalize_max_dots = 1;
2010 if (options->canonicalize_fallback_local == -1)
2011 options->canonicalize_fallback_local = 1;
2012 if (options->canonicalize_hostname == -1)
2013 options->canonicalize_hostname = SSH_CANONICALISE_NO;
2014}
2015
2016/*
Damien Miller5428f641999-11-25 11:54:57 +11002017 * Called after processing other sources of option data, this fills those
2018 * options for which no value has been specified with their default values.
2019 */
Damien Miller4af51302000-04-16 11:18:38 +10002020void
Damien Miller95def091999-11-25 00:26:21 +11002021fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002022{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002023 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002024 char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002025 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002026
Damien Miller95def091999-11-25 00:26:21 +11002027 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10002028 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002029 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11002030 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10002031 if (options->forward_x11_trusted == -1)
2032 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10002033 if (options->forward_x11_timeout == -1)
2034 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002035 /*
2036 * stdio forwarding (-W) changes the default for these but we defer
2037 * setting the values so they can be overridden.
2038 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002039 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002040 options->exit_on_forward_failure =
2041 options->stdio_forward_host != NULL ? 1 : 0;
2042 if (options->clear_forwardings == -1)
2043 options->clear_forwardings =
2044 options->stdio_forward_host != NULL ? 1 : 0;
2045 if (options->clear_forwardings == 1)
2046 clear_forwardings(options);
2047
Damien Millerd3a18572000-06-07 19:55:44 +10002048 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00002049 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10002050 if (options->fwd_opts.gateway_ports == -1)
2051 options->fwd_opts.gateway_ports = 0;
2052 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2053 options->fwd_opts.streamlocal_bind_mask = 0177;
2054 if (options->fwd_opts.streamlocal_bind_unlink == -1)
2055 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002056 if (options->pubkey_authentication == -1)
2057 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00002058 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00002059 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10002060 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10002061 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10002062 if (options->gss_deleg_creds == -1)
2063 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11002064 if (options->password_authentication == -1)
2065 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11002066 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00002067 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002068 if (options->hostbased_authentication == -1)
2069 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11002070 if (options->batch_mode == -1)
2071 options->batch_mode = 0;
2072 if (options->check_host_ip == -1)
2073 options->check_host_ip = 1;
2074 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00002075 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11002076 if (options->compression == -1)
2077 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11002078 if (options->tcp_keep_alive == -1)
2079 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002080 if (options->port == -1)
2081 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002082 if (options->address_family == -1)
2083 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002084 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002085 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002086 if (options->number_of_password_prompts == -1)
2087 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002088 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002089 if (options->add_keys_to_agent == -1)
2090 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002091 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002092 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2093 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002094#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002095 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002096 add_identity_file(options, "~/",
2097 _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002098#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002099 add_identity_file(options, "~/",
2100 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org2c557442019-11-12 19:33:08 +00002101 add_identity_file(options, "~/",
2102 _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002103 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002104 }
Damien Miller95def091999-11-25 00:26:21 +11002105 if (options->escape_char == -1)
2106 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002107 if (options->num_system_hostfiles == 0) {
2108 options->system_hostfiles[options->num_system_hostfiles++] =
2109 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2110 options->system_hostfiles[options->num_system_hostfiles++] =
2111 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2112 }
2113 if (options->num_user_hostfiles == 0) {
2114 options->user_hostfiles[options->num_user_hostfiles++] =
2115 xstrdup(_PATH_SSH_USER_HOSTFILE);
2116 options->user_hostfiles[options->num_user_hostfiles++] =
2117 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2118 }
Damien Millerfcd93202002-02-05 12:26:34 +11002119 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002120 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002121 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2122 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002123 if (options->no_host_authentication_for_localhost == - 1)
2124 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002125 if (options->identities_only == -1)
2126 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002127 if (options->enable_ssh_keysign == -1)
2128 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002129 if (options->rekey_limit == -1)
2130 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002131 if (options->rekey_interval == -1)
2132 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002133 if (options->verify_host_key_dns == -1)
2134 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002135 if (options->server_alive_interval == -1)
2136 options->server_alive_interval = 0;
2137 if (options->server_alive_count_max == -1)
2138 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002139 if (options->control_master == -1)
2140 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002141 if (options->control_persist == -1) {
2142 options->control_persist = 0;
2143 options->control_persist_timeout = 0;
2144 }
Damien Millere1776152005-03-01 21:47:37 +11002145 if (options->hash_known_hosts == -1)
2146 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002147 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002148 options->tun_open = SSH_TUNMODE_NO;
2149 if (options->tun_local == -1)
2150 options->tun_local = SSH_TUNID_ANY;
2151 if (options->tun_remote == -1)
2152 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002153 if (options->permit_local_command == -1)
2154 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002155 if (options->visual_host_key == -1)
2156 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002157 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002158 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002159 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002160 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002161 if (options->request_tty == -1)
2162 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002163 if (options->proxy_use_fdpass == -1)
2164 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002165 if (options->canonicalize_max_dots == -1)
2166 options->canonicalize_max_dots = 1;
2167 if (options->canonicalize_fallback_local == -1)
2168 options->canonicalize_fallback_local = 1;
2169 if (options->canonicalize_hostname == -1)
2170 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002171 if (options->fingerprint_hash == -1)
2172 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002173 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002174 options->update_hostkeys = 0;
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002175#ifdef ENABLE_SK_INTERNAL
2176 if (options->sk_provider == NULL)
2177 options->sk_provider = xstrdup("internal");
2178#else
djm@openbsd.org884416b2019-10-31 21:18:28 +00002179 if (options->sk_provider == NULL)
2180 options->sk_provider = xstrdup("$SSH_SK_PROVIDER");
djm@openbsd.org6bff9522019-11-14 21:27:29 +00002181#endif
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002182
2183 /* Expand KEX name lists */
2184 all_cipher = cipher_alg_list(',', 0);
2185 all_mac = mac_alg_list(',');
2186 all_kex = kex_alg_list(',');
2187 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002188 all_sig = sshkey_alg_list(0, 1, 1, ',');
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002189 /* remove unsupported algos from default lists */
2190 def_cipher = match_filter_whitelist(KEX_CLIENT_ENCRYPT, all_cipher);
2191 def_mac = match_filter_whitelist(KEX_CLIENT_MAC, all_mac);
2192 def_kex = match_filter_whitelist(KEX_CLIENT_KEX, all_kex);
2193 def_key = match_filter_whitelist(KEX_DEFAULT_PK_ALG, all_key);
2194 def_sig = match_filter_whitelist(SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002195#define ASSEMBLE(what, defaults, all) \
2196 do { \
2197 if ((r = kex_assemble_names(&options->what, \
2198 defaults, all)) != 0) \
2199 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2200 } while (0)
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002201 ASSEMBLE(ciphers, def_cipher, all_cipher);
2202 ASSEMBLE(macs, def_mac, all_mac);
2203 ASSEMBLE(kex_algorithms, def_kex, all_kex);
2204 ASSEMBLE(hostbased_key_types, def_key, all_key);
2205 ASSEMBLE(pubkey_key_types, def_key, all_key);
2206 ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002207#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002208 free(all_cipher);
2209 free(all_mac);
2210 free(all_kex);
2211 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002212 free(all_sig);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002213 free(def_cipher);
2214 free(def_mac);
2215 free(def_kex);
2216 kex_default_pk_alg_filtered = def_key; /* save for later use */
2217 free(def_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002218
Damien Millere9fc72e2013-10-15 12:14:12 +11002219#define CLEAR_ON_NONE(v) \
2220 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002221 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002222 free(v); \
2223 v = NULL; \
2224 } \
2225 } while(0)
2226 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002227 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002228 CLEAR_ON_NONE(options->proxy_command);
2229 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002230 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org95a80582019-03-01 02:08:50 +00002231 CLEAR_ON_NONE(options->pkcs11_provider);
djm@openbsd.org884416b2019-10-31 21:18:28 +00002232 CLEAR_ON_NONE(options->sk_provider);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002233 if (options->jump_host != NULL &&
2234 strcmp(options->jump_host, "none") == 0 &&
2235 options->jump_port == 0 && options->jump_user == NULL) {
2236 free(options->jump_host);
2237 options->jump_host = NULL;
2238 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002239 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002240 /* options->user will be set in the main program if appropriate */
2241 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002242 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002243 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002244}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002245
Damien Miller7acefbb2014-07-18 14:11:24 +10002246struct fwdarg {
2247 char *arg;
2248 int ispath;
2249};
2250
2251/*
2252 * parse_fwd_field
2253 * parses the next field in a port forwarding specification.
2254 * sets fwd to the parsed field and advances p past the colon
2255 * or sets it to NULL at end of string.
2256 * returns 0 on success, else non-zero.
2257 */
2258static int
2259parse_fwd_field(char **p, struct fwdarg *fwd)
2260{
2261 char *ep, *cp = *p;
2262 int ispath = 0;
2263
2264 if (*cp == '\0') {
2265 *p = NULL;
2266 return -1; /* end of string */
2267 }
2268
2269 /*
2270 * A field escaped with square brackets is used literally.
2271 * XXX - allow ']' to be escaped via backslash?
2272 */
2273 if (*cp == '[') {
2274 /* find matching ']' */
2275 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2276 if (*ep == '/')
2277 ispath = 1;
2278 }
2279 /* no matching ']' or not at end of field. */
2280 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2281 return -1;
2282 /* NUL terminate the field and advance p past the colon */
2283 *ep++ = '\0';
2284 if (*ep != '\0')
2285 *ep++ = '\0';
2286 fwd->arg = cp + 1;
2287 fwd->ispath = ispath;
2288 *p = ep;
2289 return 0;
2290 }
2291
2292 for (cp = *p; *cp != '\0'; cp++) {
2293 switch (*cp) {
2294 case '\\':
2295 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002296 if (*cp == '\0')
2297 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002298 break;
2299 case '/':
2300 ispath = 1;
2301 break;
2302 case ':':
2303 *cp++ = '\0';
2304 goto done;
2305 }
2306 }
2307done:
2308 fwd->arg = *p;
2309 fwd->ispath = ispath;
2310 *p = cp;
2311 return 0;
2312}
2313
Damien Millerf91ee4c2005-03-01 21:24:33 +11002314/*
2315 * parse_forward
2316 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002317 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002318 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2319 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002320 * dynamicfwd == 1
2321 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002322 * returns number of arguments parsed or zero on error
2323 */
2324int
Damien Miller7acefbb2014-07-18 14:11:24 +10002325parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002326{
Damien Miller7acefbb2014-07-18 14:11:24 +10002327 struct fwdarg fwdargs[4];
2328 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002329 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002330
Damien Miller7acefbb2014-07-18 14:11:24 +10002331 memset(fwd, 0, sizeof(*fwd));
2332 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002333
2334 cp = p = xstrdup(fwdspec);
2335
2336 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002337 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002338 cp++;
2339
Damien Miller7acefbb2014-07-18 14:11:24 +10002340 for (i = 0; i < 4; ++i) {
2341 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002342 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002343 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344
Damien Millerf4b39532008-11-03 19:28:21 +11002345 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002346 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002347 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002348 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002349
2350 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002351 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002352 if (fwdargs[0].ispath) {
2353 fwd->listen_path = xstrdup(fwdargs[0].arg);
2354 fwd->listen_port = PORT_STREAMLOCAL;
2355 } else {
2356 fwd->listen_host = NULL;
2357 fwd->listen_port = a2port(fwdargs[0].arg);
2358 }
Damien Millera699d952008-11-03 19:27:34 +11002359 fwd->connect_host = xstrdup("socks");
2360 break;
2361
2362 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002363 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2364 fwd->listen_path = xstrdup(fwdargs[0].arg);
2365 fwd->listen_port = PORT_STREAMLOCAL;
2366 fwd->connect_path = xstrdup(fwdargs[1].arg);
2367 fwd->connect_port = PORT_STREAMLOCAL;
2368 } else if (fwdargs[1].ispath) {
2369 fwd->listen_host = NULL;
2370 fwd->listen_port = a2port(fwdargs[0].arg);
2371 fwd->connect_path = xstrdup(fwdargs[1].arg);
2372 fwd->connect_port = PORT_STREAMLOCAL;
2373 } else {
2374 fwd->listen_host = xstrdup(fwdargs[0].arg);
2375 fwd->listen_port = a2port(fwdargs[1].arg);
2376 fwd->connect_host = xstrdup("socks");
2377 }
Damien Millera699d952008-11-03 19:27:34 +11002378 break;
2379
Damien Millerf91ee4c2005-03-01 21:24:33 +11002380 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002381 if (fwdargs[0].ispath) {
2382 fwd->listen_path = xstrdup(fwdargs[0].arg);
2383 fwd->listen_port = PORT_STREAMLOCAL;
2384 fwd->connect_host = xstrdup(fwdargs[1].arg);
2385 fwd->connect_port = a2port(fwdargs[2].arg);
2386 } else if (fwdargs[2].ispath) {
2387 fwd->listen_host = xstrdup(fwdargs[0].arg);
2388 fwd->listen_port = a2port(fwdargs[1].arg);
2389 fwd->connect_path = xstrdup(fwdargs[2].arg);
2390 fwd->connect_port = PORT_STREAMLOCAL;
2391 } else {
2392 fwd->listen_host = NULL;
2393 fwd->listen_port = a2port(fwdargs[0].arg);
2394 fwd->connect_host = xstrdup(fwdargs[1].arg);
2395 fwd->connect_port = a2port(fwdargs[2].arg);
2396 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002397 break;
2398
2399 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002400 fwd->listen_host = xstrdup(fwdargs[0].arg);
2401 fwd->listen_port = a2port(fwdargs[1].arg);
2402 fwd->connect_host = xstrdup(fwdargs[2].arg);
2403 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002404 break;
2405 default:
2406 i = 0; /* failure */
2407 }
2408
Darren Tuckera627d422013-06-02 07:31:17 +10002409 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002410
Damien Millera699d952008-11-03 19:27:34 +11002411 if (dynamicfwd) {
2412 if (!(i == 1 || i == 2))
2413 goto fail_free;
2414 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002415 if (!(i == 3 || i == 4)) {
2416 if (fwd->connect_path == NULL &&
2417 fwd->listen_path == NULL)
2418 goto fail_free;
2419 }
2420 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002421 goto fail_free;
2422 }
2423
Damien Miller7acefbb2014-07-18 14:11:24 +10002424 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2425 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002426 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002427 if (fwd->connect_host != NULL &&
2428 strlen(fwd->connect_host) >= NI_MAXHOST)
2429 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002430 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2431 if (fwd->connect_path != NULL &&
2432 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2433 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002434 if (fwd->listen_host != NULL &&
2435 strlen(fwd->listen_host) >= NI_MAXHOST)
2436 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002437 if (fwd->listen_path != NULL &&
2438 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2439 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002440
2441 return (i);
2442
2443 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002444 free(fwd->connect_host);
2445 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002446 free(fwd->connect_path);
2447 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002448 free(fwd->listen_host);
2449 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002450 free(fwd->listen_path);
2451 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002452 return (0);
2453}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002454
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002455int
2456parse_jump(const char *s, Options *o, int active)
2457{
2458 char *orig, *sdup, *cp;
2459 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002460 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002461
2462 active &= o->proxy_command == NULL && o->jump_host == NULL;
2463
2464 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002465 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002466 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002467 if (strcasecmp(s, "none") == 0)
2468 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002469 if ((cp = strrchr(sdup, ',')) == NULL)
2470 cp = sdup; /* last */
2471 else
2472 *cp++ = '\0';
2473
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002474 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002475 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002476 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2477 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002478 goto out;
2479 } else {
2480 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002481 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2482 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002483 goto out;
2484 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002485 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002486 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002487 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002488 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002489 if (strcasecmp(s, "none") == 0) {
2490 o->jump_host = xstrdup("none");
2491 o->jump_port = 0;
2492 } else {
2493 o->jump_user = user;
2494 o->jump_host = host;
2495 o->jump_port = port;
2496 o->proxy_command = xstrdup("none");
2497 user = host = NULL;
2498 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2499 o->jump_extra = xstrdup(s);
2500 o->jump_extra[cp - s] = '\0';
2501 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002502 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002503 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002504 ret = 0;
2505 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002506 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002507 free(user);
2508 free(host);
2509 return ret;
2510}
2511
millert@openbsd.org887669e2017-10-21 23:06:24 +00002512int
2513parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2514{
2515 char *path;
2516 int r;
2517
2518 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2519 if (r == 0 && path != NULL)
2520 r = -1; /* path not allowed */
2521 return r;
2522}
2523
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002524/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2525static const char *
2526fmt_multistate_int(int val, const struct multistate *m)
2527{
2528 u_int i;
2529
2530 for (i = 0; m[i].key != NULL; i++) {
2531 if (m[i].value == val)
2532 return m[i].key;
2533 }
2534 return "UNKNOWN";
2535}
2536
2537static const char *
2538fmt_intarg(OpCodes code, int val)
2539{
2540 if (val == -1)
2541 return "unset";
2542 switch (code) {
2543 case oAddressFamily:
2544 return fmt_multistate_int(val, multistate_addressfamily);
2545 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002546 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002547 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002548 case oStrictHostKeyChecking:
2549 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002550 case oControlMaster:
2551 return fmt_multistate_int(val, multistate_controlmaster);
2552 case oTunnel:
2553 return fmt_multistate_int(val, multistate_tunnel);
2554 case oRequestTTY:
2555 return fmt_multistate_int(val, multistate_requesttty);
2556 case oCanonicalizeHostname:
2557 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002558 case oAddKeysToAgent:
2559 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002560 case oFingerprintHash:
2561 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002562 default:
2563 switch (val) {
2564 case 0:
2565 return "no";
2566 case 1:
2567 return "yes";
2568 default:
2569 return "UNKNOWN";
2570 }
2571 }
2572}
2573
2574static const char *
2575lookup_opcode_name(OpCodes code)
2576{
2577 u_int i;
2578
2579 for (i = 0; keywords[i].name != NULL; i++)
2580 if (keywords[i].opcode == code)
2581 return(keywords[i].name);
2582 return "UNKNOWN";
2583}
2584
2585static void
2586dump_cfg_int(OpCodes code, int val)
2587{
2588 printf("%s %d\n", lookup_opcode_name(code), val);
2589}
2590
2591static void
2592dump_cfg_fmtint(OpCodes code, int val)
2593{
2594 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2595}
2596
2597static void
2598dump_cfg_string(OpCodes code, const char *val)
2599{
2600 if (val == NULL)
2601 return;
2602 printf("%s %s\n", lookup_opcode_name(code), val);
2603}
2604
2605static void
2606dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2607{
2608 u_int i;
2609
2610 for (i = 0; i < count; i++)
2611 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2612}
2613
2614static void
2615dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2616{
2617 u_int i;
2618
2619 printf("%s", lookup_opcode_name(code));
2620 for (i = 0; i < count; i++)
2621 printf(" %s", vals[i]);
2622 printf("\n");
2623}
2624
2625static void
2626dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2627{
2628 const struct Forward *fwd;
2629 u_int i;
2630
2631 /* oDynamicForward */
2632 for (i = 0; i < count; i++) {
2633 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002634 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002635 strcmp(fwd->connect_host, "socks") != 0)
2636 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002637 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002638 strcmp(fwd->connect_host, "socks") == 0)
2639 continue;
2640 printf("%s", lookup_opcode_name(code));
2641 if (fwd->listen_port == PORT_STREAMLOCAL)
2642 printf(" %s", fwd->listen_path);
2643 else if (fwd->listen_host == NULL)
2644 printf(" %d", fwd->listen_port);
2645 else {
2646 printf(" [%s]:%d",
2647 fwd->listen_host, fwd->listen_port);
2648 }
2649 if (code != oDynamicForward) {
2650 if (fwd->connect_port == PORT_STREAMLOCAL)
2651 printf(" %s", fwd->connect_path);
2652 else if (fwd->connect_host == NULL)
2653 printf(" %d", fwd->connect_port);
2654 else {
2655 printf(" [%s]:%d",
2656 fwd->connect_host, fwd->connect_port);
2657 }
2658 }
2659 printf("\n");
2660 }
2661}
2662
2663void
2664dump_client_config(Options *o, const char *host)
2665{
2666 int i;
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002667 char buf[8];
djm@openbsd.org60a92472015-08-21 23:53:08 +00002668
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002669 /* Most interesting options first: user, host, port */
2670 dump_cfg_string(oUser, o->user);
jmc@openbsd.org73491492019-06-12 11:31:50 +00002671 dump_cfg_string(oHostname, host);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002672 dump_cfg_int(oPort, o->port);
2673
2674 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002675 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002676 dump_cfg_fmtint(oAddressFamily, o->address_family);
2677 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2678 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2679 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2680 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2681 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2682 dump_cfg_fmtint(oCompression, o->compression);
2683 dump_cfg_fmtint(oControlMaster, o->control_master);
2684 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002685 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002686 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002687 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002688 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2689 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2690 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2691#ifdef GSSAPI
2692 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2693 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2694#endif /* GSSAPI */
2695 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2696 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2697 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2698 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2699 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2700 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2701 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002702 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2703 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2704 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002705 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2706 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2707 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2708 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002709 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2710 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002711 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002712
2713 /* Integer options */
2714 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002715 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2716 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2717 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2718 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2719 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2720
2721 /* String options */
2722 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002723 dump_cfg_string(oBindInterface, o->bind_interface);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002724 dump_cfg_string(oCiphers, o->ciphers);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002725 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002726 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002727 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002728 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002729 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002730 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002731 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002732 dump_cfg_string(oKexAlgorithms, o->kex_algorithms);
2733 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002734 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002735 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002736 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002737 dump_cfg_string(oMacs, o->macs);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002738#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002739 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002740#endif
djm@openbsd.org884416b2019-10-31 21:18:28 +00002741 dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002742 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002743 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002744 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002745 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002746
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002747 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002748 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2749 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2750 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2751
2752 /* String array options */
2753 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2754 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002755 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002756 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2757 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2758 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002759 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002760
2761 /* Special cases */
2762
djm@openbsd.org40be78f2019-12-21 02:19:13 +00002763 /* oForwardAgent */
2764 if (o->forward_agent_sock_path == NULL)
2765 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2766 else
2767 dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
2768
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002769 /* oConnectTimeout */
2770 if (o->connection_timeout == -1)
2771 printf("connecttimeout none\n");
2772 else
2773 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2774
2775 /* oTunnelDevice */
2776 printf("tunneldevice");
2777 if (o->tun_local == SSH_TUNID_ANY)
2778 printf(" any");
2779 else
2780 printf(" %d", o->tun_local);
2781 if (o->tun_remote == SSH_TUNID_ANY)
2782 printf(":any");
2783 else
2784 printf(":%d", o->tun_remote);
2785 printf("\n");
2786
2787 /* oCanonicalizePermittedCNAMEs */
2788 if ( o->num_permitted_cnames > 0) {
2789 printf("canonicalizePermittedcnames");
2790 for (i = 0; i < o->num_permitted_cnames; i++) {
2791 printf(" %s:%s", o->permitted_cnames[i].source_list,
2792 o->permitted_cnames[i].target_list);
2793 }
2794 printf("\n");
2795 }
2796
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002797 /* oControlPersist */
2798 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2799 dump_cfg_fmtint(oControlPersist, o->control_persist);
2800 else
2801 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2802
2803 /* oEscapeChar */
2804 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2805 printf("escapechar none\n");
2806 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002807 vis(buf, o->escape_char, VIS_WHITE, 0);
2808 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002809 }
2810
2811 /* oIPQoS */
2812 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2813 printf("%s\n", iptos2str(o->ip_qos_bulk));
2814
2815 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002816 printf("rekeylimit %llu %d\n",
2817 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002818
2819 /* oStreamLocalBindMask */
2820 printf("streamlocalbindmask 0%o\n",
2821 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002822
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002823 /* oLogFacility */
2824 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2825
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002826 /* oProxyCommand / oProxyJump */
2827 if (o->jump_host == NULL)
2828 dump_cfg_string(oProxyCommand, o->proxy_command);
2829 else {
2830 /* Check for numeric addresses */
2831 i = strchr(o->jump_host, ':') != NULL ||
2832 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2833 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2834 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002835 /* optional additional jump spec */
2836 o->jump_extra == NULL ? "" : o->jump_extra,
2837 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002838 /* optional user */
2839 o->jump_user == NULL ? "" : o->jump_user,
2840 o->jump_user == NULL ? "" : "@",
2841 /* opening [ if hostname is numeric */
2842 i ? "[" : "",
2843 /* mandatory hostname */
2844 o->jump_host,
2845 /* closing ] if hostname is numeric */
2846 i ? "]" : "",
2847 /* optional port number */
2848 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002849 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002850 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002851}