blob: db5f2d5476ad21027aab69c4450e5a608eace879 [file] [log] [blame]
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001/* $OpenBSD: readconf.c,v 1.297 2018/08/12 20:19:13 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien 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.orged877ef2016-07-15 00:24:30 +0000175 oPubkeyAcceptedKeyTypes, 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 },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000269 { "bindaddress", oBindAddress },
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000270 { "bindinterface", oBindInterface },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100271 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000272 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000273 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100274 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000275 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000276 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000277 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100278 { "serveraliveinterval", oServerAliveInterval },
279 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000280 { "sendenv", oSendEnv },
djm@openbsd.org7082bb52018-06-09 03:01:12 +0000281 { "setenv", oSetEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000282 { "controlpath", oControlPath },
283 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000284 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100285 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000286 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100287 { "tunnel", oTunnel },
288 { "tunneldevice", oTunnelDevice },
289 { "localcommand", oLocalCommand },
290 { "permitlocalcommand", oPermitLocalCommand },
bluhm@openbsd.org1112b532017-05-30 18:58:37 +0000291 { "remotecommand", oRemoteCommand },
Damien Miller10288242008-06-30 00:04:03 +1000292 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000293 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100294 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000295 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000296 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100297 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100298 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
299 { "canonicalizehostname", oCanonicalizeHostname },
300 { "canonicalizemaxdots", oCanonicalizeMaxDots },
301 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000302 { "streamlocalbindmask", oStreamLocalBindMask },
303 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000304 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000305 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000306 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000307 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000308 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000309 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000310 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100311
Ben Lindstrom65366a82001-12-06 16:32:47 +0000312 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100313};
314
Damien Miller5428f641999-11-25 11:54:57 +1100315/*
316 * Adds a local TCP/IP port forward to options. Never returns if there is an
317 * error.
318 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Damien Miller4af51302000-04-16 11:18:38 +1000320void
Damien Miller7acefbb2014-07-18 14:11:24 +1000321add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Damien Miller7acefbb2014-07-18 14:11:24 +1000323 struct Forward *fwd;
Darren Tucker5f41f032016-04-08 21:14:13 +1000324 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000325
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000326 /* Don't add duplicates */
327 for (i = 0; i < options->num_local_forwards; i++) {
328 if (forward_equals(newfwd, options->local_forwards + i))
329 return;
330 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000331 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000332 options->num_local_forwards + 1,
333 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100334 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100335
Damien Miller1a0442f2008-11-05 16:30:06 +1100336 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000338 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100339 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
Damien Miller5428f641999-11-25 11:54:57 +1100344/*
345 * Adds a remote TCP/IP port forward to options. Never returns if there is
346 * an error.
347 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller4af51302000-04-16 11:18:38 +1000349void
Damien Miller7acefbb2014-07-18 14:11:24 +1000350add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Damien Miller7acefbb2014-07-18 14:11:24 +1000352 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000353 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000354
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000355 /* Don't add duplicates */
356 for (i = 0; i < options->num_remote_forwards; i++) {
357 if (forward_equals(newfwd, options->remote_forwards + i))
358 return;
359 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000360 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000361 options->num_remote_forwards + 1,
362 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100363 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100364
Damien Miller1a0442f2008-11-05 16:30:06 +1100365 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100366 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000367 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100368 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100369 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000370 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100371 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000372 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373}
374
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000375static void
376clear_forwardings(Options *options)
377{
378 int i;
379
Damien Millerf91ee4c2005-03-01 21:24:33 +1100380 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000381 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000382 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000384 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100385 }
Damien Miller232cfb12010-06-26 09:50:30 +1000386 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000387 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000388 options->local_forwards = NULL;
389 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000390 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100391 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000392 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000393 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000394 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000395 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100396 }
Damien Miller232cfb12010-06-26 09:50:30 +1000397 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000399 options->remote_forwards = NULL;
400 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000401 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100402 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000403}
404
Darren Tucker19104782013-04-05 11:13:08 +1100405void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000406add_certificate_file(Options *options, const char *path, int userprovided)
407{
408 int i;
409
410 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
411 fatal("Too many certificate files specified (max %d)",
412 SSH_MAX_CERTIFICATE_FILES);
413
414 /* Avoid registering duplicates */
415 for (i = 0; i < options->num_certificate_files; i++) {
416 if (options->certificate_file_userprovided[i] == userprovided &&
417 strcmp(options->certificate_files[i], path) == 0) {
418 debug2("%s: ignoring duplicate key %s", __func__, path);
419 return;
420 }
421 }
422
423 options->certificate_file_userprovided[options->num_certificate_files] =
424 userprovided;
425 options->certificate_files[options->num_certificate_files++] =
426 xstrdup(path);
427}
428
429void
Darren Tucker19104782013-04-05 11:13:08 +1100430add_identity_file(Options *options, const char *dir, const char *filename,
431 int userprovided)
432{
433 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000434 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100435
436 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
437 fatal("Too many identity files specified (max %d)",
438 SSH_MAX_IDENTITY_FILES);
439
440 if (dir == NULL) /* no dir, filename is absolute */
441 path = xstrdup(filename);
djm@openbsd.org947a3e82017-05-20 02:35:47 +0000442 else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
443 fatal("Identity file path %s too long", path);
Darren Tucker19104782013-04-05 11:13:08 +1100444
Damien Miller15271902014-05-15 13:47:56 +1000445 /* Avoid registering duplicates */
446 for (i = 0; i < options->num_identity_files; i++) {
447 if (options->identity_file_userprovided[i] == userprovided &&
448 strcmp(options->identity_files[i], path) == 0) {
449 debug2("%s: ignoring duplicate key %s", __func__, path);
450 free(path);
451 return;
452 }
453 }
454
Darren Tucker19104782013-04-05 11:13:08 +1100455 options->identity_file_userprovided[options->num_identity_files] =
456 userprovided;
457 options->identity_files[options->num_identity_files++] = path;
458}
459
Damien Miller194fd902013-10-15 12:13:05 +1100460int
461default_ssh_port(void)
462{
463 static int port;
464 struct servent *sp;
465
466 if (port == 0) {
467 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
468 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
469 }
470 return port;
471}
472
473/*
474 * Execute a command in a shell.
475 * Return its exit status or -1 on abnormal exit.
476 */
477static int
478execute_in_shell(const char *cmd)
479{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000480 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100481 pid_t pid;
482 int devnull, status;
Damien Miller194fd902013-10-15 12:13:05 +1100483
484 if ((shell = getenv("SHELL")) == NULL)
485 shell = _PATH_BSHELL;
486
Damien Miller194fd902013-10-15 12:13:05 +1100487 /* Need this to redirect subprocess stdin/out */
488 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
489 fatal("open(/dev/null): %s", strerror(errno));
490
491 debug("Executing command: '%.500s'", cmd);
492
493 /* Fork and execute the command. */
494 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000495 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100496
Damien Miller194fd902013-10-15 12:13:05 +1100497 /* Redirect child stdin and stdout. Leave stderr */
498 if (dup2(devnull, STDIN_FILENO) == -1)
499 fatal("dup2: %s", strerror(errno));
500 if (dup2(devnull, STDOUT_FILENO) == -1)
501 fatal("dup2: %s", strerror(errno));
502 if (devnull > STDERR_FILENO)
503 close(devnull);
504 closefrom(STDERR_FILENO + 1);
505
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000506 argv[0] = shell;
507 argv[1] = "-c";
508 argv[2] = xstrdup(cmd);
509 argv[3] = NULL;
510
Damien Miller194fd902013-10-15 12:13:05 +1100511 execv(argv[0], argv);
512 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
513 /* Die with signal to make this error apparent to parent. */
514 signal(SIGTERM, SIG_DFL);
515 kill(getpid(), SIGTERM);
516 _exit(1);
517 }
518 /* Parent. */
519 if (pid < 0)
520 fatal("%s: fork: %.100s", __func__, strerror(errno));
521
522 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100523
524 while (waitpid(pid, &status, 0) == -1) {
525 if (errno != EINTR && errno != EAGAIN)
526 fatal("%s: waitpid: %s", __func__, strerror(errno));
527 }
528 if (!WIFEXITED(status)) {
529 error("command '%.100s' exited abnormally", cmd);
530 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000531 }
Damien Miller194fd902013-10-15 12:13:05 +1100532 debug3("command returned status %d", WEXITSTATUS(status));
533 return WEXITSTATUS(status);
534}
535
536/*
537 * Parse and execute a Match directive.
538 */
539static int
540match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000541 const char *host_arg, const char *original_host, int post_canon,
542 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100543{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000544 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100545 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000546 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100547 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000548 char uidstr[32];
Damien Miller194fd902013-10-15 12:13:05 +1100549
550 /*
551 * Configuration is likely to be incomplete at this point so we
552 * must be prepared to use default values.
553 */
554 port = options->port <= 0 ? default_ssh_port() : options->port;
555 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000556 if (post_canon) {
557 host = xstrdup(options->hostname);
558 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100559 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100560 host = percent_expand(options->hostname,
561 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000562 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100563 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000564 }
Damien Miller194fd902013-10-15 12:13:05 +1100565
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000566 debug2("checking match for '%s' host %s originally %s",
567 cp, host, original_host);
568 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
569 criteria = NULL;
570 this_result = 1;
571 if ((negate = attrib[0] == '!'))
572 attrib++;
573 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100574 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000575 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100576 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000577 error("%.200s line %d: '%s' cannot be combined "
578 "with other Match attributes",
579 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100580 result = -1;
581 goto out;
582 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 if (result)
584 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100585 goto out;
586 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000587 attributes++;
588 if (strcasecmp(attrib, "canonical") == 0) {
589 r = !!post_canon; /* force bitmask member to boolean */
590 if (r == (negate ? 1 : 0))
591 this_result = result = 0;
592 debug3("%.200s line %d: %smatched '%s'",
593 filename, linenum,
594 this_result ? "" : "not ", oattrib);
595 continue;
596 }
597 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100598 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
599 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100600 result = -1;
601 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100602 }
Damien Miller194fd902013-10-15 12:13:05 +1100603 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000604 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000605 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000606 if (r == (negate ? 1 : 0))
607 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100608 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000609 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000610 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 if (r == (negate ? 1 : 0))
612 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100613 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000614 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000615 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 if (r == (negate ? 1 : 0))
617 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100618 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000620 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 if (r == (negate ? 1 : 0))
622 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100623 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100624 if (gethostname(thishost, sizeof(thishost)) == -1)
625 fatal("gethostname: %s", strerror(errno));
626 strlcpy(shorthost, thishost, sizeof(shorthost));
627 shorthost[strcspn(thishost, ".")] = '\0';
628 snprintf(portstr, sizeof(portstr), "%d", port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000629 snprintf(uidstr, sizeof(uidstr), "%llu",
630 (unsigned long long)pw->pw_uid);
Damien Miller194fd902013-10-15 12:13:05 +1100631
632 cmd = percent_expand(arg,
633 "L", shorthost,
634 "d", pw->pw_dir,
635 "h", host,
636 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000637 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100638 "p", portstr,
639 "r", ruser,
640 "u", pw->pw_name,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000641 "i", uidstr,
Damien Miller194fd902013-10-15 12:13:05 +1100642 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100643 if (result != 1) {
644 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000645 debug3("%.200s line %d: skipped exec "
646 "\"%.100s\"", filename, linenum, cmd);
647 free(cmd);
648 continue;
Damien Miller06287802014-02-24 15:56:45 +1100649 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000650 r = execute_in_shell(cmd);
651 if (r == -1) {
652 fatal("%.200s line %d: match exec "
653 "'%.100s' error", filename,
654 linenum, cmd);
655 }
656 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100657 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000658 /* Force exit status to boolean */
659 r = r == 0;
660 if (r == (negate ? 1 : 0))
661 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100662 } else {
663 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100664 result = -1;
665 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100666 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000667 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
668 filename, linenum, this_result ? "": "not ",
669 oattrib, criteria);
670 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100671 }
Damien Millercf31f382013-10-24 21:02:56 +1100672 if (attributes == 0) {
673 error("One or more attributes required for Match");
674 result = -1;
675 goto out;
676 }
Damien Miller084bcd22013-10-23 16:30:51 +1100677 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000678 if (result != -1)
679 debug2("match %sfound", result ? "" : "not ");
680 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100681 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100682 return result;
683}
684
djm@openbsd.org555294a2018-04-06 13:02:39 +0000685/* Remove environment variable by pattern */
686static void
687rm_env(Options *options, const char *arg, const char *filename, int linenum)
688{
689 int i, j;
690 char *cp;
691
692 /* Remove an environment variable */
693 for (i = 0; i < options->num_send_env; ) {
694 cp = xstrdup(options->send_env[i]);
695 if (!match_pattern(cp, arg + 1)) {
696 free(cp);
697 i++;
698 continue;
699 }
700 debug3("%s line %d: removing environment %s",
701 filename, linenum, cp);
702 free(cp);
703 free(options->send_env[i]);
704 options->send_env[i] = NULL;
705 for (j = i; j < options->num_send_env - 1; j++) {
706 options->send_env[j] = options->send_env[j + 1];
707 options->send_env[j + 1] = NULL;
708 }
709 options->num_send_env--;
710 /* NB. don't increment i */
711 }
712}
713
Damien Miller5428f641999-11-25 11:54:57 +1100714/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000715 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100716 */
Damien Miller4af51302000-04-16 11:18:38 +1000717static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000718parse_token(const char *cp, const char *filename, int linenum,
719 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720{
Darren Tucker07636982013-05-16 20:30:03 +1000721 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Damien Miller95def091999-11-25 00:26:21 +1100723 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000724 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100725 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000726 if (ignored_unknown != NULL &&
727 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000728 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000729 error("%s: line %d: Bad configuration option: %s",
730 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100731 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732}
733
Damien Millere9fc72e2013-10-15 12:14:12 +1100734/* Multistate option parsing */
735struct multistate {
736 char *key;
737 int value;
738};
739static const struct multistate multistate_flag[] = {
740 { "true", 1 },
741 { "false", 0 },
742 { "yes", 1 },
743 { "no", 0 },
744 { NULL, -1 }
745};
746static const struct multistate multistate_yesnoask[] = {
747 { "true", 1 },
748 { "false", 0 },
749 { "yes", 1 },
750 { "no", 0 },
751 { "ask", 2 },
752 { NULL, -1 }
753};
djm@openbsd.org22376d22017-09-03 23:33:13 +0000754static const struct multistate multistate_strict_hostkey[] = {
755 { "true", SSH_STRICT_HOSTKEY_YES },
756 { "false", SSH_STRICT_HOSTKEY_OFF },
757 { "yes", SSH_STRICT_HOSTKEY_YES },
758 { "no", SSH_STRICT_HOSTKEY_OFF },
759 { "ask", SSH_STRICT_HOSTKEY_ASK },
760 { "off", SSH_STRICT_HOSTKEY_OFF },
761 { "accept-new", SSH_STRICT_HOSTKEY_NEW },
762 { NULL, -1 }
763};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000764static const struct multistate multistate_yesnoaskconfirm[] = {
765 { "true", 1 },
766 { "false", 0 },
767 { "yes", 1 },
768 { "no", 0 },
769 { "ask", 2 },
770 { "confirm", 3 },
771 { NULL, -1 }
772};
Damien Millere9fc72e2013-10-15 12:14:12 +1100773static const struct multistate multistate_addressfamily[] = {
774 { "inet", AF_INET },
775 { "inet6", AF_INET6 },
776 { "any", AF_UNSPEC },
777 { NULL, -1 }
778};
779static const struct multistate multistate_controlmaster[] = {
780 { "true", SSHCTL_MASTER_YES },
781 { "yes", SSHCTL_MASTER_YES },
782 { "false", SSHCTL_MASTER_NO },
783 { "no", SSHCTL_MASTER_NO },
784 { "auto", SSHCTL_MASTER_AUTO },
785 { "ask", SSHCTL_MASTER_ASK },
786 { "autoask", SSHCTL_MASTER_AUTO_ASK },
787 { NULL, -1 }
788};
789static const struct multistate multistate_tunnel[] = {
790 { "ethernet", SSH_TUNMODE_ETHERNET },
791 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
792 { "true", SSH_TUNMODE_DEFAULT },
793 { "yes", SSH_TUNMODE_DEFAULT },
794 { "false", SSH_TUNMODE_NO },
795 { "no", SSH_TUNMODE_NO },
796 { NULL, -1 }
797};
798static const struct multistate multistate_requesttty[] = {
799 { "true", REQUEST_TTY_YES },
800 { "yes", REQUEST_TTY_YES },
801 { "false", REQUEST_TTY_NO },
802 { "no", REQUEST_TTY_NO },
803 { "force", REQUEST_TTY_FORCE },
804 { "auto", REQUEST_TTY_AUTO },
805 { NULL, -1 }
806};
Damien Miller38505592013-10-17 11:48:13 +1100807static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100808 { "true", SSH_CANONICALISE_YES },
809 { "false", SSH_CANONICALISE_NO },
810 { "yes", SSH_CANONICALISE_YES },
811 { "no", SSH_CANONICALISE_NO },
812 { "always", SSH_CANONICALISE_ALWAYS },
813 { NULL, -1 }
814};
Damien Millere9fc72e2013-10-15 12:14:12 +1100815
Damien Miller5428f641999-11-25 11:54:57 +1100816/*
817 * Processes a single option line as used in the configuration files. This
818 * only sets those values that have not already been set.
819 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100820int
Damien Miller194fd902013-10-15 12:13:05 +1100821process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000822 const char *original_host, char *line, const char *filename,
823 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000825 return process_config_line_depth(options, pw, host, original_host,
826 line, filename, linenum, activep, flags, 0);
827}
828
829#define WHITESPACE " \t\r\n"
830static int
831process_config_line_depth(Options *options, struct passwd *pw, const char *host,
832 const char *original_host, char *line, const char *filename,
833 int linenum, int *activep, int flags, int depth)
834{
Damien Miller295ee632011-05-29 21:42:31 +1000835 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
836 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000837 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000838 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000839 int remotefwd, dynamicfwd;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100840 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000841 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000842 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100843 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000844 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100845 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100846 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000847 glob_t gl;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +0000848 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849
Damien Miller194fd902013-10-15 12:13:05 +1100850 if (activep == NULL) { /* We are processing a command line directive */
851 cmdline = 1;
852 activep = &cmdline;
853 }
854
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000855 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000856 if ((len = strlen(line)) == 0)
857 return 0;
858 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000859 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000860 break;
861 line[len] = '\0';
862 }
863
Damien Millerbe484b52000-07-15 14:14:16 +1000864 s = line;
865 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100866 if ((keyword = strdelim(&s)) == NULL)
867 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000868 /* Ignore leading whitespace. */
869 if (*keyword == '\0')
870 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000871 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100872 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000873 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100874 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
Darren Tucker07636982013-05-16 20:30:03 +1000876 opcode = parse_token(keyword, filename, linenum,
877 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
Damien Miller95def091999-11-25 00:26:21 +1100879 switch (opcode) {
880 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100881 /* don't panic, but count bad options */
882 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000883 case oIgnore:
884 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000885 case oIgnoredUnknownOption:
886 debug("%s line %d: Ignored unknown option \"%s\"",
887 filename, linenum, keyword);
888 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000889 case oConnectTimeout:
890 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100891parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000892 arg = strdelim(&s);
893 if (!arg || *arg == '\0')
894 fatal("%s line %d: missing time value.",
895 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000896 if (strcmp(arg, "none") == 0)
897 value = -1;
898 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000899 fatal("%s line %d: invalid time value.",
900 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100901 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000902 *intptr = value;
903 break;
904
Damien Miller95def091999-11-25 00:26:21 +1100905 case oForwardAgent:
906 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100907 parse_flag:
908 multistate_ptr = multistate_flag;
909 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000910 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000911 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100912 fatal("%s line %d: missing argument.",
913 filename, linenum);
914 value = -1;
915 for (i = 0; multistate_ptr[i].key != NULL; i++) {
916 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
917 value = multistate_ptr[i].value;
918 break;
919 }
920 }
921 if (value == -1)
922 fatal("%s line %d: unsupported option \"%s\".",
923 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100924 if (*activep && *intptr == -1)
925 *intptr = value;
926 break;
927
928 case oForwardX11:
929 intptr = &options->forward_x11;
930 goto parse_flag;
931
Darren Tucker0a118da2003-10-15 15:54:32 +1000932 case oForwardX11Trusted:
933 intptr = &options->forward_x11_trusted;
934 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000935
Damien Miller1ab6a512010-06-26 10:02:24 +1000936 case oForwardX11Timeout:
937 intptr = &options->forward_x11_timeout;
938 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000939
Damien Miller95def091999-11-25 00:26:21 +1100940 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000941 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100942 goto parse_flag;
943
Darren Tuckere7d4b192006-07-12 22:17:10 +1000944 case oExitOnForwardFailure:
945 intptr = &options->exit_on_forward_failure;
946 goto parse_flag;
947
Damien Miller95def091999-11-25 00:26:21 +1100948 case oPasswordAuthentication:
949 intptr = &options->password_authentication;
950 goto parse_flag;
951
Damien Miller874d77b2000-10-14 16:23:11 +1100952 case oKbdInteractiveAuthentication:
953 intptr = &options->kbd_interactive_authentication;
954 goto parse_flag;
955
956 case oKbdInteractiveDevices:
957 charptr = &options->kbd_interactive_devices;
958 goto parse_string;
959
Damien Miller0bc1bd82000-11-13 22:57:25 +1100960 case oPubkeyAuthentication:
961 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000962 goto parse_flag;
963
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000964 case oHostbasedAuthentication:
965 intptr = &options->hostbased_authentication;
966 goto parse_flag;
967
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000968 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000969 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100970 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000971
Darren Tucker0efd1552003-08-26 11:49:55 +1000972 case oGssAuthentication:
973 intptr = &options->gss_authentication;
974 goto parse_flag;
975
976 case oGssDelegateCreds:
977 intptr = &options->gss_deleg_creds;
978 goto parse_flag;
979
Damien Miller95def091999-11-25 00:26:21 +1100980 case oBatchMode:
981 intptr = &options->batch_mode;
982 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 case oCheckHostIP:
985 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000986 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller37876e92003-05-15 10:19:46 +1000988 case oVerifyHostKeyDNS:
989 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100990 multistate_ptr = multistate_yesnoask;
991 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000992
Damien Miller95def091999-11-25 00:26:21 +1100993 case oStrictHostKeyChecking:
994 intptr = &options->strict_host_key_checking;
djm@openbsd.org22376d22017-09-03 23:33:13 +0000995 multistate_ptr = multistate_strict_hostkey;
Damien Millere9fc72e2013-10-15 12:14:12 +1100996 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller95def091999-11-25 00:26:21 +1100998 case oCompression:
999 intptr = &options->compression;
1000 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
Damien Miller12c150e2003-12-17 16:31:10 +11001002 case oTCPKeepAlive:
1003 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001004 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001006 case oNoHostAuthenticationForLocalhost:
1007 intptr = &options->no_host_authentication_for_localhost;
1008 goto parse_flag;
1009
Damien Miller95def091999-11-25 00:26:21 +11001010 case oNumberOfPasswordPrompts:
1011 intptr = &options->number_of_password_prompts;
1012 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Millera5539d22003-04-09 20:50:06 +10001014 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001015 arg = strdelim(&s);
1016 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001017 fatal("%.200s line %d: Missing argument.", filename,
1018 linenum);
1019 if (strcmp(arg, "default") == 0) {
1020 val64 = 0;
1021 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001022 if (scan_scaled(arg, &val64) == -1)
1023 fatal("%.200s line %d: Bad number '%s': %s",
1024 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001025 if (val64 != 0 && val64 < 16)
1026 fatal("%.200s line %d: RekeyLimit too small",
1027 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001028 }
Damien Miller3dff1762008-02-10 22:25:52 +11001029 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001030 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001031 if (s != NULL) { /* optional rekey interval present */
1032 if (strcmp(s, "none") == 0) {
1033 (void)strdelim(&s); /* discard */
1034 break;
1035 }
1036 intptr = &options->rekey_interval;
1037 goto parse_time;
1038 }
Damien Millera5539d22003-04-09 20:50:06 +10001039 break;
1040
Damien Miller95def091999-11-25 00:26:21 +11001041 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001042 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001043 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001044 fatal("%.200s line %d: Missing argument.", filename, linenum);
1045 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001046 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001047 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001048 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001049 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001050 add_identity_file(options, NULL,
1051 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001052 }
1053 break;
1054
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001055 case oCertificateFile:
1056 arg = strdelim(&s);
1057 if (!arg || *arg == '\0')
1058 fatal("%.200s line %d: Missing argument.",
1059 filename, linenum);
1060 if (*activep) {
1061 intptr = &options->num_certificate_files;
1062 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1063 fatal("%.200s line %d: Too many certificate "
1064 "files specified (max %d).",
1065 filename, linenum,
1066 SSH_MAX_CERTIFICATE_FILES);
1067 }
1068 add_certificate_file(options, arg,
1069 flags & SSHCONF_USERCONF);
1070 }
1071 break;
1072
Damien Millerd3a18572000-06-07 19:55:44 +10001073 case oXAuthLocation:
1074 charptr=&options->xauth_location;
1075 goto parse_string;
1076
Damien Miller95def091999-11-25 00:26:21 +11001077 case oUser:
1078 charptr = &options->user;
1079parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001080 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001081 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001082 fatal("%.200s line %d: Missing argument.",
1083 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001084 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001085 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001086 break;
1087
1088 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001089 cpptr = (char **)&options->system_hostfiles;
1090 uintptr = &options->num_system_hostfiles;
1091 max_entries = SSH_MAX_HOSTS_FILES;
1092parse_char_array:
1093 if (*activep && *uintptr == 0) {
1094 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1095 if ((*uintptr) >= max_entries)
1096 fatal("%s line %d: "
1097 "too many authorized keys files.",
1098 filename, linenum);
1099 cpptr[(*uintptr)++] = xstrdup(arg);
1100 }
1101 }
1102 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001103
1104 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001105 cpptr = (char **)&options->user_hostfiles;
1106 uintptr = &options->num_user_hostfiles;
1107 max_entries = SSH_MAX_HOSTS_FILES;
1108 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001109
Damien Miller95def091999-11-25 00:26:21 +11001110 case oHostName:
1111 charptr = &options->hostname;
1112 goto parse_string;
1113
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001114 case oHostKeyAlias:
1115 charptr = &options->host_key_alias;
1116 goto parse_string;
1117
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001118 case oPreferredAuthentications:
1119 charptr = &options->preferred_authentications;
1120 goto parse_string;
1121
Ben Lindstrome0f88042001-04-30 13:06:24 +00001122 case oBindAddress:
1123 charptr = &options->bind_address;
1124 goto parse_string;
1125
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001126 case oBindInterface:
1127 charptr = &options->bind_interface;
1128 goto parse_string;
1129
Damien Miller7ea845e2010-02-12 09:21:02 +11001130 case oPKCS11Provider:
1131 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001132 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001133
Damien Miller95def091999-11-25 00:26:21 +11001134 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001135 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001136 /* Ignore ProxyCommand if ProxyJump already specified */
1137 if (options->jump_host != NULL)
1138 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001139parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001140 if (s == NULL)
1141 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001142 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001143 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001144 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001145 return 0;
1146
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001147 case oProxyJump:
1148 if (s == NULL) {
1149 fatal("%.200s line %d: Missing argument.",
1150 filename, linenum);
1151 }
1152 len = strspn(s, WHITESPACE "=");
1153 if (parse_jump(s + len, options, *activep) == -1) {
1154 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1155 filename, linenum, s + len);
1156 }
1157 return 0;
1158
Damien Miller95def091999-11-25 00:26:21 +11001159 case oPort:
1160 intptr = &options->port;
1161parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001162 arg = strdelim(&s);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001163 if ((errstr = atoi_err(arg, &value)) != NULL)
1164 fatal("%s line %d: integer value %s.",
1165 filename, linenum, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001166 if (*activep && *intptr == -1)
1167 *intptr = value;
1168 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169
Damien Miller95def091999-11-25 00:26:21 +11001170 case oConnectionAttempts:
1171 intptr = &options->connection_attempts;
1172 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001173
Damien Miller78928792000-04-12 20:17:38 +10001174 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001175 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001176 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001177 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001178 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001179 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001180 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001181 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001182 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001183 break;
1184
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001185 case oMacs:
1186 arg = strdelim(&s);
1187 if (!arg || *arg == '\0')
1188 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001189 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001190 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001191 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001192 if (*activep && options->macs == NULL)
1193 options->macs = xstrdup(arg);
1194 break;
1195
Damien Millerd5f62bf2010-09-24 22:11:14 +10001196 case oKexAlgorithms:
1197 arg = strdelim(&s);
1198 if (!arg || *arg == '\0')
1199 fatal("%.200s line %d: Missing argument.",
1200 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001201 if (*arg != '-' &&
1202 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001203 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1204 filename, linenum, arg ? arg : "<NONE>");
1205 if (*activep && options->kex_algorithms == NULL)
1206 options->kex_algorithms = xstrdup(arg);
1207 break;
1208
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001209 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001210 charptr = &options->hostkeyalgorithms;
1211parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001212 arg = strdelim(&s);
1213 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001214 fatal("%.200s line %d: Missing argument.",
1215 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001216 if (*arg != '-' &&
1217 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001218 fatal("%s line %d: Bad key types '%s'.",
1219 filename, linenum, arg ? arg : "<NONE>");
1220 if (*activep && *charptr == NULL)
1221 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001222 break;
1223
Damien Miller95def091999-11-25 00:26:21 +11001224 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001225 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001226 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001227 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001228 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001229 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001230 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001231 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1232 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001233 break;
1234
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001235 case oLogFacility:
1236 log_facility_ptr = &options->log_facility;
1237 arg = strdelim(&s);
1238 value = log_facility_number(arg);
1239 if (value == SYSLOG_FACILITY_NOT_SET)
1240 fatal("%.200s line %d: unsupported log facility '%s'",
1241 filename, linenum, arg ? arg : "<NONE>");
1242 if (*log_facility_ptr == -1)
1243 *log_facility_ptr = (SyslogFacility) value;
1244 break;
1245
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001246 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001247 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001248 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001249 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001250 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001251 fatal("%.200s line %d: Missing port argument.",
1252 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001253
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001254 remotefwd = (opcode == oRemoteForward);
1255 dynamicfwd = (opcode == oDynamicForward);
1256
1257 if (!dynamicfwd) {
Damien Millera699d952008-11-03 19:27:34 +11001258 arg2 = strdelim(&s);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001259 if (arg2 == NULL || *arg2 == '\0') {
1260 if (remotefwd)
1261 dynamicfwd = 1;
1262 else
1263 fatal("%.200s line %d: Missing target "
1264 "argument.", filename, linenum);
1265 } else {
1266 /* construct a string for parse_forward */
1267 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1268 arg2);
1269 }
Damien Millera699d952008-11-03 19:27:34 +11001270 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001271 if (dynamicfwd)
1272 strlcpy(fwdarg, arg, sizeof(fwdarg));
Damien Millera699d952008-11-03 19:27:34 +11001273
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001274 if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001275 fatal("%.200s line %d: Bad forwarding specification.",
1276 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001277
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001278 if (*activep) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001279 if (remotefwd) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001280 add_remote_forward(options, &fwd);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001281 } else {
1282 add_local_forward(options, &fwd);
1283 }
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001284 }
Damien Miller95def091999-11-25 00:26:21 +11001285 break;
1286
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001287 case oClearAllForwardings:
1288 intptr = &options->clear_forwardings;
1289 goto parse_flag;
1290
Damien Miller95def091999-11-25 00:26:21 +11001291 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001292 if (cmdline)
1293 fatal("Host directive not supported as a command-line "
1294 "option");
Damien Miller95def091999-11-25 00:26:21 +11001295 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001296 arg2 = NULL;
1297 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001298 if ((flags & SSHCONF_NEVERMATCH) != 0)
1299 break;
Damien Millerfe924212011-05-15 08:44:45 +10001300 negated = *arg == '!';
1301 if (negated)
1302 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001303 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001304 if (negated) {
1305 debug("%.200s line %d: Skipping Host "
1306 "block because of negated match "
1307 "for %.100s", filename, linenum,
1308 arg);
1309 *activep = 0;
1310 break;
1311 }
1312 if (!*activep)
1313 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001314 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001315 }
Damien Millerfe924212011-05-15 08:44:45 +10001316 }
1317 if (*activep)
1318 debug("%.200s line %d: Applying options for %.100s",
1319 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001320 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001321 return 0;
1322
Damien Miller194fd902013-10-15 12:13:05 +11001323 case oMatch:
1324 if (cmdline)
1325 fatal("Host directive not supported as a command-line "
1326 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001327 value = match_cfg_line(options, &s, pw, host, original_host,
1328 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001329 if (value < 0)
1330 fatal("%.200s line %d: Bad Match condition", filename,
1331 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001332 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001333 break;
1334
Damien Miller95def091999-11-25 00:26:21 +11001335 case oEscapeChar:
1336 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001337 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001338 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001339 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001340 if (strcmp(arg, "none") == 0)
1341 value = SSH_ESCAPECHAR_NONE;
1342 else if (arg[1] == '\0')
1343 value = (u_char) arg[0];
1344 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001345 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1346 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001347 else {
1348 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001349 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001350 /* NOTREACHED */
1351 value = 0; /* Avoid compiler warning. */
1352 }
1353 if (*activep && *intptr == -1)
1354 *intptr = value;
1355 break;
1356
Damien Miller20a8f972003-05-18 20:50:30 +10001357 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001358 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001359 multistate_ptr = multistate_addressfamily;
1360 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001361
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001362 case oEnableSSHKeysign:
1363 intptr = &options->enable_ssh_keysign;
1364 goto parse_flag;
1365
Damien Millerbd394c32004-03-08 23:12:36 +11001366 case oIdentitiesOnly:
1367 intptr = &options->identities_only;
1368 goto parse_flag;
1369
Damien Miller509b0102003-12-17 16:33:10 +11001370 case oServerAliveInterval:
1371 intptr = &options->server_alive_interval;
1372 goto parse_time;
1373
1374 case oServerAliveCountMax:
1375 intptr = &options->server_alive_count_max;
1376 goto parse_int;
1377
Darren Tucker46bc0752004-05-02 22:11:30 +10001378 case oSendEnv:
1379 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1380 if (strchr(arg, '=') != NULL)
1381 fatal("%s line %d: Invalid environment name.",
1382 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001383 if (!*activep)
1384 continue;
djm@openbsd.org555294a2018-04-06 13:02:39 +00001385 if (*arg == '-') {
1386 /* Removing an env var */
1387 rm_env(options, arg, filename, linenum);
1388 continue;
1389 } else {
1390 /* Adding an env var */
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001391 if (options->num_send_env >= INT_MAX)
djm@openbsd.org555294a2018-04-06 13:02:39 +00001392 fatal("%s line %d: too many send env.",
1393 filename, linenum);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001394 options->send_env = xrecallocarray(
1395 options->send_env, options->num_send_env,
djm@openbsd.org89a85d72018-06-10 23:45:41 +00001396 options->num_send_env + 1,
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001397 sizeof(*options->send_env));
djm@openbsd.org555294a2018-04-06 13:02:39 +00001398 options->send_env[options->num_send_env++] =
1399 xstrdup(arg);
1400 }
Darren Tucker46bc0752004-05-02 22:11:30 +10001401 }
1402 break;
1403
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001404 case oSetEnv:
1405 value = options->num_setenv;
1406 while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1407 if (strchr(arg, '=') == NULL)
1408 fatal("%s line %d: Invalid SetEnv.",
1409 filename, linenum);
1410 if (!*activep || value != 0)
1411 continue;
1412 /* Adding a setenv var */
1413 if (options->num_setenv >= INT_MAX)
1414 fatal("%s line %d: too many SetEnv.",
1415 filename, linenum);
1416 options->setenv = xrecallocarray(
1417 options->setenv, options->num_setenv,
1418 options->num_setenv + 1, sizeof(*options->setenv));
1419 options->setenv[options->num_setenv++] = xstrdup(arg);
1420 }
1421 break;
1422
Damien Miller0e220db2004-06-15 10:34:08 +10001423 case oControlPath:
1424 charptr = &options->control_path;
1425 goto parse_string;
1426
1427 case oControlMaster:
1428 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001429 multistate_ptr = multistate_controlmaster;
1430 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001431
Damien Millere11e1ea2010-08-03 16:04:46 +10001432 case oControlPersist:
1433 /* no/false/yes/true, or a time spec */
1434 intptr = &options->control_persist;
1435 arg = strdelim(&s);
1436 if (!arg || *arg == '\0')
1437 fatal("%.200s line %d: Missing ControlPersist"
1438 " argument.", filename, linenum);
1439 value = 0;
1440 value2 = 0; /* timeout */
1441 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1442 value = 0;
1443 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1444 value = 1;
1445 else if ((value2 = convtime(arg)) >= 0)
1446 value = 1;
1447 else
1448 fatal("%.200s line %d: Bad ControlPersist argument.",
1449 filename, linenum);
1450 if (*activep && *intptr == -1) {
1451 *intptr = value;
1452 options->control_persist_timeout = value2;
1453 }
1454 break;
1455
Damien Millere1776152005-03-01 21:47:37 +11001456 case oHashKnownHosts:
1457 intptr = &options->hash_known_hosts;
1458 goto parse_flag;
1459
Damien Millerd27b9472005-12-13 19:29:02 +11001460 case oTunnel:
1461 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001462 multistate_ptr = multistate_tunnel;
1463 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001464
1465 case oTunnelDevice:
1466 arg = strdelim(&s);
1467 if (!arg || *arg == '\0')
1468 fatal("%.200s line %d: Missing argument.", filename, linenum);
1469 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001470 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001471 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1472 if (*activep) {
1473 options->tun_local = value;
1474 options->tun_remote = value2;
1475 }
1476 break;
1477
1478 case oLocalCommand:
1479 charptr = &options->local_command;
1480 goto parse_command;
1481
1482 case oPermitLocalCommand:
1483 intptr = &options->permit_local_command;
1484 goto parse_flag;
1485
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001486 case oRemoteCommand:
1487 charptr = &options->remote_command;
1488 goto parse_command;
1489
Damien Miller10288242008-06-30 00:04:03 +10001490 case oVisualHostKey:
1491 intptr = &options->visual_host_key;
1492 goto parse_flag;
1493
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001494 case oInclude:
1495 if (cmdline)
1496 fatal("Include directive not supported as a "
1497 "command-line option");
1498 value = 0;
1499 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1500 /*
1501 * Ensure all paths are anchored. User configuration
1502 * files may begin with '~/' but system configurations
1503 * must not. If the path is relative, then treat it
1504 * as living in ~/.ssh for user configurations or
1505 * /etc/ssh for system ones.
1506 */
1507 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1508 fatal("%.200s line %d: bad include path %s.",
1509 filename, linenum, arg);
1510 if (*arg != '/' && *arg != '~') {
1511 xasprintf(&arg2, "%s/%s",
1512 (flags & SSHCONF_USERCONF) ?
1513 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1514 } else
1515 arg2 = xstrdup(arg);
1516 memset(&gl, 0, sizeof(gl));
1517 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1518 if (r == GLOB_NOMATCH) {
1519 debug("%.200s line %d: include %s matched no "
1520 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001521 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001522 continue;
1523 } else if (r != 0 || gl.gl_pathc < 0)
1524 fatal("%.200s line %d: glob failed for %s.",
1525 filename, linenum, arg2);
1526 free(arg2);
1527 oactive = *activep;
1528 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1529 debug3("%.200s line %d: Including file %s "
1530 "depth %d%s", filename, linenum,
1531 gl.gl_pathv[i], depth,
1532 oactive ? "" : " (parse only)");
1533 r = read_config_file_depth(gl.gl_pathv[i],
1534 pw, host, original_host, options,
1535 flags | SSHCONF_CHECKPERM |
1536 (oactive ? 0 : SSHCONF_NEVERMATCH),
1537 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001538 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001539 fatal("Can't open user config file "
1540 "%.100s: %.100s", gl.gl_pathv[i],
1541 strerror(errno));
1542 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001543 /*
1544 * don't let Match in includes clobber the
1545 * containing file's Match state.
1546 */
1547 *activep = oactive;
1548 if (r != 1)
1549 value = -1;
1550 }
1551 globfree(&gl);
1552 }
1553 if (value != 0)
1554 return value;
1555 break;
1556
Damien Miller0dac6fb2010-11-20 15:19:38 +11001557 case oIPQoS:
1558 arg = strdelim(&s);
1559 if ((value = parse_ipqos(arg)) == -1)
1560 fatal("%s line %d: Bad IPQoS value: %s",
1561 filename, linenum, arg);
1562 arg = strdelim(&s);
1563 if (arg == NULL)
1564 value2 = value;
1565 else if ((value2 = parse_ipqos(arg)) == -1)
1566 fatal("%s line %d: Bad IPQoS value: %s",
1567 filename, linenum, arg);
1568 if (*activep) {
1569 options->ip_qos_interactive = value;
1570 options->ip_qos_bulk = value2;
1571 }
1572 break;
1573
Damien Miller21771e22011-05-15 08:45:50 +10001574 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001575 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001576 multistate_ptr = multistate_requesttty;
1577 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001578
Darren Tucker07636982013-05-16 20:30:03 +10001579 case oIgnoreUnknown:
1580 charptr = &options->ignored_unknown;
1581 goto parse_string;
1582
Damien Miller1262b662013-08-21 02:44:24 +10001583 case oProxyUseFdpass:
1584 intptr = &options->proxy_use_fdpass;
1585 goto parse_flag;
1586
Damien Miller0faf7472013-10-17 11:47:23 +11001587 case oCanonicalDomains:
1588 value = options->num_canonical_domains != 0;
1589 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001590 if (!valid_domain(arg, 1, &errstr)) {
1591 fatal("%s line %d: %s", filename, linenum,
1592 errstr);
1593 }
Damien Miller0faf7472013-10-17 11:47:23 +11001594 if (!*activep || value)
1595 continue;
1596 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1597 fatal("%s line %d: too many hostname suffixes.",
1598 filename, linenum);
1599 options->canonical_domains[
1600 options->num_canonical_domains++] = xstrdup(arg);
1601 }
1602 break;
1603
Damien Miller38505592013-10-17 11:48:13 +11001604 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001605 value = options->num_permitted_cnames != 0;
1606 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1607 /* Either '*' for everything or 'list:list' */
1608 if (strcmp(arg, "*") == 0)
1609 arg2 = arg;
1610 else {
1611 lowercase(arg);
1612 if ((arg2 = strchr(arg, ':')) == NULL ||
1613 arg2[1] == '\0') {
1614 fatal("%s line %d: "
1615 "Invalid permitted CNAME \"%s\"",
1616 filename, linenum, arg);
1617 }
1618 *arg2 = '\0';
1619 arg2++;
1620 }
1621 if (!*activep || value)
1622 continue;
1623 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1624 fatal("%s line %d: too many permitted CNAMEs.",
1625 filename, linenum);
1626 cname = options->permitted_cnames +
1627 options->num_permitted_cnames++;
1628 cname->source_list = xstrdup(arg);
1629 cname->target_list = xstrdup(arg2);
1630 }
1631 break;
1632
Damien Miller38505592013-10-17 11:48:13 +11001633 case oCanonicalizeHostname:
1634 intptr = &options->canonicalize_hostname;
1635 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001636 goto parse_multistate;
1637
Damien Miller38505592013-10-17 11:48:13 +11001638 case oCanonicalizeMaxDots:
1639 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001640 goto parse_int;
1641
Damien Miller38505592013-10-17 11:48:13 +11001642 case oCanonicalizeFallbackLocal:
1643 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001644 goto parse_flag;
1645
Damien Miller7acefbb2014-07-18 14:11:24 +10001646 case oStreamLocalBindMask:
1647 arg = strdelim(&s);
1648 if (!arg || *arg == '\0')
1649 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1650 /* Parse mode in octal format */
1651 value = strtol(arg, &endofnumber, 8);
1652 if (arg == endofnumber || value < 0 || value > 0777)
1653 fatal("%.200s line %d: Bad mask.", filename, linenum);
1654 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1655 break;
1656
1657 case oStreamLocalBindUnlink:
1658 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1659 goto parse_flag;
1660
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001661 case oRevokedHostKeys:
1662 charptr = &options->revoked_host_keys;
1663 goto parse_string;
1664
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001665 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001666 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001667 arg = strdelim(&s);
1668 if (!arg || *arg == '\0')
1669 fatal("%.200s line %d: Missing argument.",
1670 filename, linenum);
1671 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1672 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1673 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001674 if (*activep && *intptr == -1)
1675 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001676 break;
1677
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001678 case oUpdateHostkeys:
1679 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001680 multistate_ptr = multistate_yesnoask;
1681 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001682
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001683 case oHostbasedKeyTypes:
1684 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001685 goto parse_keytypes;
1686
1687 case oPubkeyAcceptedKeyTypes:
1688 charptr = &options->pubkey_key_types;
1689 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001690
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001691 case oAddKeysToAgent:
1692 intptr = &options->add_keys_to_agent;
1693 multistate_ptr = multistate_yesnoaskconfirm;
1694 goto parse_multistate;
1695
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001696 case oIdentityAgent:
1697 charptr = &options->identity_agent;
1698 goto parse_string;
1699
Ben Lindstrom4daea862002-06-09 20:04:02 +00001700 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001701 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001702 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001703 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001704
Damien Millerf9b3feb2003-05-16 11:38:32 +10001705 case oUnsupported:
1706 error("%s line %d: Unsupported option \"%s\"",
1707 filename, linenum, keyword);
1708 return 0;
1709
Damien Miller95def091999-11-25 00:26:21 +11001710 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001711 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001712 }
1713
1714 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001715 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001716 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001717 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001718 }
Damien Miller95def091999-11-25 00:26:21 +11001719 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001720}
1721
Damien Miller5428f641999-11-25 11:54:57 +11001722/*
1723 * Reads the config file and modifies the options accordingly. Options
1724 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001725 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001726 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001727int
Damien Miller194fd902013-10-15 12:13:05 +11001728read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001729 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001730{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001731 int active = 1;
1732
1733 return read_config_file_depth(filename, pw, host, original_host,
1734 options, flags, &active, 0);
1735}
1736
1737#define READCONF_MAX_DEPTH 16
1738static int
1739read_config_file_depth(const char *filename, struct passwd *pw,
1740 const char *host, const char *original_host, Options *options,
1741 int flags, int *activep, int depth)
1742{
Damien Miller95def091999-11-25 00:26:21 +11001743 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001744 char *line = NULL;
1745 size_t linesize = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001746 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001747 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001749 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1750 fatal("Too many recursive configuration includes");
1751
Damien Miller57a44762004-04-20 20:11:57 +10001752 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001753 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001754
Darren Tuckeraefa3682013-04-05 11:18:35 +11001755 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001756 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001757
Damien Miller33793852004-06-15 10:27:55 +10001758 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001759 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001760 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001761 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001762 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001763 }
1764
Damien Miller95def091999-11-25 00:26:21 +11001765 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001766
Damien Miller5428f641999-11-25 11:54:57 +11001767 /*
1768 * Mark that we are now processing the options. This flag is turned
1769 * on/off by Host specifications.
1770 */
Damien Miller95def091999-11-25 00:26:21 +11001771 linenum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00001772 while (getline(&line, &linesize, f) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001773 /* Update line number counter. */
1774 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001775 if (process_config_line_depth(options, pw, host, original_host,
1776 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001777 bad_options++;
1778 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00001779 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001780 fclose(f);
1781 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001782 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001783 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001784 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001785}
1786
Damien Miller13f97b22014-02-24 15:57:55 +11001787/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1788int
1789option_clear_or_none(const char *o)
1790{
1791 return o == NULL || strcasecmp(o, "none") == 0;
1792}
1793
Damien Miller5428f641999-11-25 11:54:57 +11001794/*
1795 * Initializes options to special values that indicate that they have not yet
1796 * been set. Read_config_file will only set options with this value. Options
1797 * are processed in the following order: command line, user config file,
1798 * system config file. Last, fill_default_options is called.
1799 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001800
Damien Miller4af51302000-04-16 11:18:38 +10001801void
Damien Miller95def091999-11-25 00:26:21 +11001802initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001803{
Damien Miller95def091999-11-25 00:26:21 +11001804 memset(options, 'X', sizeof(*options));
1805 options->forward_agent = -1;
1806 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001807 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001808 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001809 options->stdio_forward_host = NULL;
1810 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001811 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001812 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001813 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001814 options->fwd_opts.gateway_ports = -1;
1815 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1816 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001817 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001818 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001819 options->gss_authentication = -1;
1820 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001821 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001822 options->kbd_interactive_authentication = -1;
1823 options->kbd_interactive_devices = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001824 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001825 options->batch_mode = -1;
1826 options->check_host_ip = -1;
1827 options->strict_host_key_checking = -1;
1828 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001829 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001830 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001831 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001832 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001833 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001834 options->number_of_password_prompts = -1;
Damien Miller78928792000-04-12 20:17:38 +10001835 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001836 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001837 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001838 options->hostkeyalgorithms = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001839 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001840 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001841 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001842 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001843 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001844 options->jump_user = NULL;
1845 options->jump_host = NULL;
1846 options->jump_port = -1;
1847 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001848 options->user = NULL;
1849 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001850 options->num_system_hostfiles = 0;
1851 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001852 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001853 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001854 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001855 options->num_remote_forwards = 0;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001856 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001857 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001858 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001859 options->bind_address = NULL;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001860 options->bind_interface = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001861 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001862 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001863 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001864 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001865 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001866 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001867 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001868 options->server_alive_interval = -1;
1869 options->server_alive_count_max = -1;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001870 options->send_env = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +10001871 options->num_send_env = 0;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001872 options->setenv = NULL;
1873 options->num_setenv = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001874 options->control_path = NULL;
1875 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001876 options->control_persist = -1;
1877 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001878 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001879 options->tun_open = -1;
1880 options->tun_local = -1;
1881 options->tun_remote = -1;
1882 options->local_command = NULL;
1883 options->permit_local_command = -1;
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001884 options->remote_command = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001885 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001886 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001887 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001888 options->ip_qos_interactive = -1;
1889 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001890 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001891 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001892 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001893 options->num_canonical_domains = 0;
1894 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001895 options->canonicalize_max_dots = -1;
1896 options->canonicalize_fallback_local = -1;
1897 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001898 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001899 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001900 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001901 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001902 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001903}
1904
Damien Miller5428f641999-11-25 11:54:57 +11001905/*
Damien Miller13f97b22014-02-24 15:57:55 +11001906 * A petite version of fill_default_options() that just fills the options
1907 * needed for hostname canonicalization to proceed.
1908 */
1909void
1910fill_default_options_for_canonicalization(Options *options)
1911{
1912 if (options->canonicalize_max_dots == -1)
1913 options->canonicalize_max_dots = 1;
1914 if (options->canonicalize_fallback_local == -1)
1915 options->canonicalize_fallback_local = 1;
1916 if (options->canonicalize_hostname == -1)
1917 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1918}
1919
1920/*
Damien Miller5428f641999-11-25 11:54:57 +11001921 * Called after processing other sources of option data, this fills those
1922 * options for which no value has been specified with their default values.
1923 */
Damien Miller4af51302000-04-16 11:18:38 +10001924void
Damien Miller95def091999-11-25 00:26:21 +11001925fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001926{
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001927 char *all_cipher, *all_mac, *all_kex, *all_key;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00001928 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001929
Damien Miller95def091999-11-25 00:26:21 +11001930 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001931 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001932 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001933 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001934 if (options->forward_x11_trusted == -1)
1935 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001936 if (options->forward_x11_timeout == -1)
1937 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001938 /*
1939 * stdio forwarding (-W) changes the default for these but we defer
1940 * setting the values so they can be overridden.
1941 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001942 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001943 options->exit_on_forward_failure =
1944 options->stdio_forward_host != NULL ? 1 : 0;
1945 if (options->clear_forwardings == -1)
1946 options->clear_forwardings =
1947 options->stdio_forward_host != NULL ? 1 : 0;
1948 if (options->clear_forwardings == 1)
1949 clear_forwardings(options);
1950
Damien Millerd3a18572000-06-07 19:55:44 +10001951 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001952 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001953 if (options->fwd_opts.gateway_ports == -1)
1954 options->fwd_opts.gateway_ports = 0;
1955 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1956 options->fwd_opts.streamlocal_bind_mask = 0177;
1957 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1958 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001959 if (options->pubkey_authentication == -1)
1960 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001961 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001962 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001963 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001964 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001965 if (options->gss_deleg_creds == -1)
1966 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001967 if (options->password_authentication == -1)
1968 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001969 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001970 options->kbd_interactive_authentication = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001971 if (options->hostbased_authentication == -1)
1972 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001973 if (options->batch_mode == -1)
1974 options->batch_mode = 0;
1975 if (options->check_host_ip == -1)
1976 options->check_host_ip = 1;
1977 if (options->strict_host_key_checking == -1)
djm@openbsd.org22376d22017-09-03 23:33:13 +00001978 options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
Damien Miller95def091999-11-25 00:26:21 +11001979 if (options->compression == -1)
1980 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001981 if (options->tcp_keep_alive == -1)
1982 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001983 if (options->port == -1)
1984 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001985 if (options->address_family == -1)
1986 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001987 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001988 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001989 if (options->number_of_password_prompts == -1)
1990 options->number_of_password_prompts = 3;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001991 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001992 if (options->add_keys_to_agent == -1)
1993 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001994 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001995 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1996 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001997#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001998 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001999#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00002000 add_identity_file(options, "~/",
2001 _PATH_SSH_CLIENT_ID_ED25519, 0);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002002 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10002003 }
Damien Miller95def091999-11-25 00:26:21 +11002004 if (options->escape_char == -1)
2005 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002006 if (options->num_system_hostfiles == 0) {
2007 options->system_hostfiles[options->num_system_hostfiles++] =
2008 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2009 options->system_hostfiles[options->num_system_hostfiles++] =
2010 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2011 }
2012 if (options->num_user_hostfiles == 0) {
2013 options->user_hostfiles[options->num_user_hostfiles++] =
2014 xstrdup(_PATH_SSH_USER_HOSTFILE);
2015 options->user_hostfiles[options->num_user_hostfiles++] =
2016 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2017 }
Damien Millerfcd93202002-02-05 12:26:34 +11002018 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002019 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002020 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2021 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002022 if (options->no_host_authentication_for_localhost == - 1)
2023 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002024 if (options->identities_only == -1)
2025 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002026 if (options->enable_ssh_keysign == -1)
2027 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002028 if (options->rekey_limit == -1)
2029 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002030 if (options->rekey_interval == -1)
2031 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002032 if (options->verify_host_key_dns == -1)
2033 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002034 if (options->server_alive_interval == -1)
2035 options->server_alive_interval = 0;
2036 if (options->server_alive_count_max == -1)
2037 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002038 if (options->control_master == -1)
2039 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002040 if (options->control_persist == -1) {
2041 options->control_persist = 0;
2042 options->control_persist_timeout = 0;
2043 }
Damien Millere1776152005-03-01 21:47:37 +11002044 if (options->hash_known_hosts == -1)
2045 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002046 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002047 options->tun_open = SSH_TUNMODE_NO;
2048 if (options->tun_local == -1)
2049 options->tun_local = SSH_TUNID_ANY;
2050 if (options->tun_remote == -1)
2051 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002052 if (options->permit_local_command == -1)
2053 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002054 if (options->visual_host_key == -1)
2055 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002056 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002057 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002058 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +00002059 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller21771e22011-05-15 08:45:50 +10002060 if (options->request_tty == -1)
2061 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002062 if (options->proxy_use_fdpass == -1)
2063 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002064 if (options->canonicalize_max_dots == -1)
2065 options->canonicalize_max_dots = 1;
2066 if (options->canonicalize_fallback_local == -1)
2067 options->canonicalize_fallback_local = 1;
2068 if (options->canonicalize_hostname == -1)
2069 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002070 if (options->fingerprint_hash == -1)
2071 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002072 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002073 options->update_hostkeys = 0;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002074
2075 /* Expand KEX name lists */
2076 all_cipher = cipher_alg_list(',', 0);
2077 all_mac = mac_alg_list(',');
2078 all_kex = kex_alg_list(',');
2079 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +00002080#define ASSEMBLE(what, defaults, all) \
2081 do { \
2082 if ((r = kex_assemble_names(&options->what, \
2083 defaults, all)) != 0) \
2084 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
2085 } while (0)
2086 ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
2087 ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
2088 ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
2089 ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
2090 ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
2091#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002092 free(all_cipher);
2093 free(all_mac);
2094 free(all_kex);
2095 free(all_key);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002096
Damien Millere9fc72e2013-10-15 12:14:12 +11002097#define CLEAR_ON_NONE(v) \
2098 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002099 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002100 free(v); \
2101 v = NULL; \
2102 } \
2103 } while(0)
2104 CLEAR_ON_NONE(options->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002105 CLEAR_ON_NONE(options->remote_command);
Damien Millere9fc72e2013-10-15 12:14:12 +11002106 CLEAR_ON_NONE(options->proxy_command);
2107 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002108 CLEAR_ON_NONE(options->revoked_host_keys);
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002109 if (options->jump_host != NULL &&
2110 strcmp(options->jump_host, "none") == 0 &&
2111 options->jump_port == 0 && options->jump_user == NULL) {
2112 free(options->jump_host);
2113 options->jump_host = NULL;
2114 }
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002115 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002116 /* options->user will be set in the main program if appropriate */
2117 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002118 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002119 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002120}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002121
Damien Miller7acefbb2014-07-18 14:11:24 +10002122struct fwdarg {
2123 char *arg;
2124 int ispath;
2125};
2126
2127/*
2128 * parse_fwd_field
2129 * parses the next field in a port forwarding specification.
2130 * sets fwd to the parsed field and advances p past the colon
2131 * or sets it to NULL at end of string.
2132 * returns 0 on success, else non-zero.
2133 */
2134static int
2135parse_fwd_field(char **p, struct fwdarg *fwd)
2136{
2137 char *ep, *cp = *p;
2138 int ispath = 0;
2139
2140 if (*cp == '\0') {
2141 *p = NULL;
2142 return -1; /* end of string */
2143 }
2144
2145 /*
2146 * A field escaped with square brackets is used literally.
2147 * XXX - allow ']' to be escaped via backslash?
2148 */
2149 if (*cp == '[') {
2150 /* find matching ']' */
2151 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2152 if (*ep == '/')
2153 ispath = 1;
2154 }
2155 /* no matching ']' or not at end of field. */
2156 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2157 return -1;
2158 /* NUL terminate the field and advance p past the colon */
2159 *ep++ = '\0';
2160 if (*ep != '\0')
2161 *ep++ = '\0';
2162 fwd->arg = cp + 1;
2163 fwd->ispath = ispath;
2164 *p = ep;
2165 return 0;
2166 }
2167
2168 for (cp = *p; *cp != '\0'; cp++) {
2169 switch (*cp) {
2170 case '\\':
2171 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002172 if (*cp == '\0')
2173 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002174 break;
2175 case '/':
2176 ispath = 1;
2177 break;
2178 case ':':
2179 *cp++ = '\0';
2180 goto done;
2181 }
2182 }
2183done:
2184 fwd->arg = *p;
2185 fwd->ispath = ispath;
2186 *p = cp;
2187 return 0;
2188}
2189
Damien Millerf91ee4c2005-03-01 21:24:33 +11002190/*
2191 * parse_forward
2192 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002193 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002194 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2195 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002196 * dynamicfwd == 1
2197 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002198 * returns number of arguments parsed or zero on error
2199 */
2200int
Damien Miller7acefbb2014-07-18 14:11:24 +10002201parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002202{
Damien Miller7acefbb2014-07-18 14:11:24 +10002203 struct fwdarg fwdargs[4];
2204 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002205 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002206
Damien Miller7acefbb2014-07-18 14:11:24 +10002207 memset(fwd, 0, sizeof(*fwd));
2208 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002209
2210 cp = p = xstrdup(fwdspec);
2211
2212 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002213 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002214 cp++;
2215
Damien Miller7acefbb2014-07-18 14:11:24 +10002216 for (i = 0; i < 4; ++i) {
2217 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002218 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002219 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002220
Damien Millerf4b39532008-11-03 19:28:21 +11002221 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002222 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002223 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002224 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002225
2226 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002227 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002228 if (fwdargs[0].ispath) {
2229 fwd->listen_path = xstrdup(fwdargs[0].arg);
2230 fwd->listen_port = PORT_STREAMLOCAL;
2231 } else {
2232 fwd->listen_host = NULL;
2233 fwd->listen_port = a2port(fwdargs[0].arg);
2234 }
Damien Millera699d952008-11-03 19:27:34 +11002235 fwd->connect_host = xstrdup("socks");
2236 break;
2237
2238 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002239 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2240 fwd->listen_path = xstrdup(fwdargs[0].arg);
2241 fwd->listen_port = PORT_STREAMLOCAL;
2242 fwd->connect_path = xstrdup(fwdargs[1].arg);
2243 fwd->connect_port = PORT_STREAMLOCAL;
2244 } else if (fwdargs[1].ispath) {
2245 fwd->listen_host = NULL;
2246 fwd->listen_port = a2port(fwdargs[0].arg);
2247 fwd->connect_path = xstrdup(fwdargs[1].arg);
2248 fwd->connect_port = PORT_STREAMLOCAL;
2249 } else {
2250 fwd->listen_host = xstrdup(fwdargs[0].arg);
2251 fwd->listen_port = a2port(fwdargs[1].arg);
2252 fwd->connect_host = xstrdup("socks");
2253 }
Damien Millera699d952008-11-03 19:27:34 +11002254 break;
2255
Damien Millerf91ee4c2005-03-01 21:24:33 +11002256 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002257 if (fwdargs[0].ispath) {
2258 fwd->listen_path = xstrdup(fwdargs[0].arg);
2259 fwd->listen_port = PORT_STREAMLOCAL;
2260 fwd->connect_host = xstrdup(fwdargs[1].arg);
2261 fwd->connect_port = a2port(fwdargs[2].arg);
2262 } else if (fwdargs[2].ispath) {
2263 fwd->listen_host = xstrdup(fwdargs[0].arg);
2264 fwd->listen_port = a2port(fwdargs[1].arg);
2265 fwd->connect_path = xstrdup(fwdargs[2].arg);
2266 fwd->connect_port = PORT_STREAMLOCAL;
2267 } else {
2268 fwd->listen_host = NULL;
2269 fwd->listen_port = a2port(fwdargs[0].arg);
2270 fwd->connect_host = xstrdup(fwdargs[1].arg);
2271 fwd->connect_port = a2port(fwdargs[2].arg);
2272 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002273 break;
2274
2275 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002276 fwd->listen_host = xstrdup(fwdargs[0].arg);
2277 fwd->listen_port = a2port(fwdargs[1].arg);
2278 fwd->connect_host = xstrdup(fwdargs[2].arg);
2279 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002280 break;
2281 default:
2282 i = 0; /* failure */
2283 }
2284
Darren Tuckera627d422013-06-02 07:31:17 +10002285 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002286
Damien Millera699d952008-11-03 19:27:34 +11002287 if (dynamicfwd) {
2288 if (!(i == 1 || i == 2))
2289 goto fail_free;
2290 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002291 if (!(i == 3 || i == 4)) {
2292 if (fwd->connect_path == NULL &&
2293 fwd->listen_path == NULL)
2294 goto fail_free;
2295 }
2296 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002297 goto fail_free;
2298 }
2299
Damien Miller7acefbb2014-07-18 14:11:24 +10002300 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2301 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002302 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002303 if (fwd->connect_host != NULL &&
2304 strlen(fwd->connect_host) >= NI_MAXHOST)
2305 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002306 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2307 if (fwd->connect_path != NULL &&
2308 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2309 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002310 if (fwd->listen_host != NULL &&
2311 strlen(fwd->listen_host) >= NI_MAXHOST)
2312 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002313 if (fwd->listen_path != NULL &&
2314 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2315 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002316
2317 return (i);
2318
2319 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002320 free(fwd->connect_host);
2321 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002322 free(fwd->connect_path);
2323 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002324 free(fwd->listen_host);
2325 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002326 free(fwd->listen_path);
2327 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002328 return (0);
2329}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002330
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002331int
2332parse_jump(const char *s, Options *o, int active)
2333{
2334 char *orig, *sdup, *cp;
2335 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002336 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002337
2338 active &= o->proxy_command == NULL && o->jump_host == NULL;
2339
2340 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002341 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002342 do {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002343 if (strcasecmp(s, "none") == 0)
2344 break;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002345 if ((cp = strrchr(sdup, ',')) == NULL)
2346 cp = sdup; /* last */
2347 else
2348 *cp++ = '\0';
2349
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002350 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002351 /* First argument and configuration is active */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002352 if (parse_ssh_uri(cp, &user, &host, &port) == -1 ||
2353 parse_user_host_port(cp, &user, &host, &port) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002354 goto out;
2355 } else {
2356 /* Subsequent argument or inactive configuration */
millert@openbsd.org887669e2017-10-21 23:06:24 +00002357 if (parse_ssh_uri(cp, NULL, NULL, NULL) == -1 ||
2358 parse_user_host_port(cp, NULL, NULL, NULL) != 0)
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002359 goto out;
2360 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002361 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002362 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002363 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002364 if (active) {
djm@openbsd.org4b22fd82018-05-22 00:13:26 +00002365 if (strcasecmp(s, "none") == 0) {
2366 o->jump_host = xstrdup("none");
2367 o->jump_port = 0;
2368 } else {
2369 o->jump_user = user;
2370 o->jump_host = host;
2371 o->jump_port = port;
2372 o->proxy_command = xstrdup("none");
2373 user = host = NULL;
2374 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2375 o->jump_extra = xstrdup(s);
2376 o->jump_extra[cp - s] = '\0';
2377 }
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002378 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002379 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002380 ret = 0;
2381 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002382 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002383 free(user);
2384 free(host);
2385 return ret;
2386}
2387
millert@openbsd.org887669e2017-10-21 23:06:24 +00002388int
2389parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
2390{
2391 char *path;
2392 int r;
2393
2394 r = parse_uri("ssh", uri, userp, hostp, portp, &path);
2395 if (r == 0 && path != NULL)
2396 r = -1; /* path not allowed */
2397 return r;
2398}
2399
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002400/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2401static const char *
2402fmt_multistate_int(int val, const struct multistate *m)
2403{
2404 u_int i;
2405
2406 for (i = 0; m[i].key != NULL; i++) {
2407 if (m[i].value == val)
2408 return m[i].key;
2409 }
2410 return "UNKNOWN";
2411}
2412
2413static const char *
2414fmt_intarg(OpCodes code, int val)
2415{
2416 if (val == -1)
2417 return "unset";
2418 switch (code) {
2419 case oAddressFamily:
2420 return fmt_multistate_int(val, multistate_addressfamily);
2421 case oVerifyHostKeyDNS:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002422 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002423 return fmt_multistate_int(val, multistate_yesnoask);
djm@openbsd.org22376d22017-09-03 23:33:13 +00002424 case oStrictHostKeyChecking:
2425 return fmt_multistate_int(val, multistate_strict_hostkey);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002426 case oControlMaster:
2427 return fmt_multistate_int(val, multistate_controlmaster);
2428 case oTunnel:
2429 return fmt_multistate_int(val, multistate_tunnel);
2430 case oRequestTTY:
2431 return fmt_multistate_int(val, multistate_requesttty);
2432 case oCanonicalizeHostname:
2433 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002434 case oAddKeysToAgent:
2435 return fmt_multistate_int(val, multistate_yesnoaskconfirm);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002436 case oFingerprintHash:
2437 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002438 default:
2439 switch (val) {
2440 case 0:
2441 return "no";
2442 case 1:
2443 return "yes";
2444 default:
2445 return "UNKNOWN";
2446 }
2447 }
2448}
2449
2450static const char *
2451lookup_opcode_name(OpCodes code)
2452{
2453 u_int i;
2454
2455 for (i = 0; keywords[i].name != NULL; i++)
2456 if (keywords[i].opcode == code)
2457 return(keywords[i].name);
2458 return "UNKNOWN";
2459}
2460
2461static void
2462dump_cfg_int(OpCodes code, int val)
2463{
2464 printf("%s %d\n", lookup_opcode_name(code), val);
2465}
2466
2467static void
2468dump_cfg_fmtint(OpCodes code, int val)
2469{
2470 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2471}
2472
2473static void
2474dump_cfg_string(OpCodes code, const char *val)
2475{
2476 if (val == NULL)
2477 return;
2478 printf("%s %s\n", lookup_opcode_name(code), val);
2479}
2480
2481static void
2482dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2483{
2484 u_int i;
2485
2486 for (i = 0; i < count; i++)
2487 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2488}
2489
2490static void
2491dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2492{
2493 u_int i;
2494
2495 printf("%s", lookup_opcode_name(code));
2496 for (i = 0; i < count; i++)
2497 printf(" %s", vals[i]);
2498 printf("\n");
2499}
2500
2501static void
2502dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2503{
2504 const struct Forward *fwd;
2505 u_int i;
2506
2507 /* oDynamicForward */
2508 for (i = 0; i < count; i++) {
2509 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002510 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002511 strcmp(fwd->connect_host, "socks") != 0)
2512 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002513 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002514 strcmp(fwd->connect_host, "socks") == 0)
2515 continue;
2516 printf("%s", lookup_opcode_name(code));
2517 if (fwd->listen_port == PORT_STREAMLOCAL)
2518 printf(" %s", fwd->listen_path);
2519 else if (fwd->listen_host == NULL)
2520 printf(" %d", fwd->listen_port);
2521 else {
2522 printf(" [%s]:%d",
2523 fwd->listen_host, fwd->listen_port);
2524 }
2525 if (code != oDynamicForward) {
2526 if (fwd->connect_port == PORT_STREAMLOCAL)
2527 printf(" %s", fwd->connect_path);
2528 else if (fwd->connect_host == NULL)
2529 printf(" %d", fwd->connect_port);
2530 else {
2531 printf(" [%s]:%d",
2532 fwd->connect_host, fwd->connect_port);
2533 }
2534 }
2535 printf("\n");
2536 }
2537}
2538
2539void
2540dump_client_config(Options *o, const char *host)
2541{
2542 int i;
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002543 char buf[8], *all_key;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002544
djm@openbsd.org60a92472015-08-21 23:53:08 +00002545 /* This is normally prepared in ssh_kex2 */
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002546 all_key = sshkey_alg_list(0, 0, 1, ',');
2547 if (kex_assemble_names( &o->hostkeyalgorithms,
2548 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.org60a92472015-08-21 23:53:08 +00002549 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +00002550 free(all_key);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002551
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002552 /* Most interesting options first: user, host, port */
2553 dump_cfg_string(oUser, o->user);
2554 dump_cfg_string(oHostName, host);
2555 dump_cfg_int(oPort, o->port);
2556
2557 /* Flag options */
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002558 dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002559 dump_cfg_fmtint(oAddressFamily, o->address_family);
2560 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2561 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2562 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2563 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2564 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2565 dump_cfg_fmtint(oCompression, o->compression);
2566 dump_cfg_fmtint(oControlMaster, o->control_master);
2567 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002568 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002569 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002570 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002571 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2572 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2573 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2574 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2575#ifdef GSSAPI
2576 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2577 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2578#endif /* GSSAPI */
2579 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2580 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2581 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2582 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2583 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2584 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2585 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002586 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2587 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2588 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002589 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2590 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2591 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2592 dump_cfg_fmtint(oTunnel, o->tun_open);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002593 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2594 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002595 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002596
2597 /* Integer options */
2598 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002599 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2600 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2601 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2602 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2603 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2604
2605 /* String options */
2606 dump_cfg_string(oBindAddress, o->bind_address);
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00002607 dump_cfg_string(oBindInterface, o->bind_interface);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002608 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2609 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002610 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002611 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002612 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002613 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002614 dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002615 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2616 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2617 dump_cfg_string(oLocalCommand, o->local_command);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00002618 dump_cfg_string(oRemoteCommand, o->remote_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002619 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2620 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002621#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002622 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002623#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002624 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002625 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002626 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002627 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002628
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002629 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002630 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2631 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2632 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2633
2634 /* String array options */
2635 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2636 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002637 dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002638 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2639 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2640 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002641 dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002642
2643 /* Special cases */
2644
2645 /* oConnectTimeout */
2646 if (o->connection_timeout == -1)
2647 printf("connecttimeout none\n");
2648 else
2649 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2650
2651 /* oTunnelDevice */
2652 printf("tunneldevice");
2653 if (o->tun_local == SSH_TUNID_ANY)
2654 printf(" any");
2655 else
2656 printf(" %d", o->tun_local);
2657 if (o->tun_remote == SSH_TUNID_ANY)
2658 printf(":any");
2659 else
2660 printf(":%d", o->tun_remote);
2661 printf("\n");
2662
2663 /* oCanonicalizePermittedCNAMEs */
2664 if ( o->num_permitted_cnames > 0) {
2665 printf("canonicalizePermittedcnames");
2666 for (i = 0; i < o->num_permitted_cnames; i++) {
2667 printf(" %s:%s", o->permitted_cnames[i].source_list,
2668 o->permitted_cnames[i].target_list);
2669 }
2670 printf("\n");
2671 }
2672
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002673 /* oControlPersist */
2674 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2675 dump_cfg_fmtint(oControlPersist, o->control_persist);
2676 else
2677 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2678
2679 /* oEscapeChar */
2680 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2681 printf("escapechar none\n");
2682 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002683 vis(buf, o->escape_char, VIS_WHITE, 0);
2684 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002685 }
2686
2687 /* oIPQoS */
2688 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2689 printf("%s\n", iptos2str(o->ip_qos_bulk));
2690
2691 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002692 printf("rekeylimit %llu %d\n",
2693 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002694
2695 /* oStreamLocalBindMask */
2696 printf("streamlocalbindmask 0%o\n",
2697 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002698
djm@openbsd.org30fd7f92018-04-06 03:51:27 +00002699 /* oLogFacility */
2700 printf("syslogfacility %s\n", log_facility_name(o->log_facility));
2701
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002702 /* oProxyCommand / oProxyJump */
2703 if (o->jump_host == NULL)
2704 dump_cfg_string(oProxyCommand, o->proxy_command);
2705 else {
2706 /* Check for numeric addresses */
2707 i = strchr(o->jump_host, ':') != NULL ||
2708 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2709 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2710 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002711 /* optional additional jump spec */
2712 o->jump_extra == NULL ? "" : o->jump_extra,
2713 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002714 /* optional user */
2715 o->jump_user == NULL ? "" : o->jump_user,
2716 o->jump_user == NULL ? "" : "@",
2717 /* opening [ if hostname is numeric */
2718 i ? "[" : "",
2719 /* mandatory hostname */
2720 o->jump_host,
2721 /* closing ] if hostname is numeric */
2722 i ? "]" : "",
2723 /* optional port number */
2724 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002725 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002726 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002727}