blob: d39cfa3c5bc9bd7f3648372a5acca2ef704ea9a9 [file] [log] [blame]
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001/* $OpenBSD: readconf.c,v 1.299 2018/10/03 06:38:35 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +000056#include "ssherr.h"
Damien Miller78928792000-04-12 20:17:38 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
59#include "pathnames.h"
60#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000061#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100062#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "readconf.h"
64#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "kex.h"
66#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110067#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000068#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000069#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Format of the configuration file:
72
73 # Configuration data is parsed as follows:
74 # 1. command line options
75 # 2. user-specific file
76 # 3. system-wide file
77 # Any configuration value is only changed the first time it is set.
78 # Thus, host-specific definitions should be at the beginning of the
79 # configuration file, and defaults at the end.
80
81 # Host-specific declarations. These may override anything above. A single
82 # host may match multiple declarations; these are processed in the order
83 # that they are given in.
84
85 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host fake.com
89 HostName another.host.name.real.org
90 User blaah
91 Port 34289
92 ForwardX11 no
93 ForwardAgent no
94
95 Host books.com
96 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000097 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host fascist.blob.com
100 Port 23123
101 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 PasswordAuthentication no
103
104 Host puukko.hut.fi
105 User t35124p
106 ProxyCommand ssh-proxy %h %p
107
108 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000112 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 PasswordAuthentication no
114
Damien Millerd27b9472005-12-13 19:29:02 +1100115 Host vpn.fake.com
116 Tunnel yes
117 TunnelDevice 3
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 # Defaults for various options
120 Host *
121 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 PasswordAuthentication yes
124 RSAAuthentication yes
125 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100127 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 IdentityFile ~/.ssh/identity
129 Port 22
130 EscapeChar ~
131
132*/
133
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000134static int read_config_file_depth(const char *filename, struct passwd *pw,
135 const char *host, const char *original_host, Options *options,
136 int flags, int *activep, int depth);
137static int process_config_line_depth(Options *options, struct passwd *pw,
138 const char *host, const char *original_host, char *line,
139 const char *filename, int linenum, int *activep, int flags, int depth);
140
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141/* Keyword tokens. */
142
Damien Miller95def091999-11-25 00:26:21 +1100143typedef enum {
144 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000145 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000146 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
147 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000148 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000149 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100150 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000151 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100152 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100153 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
154 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100155 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000156 oUsePrivilegedPort, oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000157 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000158 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000159 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000160 oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000161 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000162 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000163 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100164 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000165 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
Damien Millere11e1ea2010-08-03 16:04:46 +1000166 oHashKnownHosts,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000167 oTunnel, oTunnelDevice,
168 oLocalCommand, oPermitLocalCommand, oRemoteCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000169 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000170 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100171 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
172 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000173 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000174 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orgecac7e12018-09-20 03:30:44 +0000175 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
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 },
200
201 /* Sometimes-unsupported options */
202#if defined(GSSAPI)
203 { "gssapiauthentication", oGssAuthentication },
204 { "gssapidelegatecredentials", oGssDelegateCreds },
205# else
206 { "gssapiauthentication", oUnsupported },
207 { "gssapidelegatecredentials", oUnsupported },
208#endif
209#ifdef ENABLE_PKCS11
210 { "smartcarddevice", oPKCS11Provider },
211 { "pkcs11provider", oPKCS11Provider },
212# else
213 { "smartcarddevice", oUnsupported },
214 { "pkcs11provider", oUnsupported },
215#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000216 { "rsaauthentication", oUnsupported },
217 { "rhostsrsaauthentication", oUnsupported },
218 { "compressionlevel", oUnsupported },
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 },
Damien Miller95def091999-11-25 00:26:21 +1100242 { "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 },
Damien Miller01ed2272008-11-05 16:20:46 +1100312
Ben Lindstrom65366a82001-12-06 16:32:47 +0000313 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100314};
315
Damien Miller5428f641999-11-25 11:54:57 +1100316/*
317 * Adds a local TCP/IP port forward to options. Never returns if there is an
318 * error.
319 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Miller4af51302000-04-16 11:18:38 +1000321void
Damien Miller7acefbb2014-07-18 14:11:24 +1000322add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323{
Damien Miller7acefbb2014-07-18 14:11:24 +1000324 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000325 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000326
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000327 /* Don't add duplicates */
328 for (i = 0; i < options->num_local_forwards; i++) {
329 if (forward_equals(newfwd, options->local_forwards + i))
330 return;
331 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000332 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000333 options->num_local_forwards + 1,
334 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100335 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336
Damien Miller1a0442f2008-11-05 16:30:06 +1100337 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000339 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100340 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
Damien Miller5428f641999-11-25 11:54:57 +1100345/*
346 * Adds a remote TCP/IP port forward to options. Never returns if there is
347 * an error.
348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller4af51302000-04-16 11:18:38 +1000350void
Damien Miller7acefbb2014-07-18 14:11:24 +1000351add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Miller7acefbb2014-07-18 14:11:24 +1000353 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000354 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000355
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000356 /* Don't add duplicates */
357 for (i = 0; i < options->num_remote_forwards; i++) {
358 if (forward_equals(newfwd, options->remote_forwards + i))
359 return;
360 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000361 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000362 options->num_remote_forwards + 1,
363 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100364 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365
Damien Miller1a0442f2008-11-05 16:30:06 +1100366 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000368 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100369 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000371 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100372 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000373 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374}
375
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000376static void
377clear_forwardings(Options *options)
378{
379 int i;
380
Damien Millerf91ee4c2005-03-01 21:24:33 +1100381 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000383 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000385 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100386 }
Damien Miller232cfb12010-06-26 09:50:30 +1000387 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000388 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000389 options->local_forwards = NULL;
390 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000391 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000394 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000396 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100397 }
Damien Miller232cfb12010-06-26 09:50:30 +1000398 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000399 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000400 options->remote_forwards = NULL;
401 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100403 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000404}
405
Darren Tucker19104782013-04-05 11:13:08 +1100406void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000407add_certificate_file(Options *options, const char *path, int userprovided)
408{
409 int i;
410
411 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
412 fatal("Too many certificate files specified (max %d)",
413 SSH_MAX_CERTIFICATE_FILES);
414
415 /* Avoid registering duplicates */
416 for (i = 0; i < options->num_certificate_files; i++) {
417 if (options->certificate_file_userprovided[i] == userprovided &&
418 strcmp(options->certificate_files[i], path) == 0) {
419 debug2("%s: ignoring duplicate key %s", __func__, path);
420 return;
421 }
422 }
423
424 options->certificate_file_userprovided[options->num_certificate_files] =
425 userprovided;
426 options->certificate_files[options->num_certificate_files++] =
427 xstrdup(path);
428}
429
430void
Darren Tucker19104782013-04-05 11:13:08 +1100431add_identity_file(Options *options, const char *dir, const char *filename,
432 int userprovided)
433{
434 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000435 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100436
437 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
438 fatal("Too many identity files specified (max %d)",
439 SSH_MAX_IDENTITY_FILES);
440
441 if (dir == NULL) /* no dir, filename is absolute */
442 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000443 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
444 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100445
Damien Miller15271902014-05-15 13:47:56 +1000446 /* Avoid registering duplicates */
447 for (i = 0; i < options->num_identity_files; i++) {
448 if (options->identity_file_userprovided[i] == userprovided &&
449 strcmp(options->identity_files[i], path) == 0) {
450 debug2("%s: ignoring duplicate key %s", __func__, path);
451 free(path);
452 return;
453 }
454 }
455
Darren Tucker19104782013-04-05 11:13:08 +1100456 options->identity_file_userprovided[options->num_identity_files] =
457 userprovided;
458 options->identity_files[options->num_identity_files++] = path;
459}
460
Damien Miller194fd902013-10-15 12:13:05 +1100461int
462default_ssh_port(void)
463{
464 static int port;
465 struct servent *sp;
466
467 if (port == 0) {
468 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
469 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
470 }
471 return port;
472}
473
474/*
475 * Execute a command in a shell.
476 * Return its exit status or -1 on abnormal exit.
477 */
478static int
479execute_in_shell(const char *cmd)
480{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000481 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100482 pid_t pid;
483 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100484
485 if ((shell = getenv("SHELL")) == NULL)
486 shell = _PATH_BSHELL;
487
Damien Miller194fd902013-10-15 12:13:05 +1100488 /* Need this to redirect subprocess stdin/out */
489 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
490 fatal("open(/dev/null): %s", strerror(errno));
491
492 debug("Executing command: '%.500s'", cmd);
493
494 /* Fork and execute the command. */
495 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000496 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100497
Damien Miller194fd902013-10-15 12:13:05 +1100498 /* Redirect child stdin and stdout. Leave stderr */
499 if (dup2(devnull, STDIN_FILENO) == -1)
500 fatal("dup2: %s", strerror(errno));
501 if (dup2(devnull, STDOUT_FILENO) == -1)
502 fatal("dup2: %s", strerror(errno));
503 if (devnull > STDERR_FILENO)
504 close(devnull);
505 closefrom(STDERR_FILENO + 1);
506
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000507 argv[0] = shell;
508 argv[1] = "-c";
509 argv[2] = xstrdup(cmd);
510 argv[3] = NULL;
511
Damien Miller194fd902013-10-15 12:13:05 +1100512 execv(argv[0], argv);
513 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
514 /* Die with signal to make this error apparent to parent. */
515 signal(SIGTERM, SIG_DFL);
516 kill(getpid(), SIGTERM);
517 _exit(1);
518 }
519 /* Parent. */
520 if (pid < 0)
521 fatal("%s: fork: %.100s", __func__, strerror(errno));
522
523 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100524
525 while (waitpid(pid, &status, 0) == -1) {
526 if (errno != EINTR && errno != EAGAIN)
527 fatal("%s: waitpid: %s", __func__, strerror(errno));
528 }
529 if (!WIFEXITED(status)) {
530 error("command '%.100s' exited abnormally", cmd);
531 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000532 }
Damien Miller194fd902013-10-15 12:13:05 +1100533 debug3("command returned status %d", WEXITSTATUS(status));
534 return WEXITSTATUS(status);
535}
536
537/*
538 * Parse and execute a Match directive.
539 */
540static int
541match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000542 const char *host_arg, const char *original_host, int post_canon,
543 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100544{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000545 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100546 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000547 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100548 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000549 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100550
551 /*
552 * Configuration is likely to be incomplete at this point so we
553 * must be prepared to use default values.
554 */
555 port = options->port <= 0 ? default_ssh_port() : options->port;
556 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000557 if (post_canon) {
558 host = xstrdup(options->hostname);
559 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100560 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100561 host = percent_expand(options->hostname,
562 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000563 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100564 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000565 }
Damien Miller194fd902013-10-15 12:13:05 +1100566
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000567 debug2("checking match for '%s' host %s originally %s",
568 cp, host, original_host);
569 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
570 criteria = NULL;
571 this_result = 1;
572 if ((negate = attrib[0] == '!'))
573 attrib++;
574 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100575 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000576 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100577 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000578 error("%.200s line %d: '%s' cannot be combined "
579 "with other Match attributes",
580 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100581 result = -1;
582 goto out;
583 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 if (result)
585 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100586 goto out;
587 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 attributes++;
589 if (strcasecmp(attrib, "canonical") == 0) {
590 r = !!post_canon; /* force bitmask member to boolean */
591 if (r == (negate ? 1 : 0))
592 this_result = result = 0;
593 debug3("%.200s line %d: %smatched '%s'",
594 filename, linenum,
595 this_result ? "" : "not ", oattrib);
596 continue;
597 }
598 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100599 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
600 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100601 result = -1;
602 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100603 }
Damien Miller194fd902013-10-15 12:13:05 +1100604 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000605 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000606 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000607 if (r == (negate ? 1 : 0))
608 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100609 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000611 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000612 if (r == (negate ? 1 : 0))
613 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100614 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000616 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000617 if (r == (negate ? 1 : 0))
618 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100619 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000621 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000622 if (r == (negate ? 1 : 0))
623 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100624 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100625 if (gethostname(thishost, sizeof(thishost)) == -1)
626 fatal("gethostname: %s", strerror(errno));
627 strlcpy(shorthost, thishost, sizeof(shorthost));
628 shorthost[strcspn(thishost, ".")] = '\0';
629 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000630 snprintf(uidstr, sizeof(uidstr), "%llu",
631 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100632
633 cmd = percent_expand(arg,
634 "L", shorthost,
635 "d", pw->pw_dir,
636 "h", host,
637 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000638 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100639 "p", portstr,
640 "r", ruser,
641 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000642 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100643 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100644 if (result != 1) {
645 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000646 debug3("%.200s line %d: skipped exec "
647 "\"%.100s\"", filename, linenum, cmd);
648 free(cmd);
649 continue;
Damien Miller06287802014-02-24 15:56:45 +1100650 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000651 r = execute_in_shell(cmd);
652 if (r == -1) {
653 fatal("%.200s line %d: match exec "
654 "'%.100s' error", filename,
655 linenum, cmd);
656 }
657 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100658 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000659 /* Force exit status to boolean */
660 r = r == 0;
661 if (r == (negate ? 1 : 0))
662 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100663 } else {
664 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100665 result = -1;
666 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100667 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000668 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
669 filename, linenum, this_result ? "": "not ",
670 oattrib, criteria);
671 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100672 }
Damien Millercf31f382013-10-24 21:02:56 +1100673 if (attributes == 0) {
674 error("One or more attributes required for Match");
675 result = -1;
676 goto out;
677 }
Damien Miller084bcd22013-10-23 16:30:51 +1100678 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000679 if (result != -1)
680 debug2("match %sfound", result ? "" : "not ");
681 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100682 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100683 return result;
684}
685
djm@openbsd.org555294a2018-04-06 13:02:39 +0000686/* Remove environment variable by pattern */
687static void
688rm_env(Options *options, const char *arg, const char *filename, int linenum)
689{
690 int i, j;
691 char *cp;
692
693 /* Remove an environment variable */
694 for (i = 0; i < options->num_send_env; ) {
695 cp = xstrdup(options->send_env[i]);
696 if (!match_pattern(cp, arg + 1)) {
697 free(cp);
698 i++;
699 continue;
700 }
701 debug3("%s line %d: removing environment %s",
702 filename, linenum, cp);
703 free(cp);
704 free(options->send_env[i]);
705 options->send_env[i] = NULL;
706 for (j = i; j < options->num_send_env - 1; j++) {
707 options->send_env[j] = options->send_env[j + 1];
708 options->send_env[j + 1] = NULL;
709 }
710 options->num_send_env--;
711 /* NB. don't increment i */
712 }
713}
714
Damien Miller5428f641999-11-25 11:54:57 +1100715/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000716 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100717 */
Damien Miller4af51302000-04-16 11:18:38 +1000718static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000719parse_token(const char *cp, const char *filename, int linenum,
720 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721{
Darren Tucker07636982013-05-16 20:30:03 +1000722 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723
Damien Miller95def091999-11-25 00:26:21 +1100724 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000725 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100726 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000727 if (ignored_unknown != NULL &&
728 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000729 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000730 error("%s: line %d: Bad configuration option: %s",
731 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100732 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733}
734
Damien Millere9fc72e2013-10-15 12:14:12 +1100735/* Multistate option parsing */
736struct multistate {
737 char *key;
738 int value;
739};
740static const struct multistate multistate_flag[] = {
741 { "true", 1 },
742 { "false", 0 },
743 { "yes", 1 },
744 { "no", 0 },
745 { NULL, -1 }
746};
747static const struct multistate multistate_yesnoask[] = {
748 { "true", 1 },
749 { "false", 0 },
750 { "yes", 1 },
751 { "no", 0 },
752 { "ask", 2 },
753 { NULL, -1 }
754};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000755static const struct multistate multistate_strict_hostkey[] = {
756 { "true", SSH_STRICT_HOSTKEY_YES },
757 { "false", SSH_STRICT_HOSTKEY_OFF },
758 { "yes", SSH_STRICT_HOSTKEY_YES },
759 { "no", SSH_STRICT_HOSTKEY_OFF },
760 { "ask", SSH_STRICT_HOSTKEY_ASK },
761 { "off", SSH_STRICT_HOSTKEY_OFF },
762 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
763 { NULL, -1 }
764};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000765static const struct multistate multistate_yesnoaskconfirm[] = {
766 { "true", 1 },
767 { "false", 0 },
768 { "yes", 1 },
769 { "no", 0 },
770 { "ask", 2 },
771 { "confirm", 3 },
772 { NULL, -1 }
773};
Damien Millere9fc72e2013-10-15 12:14:12 +1100774static const struct multistate multistate_addressfamily[] = {
775 { "inet", AF_INET },
776 { "inet6", AF_INET6 },
777 { "any", AF_UNSPEC },
778 { NULL, -1 }
779};
780static const struct multistate multistate_controlmaster[] = {
781 { "true", SSHCTL_MASTER_YES },
782 { "yes", SSHCTL_MASTER_YES },
783 { "false", SSHCTL_MASTER_NO },
784 { "no", SSHCTL_MASTER_NO },
785 { "auto", SSHCTL_MASTER_AUTO },
786 { "ask", SSHCTL_MASTER_ASK },
787 { "autoask", SSHCTL_MASTER_AUTO_ASK },
788 { NULL, -1 }
789};
790static const struct multistate multistate_tunnel[] = {
791 { "ethernet", SSH_TUNMODE_ETHERNET },
792 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
793 { "true", SSH_TUNMODE_DEFAULT },
794 { "yes", SSH_TUNMODE_DEFAULT },
795 { "false", SSH_TUNMODE_NO },
796 { "no", SSH_TUNMODE_NO },
797 { NULL, -1 }
798};
799static const struct multistate multistate_requesttty[] = {
800 { "true", REQUEST_TTY_YES },
801 { "yes", REQUEST_TTY_YES },
802 { "false", REQUEST_TTY_NO },
803 { "no", REQUEST_TTY_NO },
804 { "force", REQUEST_TTY_FORCE },
805 { "auto", REQUEST_TTY_AUTO },
806 { NULL, -1 }
807};
Damien Miller38505592013-10-17 11:48:13 +1100808static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100809 { "true", SSH_CANONICALISE_YES },
810 { "false", SSH_CANONICALISE_NO },
811 { "yes", SSH_CANONICALISE_YES },
812 { "no", SSH_CANONICALISE_NO },
813 { "always", SSH_CANONICALISE_ALWAYS },
814 { NULL, -1 }
815};
Damien Millere9fc72e2013-10-15 12:14:12 +1100816
Damien Miller5428f641999-11-25 11:54:57 +1100817/*
818 * Processes a single option line as used in the configuration files. This
819 * only sets those values that have not already been set.
820 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100821int
Damien Miller194fd902013-10-15 12:13:05 +1100822process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000823 const char *original_host, char *line, const char *filename,
824 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000826 return process_config_line_depth(options, pw, host, original_host,
827 line, filename, linenum, activep, flags, 0);
828}
829
830#define WHITESPACE " \t\r\n"
831static int
832process_config_line_depth(Options *options, struct passwd *pw, const char *host,
833 const char *original_host, char *line, const char *filename,
834 int linenum, int *activep, int flags, int depth)
835{
Damien Miller295ee632011-05-29 21:42:31 +1000836 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
837 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000838 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000839 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000840 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100841 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000842 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000843 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100844 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000845 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100846 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100847 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000848 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000849 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850
Damien Miller194fd902013-10-15 12:13:05 +1100851 if (activep == NULL) { /* We are processing a command line directive */
852 cmdline = 1;
853 activep = &cmdline;
854 }
855
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000856 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000857 if ((len = strlen(line)) == 0)
858 return 0;
859 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000860 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000861 break;
862 line[len] = '\0';
863 }
864
Damien Millerbe484b52000-07-15 14:14:16 +1000865 s = line;
866 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100867 if ((keyword = strdelim(&s)) == NULL)
868 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000869 /* Ignore leading whitespace. */
870 if (*keyword == '\0')
871 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000872 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100873 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000874 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100875 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876
Darren Tucker07636982013-05-16 20:30:03 +1000877 opcode = parse_token(keyword, filename, linenum,
878 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 switch (opcode) {
881 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100882 /* don't panic, but count bad options */
883 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000884 case oIgnore:
885 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000886 case oIgnoredUnknownOption:
887 debug("%s line %d: Ignored unknown option \"%s\"",
888 filename, linenum, keyword);
889 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000890 case oConnectTimeout:
891 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100892parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000893 arg = strdelim(&s);
894 if (!arg || *arg == '\0')
895 fatal("%s line %d: missing time value.",
896 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000897 if (strcmp(arg, "none") == 0)
898 value = -1;
899 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000900 fatal("%s line %d: invalid time value.",
901 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100902 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000903 *intptr = value;
904 break;
905
Damien Miller95def091999-11-25 00:26:21 +1100906 case oForwardAgent:
907 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100908 parse_flag:
909 multistate_ptr = multistate_flag;
910 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000911 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000912 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100913 fatal("%s line %d: missing argument.",
914 filename, linenum);
915 value = -1;
916 for (i = 0; multistate_ptr[i].key != NULL; i++) {
917 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
918 value = multistate_ptr[i].value;
919 break;
920 }
921 }
922 if (value == -1)
923 fatal("%s line %d: unsupported option \"%s\".",
924 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100925 if (*activep && *intptr == -1)
926 *intptr = value;
927 break;
928
929 case oForwardX11:
930 intptr = &options->forward_x11;
931 goto parse_flag;
932
Darren Tucker0a118da2003-10-15 15:54:32 +1000933 case oForwardX11Trusted:
934 intptr = &options->forward_x11_trusted;
935 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000936
Damien Miller1ab6a512010-06-26 10:02:24 +1000937 case oForwardX11Timeout:
938 intptr = &options->forward_x11_timeout;
939 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000940
Damien Miller95def091999-11-25 00:26:21 +1100941 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000942 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100943 goto parse_flag;
944
Darren Tuckere7d4b192006-07-12 22:17:10 +1000945 case oExitOnForwardFailure:
946 intptr = &options->exit_on_forward_failure;
947 goto parse_flag;
948
Damien Miller95def091999-11-25 00:26:21 +1100949 case oPasswordAuthentication:
950 intptr = &options->password_authentication;
951 goto parse_flag;
952
Damien Miller874d77b2000-10-14 16:23:11 +1100953 case oKbdInteractiveAuthentication:
954 intptr = &options->kbd_interactive_authentication;
955 goto parse_flag;
956
957 case oKbdInteractiveDevices:
958 charptr = &options->kbd_interactive_devices;
959 goto parse_string;
960
Damien Miller0bc1bd82000-11-13 22:57:25 +1100961 case oPubkeyAuthentication:
962 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000963 goto parse_flag;
964
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000965 case oHostbasedAuthentication:
966 intptr = &options->hostbased_authentication;
967 goto parse_flag;
968
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000969 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000970 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100971 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000972
Darren Tucker0efd1552003-08-26 11:49:55 +1000973 case oGssAuthentication:
974 intptr = &options->gss_authentication;
975 goto parse_flag;
976
977 case oGssDelegateCreds:
978 intptr = &options->gss_deleg_creds;
979 goto parse_flag;
980
Damien Miller95def091999-11-25 00:26:21 +1100981 case oBatchMode:
982 intptr = &options->batch_mode;
983 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Damien Miller95def091999-11-25 00:26:21 +1100985 case oCheckHostIP:
986 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000987 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller37876e92003-05-15 10:19:46 +1000989 case oVerifyHostKeyDNS:
990 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100991 multistate_ptr = multistate_yesnoask;
992 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 case oStrictHostKeyChecking:
995 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000996 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +1100997 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller95def091999-11-25 00:26:21 +1100999 case oCompression:
1000 intptr = &options->compression;
1001 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller12c150e2003-12-17 16:31:10 +11001003 case oTCPKeepAlive:
1004 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001005 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001007 case oNoHostAuthenticationForLocalhost:
1008 intptr = &options->no_host_authentication_for_localhost;
1009 goto parse_flag;
1010
Damien Miller95def091999-11-25 00:26:21 +11001011 case oNumberOfPasswordPrompts:
1012 intptr = &options->number_of_password_prompts;
1013 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Millera5539d22003-04-09 20:50:06 +10001015 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001016 arg = strdelim(&s);
1017 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001018 fatal("%.200s line %d: Missing argument.", filename,
1019 linenum);
1020 if (strcmp(arg, "default") == 0) {
1021 val64 = 0;
1022 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001023 if (scan_scaled(arg, &val64) == -1)
1024 fatal("%.200s line %d: Bad number '%s': %s",
1025 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001026 if (val64 != 0 && val64 < 16)
1027 fatal("%.200s line %d: RekeyLimit too small",
1028 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001029 }
Damien Miller3dff1762008-02-10 22:25:52 +11001030 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001031 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001032 if (s != NULL) { /* optional rekey interval present */
1033 if (strcmp(s, "none") == 0) {
1034 (void)strdelim(&s); /* discard */
1035 break;
1036 }
1037 intptr = &options->rekey_interval;
1038 goto parse_time;
1039 }
Damien Millera5539d22003-04-09 20:50:06 +10001040 break;
1041
Damien Miller95def091999-11-25 00:26:21 +11001042 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001043 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001044 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001045 fatal("%.200s line %d: Missing argument.", filename, linenum);
1046 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001047 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001048 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001049 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001050 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001051 add_identity_file(options, NULL,
1052 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001053 }
1054 break;
1055
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001056 case oCertificateFile:
1057 arg = strdelim(&s);
1058 if (!arg || *arg == '\0')
1059 fatal("%.200s line %d: Missing argument.",
1060 filename, linenum);
1061 if (*activep) {
1062 intptr = &options->num_certificate_files;
1063 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1064 fatal("%.200s line %d: Too many certificate "
1065 "files specified (max %d).",
1066 filename, linenum,
1067 SSH_MAX_CERTIFICATE_FILES);
1068 }
1069 add_certificate_file(options, arg,
1070 flags & SSHCONF_USERCONF);
1071 }
1072 break;
1073
Damien Millerd3a18572000-06-07 19:55:44 +10001074 case oXAuthLocation:
1075 charptr=&options->xauth_location;
1076 goto parse_string;
1077
Damien Miller95def091999-11-25 00:26:21 +11001078 case oUser:
1079 charptr = &options->user;
1080parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001081 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001082 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001083 fatal("%.200s line %d: Missing argument.",
1084 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001085 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001086 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001087 break;
1088
1089 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001090 cpptr = (char **)&options->system_hostfiles;
1091 uintptr = &options->num_system_hostfiles;
1092 max_entries = SSH_MAX_HOSTS_FILES;
1093parse_char_array:
1094 if (*activep && *uintptr == 0) {
1095 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1096 if ((*uintptr) >= max_entries)
1097 fatal("%s line %d: "
1098 "too many authorized keys files.",
1099 filename, linenum);
1100 cpptr[(*uintptr)++] = xstrdup(arg);
1101 }
1102 }
1103 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001104
1105 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001106 cpptr = (char **)&options->user_hostfiles;
1107 uintptr = &options->num_user_hostfiles;
1108 max_entries = SSH_MAX_HOSTS_FILES;
1109 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001110
Damien Miller95def091999-11-25 00:26:21 +11001111 case oHostName:
1112 charptr = &options->hostname;
1113 goto parse_string;
1114
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001115 case oHostKeyAlias:
1116 charptr = &options->host_key_alias;
1117 goto parse_string;
1118
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001119 case oPreferredAuthentications:
1120 charptr = &options->preferred_authentications;
1121 goto parse_string;
1122
Ben Lindstrome0f88042001-04-30 13:06:24 +00001123 case oBindAddress:
1124 charptr = &options->bind_address;
1125 goto parse_string;
1126
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001127 case oBindInterface:
1128 charptr = &options->bind_interface;
1129 goto parse_string;
1130
Damien Miller7ea845e2010-02-12 09:21:02 +11001131 case oPKCS11Provider:
1132 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001133 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001134
Damien Miller95def091999-11-25 00:26:21 +11001135 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001136 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001137 /* Ignore ProxyCommand if ProxyJump already specified */
1138 if (options->jump_host != NULL)
1139 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001140parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001141 if (s == NULL)
1142 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001143 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001144 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001145 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001146 return 0;
1147
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001148 case oProxyJump:
1149 if (s == NULL) {
1150 fatal("%.200s line %d: Missing argument.",
1151 filename, linenum);
1152 }
1153 len = strspn(s, WHITESPACE "=");
1154 if (parse_jump(s + len, options, *activep) == -1) {
1155 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1156 filename, linenum, s + len);
1157 }
1158 return 0;
1159
Damien Miller95def091999-11-25 00:26:21 +11001160 case oPort:
1161 intptr = &options->port;
1162parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001163 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001164 if ((errstr = atoi_err(arg, &value)) != NULL)
1165 fatal("%s line %d: integer value %s.",
1166 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001167 if (*activep && *intptr == -1)
1168 *intptr = value;
1169 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170
Damien Miller95def091999-11-25 00:26:21 +11001171 case oConnectionAttempts:
1172 intptr = &options->connection_attempts;
1173 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001174
Damien Miller78928792000-04-12 20:17:38 +10001175 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001176 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001177 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001178 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001179 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001180 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001181 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001182 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001183 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001184 break;
1185
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001186 case oMacs:
1187 arg = strdelim(&s);
1188 if (!arg || *arg == '\0')
1189 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001190 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001191 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001192 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001193 if (*activep && options->macs == NULL)
1194 options->macs = xstrdup(arg);
1195 break;
1196
Damien Millerd5f62bf2010-09-24 22:11:14 +10001197 case oKexAlgorithms:
1198 arg = strdelim(&s);
1199 if (!arg || *arg == '\0')
1200 fatal("%.200s line %d: Missing argument.",
1201 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001202 if (*arg != '-' &&
1203 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001204 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1205 filename, linenum, arg ? arg : "<NONE>");
1206 if (*activep && options->kex_algorithms == NULL)
1207 options->kex_algorithms = xstrdup(arg);
1208 break;
1209
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001210 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001211 charptr = &options->hostkeyalgorithms;
1212parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001213 arg = strdelim(&s);
1214 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001215 fatal("%.200s line %d: Missing argument.",
1216 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001217 if (*arg != '-' &&
1218 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001219 fatal("%s line %d: Bad key types '%s'.",
1220 filename, linenum, arg ? arg : "<NONE>");
1221 if (*activep && *charptr == NULL)
1222 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001223 break;
1224
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001225 case oCASignatureAlgorithms:
1226 charptr = &options->ca_sign_algorithms;
1227 goto parse_keytypes;
1228
Damien Miller95def091999-11-25 00:26:21 +11001229 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001230 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001231 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001232 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001233 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001234 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001235 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001236 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1237 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001238 break;
1239
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001240 case oLogFacility:
1241 log_facility_ptr = &options->log_facility;
1242 arg = strdelim(&s);
1243 value = log_facility_number(arg);
1244 if (value == SYSLOG_FACILITY_NOT_SET)
1245 fatal("%.200s line %d: unsupported log facility '%s'",
1246 filename, linenum, arg ? arg : "<NONE>");
1247 if (*log_facility_ptr == -1)
1248 *log_facility_ptr = (SyslogFacility) value;
1249 break;
1250
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001251 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001252 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001253 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001254 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001255 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001256 fatal("%.200s line %d: Missing port argument.",
1257 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001258
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001259 remotefwd = (opcode == oRemoteForward);
1260 dynamicfwd = (opcode == oDynamicForward);
1261
1262 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001263 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001264 if (arg2 == NULL || *arg2 == '\0') {
1265 if (remotefwd)
1266 dynamicfwd = 1;
1267 else
1268 fatal("%.200s line %d: Missing target "
1269 "argument.", filename, linenum);
1270 } else {
1271 /* construct a string for parse_forward */
1272 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1273 arg2);
1274 }
Damien Millera699d952008-11-03 19:27:34 +11001275 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001276 if (dynamicfwd)
1277 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001278
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001279 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001280 fatal("%.200s line %d: Bad forwarding specification.",
1281 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001282
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001283 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001284 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001285 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001286 } else {
1287 add_local_forward(options, &fwd);
1288 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001289 }
Damien Miller95def091999-11-25 00:26:21 +11001290 break;
1291
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001292 case oClearAllForwardings:
1293 intptr = &options->clear_forwardings;
1294 goto parse_flag;
1295
Damien Miller95def091999-11-25 00:26:21 +11001296 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001297 if (cmdline)
1298 fatal("Host directive not supported as a command-line "
1299 "option");
Damien Miller95def091999-11-25 00:26:21 +11001300 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001301 arg2 = NULL;
1302 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001303 if ((flags & SSHCONF_NEVERMATCH) != 0)
1304 break;
Damien Millerfe924212011-05-15 08:44:45 +10001305 negated = *arg == '!';
1306 if (negated)
1307 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001308 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001309 if (negated) {
1310 debug("%.200s line %d: Skipping Host "
1311 "block because of negated match "
1312 "for %.100s", filename, linenum,
1313 arg);
1314 *activep = 0;
1315 break;
1316 }
1317 if (!*activep)
1318 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001319 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001320 }
Damien Millerfe924212011-05-15 08:44:45 +10001321 }
1322 if (*activep)
1323 debug("%.200s line %d: Applying options for %.100s",
1324 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001325 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001326 return 0;
1327
Damien Miller194fd902013-10-15 12:13:05 +11001328 case oMatch:
1329 if (cmdline)
1330 fatal("Host directive not supported as a command-line "
1331 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001332 value = match_cfg_line(options, &s, pw, host, original_host,
1333 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001334 if (value < 0)
1335 fatal("%.200s line %d: Bad Match condition", filename,
1336 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001337 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001338 break;
1339
Damien Miller95def091999-11-25 00:26:21 +11001340 case oEscapeChar:
1341 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001342 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001343 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001344 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001345 if (strcmp(arg, "none") == 0)
1346 value = SSH_ESCAPECHAR_NONE;
1347 else if (arg[1] == '\0')
1348 value = (u_char) arg[0];
1349 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001350 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1351 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001352 else {
1353 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001354 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001355 /* NOTREACHED */
1356 value = 0; /* Avoid compiler warning. */
1357 }
1358 if (*activep && *intptr == -1)
1359 *intptr = value;
1360 break;
1361
Damien Miller20a8f972003-05-18 20:50:30 +10001362 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001363 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001364 multistate_ptr = multistate_addressfamily;
1365 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001366
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001367 case oEnableSSHKeysign:
1368 intptr = &options->enable_ssh_keysign;
1369 goto parse_flag;
1370
Damien Millerbd394c32004-03-08 23:12:36 +11001371 case oIdentitiesOnly:
1372 intptr = &options->identities_only;
1373 goto parse_flag;
1374
Damien Miller509b0102003-12-17 16:33:10 +11001375 case oServerAliveInterval:
1376 intptr = &options->server_alive_interval;
1377 goto parse_time;
1378
1379 case oServerAliveCountMax:
1380 intptr = &options->server_alive_count_max;
1381 goto parse_int;
1382
Darren Tucker46bc0752004-05-02 22:11:30 +10001383 case oSendEnv:
1384 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1385 if (strchr(arg, '=') != NULL)
1386 fatal("%s line %d: Invalid environment name.",
1387 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001388 if (!*activep)
1389 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001390 if (*arg == '-') {
1391 /* Removing an env var */
1392 rm_env(options, arg, filename, linenum);
1393 continue;
1394 } else {
1395 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001396 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001397 fatal("%s line %d: too many send env.",
1398 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001399 options->send_env = xrecallocarray(
1400 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001401 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001402 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001403 options->send_env[options->num_send_env++] =
1404 xstrdup(arg);
1405 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001406 }
1407 break;
1408
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001409 case oSetEnv:
1410 value = options->num_setenv;
1411 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1412 if (strchr(arg, '=') == NULL)
1413 fatal("%s line %d: Invalid SetEnv.",
1414 filename, linenum);
1415 if (!*activep || value != 0)
1416 continue;
1417 /* Adding a setenv var */
1418 if (options->num_setenv >= INT_MAX)
1419 fatal("%s line %d: too many SetEnv.",
1420 filename, linenum);
1421 options->setenv = xrecallocarray(
1422 options->setenv, options->num_setenv,
1423 options->num_setenv + 1, sizeof(*options->setenv));
1424 options->setenv[options->num_setenv++] = xstrdup(arg);
1425 }
1426 break;
1427
Damien Miller0e220db2004-06-15 10:34:08 +10001428 case oControlPath:
1429 charptr = &options->control_path;
1430 goto parse_string;
1431
1432 case oControlMaster:
1433 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001434 multistate_ptr = multistate_controlmaster;
1435 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001436
Damien Millere11e1ea2010-08-03 16:04:46 +10001437 case oControlPersist:
1438 /* no/false/yes/true, or a time spec */
1439 intptr = &options->control_persist;
1440 arg = strdelim(&s);
1441 if (!arg || *arg == '\0')
1442 fatal("%.200s line %d: Missing ControlPersist"
1443 " argument.", filename, linenum);
1444 value = 0;
1445 value2 = 0; /* timeout */
1446 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1447 value = 0;
1448 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1449 value = 1;
1450 else if ((value2 = convtime(arg)) >= 0)
1451 value = 1;
1452 else
1453 fatal("%.200s line %d: Bad ControlPersist argument.",
1454 filename, linenum);
1455 if (*activep && *intptr == -1) {
1456 *intptr = value;
1457 options->control_persist_timeout = value2;
1458 }
1459 break;
1460
Damien Millere1776152005-03-01 21:47:37 +11001461 case oHashKnownHosts:
1462 intptr = &options->hash_known_hosts;
1463 goto parse_flag;
1464
Damien Millerd27b9472005-12-13 19:29:02 +11001465 case oTunnel:
1466 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001467 multistate_ptr = multistate_tunnel;
1468 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001469
1470 case oTunnelDevice:
1471 arg = strdelim(&s);
1472 if (!arg || *arg == '\0')
1473 fatal("%.200s line %d: Missing argument.", filename, linenum);
1474 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001475 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001476 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1477 if (*activep) {
1478 options->tun_local = value;
1479 options->tun_remote = value2;
1480 }
1481 break;
1482
1483 case oLocalCommand:
1484 charptr = &options->local_command;
1485 goto parse_command;
1486
1487 case oPermitLocalCommand:
1488 intptr = &options->permit_local_command;
1489 goto parse_flag;
1490
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001491 case oRemoteCommand:
1492 charptr = &options->remote_command;
1493 goto parse_command;
1494
Damien Miller10288242008-06-30 00:04:03 +10001495 case oVisualHostKey:
1496 intptr = &options->visual_host_key;
1497 goto parse_flag;
1498
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001499 case oInclude:
1500 if (cmdline)
1501 fatal("Include directive not supported as a "
1502 "command-line option");
1503 value = 0;
1504 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1505 /*
1506 * Ensure all paths are anchored. User configuration
1507 * files may begin with '~/' but system configurations
1508 * must not. If the path is relative, then treat it
1509 * as living in ~/.ssh for user configurations or
1510 * /etc/ssh for system ones.
1511 */
1512 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1513 fatal("%.200s line %d: bad include path %s.",
1514 filename, linenum, arg);
1515 if (*arg != '/' && *arg != '~') {
1516 xasprintf(&arg2, "%s/%s",
1517 (flags & SSHCONF_USERCONF) ?
1518 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1519 } else
1520 arg2 = xstrdup(arg);
1521 memset(&gl, 0, sizeof(gl));
1522 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1523 if (r == GLOB_NOMATCH) {
1524 debug("%.200s line %d: include %s matched no "
1525 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001526 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001527 continue;
1528 } else if (r != 0 || gl.gl_pathc < 0)
1529 fatal("%.200s line %d: glob failed for %s.",
1530 filename, linenum, arg2);
1531 free(arg2);
1532 oactive = *activep;
1533 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1534 debug3("%.200s line %d: Including file %s "
1535 "depth %d%s", filename, linenum,
1536 gl.gl_pathv[i], depth,
1537 oactive ? "" : " (parse only)");
1538 r = read_config_file_depth(gl.gl_pathv[i],
1539 pw, host, original_host, options,
1540 flags | SSHCONF_CHECKPERM |
1541 (oactive ? 0 : SSHCONF_NEVERMATCH),
1542 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001543 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001544 fatal("Can't open user config file "
1545 "%.100s: %.100s", gl.gl_pathv[i],
1546 strerror(errno));
1547 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001548 /*
1549 * don't let Match in includes clobber the
1550 * containing file's Match state.
1551 */
1552 *activep = oactive;
1553 if (r != 1)
1554 value = -1;
1555 }
1556 globfree(&gl);
1557 }
1558 if (value != 0)
1559 return value;
1560 break;
1561
Damien Miller0dac6fb2010-11-20 15:19:38 +11001562 case oIPQoS:
1563 arg = strdelim(&s);
1564 if ((value = parse_ipqos(arg)) == -1)
1565 fatal("%s line %d: Bad IPQoS value: %s",
1566 filename, linenum, arg);
1567 arg = strdelim(&s);
1568 if (arg == NULL)
1569 value2 = value;
1570 else if ((value2 = parse_ipqos(arg)) == -1)
1571 fatal("%s line %d: Bad IPQoS value: %s",
1572 filename, linenum, arg);
1573 if (*activep) {
1574 options->ip_qos_interactive = value;
1575 options->ip_qos_bulk = value2;
1576 }
1577 break;
1578
Damien Miller21771e22011-05-15 08:45:50 +10001579 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001580 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001581 multistate_ptr = multistate_requesttty;
1582 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001583
Darren Tucker07636982013-05-16 20:30:03 +10001584 case oIgnoreUnknown:
1585 charptr = &options->ignored_unknown;
1586 goto parse_string;
1587
Damien Miller1262b662013-08-21 02:44:24 +10001588 case oProxyUseFdpass:
1589 intptr = &options->proxy_use_fdpass;
1590 goto parse_flag;
1591
Damien Miller0faf7472013-10-17 11:47:23 +11001592 case oCanonicalDomains:
1593 value = options->num_canonical_domains != 0;
1594 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001595 if (!valid_domain(arg, 1, &errstr)) {
1596 fatal("%s line %d: %s", filename, linenum,
1597 errstr);
1598 }
Damien Miller0faf7472013-10-17 11:47:23 +11001599 if (!*activep || value)
1600 continue;
1601 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1602 fatal("%s line %d: too many hostname suffixes.",
1603 filename, linenum);
1604 options->canonical_domains[
1605 options->num_canonical_domains++] = xstrdup(arg);
1606 }
1607 break;
1608
Damien Miller38505592013-10-17 11:48:13 +11001609 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001610 value = options->num_permitted_cnames != 0;
1611 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1612 /* Either '*' for everything or 'list:list' */
1613 if (strcmp(arg, "*") == 0)
1614 arg2 = arg;
1615 else {
1616 lowercase(arg);
1617 if ((arg2 = strchr(arg, ':')) == NULL ||
1618 arg2[1] == '\0') {
1619 fatal("%s line %d: "
1620 "Invalid permitted CNAME \"%s\"",
1621 filename, linenum, arg);
1622 }
1623 *arg2 = '\0';
1624 arg2++;
1625 }
1626 if (!*activep || value)
1627 continue;
1628 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1629 fatal("%s line %d: too many permitted CNAMEs.",
1630 filename, linenum);
1631 cname = options->permitted_cnames +
1632 options->num_permitted_cnames++;
1633 cname->source_list = xstrdup(arg);
1634 cname->target_list = xstrdup(arg2);
1635 }
1636 break;
1637
Damien Miller38505592013-10-17 11:48:13 +11001638 case oCanonicalizeHostname:
1639 intptr = &options->canonicalize_hostname;
1640 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001641 goto parse_multistate;
1642
Damien Miller38505592013-10-17 11:48:13 +11001643 case oCanonicalizeMaxDots:
1644 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001645 goto parse_int;
1646
Damien Miller38505592013-10-17 11:48:13 +11001647 case oCanonicalizeFallbackLocal:
1648 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001649 goto parse_flag;
1650
Damien Miller7acefbb2014-07-18 14:11:24 +10001651 case oStreamLocalBindMask:
1652 arg = strdelim(&s);
1653 if (!arg || *arg == '\0')
1654 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1655 /* Parse mode in octal format */
1656 value = strtol(arg, &endofnumber, 8);
1657 if (arg == endofnumber || value < 0 || value > 0777)
1658 fatal("%.200s line %d: Bad mask.", filename, linenum);
1659 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1660 break;
1661
1662 case oStreamLocalBindUnlink:
1663 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1664 goto parse_flag;
1665
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001666 case oRevokedHostKeys:
1667 charptr = &options->revoked_host_keys;
1668 goto parse_string;
1669
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001670 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001671 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001672 arg = strdelim(&s);
1673 if (!arg || *arg == '\0')
1674 fatal("%.200s line %d: Missing argument.",
1675 filename, linenum);
1676 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1677 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1678 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001679 if (*activep && *intptr == -1)
1680 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001681 break;
1682
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001683 case oUpdateHostkeys:
1684 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001685 multistate_ptr = multistate_yesnoask;
1686 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001687
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001688 case oHostbasedKeyTypes:
1689 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001690 goto parse_keytypes;
1691
1692 case oPubkeyAcceptedKeyTypes:
1693 charptr = &options->pubkey_key_types;
1694 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001695
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001696 case oAddKeysToAgent:
1697 intptr = &options->add_keys_to_agent;
1698 multistate_ptr = multistate_yesnoaskconfirm;
1699 goto parse_multistate;
1700
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001701 case oIdentityAgent:
1702 charptr = &options->identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001703 arg = strdelim(&s);
1704 if (!arg || *arg == '\0')
1705 fatal("%.200s line %d: Missing argument.",
1706 filename, linenum);
1707 /* Extra validation if the string represents an env var. */
1708 if (arg[0] == '$' && !valid_env_name(arg + 1)) {
1709 fatal("%.200s line %d: Invalid environment name %s.",
1710 filename, linenum, arg);
1711 }
1712 if (*activep && *charptr == NULL)
1713 *charptr = xstrdup(arg);
1714 break;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001715
Ben Lindstrom4daea862002-06-09 20:04:02 +00001716 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001717 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001718 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001719 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001720
Damien Millerf9b3feb2003-05-16 11:38:32 +10001721 case oUnsupported:
1722 error("%s line %d: Unsupported option \"%s\"",
1723 filename, linenum, keyword);
1724 return 0;
1725
Damien Miller95def091999-11-25 00:26:21 +11001726 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001727 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001728 }
1729
1730 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001731 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001732 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001733 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001734 }
Damien Miller95def091999-11-25 00:26:21 +11001735 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001736}
1737
Damien Miller5428f641999-11-25 11:54:57 +11001738/*
1739 * Reads the config file and modifies the options accordingly. Options
1740 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001741 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001742 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001743int
Damien Miller194fd902013-10-15 12:13:05 +11001744read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001745 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001746{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001747 int active = 1;
1748
1749 return read_config_file_depth(filename, pw, host, original_host,
1750 options, flags, &active, 0);
1751}
1752
1753#define READCONF_MAX_DEPTH 16
1754static int
1755read_config_file_depth(const char *filename, struct passwd *pw,
1756 const char *host, const char *original_host, Options *options,
1757 int flags, int *activep, int depth)
1758{
Damien Miller95def091999-11-25 00:26:21 +11001759 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001760 char *line = NULL;
1761 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001762 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001763 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001764
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001765 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1766 fatal("Too many recursive configuration includes");
1767
Damien Miller57a44762004-04-20 20:11:57 +10001768 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001769 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001770
Darren Tuckeraefa3682013-04-05 11:18:35 +11001771 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001772 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001773
Damien Miller33793852004-06-15 10:27:55 +10001774 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001775 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001776 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001777 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001778 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001779 }
1780
Damien Miller95def091999-11-25 00:26:21 +11001781 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001782
Damien Miller5428f641999-11-25 11:54:57 +11001783 /*
1784 * Mark that we are now processing the options. This flag is turned
1785 * on/off by Host specifications.
1786 */
Damien Miller95def091999-11-25 00:26:21 +11001787 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001788 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001789 /* Update line number counter. */
1790 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001791 if (process_config_line_depth(options, pw, host, original_host,
1792 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001793 bad_options++;
1794 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001795 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001796 fclose(f);
1797 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001798 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001799 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001800 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001801}
1802
Damien Miller13f97b22014-02-24 15:57:55 +11001803/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1804int
1805option_clear_or_none(const char *o)
1806{
1807 return o == NULL || strcasecmp(o, "none") == 0;
1808}
1809
Damien Miller5428f641999-11-25 11:54:57 +11001810/*
1811 * Initializes options to special values that indicate that they have not yet
1812 * been set. Read_config_file will only set options with this value. Options
1813 * are processed in the following order: command line, user config file,
1814 * system config file. Last, fill_default_options is called.
1815 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001816
Damien Miller4af51302000-04-16 11:18:38 +10001817void
Damien Miller95def091999-11-25 00:26:21 +11001818initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001819{
Damien Miller95def091999-11-25 00:26:21 +11001820 memset(options, 'X', sizeof(*options));
1821 options->forward_agent = -1;
1822 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001823 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001824 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001825 options->stdio_forward_host = NULL;
1826 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001827 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001828 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001829 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001830 options->fwd_opts.gateway_ports = -1;
1831 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1832 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001833 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001834 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001835 options->gss_authentication = -1;
1836 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001837 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001838 options->kbd_interactive_authentication = -1;
1839 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001840 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001841 options->batch_mode = -1;
1842 options->check_host_ip = -1;
1843 options->strict_host_key_checking = -1;
1844 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001845 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001846 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001847 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001848 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001849 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001850 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001851 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001852 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001853 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001854 options->hostkeyalgorithms = NULL;
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001855 options->ca_sign_algorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001856 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001857 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001858 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001859 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001860 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001861 options->jump_user = NULL;
1862 options->jump_host = NULL;
1863 options->jump_port = -1;
1864 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001865 options->user = NULL;
1866 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001867 options->num_system_hostfiles = 0;
1868 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001869 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001870 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001871 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001872 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001873 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001874 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001875 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001876 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001877 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001878 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001879 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001880 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001881 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001882 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001883 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001884 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001885 options->server_alive_interval = -1;
1886 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001887 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001888 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001889 options->setenv = NULL;
1890 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001891 options->control_path = NULL;
1892 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001893 options->control_persist = -1;
1894 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001895 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001896 options->tun_open = -1;
1897 options->tun_local = -1;
1898 options->tun_remote = -1;
1899 options->local_command = NULL;
1900 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001901 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001902 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001903 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001904 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001905 options->ip_qos_interactive = -1;
1906 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001907 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001908 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001909 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001910 options->num_canonical_domains = 0;
1911 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001912 options->canonicalize_max_dots = -1;
1913 options->canonicalize_fallback_local = -1;
1914 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001915 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001916 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001917 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001918 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001919 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920}
1921
Damien Miller5428f641999-11-25 11:54:57 +11001922/*
Damien Miller13f97b22014-02-24 15:57:55 +11001923 * A petite version of fill_default_options() that just fills the options
1924 * needed for hostname canonicalization to proceed.
1925 */
1926void
1927fill_default_options_for_canonicalization(Options *options)
1928{
1929 if (options->canonicalize_max_dots == -1)
1930 options->canonicalize_max_dots = 1;
1931 if (options->canonicalize_fallback_local == -1)
1932 options->canonicalize_fallback_local = 1;
1933 if (options->canonicalize_hostname == -1)
1934 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1935}
1936
1937/*
Damien Miller5428f641999-11-25 11:54:57 +11001938 * Called after processing other sources of option data, this fills those
1939 * options for which no value has been specified with their default values.
1940 */
Damien Miller4af51302000-04-16 11:18:38 +10001941void
Damien Miller95def091999-11-25 00:26:21 +11001942fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001943{
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00001944 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001945 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001946
Damien Miller95def091999-11-25 00:26:21 +11001947 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001948 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001949 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001950 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001951 if (options->forward_x11_trusted == -1)
1952 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001953 if (options->forward_x11_timeout == -1)
1954 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001955 /*
1956 * stdio forwarding (-W) changes the default for these but we defer
1957 * setting the values so they can be overridden.
1958 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001959 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001960 options->exit_on_forward_failure =
1961 options->stdio_forward_host != NULL ? 1 : 0;
1962 if (options->clear_forwardings == -1)
1963 options->clear_forwardings =
1964 options->stdio_forward_host != NULL ? 1 : 0;
1965 if (options->clear_forwardings == 1)
1966 clear_forwardings(options);
1967
Damien Millerd3a18572000-06-07 19:55:44 +10001968 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001969 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001970 if (options->fwd_opts.gateway_ports == -1)
1971 options->fwd_opts.gateway_ports = 0;
1972 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1973 options->fwd_opts.streamlocal_bind_mask = 0177;
1974 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1975 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001976 if (options->pubkey_authentication == -1)
1977 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001978 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001979 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001980 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001981 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001982 if (options->gss_deleg_creds == -1)
1983 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001984 if (options->password_authentication == -1)
1985 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001986 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001987 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001988 if (options->hostbased_authentication == -1)
1989 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001990 if (options->batch_mode == -1)
1991 options->batch_mode = 0;
1992 if (options->check_host_ip == -1)
1993 options->check_host_ip = 1;
1994 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001995 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001996 if (options->compression == -1)
1997 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001998 if (options->tcp_keep_alive == -1)
1999 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11002000 if (options->port == -1)
2001 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10002002 if (options->address_family == -1)
2003 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11002004 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00002005 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11002006 if (options->number_of_password_prompts == -1)
2007 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00002008 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00002009 if (options->add_keys_to_agent == -1)
2010 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11002011 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002012 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2013 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002014#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002015 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10002016#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002017 add_identity_file(options, "~/",
2018 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002019 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002020 }
Damien Miller95def091999-11-25 00:26:21 +11002021 if (options->escape_char == -1)
2022 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002023 if (options->num_system_hostfiles == 0) {
2024 options->system_hostfiles[options->num_system_hostfiles++] =
2025 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2026 options->system_hostfiles[options->num_system_hostfiles++] =
2027 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2028 }
2029 if (options->num_user_hostfiles == 0) {
2030 options->user_hostfiles[options->num_user_hostfiles++] =
2031 xstrdup(_PATH_SSH_USER_HOSTFILE);
2032 options->user_hostfiles[options->num_user_hostfiles++] =
2033 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2034 }
Damien Millerfcd93202002-02-05 12:26:34 +11002035 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002036 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002037 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2038 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002039 if (options->no_host_authentication_for_localhost == - 1)
2040 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002041 if (options->identities_only == -1)
2042 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002043 if (options->enable_ssh_keysign == -1)
2044 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002045 if (options->rekey_limit == -1)
2046 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002047 if (options->rekey_interval == -1)
2048 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002049 if (options->verify_host_key_dns == -1)
2050 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002051 if (options->server_alive_interval == -1)
2052 options->server_alive_interval = 0;
2053 if (options->server_alive_count_max == -1)
2054 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002055 if (options->control_master == -1)
2056 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002057 if (options->control_persist == -1) {
2058 options->control_persist = 0;
2059 options->control_persist_timeout = 0;
2060 }
Damien Millere1776152005-03-01 21:47:37 +11002061 if (options->hash_known_hosts == -1)
2062 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002063 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002064 options->tun_open = SSH_TUNMODE_NO;
2065 if (options->tun_local == -1)
2066 options->tun_local = SSH_TUNID_ANY;
2067 if (options->tun_remote == -1)
2068 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002069 if (options->permit_local_command == -1)
2070 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002071 if (options->visual_host_key == -1)
2072 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002073 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002074 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002075 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002076 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002077 if (options->request_tty == -1)
2078 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002079 if (options->proxy_use_fdpass == -1)
2080 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002081 if (options->canonicalize_max_dots == -1)
2082 options->canonicalize_max_dots = 1;
2083 if (options->canonicalize_fallback_local == -1)
2084 options->canonicalize_fallback_local = 1;
2085 if (options->canonicalize_hostname == -1)
2086 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002087 if (options->fingerprint_hash == -1)
2088 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002089 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002090 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002091
2092 /* Expand KEX name lists */
2093 all_cipher = cipher_alg_list(',', 0);
2094 all_mac = mac_alg_list(',');
2095 all_kex = kex_alg_list(',');
2096 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002097 all_sig = sshkey_alg_list(0, 1, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002098#define ASSEMBLE(what, defaults, all) \
2099 do { \
2100 if ((r = kex_assemble_names(&options->what, \
2101 defaults, all)) != 0) \
2102 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2103 } while (0)
2104 ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
2105 ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
2106 ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
2107 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
2108 ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002109 ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002110#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002111 free(all_cipher);
2112 free(all_mac);
2113 free(all_kex);
2114 free(all_key);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002115 free(all_sig);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002116
Damien Millere9fc72e2013-10-15 12:14:12 +11002117#define CLEAR_ON_NONE(v) \
2118 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002119 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002120 free(v); \
2121 v = NULL; \
2122 } \
2123 } while(0)
2124 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002125 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002126 CLEAR_ON_NONE(options->proxy_command);
2127 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002128 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002129 if (options->jump_host != NULL &&
2130 strcmp(options->jump_host, "none") == 0 &&
2131 options->jump_port == 0 && options->jump_user == NULL) {
2132 free(options->jump_host);
2133 options->jump_host = NULL;
2134 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002135 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002136 /* options->user will be set in the main program if appropriate */
2137 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002138 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002139 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002140}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002141
Damien Miller7acefbb2014-07-18 14:11:24 +10002142struct fwdarg {
2143 char *arg;
2144 int ispath;
2145};
2146
2147/*
2148 * parse_fwd_field
2149 * parses the next field in a port forwarding specification.
2150 * sets fwd to the parsed field and advances p past the colon
2151 * or sets it to NULL at end of string.
2152 * returns 0 on success, else non-zero.
2153 */
2154static int
2155parse_fwd_field(char **p, struct fwdarg *fwd)
2156{
2157 char *ep, *cp = *p;
2158 int ispath = 0;
2159
2160 if (*cp == '\0') {
2161 *p = NULL;
2162 return -1; /* end of string */
2163 }
2164
2165 /*
2166 * A field escaped with square brackets is used literally.
2167 * XXX - allow ']' to be escaped via backslash?
2168 */
2169 if (*cp == '[') {
2170 /* find matching ']' */
2171 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2172 if (*ep == '/')
2173 ispath = 1;
2174 }
2175 /* no matching ']' or not at end of field. */
2176 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2177 return -1;
2178 /* NUL terminate the field and advance p past the colon */
2179 *ep++ = '\0';
2180 if (*ep != '\0')
2181 *ep++ = '\0';
2182 fwd->arg = cp + 1;
2183 fwd->ispath = ispath;
2184 *p = ep;
2185 return 0;
2186 }
2187
2188 for (cp = *p; *cp != '\0'; cp++) {
2189 switch (*cp) {
2190 case '\\':
2191 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002192 if (*cp == '\0')
2193 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002194 break;
2195 case '/':
2196 ispath = 1;
2197 break;
2198 case ':':
2199 *cp++ = '\0';
2200 goto done;
2201 }
2202 }
2203done:
2204 fwd->arg = *p;
2205 fwd->ispath = ispath;
2206 *p = cp;
2207 return 0;
2208}
2209
Damien Millerf91ee4c2005-03-01 21:24:33 +11002210/*
2211 * parse_forward
2212 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002213 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002214 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2215 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002216 * dynamicfwd == 1
2217 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002218 * returns number of arguments parsed or zero on error
2219 */
2220int
Damien Miller7acefbb2014-07-18 14:11:24 +10002221parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002222{
Damien Miller7acefbb2014-07-18 14:11:24 +10002223 struct fwdarg fwdargs[4];
2224 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002225 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002226
Damien Miller7acefbb2014-07-18 14:11:24 +10002227 memset(fwd, 0, sizeof(*fwd));
2228 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002229
2230 cp = p = xstrdup(fwdspec);
2231
2232 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002233 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002234 cp++;
2235
Damien Miller7acefbb2014-07-18 14:11:24 +10002236 for (i = 0; i < 4; ++i) {
2237 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002238 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002239 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002240
Damien Millerf4b39532008-11-03 19:28:21 +11002241 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002242 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002243 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002244 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002245
2246 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002247 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002248 if (fwdargs[0].ispath) {
2249 fwd->listen_path = xstrdup(fwdargs[0].arg);
2250 fwd->listen_port = PORT_STREAMLOCAL;
2251 } else {
2252 fwd->listen_host = NULL;
2253 fwd->listen_port = a2port(fwdargs[0].arg);
2254 }
Damien Millera699d952008-11-03 19:27:34 +11002255 fwd->connect_host = xstrdup("socks");
2256 break;
2257
2258 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002259 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2260 fwd->listen_path = xstrdup(fwdargs[0].arg);
2261 fwd->listen_port = PORT_STREAMLOCAL;
2262 fwd->connect_path = xstrdup(fwdargs[1].arg);
2263 fwd->connect_port = PORT_STREAMLOCAL;
2264 } else if (fwdargs[1].ispath) {
2265 fwd->listen_host = NULL;
2266 fwd->listen_port = a2port(fwdargs[0].arg);
2267 fwd->connect_path = xstrdup(fwdargs[1].arg);
2268 fwd->connect_port = PORT_STREAMLOCAL;
2269 } else {
2270 fwd->listen_host = xstrdup(fwdargs[0].arg);
2271 fwd->listen_port = a2port(fwdargs[1].arg);
2272 fwd->connect_host = xstrdup("socks");
2273 }
Damien Millera699d952008-11-03 19:27:34 +11002274 break;
2275
Damien Millerf91ee4c2005-03-01 21:24:33 +11002276 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002277 if (fwdargs[0].ispath) {
2278 fwd->listen_path = xstrdup(fwdargs[0].arg);
2279 fwd->listen_port = PORT_STREAMLOCAL;
2280 fwd->connect_host = xstrdup(fwdargs[1].arg);
2281 fwd->connect_port = a2port(fwdargs[2].arg);
2282 } else if (fwdargs[2].ispath) {
2283 fwd->listen_host = xstrdup(fwdargs[0].arg);
2284 fwd->listen_port = a2port(fwdargs[1].arg);
2285 fwd->connect_path = xstrdup(fwdargs[2].arg);
2286 fwd->connect_port = PORT_STREAMLOCAL;
2287 } else {
2288 fwd->listen_host = NULL;
2289 fwd->listen_port = a2port(fwdargs[0].arg);
2290 fwd->connect_host = xstrdup(fwdargs[1].arg);
2291 fwd->connect_port = a2port(fwdargs[2].arg);
2292 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002293 break;
2294
2295 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002296 fwd->listen_host = xstrdup(fwdargs[0].arg);
2297 fwd->listen_port = a2port(fwdargs[1].arg);
2298 fwd->connect_host = xstrdup(fwdargs[2].arg);
2299 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002300 break;
2301 default:
2302 i = 0; /* failure */
2303 }
2304
Darren Tuckera627d422013-06-02 07:31:17 +10002305 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002306
Damien Millera699d952008-11-03 19:27:34 +11002307 if (dynamicfwd) {
2308 if (!(i == 1 || i == 2))
2309 goto fail_free;
2310 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002311 if (!(i == 3 || i == 4)) {
2312 if (fwd->connect_path == NULL &&
2313 fwd->listen_path == NULL)
2314 goto fail_free;
2315 }
2316 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002317 goto fail_free;
2318 }
2319
Damien Miller7acefbb2014-07-18 14:11:24 +10002320 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2321 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002322 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002323 if (fwd->connect_host != NULL &&
2324 strlen(fwd->connect_host) >= NI_MAXHOST)
2325 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002326 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2327 if (fwd->connect_path != NULL &&
2328 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2329 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002330 if (fwd->listen_host != NULL &&
2331 strlen(fwd->listen_host) >= NI_MAXHOST)
2332 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002333 if (fwd->listen_path != NULL &&
2334 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2335 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002336
2337 return (i);
2338
2339 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002340 free(fwd->connect_host);
2341 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002342 free(fwd->connect_path);
2343 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002344 free(fwd->listen_host);
2345 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002346 free(fwd->listen_path);
2347 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002348 return (0);
2349}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002350
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002351int
2352parse_jump(const char *s, Options *o, int active)
2353{
2354 char *orig, *sdup, *cp;
2355 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002356 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002357
2358 active &= o->proxy_command == NULL && o->jump_host == NULL;
2359
2360 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002361 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002362 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002363 if (strcasecmp(s, "none") == 0)
2364 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002365 if ((cp = strrchr(sdup, ',')) == NULL)
2366 cp = sdup; /* last */
2367 else
2368 *cp++ = '\0';
2369
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002370 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002371 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002372 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2373 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002374 goto out;
2375 } else {
2376 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002377 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2378 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002379 goto out;
2380 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002381 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002382 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002383 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002384 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002385 if (strcasecmp(s, "none") == 0) {
2386 o->jump_host = xstrdup("none");
2387 o->jump_port = 0;
2388 } else {
2389 o->jump_user = user;
2390 o->jump_host = host;
2391 o->jump_port = port;
2392 o->proxy_command = xstrdup("none");
2393 user = host = NULL;
2394 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2395 o->jump_extra = xstrdup(s);
2396 o->jump_extra[cp - s] = '\0';
2397 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002398 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002399 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002400 ret = 0;
2401 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002402 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002403 free(user);
2404 free(host);
2405 return ret;
2406}
2407
millert@openbsd.org887669e2017-10-21 23:06:24 +00002408int
2409parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2410{
2411 char *path;
2412 int r;
2413
2414 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2415 if (r == 0 && path != NULL)
2416 r = -1; /* path not allowed */
2417 return r;
2418}
2419
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002420/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2421static const char *
2422fmt_multistate_int(int val, const struct multistate *m)
2423{
2424 u_int i;
2425
2426 for (i = 0; m[i].key != NULL; i++) {
2427 if (m[i].value == val)
2428 return m[i].key;
2429 }
2430 return "UNKNOWN";
2431}
2432
2433static const char *
2434fmt_intarg(OpCodes code, int val)
2435{
2436 if (val == -1)
2437 return "unset";
2438 switch (code) {
2439 case oAddressFamily:
2440 return fmt_multistate_int(val, multistate_addressfamily);
2441 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002442 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002443 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002444 case oStrictHostKeyChecking:
2445 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002446 case oControlMaster:
2447 return fmt_multistate_int(val, multistate_controlmaster);
2448 case oTunnel:
2449 return fmt_multistate_int(val, multistate_tunnel);
2450 case oRequestTTY:
2451 return fmt_multistate_int(val, multistate_requesttty);
2452 case oCanonicalizeHostname:
2453 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002454 case oAddKeysToAgent:
2455 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002456 case oFingerprintHash:
2457 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002458 default:
2459 switch (val) {
2460 case 0:
2461 return "no";
2462 case 1:
2463 return "yes";
2464 default:
2465 return "UNKNOWN";
2466 }
2467 }
2468}
2469
2470static const char *
2471lookup_opcode_name(OpCodes code)
2472{
2473 u_int i;
2474
2475 for (i = 0; keywords[i].name != NULL; i++)
2476 if (keywords[i].opcode == code)
2477 return(keywords[i].name);
2478 return "UNKNOWN";
2479}
2480
2481static void
2482dump_cfg_int(OpCodes code, int val)
2483{
2484 printf("%s %d\n", lookup_opcode_name(code), val);
2485}
2486
2487static void
2488dump_cfg_fmtint(OpCodes code, int val)
2489{
2490 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2491}
2492
2493static void
2494dump_cfg_string(OpCodes code, const char *val)
2495{
2496 if (val == NULL)
2497 return;
2498 printf("%s %s\n", lookup_opcode_name(code), val);
2499}
2500
2501static void
2502dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2503{
2504 u_int i;
2505
2506 for (i = 0; i < count; i++)
2507 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2508}
2509
2510static void
2511dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2512{
2513 u_int i;
2514
2515 printf("%s", lookup_opcode_name(code));
2516 for (i = 0; i < count; i++)
2517 printf(" %s", vals[i]);
2518 printf("\n");
2519}
2520
2521static void
2522dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2523{
2524 const struct Forward *fwd;
2525 u_int i;
2526
2527 /* oDynamicForward */
2528 for (i = 0; i < count; i++) {
2529 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002530 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002531 strcmp(fwd->connect_host, "socks") != 0)
2532 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002533 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002534 strcmp(fwd->connect_host, "socks") == 0)
2535 continue;
2536 printf("%s", lookup_opcode_name(code));
2537 if (fwd->listen_port == PORT_STREAMLOCAL)
2538 printf(" %s", fwd->listen_path);
2539 else if (fwd->listen_host == NULL)
2540 printf(" %d", fwd->listen_port);
2541 else {
2542 printf(" [%s]:%d",
2543 fwd->listen_host, fwd->listen_port);
2544 }
2545 if (code != oDynamicForward) {
2546 if (fwd->connect_port == PORT_STREAMLOCAL)
2547 printf(" %s", fwd->connect_path);
2548 else if (fwd->connect_host == NULL)
2549 printf(" %d", fwd->connect_port);
2550 else {
2551 printf(" [%s]:%d",
2552 fwd->connect_host, fwd->connect_port);
2553 }
2554 }
2555 printf("\n");
2556 }
2557}
2558
2559void
2560dump_client_config(Options *o, const char *host)
2561{
2562 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002563 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002564
djm@openbsd.org60a92472015-08-21 23:53:08 +00002565 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002566 all_key = sshkey_alg_list(0, 0, 1, ',');
2567 if (kex_assemble_names( &o->hostkeyalgorithms,
2568 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002569 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002570 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002571
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002572 /* Most interesting options first: user, host, port */
2573 dump_cfg_string(oUser, o->user);
2574 dump_cfg_string(oHostName, host);
2575 dump_cfg_int(oPort, o->port);
2576
2577 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002578 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002579 dump_cfg_fmtint(oAddressFamily, o->address_family);
2580 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2581 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2582 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2583 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2584 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2585 dump_cfg_fmtint(oCompression, o->compression);
2586 dump_cfg_fmtint(oControlMaster, o->control_master);
2587 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002588 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002589 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002590 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002591 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2592 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2593 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2594 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2595#ifdef GSSAPI
2596 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2597 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2598#endif /* GSSAPI */
2599 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2600 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2601 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2602 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2603 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2604 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2605 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002606 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2607 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2608 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002609 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2610 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2611 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2612 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002613 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2614 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002615 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002616
2617 /* Integer options */
2618 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002619 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2620 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2621 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2622 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2623 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2624
2625 /* String options */
2626 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002627 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002628 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2629 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002630 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002631 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002632 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002633 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002634 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002635 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2636 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
djm@openbsd.orgecac7e12018-09-20 03:30:44 +00002637 dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms ? o->ca_sign_algorithms : SSH_ALLOWED_CA_SIGALGS);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002638 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002639 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002640 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2641 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002642#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002643 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002644#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002645 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002646 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002647 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002648 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002649
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002650 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002651 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2652 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2653 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2654
2655 /* String array options */
2656 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2657 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002658 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002659 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2660 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2661 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002662 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002663
2664 /* Special cases */
2665
2666 /* oConnectTimeout */
2667 if (o->connection_timeout == -1)
2668 printf("connecttimeout none\n");
2669 else
2670 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2671
2672 /* oTunnelDevice */
2673 printf("tunneldevice");
2674 if (o->tun_local == SSH_TUNID_ANY)
2675 printf(" any");
2676 else
2677 printf(" %d", o->tun_local);
2678 if (o->tun_remote == SSH_TUNID_ANY)
2679 printf(":any");
2680 else
2681 printf(":%d", o->tun_remote);
2682 printf("\n");
2683
2684 /* oCanonicalizePermittedCNAMEs */
2685 if ( o->num_permitted_cnames > 0) {
2686 printf("canonicalizePermittedcnames");
2687 for (i = 0; i < o->num_permitted_cnames; i++) {
2688 printf(" %s:%s", o->permitted_cnames[i].source_list,
2689 o->permitted_cnames[i].target_list);
2690 }
2691 printf("\n");
2692 }
2693
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002694 /* oControlPersist */
2695 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2696 dump_cfg_fmtint(oControlPersist, o->control_persist);
2697 else
2698 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2699
2700 /* oEscapeChar */
2701 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2702 printf("escapechar none\n");
2703 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002704 vis(buf, o->escape_char, VIS_WHITE, 0);
2705 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002706 }
2707
2708 /* oIPQoS */
2709 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2710 printf("%s\n", iptos2str(o->ip_qos_bulk));
2711
2712 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002713 printf("rekeylimit %llu %d\n",
2714 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002715
2716 /* oStreamLocalBindMask */
2717 printf("streamlocalbindmask 0%o\n",
2718 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002719
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002720 /* oLogFacility */
2721 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2722
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002723 /* oProxyCommand / oProxyJump */
2724 if (o->jump_host == NULL)
2725 dump_cfg_string(oProxyCommand, o->proxy_command);
2726 else {
2727 /* Check for numeric addresses */
2728 i = strchr(o->jump_host, ':') != NULL ||
2729 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2730 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2731 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002732 /* optional additional jump spec */
2733 o->jump_extra == NULL ? "" : o->jump_extra,
2734 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002735 /* optional user */
2736 o->jump_user == NULL ? "" : o->jump_user,
2737 o->jump_user == NULL ? "" : "@",
2738 /* opening [ if hostname is numeric */
2739 i ? "[" : "",
2740 /* mandatory hostname */
2741 o->jump_host,
2742 /* closing ] if hostname is numeric */
2743 i ? "]" : "",
2744 /* optional port number */
2745 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002746 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002747 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002748}