blob: c62c2eaba7435b1ef1bde536b3cfcc157b04c08b [file] [log] [blame]
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001/* $OpenBSD: readconf.c,v 1.269 2017/03/10 03:24:48 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien 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"
Damien Miller78928792000-04-12 20:17:38 +100056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "cipher.h"
58#include "pathnames.h"
59#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000060#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100061#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "readconf.h"
63#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000064#include "kex.h"
65#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110066#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000067#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000068#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
70/* Format of the configuration file:
71
72 # Configuration data is parsed as follows:
73 # 1. command line options
74 # 2. user-specific file
75 # 3. system-wide file
76 # Any configuration value is only changed the first time it is set.
77 # Thus, host-specific definitions should be at the beginning of the
78 # configuration file, and defaults at the end.
79
80 # Host-specific declarations. These may override anything above. A single
81 # host may match multiple declarations; these are processed in the order
82 # that they are given in.
83
84 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000085 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87 Host fake.com
88 HostName another.host.name.real.org
89 User blaah
90 Port 34289
91 ForwardX11 no
92 ForwardAgent no
93
94 Host books.com
95 RemoteForward 9999 shadows.cs.hut.fi:9999
djm@openbsd.org381a2612017-01-30 00:38:50 +000096 Ciphers 3des-cbc
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98 Host fascist.blob.com
99 Port 23123
100 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 PasswordAuthentication no
102
103 Host puukko.hut.fi
104 User t35124p
105 ProxyCommand ssh-proxy %h %p
106
107 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000108 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110 Host *.su
djm@openbsd.org381a2612017-01-30 00:38:50 +0000111 Ciphers aes128-ctr
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 PasswordAuthentication no
113
Damien Millerd27b9472005-12-13 19:29:02 +1100114 Host vpn.fake.com
115 Tunnel yes
116 TunnelDevice 3
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 # Defaults for various options
119 Host *
120 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 PasswordAuthentication yes
123 RSAAuthentication yes
124 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100126 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127 IdentityFile ~/.ssh/identity
128 Port 22
129 EscapeChar ~
130
131*/
132
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000133static int read_config_file_depth(const char *filename, struct passwd *pw,
134 const char *host, const char *original_host, Options *options,
135 int flags, int *activep, int depth);
136static int process_config_line_depth(Options *options, struct passwd *pw,
137 const char *host, const char *original_host, char *line,
138 const char *filename, int linenum, int *activep, int flags, int depth);
139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000147 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100149 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100151 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100154 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000155 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100159 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000164 oSendEnv, oControlPath, oControlMaster, oControlPersist,
165 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100166 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000167 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000168 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100169 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
170 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000171 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000172 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000173 oPubkeyAcceptedKeyTypes, oProxyJump,
Darren Tucker07636982013-05-16 20:30:03 +1000174 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175} OpCodes;
176
177/* Textual representations of the tokens. */
178
Damien Miller95def091999-11-25 00:26:21 +1100179static struct {
180 const char *name;
181 OpCodes opcode;
182} keywords[] = {
djm@openbsd.org381a2612017-01-30 00:38:50 +0000183 /* Deprecated options */
184 { "fallbacktorsh", oDeprecated },
185 { "globalknownhostsfile2", oDeprecated },
186 { "rhostsauthentication", oDeprecated },
187 { "userknownhostsfile2", oDeprecated },
188 { "useroaming", oDeprecated },
189 { "usersh", oDeprecated },
190
191 /* Unsupported options */
192 { "afstokenpassing", oUnsupported },
193 { "kerberosauthentication", oUnsupported },
194 { "kerberostgtpassing", oUnsupported },
195
196 /* Sometimes-unsupported options */
197#if defined(GSSAPI)
198 { "gssapiauthentication", oGssAuthentication },
199 { "gssapidelegatecredentials", oGssDelegateCreds },
200# else
201 { "gssapiauthentication", oUnsupported },
202 { "gssapidelegatecredentials", oUnsupported },
203#endif
204#ifdef ENABLE_PKCS11
205 { "smartcarddevice", oPKCS11Provider },
206 { "pkcs11provider", oPKCS11Provider },
207# else
208 { "smartcarddevice", oUnsupported },
209 { "pkcs11provider", oUnsupported },
210#endif
211#ifdef WITH_SSH1
212 { "rsaauthentication", oRSAAuthentication },
213 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
214 { "compressionlevel", oCompressionLevel },
215# else
216 { "rsaauthentication", oUnsupported },
217 { "rhostsrsaauthentication", oUnsupported },
218 { "compressionlevel", oUnsupported },
219#endif
220
Damien Miller95def091999-11-25 00:26:21 +1100221 { "forwardagent", oForwardAgent },
222 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000223 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000224 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000225 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000226 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100227 { "gatewayports", oGatewayPorts },
228 { "useprivilegedport", oUsePrivilegedPort },
Damien Miller95def091999-11-25 00:26:21 +1100229 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100230 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
231 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100232 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000233 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000234 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000235 { "challengeresponseauthentication", oChallengeResponseAuthentication },
236 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
237 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100238 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100239 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100240 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000241 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000242 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000243 { "identityagent", oIdentityAgent },
Damien Miller95def091999-11-25 00:26:21 +1100244 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000245 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100246 { "proxycommand", oProxyCommand },
247 { "port", oPort },
248 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000249 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000250 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000251 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100252 { "remoteforward", oRemoteForward },
253 { "localforward", oLocalForward },
254 { "user", oUser },
255 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100256 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100257 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100258 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100259 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100260 { "connectionattempts", oConnectionAttempts },
261 { "batchmode", oBatchMode },
262 { "checkhostip", oCheckHostIP },
263 { "stricthostkeychecking", oStrictHostKeyChecking },
264 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100265 { "tcpkeepalive", oTCPKeepAlive },
266 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100267 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100268 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000269 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000270 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000271 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000272 { "bindaddress", oBindAddress },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100273 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000274 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000275 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100276 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000277 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000278 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000279 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100280 { "serveraliveinterval", oServerAliveInterval },
281 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000282 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000283 { "controlpath", oControlPath },
284 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000285 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100286 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000287 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100288 { "tunnel", oTunnel },
289 { "tunneldevice", oTunnelDevice },
290 { "localcommand", oLocalCommand },
291 { "permitlocalcommand", oPermitLocalCommand },
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;
Damien Miller95def091999-11-25 00:26:21 +1100324 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000325 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000326
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000327 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000328 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000329 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000330 /* Don't add duplicates */
331 for (i = 0; i < options->num_local_forwards; i++) {
332 if (forward_equals(newfwd, options->local_forwards + i))
333 return;
334 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000335 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000336 options->num_local_forwards + 1,
337 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100338 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339
Damien Miller1a0442f2008-11-05 16:30:06 +1100340 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100343 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100344 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000345 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
Damien Miller5428f641999-11-25 11:54:57 +1100348/*
349 * Adds a remote TCP/IP port forward to options. Never returns if there is
350 * an error.
351 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller4af51302000-04-16 11:18:38 +1000353void
Damien Miller7acefbb2014-07-18 14:11:24 +1000354add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller7acefbb2014-07-18 14:11:24 +1000356 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000357 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000358
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000359 /* Don't add duplicates */
360 for (i = 0; i < options->num_remote_forwards; i++) {
361 if (forward_equals(newfwd, options->remote_forwards + i))
362 return;
363 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000364 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000365 options->num_remote_forwards + 1,
366 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100367 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368
Damien Miller1a0442f2008-11-05 16:30:06 +1100369 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000371 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100372 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100373 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000374 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100375 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000376 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000379static void
380clear_forwardings(Options *options)
381{
382 int i;
383
Damien Millerf91ee4c2005-03-01 21:24:33 +1100384 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000386 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000387 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000388 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100389 }
Damien Miller232cfb12010-06-26 09:50:30 +1000390 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000392 options->local_forwards = NULL;
393 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000394 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100395 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000397 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000399 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100400 }
Damien Miller232cfb12010-06-26 09:50:30 +1000401 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000402 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000403 options->remote_forwards = NULL;
404 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100406 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000407}
408
Darren Tucker19104782013-04-05 11:13:08 +1100409void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000410add_certificate_file(Options *options, const char *path, int userprovided)
411{
412 int i;
413
414 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
415 fatal("Too many certificate files specified (max %d)",
416 SSH_MAX_CERTIFICATE_FILES);
417
418 /* Avoid registering duplicates */
419 for (i = 0; i < options->num_certificate_files; i++) {
420 if (options->certificate_file_userprovided[i] == userprovided &&
421 strcmp(options->certificate_files[i], path) == 0) {
422 debug2("%s: ignoring duplicate key %s", __func__, path);
423 return;
424 }
425 }
426
427 options->certificate_file_userprovided[options->num_certificate_files] =
428 userprovided;
429 options->certificate_files[options->num_certificate_files++] =
430 xstrdup(path);
431}
432
433void
Darren Tucker19104782013-04-05 11:13:08 +1100434add_identity_file(Options *options, const char *dir, const char *filename,
435 int userprovided)
436{
437 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000438 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100439
440 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
441 fatal("Too many identity files specified (max %d)",
442 SSH_MAX_IDENTITY_FILES);
443
444 if (dir == NULL) /* no dir, filename is absolute */
445 path = xstrdup(filename);
446 else
447 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
448
Damien Miller15271902014-05-15 13:47:56 +1000449 /* Avoid registering duplicates */
450 for (i = 0; i < options->num_identity_files; i++) {
451 if (options->identity_file_userprovided[i] == userprovided &&
452 strcmp(options->identity_files[i], path) == 0) {
453 debug2("%s: ignoring duplicate key %s", __func__, path);
454 free(path);
455 return;
456 }
457 }
458
Darren Tucker19104782013-04-05 11:13:08 +1100459 options->identity_file_userprovided[options->num_identity_files] =
460 userprovided;
461 options->identity_files[options->num_identity_files++] = path;
462}
463
Damien Miller194fd902013-10-15 12:13:05 +1100464int
465default_ssh_port(void)
466{
467 static int port;
468 struct servent *sp;
469
470 if (port == 0) {
471 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
472 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
473 }
474 return port;
475}
476
477/*
478 * Execute a command in a shell.
479 * Return its exit status or -1 on abnormal exit.
480 */
481static int
482execute_in_shell(const char *cmd)
483{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000484 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100485 pid_t pid;
486 int devnull, status;
487 extern uid_t original_real_uid;
488
489 if ((shell = getenv("SHELL")) == NULL)
490 shell = _PATH_BSHELL;
491
Damien Miller194fd902013-10-15 12:13:05 +1100492 /* Need this to redirect subprocess stdin/out */
493 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
494 fatal("open(/dev/null): %s", strerror(errno));
495
496 debug("Executing command: '%.500s'", cmd);
497
498 /* Fork and execute the command. */
499 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000500 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100501
502 /* Child. Permanently give up superuser privileges. */
503 permanently_drop_suid(original_real_uid);
504
505 /* Redirect child stdin and stdout. Leave stderr */
506 if (dup2(devnull, STDIN_FILENO) == -1)
507 fatal("dup2: %s", strerror(errno));
508 if (dup2(devnull, STDOUT_FILENO) == -1)
509 fatal("dup2: %s", strerror(errno));
510 if (devnull > STDERR_FILENO)
511 close(devnull);
512 closefrom(STDERR_FILENO + 1);
513
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000514 argv[0] = shell;
515 argv[1] = "-c";
516 argv[2] = xstrdup(cmd);
517 argv[3] = NULL;
518
Damien Miller194fd902013-10-15 12:13:05 +1100519 execv(argv[0], argv);
520 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
521 /* Die with signal to make this error apparent to parent. */
522 signal(SIGTERM, SIG_DFL);
523 kill(getpid(), SIGTERM);
524 _exit(1);
525 }
526 /* Parent. */
527 if (pid < 0)
528 fatal("%s: fork: %.100s", __func__, strerror(errno));
529
530 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100531
532 while (waitpid(pid, &status, 0) == -1) {
533 if (errno != EINTR && errno != EAGAIN)
534 fatal("%s: waitpid: %s", __func__, strerror(errno));
535 }
536 if (!WIFEXITED(status)) {
537 error("command '%.100s' exited abnormally", cmd);
538 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000539 }
Damien Miller194fd902013-10-15 12:13:05 +1100540 debug3("command returned status %d", WEXITSTATUS(status));
541 return WEXITSTATUS(status);
542}
543
544/*
545 * Parse and execute a Match directive.
546 */
547static int
548match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 const char *host_arg, const char *original_host, int post_canon,
550 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100551{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000552 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100553 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000554 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100555 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
556
557 /*
558 * Configuration is likely to be incomplete at this point so we
559 * must be prepared to use default values.
560 */
561 port = options->port <= 0 ? default_ssh_port() : options->port;
562 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000563 if (post_canon) {
564 host = xstrdup(options->hostname);
565 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100566 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100567 host = percent_expand(options->hostname,
568 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000569 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100570 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000573 debug2("checking match for '%s' host %s originally %s",
574 cp, host, original_host);
575 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
576 criteria = NULL;
577 this_result = 1;
578 if ((negate = attrib[0] == '!'))
579 attrib++;
580 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100581 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100583 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 error("%.200s line %d: '%s' cannot be combined "
585 "with other Match attributes",
586 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100587 result = -1;
588 goto out;
589 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 if (result)
591 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100592 goto out;
593 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000594 attributes++;
595 if (strcasecmp(attrib, "canonical") == 0) {
596 r = !!post_canon; /* force bitmask member to boolean */
597 if (r == (negate ? 1 : 0))
598 this_result = result = 0;
599 debug3("%.200s line %d: %smatched '%s'",
600 filename, linenum,
601 this_result ? "" : "not ", oattrib);
602 continue;
603 }
604 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100605 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
606 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100607 result = -1;
608 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100609 }
Damien Miller194fd902013-10-15 12:13:05 +1100610 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000612 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 if (r == (negate ? 1 : 0))
614 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100615 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000617 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 if (r == (negate ? 1 : 0))
619 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100620 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000622 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 if (r == (negate ? 1 : 0))
624 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100625 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000626 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000627 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000628 if (r == (negate ? 1 : 0))
629 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100630 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100631 if (gethostname(thishost, sizeof(thishost)) == -1)
632 fatal("gethostname: %s", strerror(errno));
633 strlcpy(shorthost, thishost, sizeof(shorthost));
634 shorthost[strcspn(thishost, ".")] = '\0';
635 snprintf(portstr, sizeof(portstr), "%d", port);
636
637 cmd = percent_expand(arg,
638 "L", shorthost,
639 "d", pw->pw_dir,
640 "h", host,
641 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000642 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100643 "p", portstr,
644 "r", ruser,
645 "u", pw->pw_name,
646 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100647 if (result != 1) {
648 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000649 debug3("%.200s line %d: skipped exec "
650 "\"%.100s\"", filename, linenum, cmd);
651 free(cmd);
652 continue;
Damien Miller06287802014-02-24 15:56:45 +1100653 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000654 r = execute_in_shell(cmd);
655 if (r == -1) {
656 fatal("%.200s line %d: match exec "
657 "'%.100s' error", filename,
658 linenum, cmd);
659 }
660 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100661 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000662 /* Force exit status to boolean */
663 r = r == 0;
664 if (r == (negate ? 1 : 0))
665 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100666 } else {
667 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100668 result = -1;
669 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100670 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000671 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
672 filename, linenum, this_result ? "": "not ",
673 oattrib, criteria);
674 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100675 }
Damien Millercf31f382013-10-24 21:02:56 +1100676 if (attributes == 0) {
677 error("One or more attributes required for Match");
678 result = -1;
679 goto out;
680 }
Damien Miller084bcd22013-10-23 16:30:51 +1100681 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000682 if (result != -1)
683 debug2("match %sfound", result ? "" : "not ");
684 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100685 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100686 return result;
687}
688
Damien Miller0faf7472013-10-17 11:47:23 +1100689/* Check and prepare a domain name: removes trailing '.' and lowercases */
690static void
691valid_domain(char *name, const char *filename, int linenum)
692{
693 size_t i, l = strlen(name);
694 u_char c, last = '\0';
695
696 if (l == 0)
697 fatal("%s line %d: empty hostname suffix", filename, linenum);
698 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
699 fatal("%s line %d: hostname suffix \"%.100s\" "
700 "starts with invalid character", filename, linenum, name);
701 for (i = 0; i < l; i++) {
702 c = tolower((u_char)name[i]);
703 name[i] = (char)c;
704 if (last == '.' && c == '.')
705 fatal("%s line %d: hostname suffix \"%.100s\" contains "
706 "consecutive separators", filename, linenum, name);
707 if (c != '.' && c != '-' && !isalnum(c) &&
708 c != '_') /* technically invalid, but common */
709 fatal("%s line %d: hostname suffix \"%.100s\" contains "
710 "invalid characters", filename, linenum, name);
711 last = c;
712 }
713 if (name[l - 1] == '.')
714 name[l - 1] = '\0';
715}
716
Damien Miller5428f641999-11-25 11:54:57 +1100717/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000718 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100719 */
Damien Miller4af51302000-04-16 11:18:38 +1000720static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000721parse_token(const char *cp, const char *filename, int linenum,
722 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723{
Darren Tucker07636982013-05-16 20:30:03 +1000724 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller95def091999-11-25 00:26:21 +1100726 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000727 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100728 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000729 if (ignored_unknown != NULL &&
730 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000731 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000732 error("%s: line %d: Bad configuration option: %s",
733 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100734 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735}
736
Damien Millere9fc72e2013-10-15 12:14:12 +1100737/* Multistate option parsing */
738struct multistate {
739 char *key;
740 int value;
741};
742static const struct multistate multistate_flag[] = {
743 { "true", 1 },
744 { "false", 0 },
745 { "yes", 1 },
746 { "no", 0 },
747 { NULL, -1 }
748};
749static const struct multistate multistate_yesnoask[] = {
750 { "true", 1 },
751 { "false", 0 },
752 { "yes", 1 },
753 { "no", 0 },
754 { "ask", 2 },
755 { NULL, -1 }
756};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000757static const struct multistate multistate_yesnoaskconfirm[] = {
758 { "true", 1 },
759 { "false", 0 },
760 { "yes", 1 },
761 { "no", 0 },
762 { "ask", 2 },
763 { "confirm", 3 },
764 { NULL, -1 }
765};
Damien Millere9fc72e2013-10-15 12:14:12 +1100766static const struct multistate multistate_addressfamily[] = {
767 { "inet", AF_INET },
768 { "inet6", AF_INET6 },
769 { "any", AF_UNSPEC },
770 { NULL, -1 }
771};
772static const struct multistate multistate_controlmaster[] = {
773 { "true", SSHCTL_MASTER_YES },
774 { "yes", SSHCTL_MASTER_YES },
775 { "false", SSHCTL_MASTER_NO },
776 { "no", SSHCTL_MASTER_NO },
777 { "auto", SSHCTL_MASTER_AUTO },
778 { "ask", SSHCTL_MASTER_ASK },
779 { "autoask", SSHCTL_MASTER_AUTO_ASK },
780 { NULL, -1 }
781};
782static const struct multistate multistate_tunnel[] = {
783 { "ethernet", SSH_TUNMODE_ETHERNET },
784 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
785 { "true", SSH_TUNMODE_DEFAULT },
786 { "yes", SSH_TUNMODE_DEFAULT },
787 { "false", SSH_TUNMODE_NO },
788 { "no", SSH_TUNMODE_NO },
789 { NULL, -1 }
790};
791static const struct multistate multistate_requesttty[] = {
792 { "true", REQUEST_TTY_YES },
793 { "yes", REQUEST_TTY_YES },
794 { "false", REQUEST_TTY_NO },
795 { "no", REQUEST_TTY_NO },
796 { "force", REQUEST_TTY_FORCE },
797 { "auto", REQUEST_TTY_AUTO },
798 { NULL, -1 }
799};
Damien Miller38505592013-10-17 11:48:13 +1100800static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100801 { "true", SSH_CANONICALISE_YES },
802 { "false", SSH_CANONICALISE_NO },
803 { "yes", SSH_CANONICALISE_YES },
804 { "no", SSH_CANONICALISE_NO },
805 { "always", SSH_CANONICALISE_ALWAYS },
806 { NULL, -1 }
807};
Damien Millere9fc72e2013-10-15 12:14:12 +1100808
Damien Miller5428f641999-11-25 11:54:57 +1100809/*
810 * Processes a single option line as used in the configuration files. This
811 * only sets those values that have not already been set.
812 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100813int
Damien Miller194fd902013-10-15 12:13:05 +1100814process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000815 const char *original_host, char *line, const char *filename,
816 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000818 return process_config_line_depth(options, pw, host, original_host,
819 line, filename, linenum, activep, flags, 0);
820}
821
822#define WHITESPACE " \t\r\n"
823static int
824process_config_line_depth(Options *options, struct passwd *pw, const char *host,
825 const char *original_host, char *line, const char *filename,
826 int linenum, int *activep, int flags, int depth)
827{
Damien Miller295ee632011-05-29 21:42:31 +1000828 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
829 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000830 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000831 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100832 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000833 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100834 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000835 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100836 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100837 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000838 glob_t gl;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839
Damien Miller194fd902013-10-15 12:13:05 +1100840 if (activep == NULL) { /* We are processing a command line directive */
841 cmdline = 1;
842 activep = &cmdline;
843 }
844
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000845 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000846 if ((len = strlen(line)) == 0)
847 return 0;
848 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000849 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000850 break;
851 line[len] = '\0';
852 }
853
Damien Millerbe484b52000-07-15 14:14:16 +1000854 s = line;
855 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100856 if ((keyword = strdelim(&s)) == NULL)
857 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000858 /* Ignore leading whitespace. */
859 if (*keyword == '\0')
860 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000861 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100862 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000863 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100864 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Darren Tucker07636982013-05-16 20:30:03 +1000866 opcode = parse_token(keyword, filename, linenum,
867 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868
Damien Miller95def091999-11-25 00:26:21 +1100869 switch (opcode) {
870 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100871 /* don't panic, but count bad options */
872 return -1;
Darren Tucker07636982013-05-16 20:30:03 +1000873 case oIgnoredUnknownOption:
874 debug("%s line %d: Ignored unknown option \"%s\"",
875 filename, linenum, keyword);
876 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000877 case oConnectTimeout:
878 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100879parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000880 arg = strdelim(&s);
881 if (!arg || *arg == '\0')
882 fatal("%s line %d: missing time value.",
883 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000884 if (strcmp(arg, "none") == 0)
885 value = -1;
886 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000887 fatal("%s line %d: invalid time value.",
888 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100889 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000890 *intptr = value;
891 break;
892
Damien Miller95def091999-11-25 00:26:21 +1100893 case oForwardAgent:
894 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100895 parse_flag:
896 multistate_ptr = multistate_flag;
897 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000898 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000899 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100900 fatal("%s line %d: missing argument.",
901 filename, linenum);
902 value = -1;
903 for (i = 0; multistate_ptr[i].key != NULL; i++) {
904 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
905 value = multistate_ptr[i].value;
906 break;
907 }
908 }
909 if (value == -1)
910 fatal("%s line %d: unsupported option \"%s\".",
911 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100912 if (*activep && *intptr == -1)
913 *intptr = value;
914 break;
915
916 case oForwardX11:
917 intptr = &options->forward_x11;
918 goto parse_flag;
919
Darren Tucker0a118da2003-10-15 15:54:32 +1000920 case oForwardX11Trusted:
921 intptr = &options->forward_x11_trusted;
922 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000923
Damien Miller1ab6a512010-06-26 10:02:24 +1000924 case oForwardX11Timeout:
925 intptr = &options->forward_x11_timeout;
926 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000927
Damien Miller95def091999-11-25 00:26:21 +1100928 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000929 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100930 goto parse_flag;
931
Darren Tuckere7d4b192006-07-12 22:17:10 +1000932 case oExitOnForwardFailure:
933 intptr = &options->exit_on_forward_failure;
934 goto parse_flag;
935
Damien Miller95def091999-11-25 00:26:21 +1100936 case oUsePrivilegedPort:
937 intptr = &options->use_privileged_port;
938 goto parse_flag;
939
Damien Miller95def091999-11-25 00:26:21 +1100940 case oPasswordAuthentication:
941 intptr = &options->password_authentication;
942 goto parse_flag;
943
Damien Miller874d77b2000-10-14 16:23:11 +1100944 case oKbdInteractiveAuthentication:
945 intptr = &options->kbd_interactive_authentication;
946 goto parse_flag;
947
948 case oKbdInteractiveDevices:
949 charptr = &options->kbd_interactive_devices;
950 goto parse_string;
951
Damien Miller0bc1bd82000-11-13 22:57:25 +1100952 case oPubkeyAuthentication:
953 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000954 goto parse_flag;
955
Damien Miller95def091999-11-25 00:26:21 +1100956 case oRSAAuthentication:
957 intptr = &options->rsa_authentication;
958 goto parse_flag;
959
960 case oRhostsRSAAuthentication:
961 intptr = &options->rhosts_rsa_authentication;
962 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;
Damien Millere9fc72e2013-10-15 12:14:12 +1100995 multistate_ptr = multistate_yesnoask;
996 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 Miller95def091999-11-25 00:26:21 +11001014 case oCompressionLevel:
1015 intptr = &options->compression_level;
1016 goto parse_int;
1017
Damien Millera5539d22003-04-09 20:50:06 +10001018 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001019 arg = strdelim(&s);
1020 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001021 fatal("%.200s line %d: Missing argument.", filename,
1022 linenum);
1023 if (strcmp(arg, "default") == 0) {
1024 val64 = 0;
1025 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001026 if (scan_scaled(arg, &val64) == -1)
1027 fatal("%.200s line %d: Bad number '%s': %s",
1028 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001029 if (val64 != 0 && val64 < 16)
1030 fatal("%.200s line %d: RekeyLimit too small",
1031 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001032 }
Damien Miller3dff1762008-02-10 22:25:52 +11001033 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001034 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001035 if (s != NULL) { /* optional rekey interval present */
1036 if (strcmp(s, "none") == 0) {
1037 (void)strdelim(&s); /* discard */
1038 break;
1039 }
1040 intptr = &options->rekey_interval;
1041 goto parse_time;
1042 }
Damien Millera5539d22003-04-09 20:50:06 +10001043 break;
1044
Damien Miller95def091999-11-25 00:26:21 +11001045 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001046 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001047 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001048 fatal("%.200s line %d: Missing argument.", filename, linenum);
1049 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001050 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001051 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001052 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001053 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001054 add_identity_file(options, NULL,
1055 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001056 }
1057 break;
1058
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001059 case oCertificateFile:
1060 arg = strdelim(&s);
1061 if (!arg || *arg == '\0')
1062 fatal("%.200s line %d: Missing argument.",
1063 filename, linenum);
1064 if (*activep) {
1065 intptr = &options->num_certificate_files;
1066 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1067 fatal("%.200s line %d: Too many certificate "
1068 "files specified (max %d).",
1069 filename, linenum,
1070 SSH_MAX_CERTIFICATE_FILES);
1071 }
1072 add_certificate_file(options, arg,
1073 flags & SSHCONF_USERCONF);
1074 }
1075 break;
1076
Damien Millerd3a18572000-06-07 19:55:44 +10001077 case oXAuthLocation:
1078 charptr=&options->xauth_location;
1079 goto parse_string;
1080
Damien Miller95def091999-11-25 00:26:21 +11001081 case oUser:
1082 charptr = &options->user;
1083parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001084 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001085 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001086 fatal("%.200s line %d: Missing argument.",
1087 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001088 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001089 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001090 break;
1091
1092 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001093 cpptr = (char **)&options->system_hostfiles;
1094 uintptr = &options->num_system_hostfiles;
1095 max_entries = SSH_MAX_HOSTS_FILES;
1096parse_char_array:
1097 if (*activep && *uintptr == 0) {
1098 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1099 if ((*uintptr) >= max_entries)
1100 fatal("%s line %d: "
1101 "too many authorized keys files.",
1102 filename, linenum);
1103 cpptr[(*uintptr)++] = xstrdup(arg);
1104 }
1105 }
1106 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001107
1108 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001109 cpptr = (char **)&options->user_hostfiles;
1110 uintptr = &options->num_user_hostfiles;
1111 max_entries = SSH_MAX_HOSTS_FILES;
1112 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001113
Damien Miller95def091999-11-25 00:26:21 +11001114 case oHostName:
1115 charptr = &options->hostname;
1116 goto parse_string;
1117
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001118 case oHostKeyAlias:
1119 charptr = &options->host_key_alias;
1120 goto parse_string;
1121
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001122 case oPreferredAuthentications:
1123 charptr = &options->preferred_authentications;
1124 goto parse_string;
1125
Ben Lindstrome0f88042001-04-30 13:06:24 +00001126 case oBindAddress:
1127 charptr = &options->bind_address;
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);
Damien Miller37023962000-07-11 17:31:38 +10001163 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001164 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001165 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001166 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001167
1168 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001169 value = strtol(arg, &endofnumber, 0);
1170 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001171 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001172 if (*activep && *intptr == -1)
1173 *intptr = value;
1174 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
Damien Miller95def091999-11-25 00:26:21 +11001176 case oConnectionAttempts:
1177 intptr = &options->connection_attempts;
1178 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001179
Damien Miller95def091999-11-25 00:26:21 +11001180 case oCipher:
1181 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001182 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001183 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001184 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001185 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001186 if (value == -1)
1187 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001188 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001189 if (*activep && *intptr == -1)
1190 *intptr = value;
1191 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
Damien Miller78928792000-04-12 20:17:38 +10001193 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001194 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001195 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001196 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001197 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001198 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001199 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001200 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001201 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001202 break;
1203
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001204 case oMacs:
1205 arg = strdelim(&s);
1206 if (!arg || *arg == '\0')
1207 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001208 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001209 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001210 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001211 if (*activep && options->macs == NULL)
1212 options->macs = xstrdup(arg);
1213 break;
1214
Damien Millerd5f62bf2010-09-24 22:11:14 +10001215 case oKexAlgorithms:
1216 arg = strdelim(&s);
1217 if (!arg || *arg == '\0')
1218 fatal("%.200s line %d: Missing argument.",
1219 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001220 if (*arg != '-' &&
1221 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001222 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1223 filename, linenum, arg ? arg : "<NONE>");
1224 if (*activep && options->kex_algorithms == NULL)
1225 options->kex_algorithms = xstrdup(arg);
1226 break;
1227
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001228 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001229 charptr = &options->hostkeyalgorithms;
1230parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001231 arg = strdelim(&s);
1232 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001233 fatal("%.200s line %d: Missing argument.",
1234 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001235 if (*arg != '-' &&
1236 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001237 fatal("%s line %d: Bad key types '%s'.",
1238 filename, linenum, arg ? arg : "<NONE>");
1239 if (*activep && *charptr == NULL)
1240 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001241 break;
1242
Damien Miller78928792000-04-12 20:17:38 +10001243 case oProtocol:
1244 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001245 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001246 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001247 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001248 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001249 if (value == SSH_PROTO_UNKNOWN)
1250 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001251 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001252 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1253 *intptr = value;
1254 break;
1255
Damien Miller95def091999-11-25 00:26:21 +11001256 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001257 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001258 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001259 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001260 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001261 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001262 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001263 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1264 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001265 break;
1266
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001267 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001268 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001269 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001270 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001271 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001272 fatal("%.200s line %d: Missing port argument.",
1273 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001274
Damien Millera699d952008-11-03 19:27:34 +11001275 if (opcode == oLocalForward ||
1276 opcode == oRemoteForward) {
1277 arg2 = strdelim(&s);
1278 if (arg2 == NULL || *arg2 == '\0')
1279 fatal("%.200s line %d: Missing target argument.",
1280 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001281
Damien Millera699d952008-11-03 19:27:34 +11001282 /* construct a string for parse_forward */
1283 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1284 } else if (opcode == oDynamicForward) {
1285 strlcpy(fwdarg, arg, sizeof(fwdarg));
1286 }
1287
1288 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001289 opcode == oDynamicForward ? 1 : 0,
1290 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001291 fatal("%.200s line %d: Bad forwarding specification.",
1292 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001293
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001294 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001295 if (opcode == oLocalForward ||
1296 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001297 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001298 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001299 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001300 }
Damien Miller95def091999-11-25 00:26:21 +11001301 break;
1302
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001303 case oClearAllForwardings:
1304 intptr = &options->clear_forwardings;
1305 goto parse_flag;
1306
Damien Miller95def091999-11-25 00:26:21 +11001307 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001308 if (cmdline)
1309 fatal("Host directive not supported as a command-line "
1310 "option");
Damien Miller95def091999-11-25 00:26:21 +11001311 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001312 arg2 = NULL;
1313 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001314 if ((flags & SSHCONF_NEVERMATCH) != 0)
1315 break;
Damien Millerfe924212011-05-15 08:44:45 +10001316 negated = *arg == '!';
1317 if (negated)
1318 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001319 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001320 if (negated) {
1321 debug("%.200s line %d: Skipping Host "
1322 "block because of negated match "
1323 "for %.100s", filename, linenum,
1324 arg);
1325 *activep = 0;
1326 break;
1327 }
1328 if (!*activep)
1329 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001330 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001331 }
Damien Millerfe924212011-05-15 08:44:45 +10001332 }
1333 if (*activep)
1334 debug("%.200s line %d: Applying options for %.100s",
1335 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001336 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001337 return 0;
1338
Damien Miller194fd902013-10-15 12:13:05 +11001339 case oMatch:
1340 if (cmdline)
1341 fatal("Host directive not supported as a command-line "
1342 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001343 value = match_cfg_line(options, &s, pw, host, original_host,
1344 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001345 if (value < 0)
1346 fatal("%.200s line %d: Bad Match condition", filename,
1347 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001348 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001349 break;
1350
Damien Miller95def091999-11-25 00:26:21 +11001351 case oEscapeChar:
1352 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001353 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001354 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001355 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001356 if (strcmp(arg, "none") == 0)
1357 value = SSH_ESCAPECHAR_NONE;
1358 else if (arg[1] == '\0')
1359 value = (u_char) arg[0];
1360 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001361 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1362 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001363 else {
1364 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001365 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001366 /* NOTREACHED */
1367 value = 0; /* Avoid compiler warning. */
1368 }
1369 if (*activep && *intptr == -1)
1370 *intptr = value;
1371 break;
1372
Damien Miller20a8f972003-05-18 20:50:30 +10001373 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001374 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001375 multistate_ptr = multistate_addressfamily;
1376 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001377
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001378 case oEnableSSHKeysign:
1379 intptr = &options->enable_ssh_keysign;
1380 goto parse_flag;
1381
Damien Millerbd394c32004-03-08 23:12:36 +11001382 case oIdentitiesOnly:
1383 intptr = &options->identities_only;
1384 goto parse_flag;
1385
Damien Miller509b0102003-12-17 16:33:10 +11001386 case oServerAliveInterval:
1387 intptr = &options->server_alive_interval;
1388 goto parse_time;
1389
1390 case oServerAliveCountMax:
1391 intptr = &options->server_alive_count_max;
1392 goto parse_int;
1393
Darren Tucker46bc0752004-05-02 22:11:30 +10001394 case oSendEnv:
1395 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1396 if (strchr(arg, '=') != NULL)
1397 fatal("%s line %d: Invalid environment name.",
1398 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001399 if (!*activep)
1400 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001401 if (options->num_send_env >= MAX_SEND_ENV)
1402 fatal("%s line %d: too many send env.",
1403 filename, linenum);
1404 options->send_env[options->num_send_env++] =
1405 xstrdup(arg);
1406 }
1407 break;
1408
Damien Miller0e220db2004-06-15 10:34:08 +10001409 case oControlPath:
1410 charptr = &options->control_path;
1411 goto parse_string;
1412
1413 case oControlMaster:
1414 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001415 multistate_ptr = multistate_controlmaster;
1416 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001417
Damien Millere11e1ea2010-08-03 16:04:46 +10001418 case oControlPersist:
1419 /* no/false/yes/true, or a time spec */
1420 intptr = &options->control_persist;
1421 arg = strdelim(&s);
1422 if (!arg || *arg == '\0')
1423 fatal("%.200s line %d: Missing ControlPersist"
1424 " argument.", filename, linenum);
1425 value = 0;
1426 value2 = 0; /* timeout */
1427 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1428 value = 0;
1429 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1430 value = 1;
1431 else if ((value2 = convtime(arg)) >= 0)
1432 value = 1;
1433 else
1434 fatal("%.200s line %d: Bad ControlPersist argument.",
1435 filename, linenum);
1436 if (*activep && *intptr == -1) {
1437 *intptr = value;
1438 options->control_persist_timeout = value2;
1439 }
1440 break;
1441
Damien Millere1776152005-03-01 21:47:37 +11001442 case oHashKnownHosts:
1443 intptr = &options->hash_known_hosts;
1444 goto parse_flag;
1445
Damien Millerd27b9472005-12-13 19:29:02 +11001446 case oTunnel:
1447 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001448 multistate_ptr = multistate_tunnel;
1449 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001450
1451 case oTunnelDevice:
1452 arg = strdelim(&s);
1453 if (!arg || *arg == '\0')
1454 fatal("%.200s line %d: Missing argument.", filename, linenum);
1455 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001456 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001457 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1458 if (*activep) {
1459 options->tun_local = value;
1460 options->tun_remote = value2;
1461 }
1462 break;
1463
1464 case oLocalCommand:
1465 charptr = &options->local_command;
1466 goto parse_command;
1467
1468 case oPermitLocalCommand:
1469 intptr = &options->permit_local_command;
1470 goto parse_flag;
1471
Damien Miller10288242008-06-30 00:04:03 +10001472 case oVisualHostKey:
1473 intptr = &options->visual_host_key;
1474 goto parse_flag;
1475
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001476 case oInclude:
1477 if (cmdline)
1478 fatal("Include directive not supported as a "
1479 "command-line option");
1480 value = 0;
1481 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1482 /*
1483 * Ensure all paths are anchored. User configuration
1484 * files may begin with '~/' but system configurations
1485 * must not. If the path is relative, then treat it
1486 * as living in ~/.ssh for user configurations or
1487 * /etc/ssh for system ones.
1488 */
1489 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1490 fatal("%.200s line %d: bad include path %s.",
1491 filename, linenum, arg);
1492 if (*arg != '/' && *arg != '~') {
1493 xasprintf(&arg2, "%s/%s",
1494 (flags & SSHCONF_USERCONF) ?
1495 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1496 } else
1497 arg2 = xstrdup(arg);
1498 memset(&gl, 0, sizeof(gl));
1499 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1500 if (r == GLOB_NOMATCH) {
1501 debug("%.200s line %d: include %s matched no "
1502 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001503 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001504 continue;
1505 } else if (r != 0 || gl.gl_pathc < 0)
1506 fatal("%.200s line %d: glob failed for %s.",
1507 filename, linenum, arg2);
1508 free(arg2);
1509 oactive = *activep;
1510 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1511 debug3("%.200s line %d: Including file %s "
1512 "depth %d%s", filename, linenum,
1513 gl.gl_pathv[i], depth,
1514 oactive ? "" : " (parse only)");
1515 r = read_config_file_depth(gl.gl_pathv[i],
1516 pw, host, original_host, options,
1517 flags | SSHCONF_CHECKPERM |
1518 (oactive ? 0 : SSHCONF_NEVERMATCH),
1519 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001520 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001521 fatal("Can't open user config file "
1522 "%.100s: %.100s", gl.gl_pathv[i],
1523 strerror(errno));
1524 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001525 /*
1526 * don't let Match in includes clobber the
1527 * containing file's Match state.
1528 */
1529 *activep = oactive;
1530 if (r != 1)
1531 value = -1;
1532 }
1533 globfree(&gl);
1534 }
1535 if (value != 0)
1536 return value;
1537 break;
1538
Damien Miller0dac6fb2010-11-20 15:19:38 +11001539 case oIPQoS:
1540 arg = strdelim(&s);
1541 if ((value = parse_ipqos(arg)) == -1)
1542 fatal("%s line %d: Bad IPQoS value: %s",
1543 filename, linenum, arg);
1544 arg = strdelim(&s);
1545 if (arg == NULL)
1546 value2 = value;
1547 else if ((value2 = parse_ipqos(arg)) == -1)
1548 fatal("%s line %d: Bad IPQoS value: %s",
1549 filename, linenum, arg);
1550 if (*activep) {
1551 options->ip_qos_interactive = value;
1552 options->ip_qos_bulk = value2;
1553 }
1554 break;
1555
Damien Miller21771e22011-05-15 08:45:50 +10001556 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001557 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001558 multistate_ptr = multistate_requesttty;
1559 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001560
Darren Tucker07636982013-05-16 20:30:03 +10001561 case oIgnoreUnknown:
1562 charptr = &options->ignored_unknown;
1563 goto parse_string;
1564
Damien Miller1262b662013-08-21 02:44:24 +10001565 case oProxyUseFdpass:
1566 intptr = &options->proxy_use_fdpass;
1567 goto parse_flag;
1568
Damien Miller0faf7472013-10-17 11:47:23 +11001569 case oCanonicalDomains:
1570 value = options->num_canonical_domains != 0;
1571 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1572 valid_domain(arg, filename, linenum);
1573 if (!*activep || value)
1574 continue;
1575 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1576 fatal("%s line %d: too many hostname suffixes.",
1577 filename, linenum);
1578 options->canonical_domains[
1579 options->num_canonical_domains++] = xstrdup(arg);
1580 }
1581 break;
1582
Damien Miller38505592013-10-17 11:48:13 +11001583 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001584 value = options->num_permitted_cnames != 0;
1585 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1586 /* Either '*' for everything or 'list:list' */
1587 if (strcmp(arg, "*") == 0)
1588 arg2 = arg;
1589 else {
1590 lowercase(arg);
1591 if ((arg2 = strchr(arg, ':')) == NULL ||
1592 arg2[1] == '\0') {
1593 fatal("%s line %d: "
1594 "Invalid permitted CNAME \"%s\"",
1595 filename, linenum, arg);
1596 }
1597 *arg2 = '\0';
1598 arg2++;
1599 }
1600 if (!*activep || value)
1601 continue;
1602 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1603 fatal("%s line %d: too many permitted CNAMEs.",
1604 filename, linenum);
1605 cname = options->permitted_cnames +
1606 options->num_permitted_cnames++;
1607 cname->source_list = xstrdup(arg);
1608 cname->target_list = xstrdup(arg2);
1609 }
1610 break;
1611
Damien Miller38505592013-10-17 11:48:13 +11001612 case oCanonicalizeHostname:
1613 intptr = &options->canonicalize_hostname;
1614 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001615 goto parse_multistate;
1616
Damien Miller38505592013-10-17 11:48:13 +11001617 case oCanonicalizeMaxDots:
1618 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001619 goto parse_int;
1620
Damien Miller38505592013-10-17 11:48:13 +11001621 case oCanonicalizeFallbackLocal:
1622 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001623 goto parse_flag;
1624
Damien Miller7acefbb2014-07-18 14:11:24 +10001625 case oStreamLocalBindMask:
1626 arg = strdelim(&s);
1627 if (!arg || *arg == '\0')
1628 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1629 /* Parse mode in octal format */
1630 value = strtol(arg, &endofnumber, 8);
1631 if (arg == endofnumber || value < 0 || value > 0777)
1632 fatal("%.200s line %d: Bad mask.", filename, linenum);
1633 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1634 break;
1635
1636 case oStreamLocalBindUnlink:
1637 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1638 goto parse_flag;
1639
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001640 case oRevokedHostKeys:
1641 charptr = &options->revoked_host_keys;
1642 goto parse_string;
1643
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001644 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001645 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001646 arg = strdelim(&s);
1647 if (!arg || *arg == '\0')
1648 fatal("%.200s line %d: Missing argument.",
1649 filename, linenum);
1650 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1651 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1652 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001653 if (*activep && *intptr == -1)
1654 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001655 break;
1656
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001657 case oUpdateHostkeys:
1658 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001659 multistate_ptr = multistate_yesnoask;
1660 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001661
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001662 case oHostbasedKeyTypes:
1663 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001664 goto parse_keytypes;
1665
1666 case oPubkeyAcceptedKeyTypes:
1667 charptr = &options->pubkey_key_types;
1668 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001669
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001670 case oAddKeysToAgent:
1671 intptr = &options->add_keys_to_agent;
1672 multistate_ptr = multistate_yesnoaskconfirm;
1673 goto parse_multistate;
1674
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001675 case oIdentityAgent:
1676 charptr = &options->identity_agent;
1677 goto parse_string;
1678
Ben Lindstrom4daea862002-06-09 20:04:02 +00001679 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001680 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001681 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001682 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001683
Damien Millerf9b3feb2003-05-16 11:38:32 +10001684 case oUnsupported:
1685 error("%s line %d: Unsupported option \"%s\"",
1686 filename, linenum, keyword);
1687 return 0;
1688
Damien Miller95def091999-11-25 00:26:21 +11001689 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001690 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001691 }
1692
1693 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001694 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001695 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001696 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001697 }
Damien Miller95def091999-11-25 00:26:21 +11001698 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001699}
1700
Damien Miller5428f641999-11-25 11:54:57 +11001701/*
1702 * Reads the config file and modifies the options accordingly. Options
1703 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001704 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001705 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001706int
Damien Miller194fd902013-10-15 12:13:05 +11001707read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001708 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001710 int active = 1;
1711
1712 return read_config_file_depth(filename, pw, host, original_host,
1713 options, flags, &active, 0);
1714}
1715
1716#define READCONF_MAX_DEPTH 16
1717static int
1718read_config_file_depth(const char *filename, struct passwd *pw,
1719 const char *host, const char *original_host, Options *options,
1720 int flags, int *activep, int depth)
1721{
Damien Miller95def091999-11-25 00:26:21 +11001722 FILE *f;
1723 char line[1024];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001724 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001725 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001727 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1728 fatal("Too many recursive configuration includes");
1729
Damien Miller57a44762004-04-20 20:11:57 +10001730 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001731 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001732
Darren Tuckeraefa3682013-04-05 11:18:35 +11001733 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001734 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001735
Damien Miller33793852004-06-15 10:27:55 +10001736 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001737 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001738 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001739 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001740 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001741 }
1742
Damien Miller95def091999-11-25 00:26:21 +11001743 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001744
Damien Miller5428f641999-11-25 11:54:57 +11001745 /*
1746 * Mark that we are now processing the options. This flag is turned
1747 * on/off by Host specifications.
1748 */
Damien Miller95def091999-11-25 00:26:21 +11001749 linenum = 0;
1750 while (fgets(line, sizeof(line), f)) {
1751 /* Update line number counter. */
1752 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001753 if (process_config_line_depth(options, pw, host, original_host,
1754 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001755 bad_options++;
1756 }
1757 fclose(f);
1758 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001759 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001760 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001761 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762}
1763
Damien Miller13f97b22014-02-24 15:57:55 +11001764/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1765int
1766option_clear_or_none(const char *o)
1767{
1768 return o == NULL || strcasecmp(o, "none") == 0;
1769}
1770
Damien Miller5428f641999-11-25 11:54:57 +11001771/*
1772 * Initializes options to special values that indicate that they have not yet
1773 * been set. Read_config_file will only set options with this value. Options
1774 * are processed in the following order: command line, user config file,
1775 * system config file. Last, fill_default_options is called.
1776 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001777
Damien Miller4af51302000-04-16 11:18:38 +10001778void
Damien Miller95def091999-11-25 00:26:21 +11001779initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001780{
Damien Miller95def091999-11-25 00:26:21 +11001781 memset(options, 'X', sizeof(*options));
1782 options->forward_agent = -1;
1783 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001784 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001785 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001786 options->stdio_forward_host = NULL;
1787 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001788 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001789 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001790 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001791 options->fwd_opts.gateway_ports = -1;
1792 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1793 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001794 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001795 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001796 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001797 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001798 options->gss_authentication = -1;
1799 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001800 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001801 options->kbd_interactive_authentication = -1;
1802 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001803 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001804 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001805 options->batch_mode = -1;
1806 options->check_host_ip = -1;
1807 options->strict_host_key_checking = -1;
1808 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001809 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001810 options->compression_level = -1;
1811 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001812 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001813 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001814 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001815 options->number_of_password_prompts = -1;
1816 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001817 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001818 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001819 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001820 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001821 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001822 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001823 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001824 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001825 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001826 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001827 options->jump_user = NULL;
1828 options->jump_host = NULL;
1829 options->jump_port = -1;
1830 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001831 options->user = NULL;
1832 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001833 options->num_system_hostfiles = 0;
1834 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001835 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001836 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001837 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001838 options->num_remote_forwards = 0;
Damien Millerfcd93202002-02-05 12:26:34 +11001839 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001840 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001841 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001842 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001843 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001844 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001845 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001846 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001847 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001848 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001849 options->server_alive_interval = -1;
1850 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001851 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001852 options->control_path = NULL;
1853 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001854 options->control_persist = -1;
1855 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001856 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001857 options->tun_open = -1;
1858 options->tun_local = -1;
1859 options->tun_remote = -1;
1860 options->local_command = NULL;
1861 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001862 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001863 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001864 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001865 options->ip_qos_interactive = -1;
1866 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001867 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001868 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001869 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001870 options->num_canonical_domains = 0;
1871 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001872 options->canonicalize_max_dots = -1;
1873 options->canonicalize_fallback_local = -1;
1874 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001875 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001876 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001877 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001878 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001879 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001880}
1881
Damien Miller5428f641999-11-25 11:54:57 +11001882/*
Damien Miller13f97b22014-02-24 15:57:55 +11001883 * A petite version of fill_default_options() that just fills the options
1884 * needed for hostname canonicalization to proceed.
1885 */
1886void
1887fill_default_options_for_canonicalization(Options *options)
1888{
1889 if (options->canonicalize_max_dots == -1)
1890 options->canonicalize_max_dots = 1;
1891 if (options->canonicalize_fallback_local == -1)
1892 options->canonicalize_fallback_local = 1;
1893 if (options->canonicalize_hostname == -1)
1894 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1895}
1896
1897/*
Damien Miller5428f641999-11-25 11:54:57 +11001898 * Called after processing other sources of option data, this fills those
1899 * options for which no value has been specified with their default values.
1900 */
Damien Miller4af51302000-04-16 11:18:38 +10001901void
Damien Miller95def091999-11-25 00:26:21 +11001902fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001903{
Damien Miller95def091999-11-25 00:26:21 +11001904 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001905 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001906 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001907 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001908 if (options->forward_x11_trusted == -1)
1909 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001910 if (options->forward_x11_timeout == -1)
1911 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001912 /*
1913 * stdio forwarding (-W) changes the default for these but we defer
1914 * setting the values so they can be overridden.
1915 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001916 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001917 options->exit_on_forward_failure =
1918 options->stdio_forward_host != NULL ? 1 : 0;
1919 if (options->clear_forwardings == -1)
1920 options->clear_forwardings =
1921 options->stdio_forward_host != NULL ? 1 : 0;
1922 if (options->clear_forwardings == 1)
1923 clear_forwardings(options);
1924
Damien Millerd3a18572000-06-07 19:55:44 +10001925 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001926 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001927 if (options->fwd_opts.gateway_ports == -1)
1928 options->fwd_opts.gateway_ports = 0;
1929 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1930 options->fwd_opts.streamlocal_bind_mask = 0177;
1931 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1932 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001933 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001934 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001935 if (options->rsa_authentication == -1)
1936 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001937 if (options->pubkey_authentication == -1)
1938 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001939 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001940 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001941 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001942 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001943 if (options->gss_deleg_creds == -1)
1944 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001945 if (options->password_authentication == -1)
1946 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001947 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001948 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001949 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001950 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001951 if (options->hostbased_authentication == -1)
1952 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001953 if (options->batch_mode == -1)
1954 options->batch_mode = 0;
1955 if (options->check_host_ip == -1)
1956 options->check_host_ip = 1;
1957 if (options->strict_host_key_checking == -1)
1958 options->strict_host_key_checking = 2; /* 2 is default */
1959 if (options->compression == -1)
1960 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001961 if (options->tcp_keep_alive == -1)
1962 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001963 if (options->compression_level == -1)
1964 options->compression_level = 6;
1965 if (options->port == -1)
1966 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001967 if (options->address_family == -1)
1968 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001969 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001970 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001971 if (options->number_of_password_prompts == -1)
1972 options->number_of_password_prompts = 3;
1973 /* Selected in ssh_login(). */
1974 if (options->cipher == -1)
1975 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001976 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001977 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001978 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001979 if (options->add_keys_to_agent == -1)
1980 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001981 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001982 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001983 add_identity_file(options, "~/",
1984 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001985 }
1986 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001987 add_identity_file(options, "~/",
1988 _PATH_SSH_CLIENT_ID_RSA, 0);
1989 add_identity_file(options, "~/",
1990 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001991#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001992 add_identity_file(options, "~/",
1993 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001994#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001995 add_identity_file(options, "~/",
1996 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001997 }
Damien Millereba71ba2000-04-29 23:57:08 +10001998 }
Damien Miller95def091999-11-25 00:26:21 +11001999 if (options->escape_char == -1)
2000 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10002001 if (options->num_system_hostfiles == 0) {
2002 options->system_hostfiles[options->num_system_hostfiles++] =
2003 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2004 options->system_hostfiles[options->num_system_hostfiles++] =
2005 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2006 }
2007 if (options->num_user_hostfiles == 0) {
2008 options->user_hostfiles[options->num_user_hostfiles++] =
2009 xstrdup(_PATH_SSH_USER_HOSTFILE);
2010 options->user_hostfiles[options->num_user_hostfiles++] =
2011 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2012 }
Damien Millerfcd93202002-02-05 12:26:34 +11002013 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002014 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002015 if (options->no_host_authentication_for_localhost == - 1)
2016 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002017 if (options->identities_only == -1)
2018 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002019 if (options->enable_ssh_keysign == -1)
2020 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002021 if (options->rekey_limit == -1)
2022 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002023 if (options->rekey_interval == -1)
2024 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002025 if (options->verify_host_key_dns == -1)
2026 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002027 if (options->server_alive_interval == -1)
2028 options->server_alive_interval = 0;
2029 if (options->server_alive_count_max == -1)
2030 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002031 if (options->control_master == -1)
2032 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002033 if (options->control_persist == -1) {
2034 options->control_persist = 0;
2035 options->control_persist_timeout = 0;
2036 }
Damien Millere1776152005-03-01 21:47:37 +11002037 if (options->hash_known_hosts == -1)
2038 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002039 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002040 options->tun_open = SSH_TUNMODE_NO;
2041 if (options->tun_local == -1)
2042 options->tun_local = SSH_TUNID_ANY;
2043 if (options->tun_remote == -1)
2044 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002045 if (options->permit_local_command == -1)
2046 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002047 if (options->visual_host_key == -1)
2048 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002049 if (options->ip_qos_interactive == -1)
2050 options->ip_qos_interactive = IPTOS_LOWDELAY;
2051 if (options->ip_qos_bulk == -1)
2052 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002053 if (options->request_tty == -1)
2054 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002055 if (options->proxy_use_fdpass == -1)
2056 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002057 if (options->canonicalize_max_dots == -1)
2058 options->canonicalize_max_dots = 1;
2059 if (options->canonicalize_fallback_local == -1)
2060 options->canonicalize_fallback_local = 1;
2061 if (options->canonicalize_hostname == -1)
2062 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002063 if (options->fingerprint_hash == -1)
2064 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002065 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002066 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002067 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2068 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2069 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2070 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2071 &options->hostbased_key_types) != 0 ||
2072 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2073 &options->pubkey_key_types) != 0)
2074 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002075
Damien Millere9fc72e2013-10-15 12:14:12 +11002076#define CLEAR_ON_NONE(v) \
2077 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002078 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002079 free(v); \
2080 v = NULL; \
2081 } \
2082 } while(0)
2083 CLEAR_ON_NONE(options->local_command);
2084 CLEAR_ON_NONE(options->proxy_command);
2085 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002086 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002087 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002088 /* options->user will be set in the main program if appropriate */
2089 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002090 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002091 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002092}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002093
Damien Miller7acefbb2014-07-18 14:11:24 +10002094struct fwdarg {
2095 char *arg;
2096 int ispath;
2097};
2098
2099/*
2100 * parse_fwd_field
2101 * parses the next field in a port forwarding specification.
2102 * sets fwd to the parsed field and advances p past the colon
2103 * or sets it to NULL at end of string.
2104 * returns 0 on success, else non-zero.
2105 */
2106static int
2107parse_fwd_field(char **p, struct fwdarg *fwd)
2108{
2109 char *ep, *cp = *p;
2110 int ispath = 0;
2111
2112 if (*cp == '\0') {
2113 *p = NULL;
2114 return -1; /* end of string */
2115 }
2116
2117 /*
2118 * A field escaped with square brackets is used literally.
2119 * XXX - allow ']' to be escaped via backslash?
2120 */
2121 if (*cp == '[') {
2122 /* find matching ']' */
2123 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2124 if (*ep == '/')
2125 ispath = 1;
2126 }
2127 /* no matching ']' or not at end of field. */
2128 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2129 return -1;
2130 /* NUL terminate the field and advance p past the colon */
2131 *ep++ = '\0';
2132 if (*ep != '\0')
2133 *ep++ = '\0';
2134 fwd->arg = cp + 1;
2135 fwd->ispath = ispath;
2136 *p = ep;
2137 return 0;
2138 }
2139
2140 for (cp = *p; *cp != '\0'; cp++) {
2141 switch (*cp) {
2142 case '\\':
2143 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002144 if (*cp == '\0')
2145 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002146 break;
2147 case '/':
2148 ispath = 1;
2149 break;
2150 case ':':
2151 *cp++ = '\0';
2152 goto done;
2153 }
2154 }
2155done:
2156 fwd->arg = *p;
2157 fwd->ispath = ispath;
2158 *p = cp;
2159 return 0;
2160}
2161
Damien Millerf91ee4c2005-03-01 21:24:33 +11002162/*
2163 * parse_forward
2164 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002165 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002166 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2167 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002168 * dynamicfwd == 1
2169 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002170 * returns number of arguments parsed or zero on error
2171 */
2172int
Damien Miller7acefbb2014-07-18 14:11:24 +10002173parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002174{
Damien Miller7acefbb2014-07-18 14:11:24 +10002175 struct fwdarg fwdargs[4];
2176 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002177 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002178
Damien Miller7acefbb2014-07-18 14:11:24 +10002179 memset(fwd, 0, sizeof(*fwd));
2180 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002181
2182 cp = p = xstrdup(fwdspec);
2183
2184 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002185 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002186 cp++;
2187
Damien Miller7acefbb2014-07-18 14:11:24 +10002188 for (i = 0; i < 4; ++i) {
2189 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002190 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002191 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002192
Damien Millerf4b39532008-11-03 19:28:21 +11002193 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002194 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002195 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002196 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002197
2198 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002199 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002200 if (fwdargs[0].ispath) {
2201 fwd->listen_path = xstrdup(fwdargs[0].arg);
2202 fwd->listen_port = PORT_STREAMLOCAL;
2203 } else {
2204 fwd->listen_host = NULL;
2205 fwd->listen_port = a2port(fwdargs[0].arg);
2206 }
Damien Millera699d952008-11-03 19:27:34 +11002207 fwd->connect_host = xstrdup("socks");
2208 break;
2209
2210 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002211 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2212 fwd->listen_path = xstrdup(fwdargs[0].arg);
2213 fwd->listen_port = PORT_STREAMLOCAL;
2214 fwd->connect_path = xstrdup(fwdargs[1].arg);
2215 fwd->connect_port = PORT_STREAMLOCAL;
2216 } else if (fwdargs[1].ispath) {
2217 fwd->listen_host = NULL;
2218 fwd->listen_port = a2port(fwdargs[0].arg);
2219 fwd->connect_path = xstrdup(fwdargs[1].arg);
2220 fwd->connect_port = PORT_STREAMLOCAL;
2221 } else {
2222 fwd->listen_host = xstrdup(fwdargs[0].arg);
2223 fwd->listen_port = a2port(fwdargs[1].arg);
2224 fwd->connect_host = xstrdup("socks");
2225 }
Damien Millera699d952008-11-03 19:27:34 +11002226 break;
2227
Damien Millerf91ee4c2005-03-01 21:24:33 +11002228 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002229 if (fwdargs[0].ispath) {
2230 fwd->listen_path = xstrdup(fwdargs[0].arg);
2231 fwd->listen_port = PORT_STREAMLOCAL;
2232 fwd->connect_host = xstrdup(fwdargs[1].arg);
2233 fwd->connect_port = a2port(fwdargs[2].arg);
2234 } else if (fwdargs[2].ispath) {
2235 fwd->listen_host = xstrdup(fwdargs[0].arg);
2236 fwd->listen_port = a2port(fwdargs[1].arg);
2237 fwd->connect_path = xstrdup(fwdargs[2].arg);
2238 fwd->connect_port = PORT_STREAMLOCAL;
2239 } else {
2240 fwd->listen_host = NULL;
2241 fwd->listen_port = a2port(fwdargs[0].arg);
2242 fwd->connect_host = xstrdup(fwdargs[1].arg);
2243 fwd->connect_port = a2port(fwdargs[2].arg);
2244 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002245 break;
2246
2247 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002248 fwd->listen_host = xstrdup(fwdargs[0].arg);
2249 fwd->listen_port = a2port(fwdargs[1].arg);
2250 fwd->connect_host = xstrdup(fwdargs[2].arg);
2251 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002252 break;
2253 default:
2254 i = 0; /* failure */
2255 }
2256
Darren Tuckera627d422013-06-02 07:31:17 +10002257 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002258
Damien Millera699d952008-11-03 19:27:34 +11002259 if (dynamicfwd) {
2260 if (!(i == 1 || i == 2))
2261 goto fail_free;
2262 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002263 if (!(i == 3 || i == 4)) {
2264 if (fwd->connect_path == NULL &&
2265 fwd->listen_path == NULL)
2266 goto fail_free;
2267 }
2268 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002269 goto fail_free;
2270 }
2271
Damien Miller7acefbb2014-07-18 14:11:24 +10002272 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2273 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002274 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002275 if (fwd->connect_host != NULL &&
2276 strlen(fwd->connect_host) >= NI_MAXHOST)
2277 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002278 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2279 if (fwd->connect_path != NULL &&
2280 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2281 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002282 if (fwd->listen_host != NULL &&
2283 strlen(fwd->listen_host) >= NI_MAXHOST)
2284 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002285 if (fwd->listen_path != NULL &&
2286 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2287 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002288
2289 return (i);
2290
2291 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002292 free(fwd->connect_host);
2293 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002294 free(fwd->connect_path);
2295 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002296 free(fwd->listen_host);
2297 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002298 free(fwd->listen_path);
2299 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002300 return (0);
2301}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002302
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002303int
2304parse_jump(const char *s, Options *o, int active)
2305{
2306 char *orig, *sdup, *cp;
2307 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002308 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002309
2310 active &= o->proxy_command == NULL && o->jump_host == NULL;
2311
2312 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002313 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002314 do {
2315 if ((cp = strrchr(sdup, ',')) == NULL)
2316 cp = sdup; /* last */
2317 else
2318 *cp++ = '\0';
2319
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002320 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002321 /* First argument and configuration is active */
2322 if (parse_user_host_port(cp, &user, &host, &port) != 0)
2323 goto out;
2324 } else {
2325 /* Subsequent argument or inactive configuration */
2326 if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2327 goto out;
2328 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002329 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002330 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002331 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002332 if (active) {
2333 o->jump_user = user;
2334 o->jump_host = host;
2335 o->jump_port = port;
2336 o->proxy_command = xstrdup("none");
2337 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002338 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2339 o->jump_extra = xstrdup(s);
2340 o->jump_extra[cp - s] = '\0';
2341 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002342 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002343 ret = 0;
2344 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002345 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002346 free(user);
2347 free(host);
2348 return ret;
2349}
2350
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002351/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2352static const char *
2353fmt_multistate_int(int val, const struct multistate *m)
2354{
2355 u_int i;
2356
2357 for (i = 0; m[i].key != NULL; i++) {
2358 if (m[i].value == val)
2359 return m[i].key;
2360 }
2361 return "UNKNOWN";
2362}
2363
2364static const char *
2365fmt_intarg(OpCodes code, int val)
2366{
2367 if (val == -1)
2368 return "unset";
2369 switch (code) {
2370 case oAddressFamily:
2371 return fmt_multistate_int(val, multistate_addressfamily);
2372 case oVerifyHostKeyDNS:
2373 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002374 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002375 return fmt_multistate_int(val, multistate_yesnoask);
2376 case oControlMaster:
2377 return fmt_multistate_int(val, multistate_controlmaster);
2378 case oTunnel:
2379 return fmt_multistate_int(val, multistate_tunnel);
2380 case oRequestTTY:
2381 return fmt_multistate_int(val, multistate_requesttty);
2382 case oCanonicalizeHostname:
2383 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002384 case oFingerprintHash:
2385 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002386 case oProtocol:
2387 switch (val) {
2388 case SSH_PROTO_1:
2389 return "1";
2390 case SSH_PROTO_2:
2391 return "2";
2392 case (SSH_PROTO_1|SSH_PROTO_2):
2393 return "2,1";
2394 default:
2395 return "UNKNOWN";
2396 }
2397 default:
2398 switch (val) {
2399 case 0:
2400 return "no";
2401 case 1:
2402 return "yes";
2403 default:
2404 return "UNKNOWN";
2405 }
2406 }
2407}
2408
2409static const char *
2410lookup_opcode_name(OpCodes code)
2411{
2412 u_int i;
2413
2414 for (i = 0; keywords[i].name != NULL; i++)
2415 if (keywords[i].opcode == code)
2416 return(keywords[i].name);
2417 return "UNKNOWN";
2418}
2419
2420static void
2421dump_cfg_int(OpCodes code, int val)
2422{
2423 printf("%s %d\n", lookup_opcode_name(code), val);
2424}
2425
2426static void
2427dump_cfg_fmtint(OpCodes code, int val)
2428{
2429 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2430}
2431
2432static void
2433dump_cfg_string(OpCodes code, const char *val)
2434{
2435 if (val == NULL)
2436 return;
2437 printf("%s %s\n", lookup_opcode_name(code), val);
2438}
2439
2440static void
2441dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2442{
2443 u_int i;
2444
2445 for (i = 0; i < count; i++)
2446 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2447}
2448
2449static void
2450dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2451{
2452 u_int i;
2453
2454 printf("%s", lookup_opcode_name(code));
2455 for (i = 0; i < count; i++)
2456 printf(" %s", vals[i]);
2457 printf("\n");
2458}
2459
2460static void
2461dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2462{
2463 const struct Forward *fwd;
2464 u_int i;
2465
2466 /* oDynamicForward */
2467 for (i = 0; i < count; i++) {
2468 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002469 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002470 strcmp(fwd->connect_host, "socks") != 0)
2471 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002472 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002473 strcmp(fwd->connect_host, "socks") == 0)
2474 continue;
2475 printf("%s", lookup_opcode_name(code));
2476 if (fwd->listen_port == PORT_STREAMLOCAL)
2477 printf(" %s", fwd->listen_path);
2478 else if (fwd->listen_host == NULL)
2479 printf(" %d", fwd->listen_port);
2480 else {
2481 printf(" [%s]:%d",
2482 fwd->listen_host, fwd->listen_port);
2483 }
2484 if (code != oDynamicForward) {
2485 if (fwd->connect_port == PORT_STREAMLOCAL)
2486 printf(" %s", fwd->connect_path);
2487 else if (fwd->connect_host == NULL)
2488 printf(" %d", fwd->connect_port);
2489 else {
2490 printf(" [%s]:%d",
2491 fwd->connect_host, fwd->connect_port);
2492 }
2493 }
2494 printf("\n");
2495 }
2496}
2497
2498void
2499dump_client_config(Options *o, const char *host)
2500{
2501 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002502 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002503
djm@openbsd.org60a92472015-08-21 23:53:08 +00002504 /* This is normally prepared in ssh_kex2 */
2505 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2506 fatal("%s: kex_assemble_names failed", __func__);
2507
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002508 /* Most interesting options first: user, host, port */
2509 dump_cfg_string(oUser, o->user);
2510 dump_cfg_string(oHostName, host);
2511 dump_cfg_int(oPort, o->port);
2512
2513 /* Flag options */
2514 dump_cfg_fmtint(oAddressFamily, o->address_family);
2515 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2516 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2517 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2518 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2519 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2520 dump_cfg_fmtint(oCompression, o->compression);
2521 dump_cfg_fmtint(oControlMaster, o->control_master);
2522 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002523 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002524 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002525 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002526 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2527 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2528 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2529 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2530#ifdef GSSAPI
2531 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2532 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2533#endif /* GSSAPI */
2534 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2535 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2536 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2537 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2538 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2539 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2540 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2541 dump_cfg_fmtint(oProtocol, o->protocol);
2542 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2543 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2544 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002545#ifdef WITH_RSA1
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002546 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2547 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002548#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002549 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2550 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2551 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2552 dump_cfg_fmtint(oTunnel, o->tun_open);
2553 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2554 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2555 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002556 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002557
2558 /* Integer options */
2559 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002560#ifdef WITH_SSH1
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002561 dump_cfg_int(oCompressionLevel, o->compression_level);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002562#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002563 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2564 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2565 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2566 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2567 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2568
2569 /* String options */
2570 dump_cfg_string(oBindAddress, o->bind_address);
2571 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2572 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002573 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002574 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002575 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002576 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002577 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2578 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2579 dump_cfg_string(oLocalCommand, o->local_command);
2580 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2581 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002582#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002583 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002584#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002585 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002586 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002587 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002588 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002589
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002590 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002591 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2592 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2593 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2594
2595 /* String array options */
2596 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2597 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2598 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2599 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2600 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2601
2602 /* Special cases */
2603
2604 /* oConnectTimeout */
2605 if (o->connection_timeout == -1)
2606 printf("connecttimeout none\n");
2607 else
2608 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2609
2610 /* oTunnelDevice */
2611 printf("tunneldevice");
2612 if (o->tun_local == SSH_TUNID_ANY)
2613 printf(" any");
2614 else
2615 printf(" %d", o->tun_local);
2616 if (o->tun_remote == SSH_TUNID_ANY)
2617 printf(":any");
2618 else
2619 printf(":%d", o->tun_remote);
2620 printf("\n");
2621
2622 /* oCanonicalizePermittedCNAMEs */
2623 if ( o->num_permitted_cnames > 0) {
2624 printf("canonicalizePermittedcnames");
2625 for (i = 0; i < o->num_permitted_cnames; i++) {
2626 printf(" %s:%s", o->permitted_cnames[i].source_list,
2627 o->permitted_cnames[i].target_list);
2628 }
2629 printf("\n");
2630 }
2631
2632 /* oCipher */
2633 if (o->cipher != SSH_CIPHER_NOT_SET)
2634 printf("Cipher %s\n", cipher_name(o->cipher));
2635
2636 /* oControlPersist */
2637 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2638 dump_cfg_fmtint(oControlPersist, o->control_persist);
2639 else
2640 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2641
2642 /* oEscapeChar */
2643 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2644 printf("escapechar none\n");
2645 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002646 vis(buf, o->escape_char, VIS_WHITE, 0);
2647 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002648 }
2649
2650 /* oIPQoS */
2651 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2652 printf("%s\n", iptos2str(o->ip_qos_bulk));
2653
2654 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002655 printf("rekeylimit %llu %d\n",
2656 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002657
2658 /* oStreamLocalBindMask */
2659 printf("streamlocalbindmask 0%o\n",
2660 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002661
2662 /* oProxyCommand / oProxyJump */
2663 if (o->jump_host == NULL)
2664 dump_cfg_string(oProxyCommand, o->proxy_command);
2665 else {
2666 /* Check for numeric addresses */
2667 i = strchr(o->jump_host, ':') != NULL ||
2668 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2669 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2670 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002671 /* optional additional jump spec */
2672 o->jump_extra == NULL ? "" : o->jump_extra,
2673 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002674 /* optional user */
2675 o->jump_user == NULL ? "" : o->jump_user,
2676 o->jump_user == NULL ? "" : "@",
2677 /* opening [ if hostname is numeric */
2678 i ? "[" : "",
2679 /* mandatory hostname */
2680 o->jump_host,
2681 /* closing ] if hostname is numeric */
2682 i ? "]" : "",
2683 /* optional port number */
2684 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002685 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002686 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002687}