blob: 72b4a637c301caf9426fd0187b8f892f67cdd13c [file] [log] [blame]
djm@openbsd.org381a2612017-01-30 00:38:50 +00001/* $OpenBSD: readconf.c,v 1.266 2017/01/30 00:38:50 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +000042#ifdef USE_SYSTEM_GLOB
43# include <glob.h>
44#else
45# include "openbsd-compat/glob.h"
46#endif
Darren Tuckere194ba42013-05-16 20:47:31 +100047#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100048#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100049#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000050#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
51# include <vis.h>
52#endif
Damien Millerc7b06362006-03-15 11:53:45 +110053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "ssh.h"
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
Damien Millerc652cac2003-05-14 13:40:54 +1000845 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000846 if ((len = strlen(line)) == 0)
847 return 0;
848 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000849 if (strchr(WHITESPACE, line[len]) == NULL)
850 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.orgf9eca242015-07-30 00:01:34 +00001197 if (!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.orgf9eca242015-07-30 00:01:34 +00001208 if (!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.orgf9eca242015-07-30 00:01:34 +00001220 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001221 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1222 filename, linenum, arg ? arg : "<NONE>");
1223 if (*activep && options->kex_algorithms == NULL)
1224 options->kex_algorithms = xstrdup(arg);
1225 break;
1226
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001227 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001228 charptr = &options->hostkeyalgorithms;
1229parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001230 arg = strdelim(&s);
1231 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001232 fatal("%.200s line %d: Missing argument.",
1233 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001234 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001235 fatal("%s line %d: Bad key types '%s'.",
1236 filename, linenum, arg ? arg : "<NONE>");
1237 if (*activep && *charptr == NULL)
1238 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001239 break;
1240
Damien Miller78928792000-04-12 20:17:38 +10001241 case oProtocol:
1242 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001243 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001244 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001245 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001246 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001247 if (value == SSH_PROTO_UNKNOWN)
1248 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001249 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001250 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1251 *intptr = value;
1252 break;
1253
Damien Miller95def091999-11-25 00:26:21 +11001254 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001255 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001256 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001257 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001258 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001259 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001260 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001261 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1262 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001263 break;
1264
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001265 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001266 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001267 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001268 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001269 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001270 fatal("%.200s line %d: Missing port argument.",
1271 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001272
Damien Millera699d952008-11-03 19:27:34 +11001273 if (opcode == oLocalForward ||
1274 opcode == oRemoteForward) {
1275 arg2 = strdelim(&s);
1276 if (arg2 == NULL || *arg2 == '\0')
1277 fatal("%.200s line %d: Missing target argument.",
1278 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001279
Damien Millera699d952008-11-03 19:27:34 +11001280 /* construct a string for parse_forward */
1281 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1282 } else if (opcode == oDynamicForward) {
1283 strlcpy(fwdarg, arg, sizeof(fwdarg));
1284 }
1285
1286 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001287 opcode == oDynamicForward ? 1 : 0,
1288 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001289 fatal("%.200s line %d: Bad forwarding specification.",
1290 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001291
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001292 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001293 if (opcode == oLocalForward ||
1294 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001295 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001296 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001297 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001298 }
Damien Miller95def091999-11-25 00:26:21 +11001299 break;
1300
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001301 case oClearAllForwardings:
1302 intptr = &options->clear_forwardings;
1303 goto parse_flag;
1304
Damien Miller95def091999-11-25 00:26:21 +11001305 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001306 if (cmdline)
1307 fatal("Host directive not supported as a command-line "
1308 "option");
Damien Miller95def091999-11-25 00:26:21 +11001309 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001310 arg2 = NULL;
1311 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001312 if ((flags & SSHCONF_NEVERMATCH) != 0)
1313 break;
Damien Millerfe924212011-05-15 08:44:45 +10001314 negated = *arg == '!';
1315 if (negated)
1316 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001317 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001318 if (negated) {
1319 debug("%.200s line %d: Skipping Host "
1320 "block because of negated match "
1321 "for %.100s", filename, linenum,
1322 arg);
1323 *activep = 0;
1324 break;
1325 }
1326 if (!*activep)
1327 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001328 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001329 }
Damien Millerfe924212011-05-15 08:44:45 +10001330 }
1331 if (*activep)
1332 debug("%.200s line %d: Applying options for %.100s",
1333 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001334 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001335 return 0;
1336
Damien Miller194fd902013-10-15 12:13:05 +11001337 case oMatch:
1338 if (cmdline)
1339 fatal("Host directive not supported as a command-line "
1340 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001341 value = match_cfg_line(options, &s, pw, host, original_host,
1342 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001343 if (value < 0)
1344 fatal("%.200s line %d: Bad Match condition", filename,
1345 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001346 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001347 break;
1348
Damien Miller95def091999-11-25 00:26:21 +11001349 case oEscapeChar:
1350 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001351 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001352 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001353 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001354 if (strcmp(arg, "none") == 0)
1355 value = SSH_ESCAPECHAR_NONE;
1356 else if (arg[1] == '\0')
1357 value = (u_char) arg[0];
1358 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001359 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1360 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001361 else {
1362 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001363 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001364 /* NOTREACHED */
1365 value = 0; /* Avoid compiler warning. */
1366 }
1367 if (*activep && *intptr == -1)
1368 *intptr = value;
1369 break;
1370
Damien Miller20a8f972003-05-18 20:50:30 +10001371 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001372 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001373 multistate_ptr = multistate_addressfamily;
1374 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001375
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001376 case oEnableSSHKeysign:
1377 intptr = &options->enable_ssh_keysign;
1378 goto parse_flag;
1379
Damien Millerbd394c32004-03-08 23:12:36 +11001380 case oIdentitiesOnly:
1381 intptr = &options->identities_only;
1382 goto parse_flag;
1383
Damien Miller509b0102003-12-17 16:33:10 +11001384 case oServerAliveInterval:
1385 intptr = &options->server_alive_interval;
1386 goto parse_time;
1387
1388 case oServerAliveCountMax:
1389 intptr = &options->server_alive_count_max;
1390 goto parse_int;
1391
Darren Tucker46bc0752004-05-02 22:11:30 +10001392 case oSendEnv:
1393 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1394 if (strchr(arg, '=') != NULL)
1395 fatal("%s line %d: Invalid environment name.",
1396 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001397 if (!*activep)
1398 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001399 if (options->num_send_env >= MAX_SEND_ENV)
1400 fatal("%s line %d: too many send env.",
1401 filename, linenum);
1402 options->send_env[options->num_send_env++] =
1403 xstrdup(arg);
1404 }
1405 break;
1406
Damien Miller0e220db2004-06-15 10:34:08 +10001407 case oControlPath:
1408 charptr = &options->control_path;
1409 goto parse_string;
1410
1411 case oControlMaster:
1412 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001413 multistate_ptr = multistate_controlmaster;
1414 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001415
Damien Millere11e1ea2010-08-03 16:04:46 +10001416 case oControlPersist:
1417 /* no/false/yes/true, or a time spec */
1418 intptr = &options->control_persist;
1419 arg = strdelim(&s);
1420 if (!arg || *arg == '\0')
1421 fatal("%.200s line %d: Missing ControlPersist"
1422 " argument.", filename, linenum);
1423 value = 0;
1424 value2 = 0; /* timeout */
1425 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1426 value = 0;
1427 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1428 value = 1;
1429 else if ((value2 = convtime(arg)) >= 0)
1430 value = 1;
1431 else
1432 fatal("%.200s line %d: Bad ControlPersist argument.",
1433 filename, linenum);
1434 if (*activep && *intptr == -1) {
1435 *intptr = value;
1436 options->control_persist_timeout = value2;
1437 }
1438 break;
1439
Damien Millere1776152005-03-01 21:47:37 +11001440 case oHashKnownHosts:
1441 intptr = &options->hash_known_hosts;
1442 goto parse_flag;
1443
Damien Millerd27b9472005-12-13 19:29:02 +11001444 case oTunnel:
1445 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001446 multistate_ptr = multistate_tunnel;
1447 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001448
1449 case oTunnelDevice:
1450 arg = strdelim(&s);
1451 if (!arg || *arg == '\0')
1452 fatal("%.200s line %d: Missing argument.", filename, linenum);
1453 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001454 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001455 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1456 if (*activep) {
1457 options->tun_local = value;
1458 options->tun_remote = value2;
1459 }
1460 break;
1461
1462 case oLocalCommand:
1463 charptr = &options->local_command;
1464 goto parse_command;
1465
1466 case oPermitLocalCommand:
1467 intptr = &options->permit_local_command;
1468 goto parse_flag;
1469
Damien Miller10288242008-06-30 00:04:03 +10001470 case oVisualHostKey:
1471 intptr = &options->visual_host_key;
1472 goto parse_flag;
1473
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001474 case oInclude:
1475 if (cmdline)
1476 fatal("Include directive not supported as a "
1477 "command-line option");
1478 value = 0;
1479 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1480 /*
1481 * Ensure all paths are anchored. User configuration
1482 * files may begin with '~/' but system configurations
1483 * must not. If the path is relative, then treat it
1484 * as living in ~/.ssh for user configurations or
1485 * /etc/ssh for system ones.
1486 */
1487 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1488 fatal("%.200s line %d: bad include path %s.",
1489 filename, linenum, arg);
1490 if (*arg != '/' && *arg != '~') {
1491 xasprintf(&arg2, "%s/%s",
1492 (flags & SSHCONF_USERCONF) ?
1493 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1494 } else
1495 arg2 = xstrdup(arg);
1496 memset(&gl, 0, sizeof(gl));
1497 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1498 if (r == GLOB_NOMATCH) {
1499 debug("%.200s line %d: include %s matched no "
1500 "files",filename, linenum, arg2);
1501 continue;
1502 } else if (r != 0 || gl.gl_pathc < 0)
1503 fatal("%.200s line %d: glob failed for %s.",
1504 filename, linenum, arg2);
1505 free(arg2);
1506 oactive = *activep;
1507 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1508 debug3("%.200s line %d: Including file %s "
1509 "depth %d%s", filename, linenum,
1510 gl.gl_pathv[i], depth,
1511 oactive ? "" : " (parse only)");
1512 r = read_config_file_depth(gl.gl_pathv[i],
1513 pw, host, original_host, options,
1514 flags | SSHCONF_CHECKPERM |
1515 (oactive ? 0 : SSHCONF_NEVERMATCH),
1516 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001517 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001518 fatal("Can't open user config file "
1519 "%.100s: %.100s", gl.gl_pathv[i],
1520 strerror(errno));
1521 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001522 /*
1523 * don't let Match in includes clobber the
1524 * containing file's Match state.
1525 */
1526 *activep = oactive;
1527 if (r != 1)
1528 value = -1;
1529 }
1530 globfree(&gl);
1531 }
1532 if (value != 0)
1533 return value;
1534 break;
1535
Damien Miller0dac6fb2010-11-20 15:19:38 +11001536 case oIPQoS:
1537 arg = strdelim(&s);
1538 if ((value = parse_ipqos(arg)) == -1)
1539 fatal("%s line %d: Bad IPQoS value: %s",
1540 filename, linenum, arg);
1541 arg = strdelim(&s);
1542 if (arg == NULL)
1543 value2 = value;
1544 else if ((value2 = parse_ipqos(arg)) == -1)
1545 fatal("%s line %d: Bad IPQoS value: %s",
1546 filename, linenum, arg);
1547 if (*activep) {
1548 options->ip_qos_interactive = value;
1549 options->ip_qos_bulk = value2;
1550 }
1551 break;
1552
Damien Miller21771e22011-05-15 08:45:50 +10001553 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001554 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001555 multistate_ptr = multistate_requesttty;
1556 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001557
Darren Tucker07636982013-05-16 20:30:03 +10001558 case oIgnoreUnknown:
1559 charptr = &options->ignored_unknown;
1560 goto parse_string;
1561
Damien Miller1262b662013-08-21 02:44:24 +10001562 case oProxyUseFdpass:
1563 intptr = &options->proxy_use_fdpass;
1564 goto parse_flag;
1565
Damien Miller0faf7472013-10-17 11:47:23 +11001566 case oCanonicalDomains:
1567 value = options->num_canonical_domains != 0;
1568 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1569 valid_domain(arg, filename, linenum);
1570 if (!*activep || value)
1571 continue;
1572 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1573 fatal("%s line %d: too many hostname suffixes.",
1574 filename, linenum);
1575 options->canonical_domains[
1576 options->num_canonical_domains++] = xstrdup(arg);
1577 }
1578 break;
1579
Damien Miller38505592013-10-17 11:48:13 +11001580 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001581 value = options->num_permitted_cnames != 0;
1582 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1583 /* Either '*' for everything or 'list:list' */
1584 if (strcmp(arg, "*") == 0)
1585 arg2 = arg;
1586 else {
1587 lowercase(arg);
1588 if ((arg2 = strchr(arg, ':')) == NULL ||
1589 arg2[1] == '\0') {
1590 fatal("%s line %d: "
1591 "Invalid permitted CNAME \"%s\"",
1592 filename, linenum, arg);
1593 }
1594 *arg2 = '\0';
1595 arg2++;
1596 }
1597 if (!*activep || value)
1598 continue;
1599 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1600 fatal("%s line %d: too many permitted CNAMEs.",
1601 filename, linenum);
1602 cname = options->permitted_cnames +
1603 options->num_permitted_cnames++;
1604 cname->source_list = xstrdup(arg);
1605 cname->target_list = xstrdup(arg2);
1606 }
1607 break;
1608
Damien Miller38505592013-10-17 11:48:13 +11001609 case oCanonicalizeHostname:
1610 intptr = &options->canonicalize_hostname;
1611 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001612 goto parse_multistate;
1613
Damien Miller38505592013-10-17 11:48:13 +11001614 case oCanonicalizeMaxDots:
1615 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001616 goto parse_int;
1617
Damien Miller38505592013-10-17 11:48:13 +11001618 case oCanonicalizeFallbackLocal:
1619 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001620 goto parse_flag;
1621
Damien Miller7acefbb2014-07-18 14:11:24 +10001622 case oStreamLocalBindMask:
1623 arg = strdelim(&s);
1624 if (!arg || *arg == '\0')
1625 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1626 /* Parse mode in octal format */
1627 value = strtol(arg, &endofnumber, 8);
1628 if (arg == endofnumber || value < 0 || value > 0777)
1629 fatal("%.200s line %d: Bad mask.", filename, linenum);
1630 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1631 break;
1632
1633 case oStreamLocalBindUnlink:
1634 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1635 goto parse_flag;
1636
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001637 case oRevokedHostKeys:
1638 charptr = &options->revoked_host_keys;
1639 goto parse_string;
1640
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001641 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001642 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001643 arg = strdelim(&s);
1644 if (!arg || *arg == '\0')
1645 fatal("%.200s line %d: Missing argument.",
1646 filename, linenum);
1647 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1648 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1649 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001650 if (*activep && *intptr == -1)
1651 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001652 break;
1653
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001654 case oUpdateHostkeys:
1655 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001656 multistate_ptr = multistate_yesnoask;
1657 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001658
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001659 case oHostbasedKeyTypes:
1660 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001661 goto parse_keytypes;
1662
1663 case oPubkeyAcceptedKeyTypes:
1664 charptr = &options->pubkey_key_types;
1665 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001666
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001667 case oAddKeysToAgent:
1668 intptr = &options->add_keys_to_agent;
1669 multistate_ptr = multistate_yesnoaskconfirm;
1670 goto parse_multistate;
1671
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001672 case oIdentityAgent:
1673 charptr = &options->identity_agent;
1674 goto parse_string;
1675
Ben Lindstrom4daea862002-06-09 20:04:02 +00001676 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001677 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001678 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001679 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001680
Damien Millerf9b3feb2003-05-16 11:38:32 +10001681 case oUnsupported:
1682 error("%s line %d: Unsupported option \"%s\"",
1683 filename, linenum, keyword);
1684 return 0;
1685
Damien Miller95def091999-11-25 00:26:21 +11001686 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001687 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001688 }
1689
1690 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001691 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001692 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001693 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001694 }
Damien Miller95def091999-11-25 00:26:21 +11001695 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696}
1697
Damien Miller5428f641999-11-25 11:54:57 +11001698/*
1699 * Reads the config file and modifies the options accordingly. Options
1700 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001701 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001702 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001703int
Damien Miller194fd902013-10-15 12:13:05 +11001704read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001705 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001706{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001707 int active = 1;
1708
1709 return read_config_file_depth(filename, pw, host, original_host,
1710 options, flags, &active, 0);
1711}
1712
1713#define READCONF_MAX_DEPTH 16
1714static int
1715read_config_file_depth(const char *filename, struct passwd *pw,
1716 const char *host, const char *original_host, Options *options,
1717 int flags, int *activep, int depth)
1718{
Damien Miller95def091999-11-25 00:26:21 +11001719 FILE *f;
1720 char line[1024];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001721 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001722 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001723
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001724 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1725 fatal("Too many recursive configuration includes");
1726
Damien Miller57a44762004-04-20 20:11:57 +10001727 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001728 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729
Darren Tuckeraefa3682013-04-05 11:18:35 +11001730 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001731 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001732
Damien Miller33793852004-06-15 10:27:55 +10001733 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001734 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001735 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001736 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001737 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001738 }
1739
Damien Miller95def091999-11-25 00:26:21 +11001740 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741
Damien Miller5428f641999-11-25 11:54:57 +11001742 /*
1743 * Mark that we are now processing the options. This flag is turned
1744 * on/off by Host specifications.
1745 */
Damien Miller95def091999-11-25 00:26:21 +11001746 linenum = 0;
1747 while (fgets(line, sizeof(line), f)) {
1748 /* Update line number counter. */
1749 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001750 if (process_config_line_depth(options, pw, host, original_host,
1751 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001752 bad_options++;
1753 }
1754 fclose(f);
1755 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001756 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001757 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001758 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001759}
1760
Damien Miller13f97b22014-02-24 15:57:55 +11001761/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1762int
1763option_clear_or_none(const char *o)
1764{
1765 return o == NULL || strcasecmp(o, "none") == 0;
1766}
1767
Damien Miller5428f641999-11-25 11:54:57 +11001768/*
1769 * Initializes options to special values that indicate that they have not yet
1770 * been set. Read_config_file will only set options with this value. Options
1771 * are processed in the following order: command line, user config file,
1772 * system config file. Last, fill_default_options is called.
1773 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001774
Damien Miller4af51302000-04-16 11:18:38 +10001775void
Damien Miller95def091999-11-25 00:26:21 +11001776initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001777{
Damien Miller95def091999-11-25 00:26:21 +11001778 memset(options, 'X', sizeof(*options));
1779 options->forward_agent = -1;
1780 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001781 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001782 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001783 options->stdio_forward_host = NULL;
1784 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001785 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001786 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001787 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001788 options->fwd_opts.gateway_ports = -1;
1789 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1790 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001791 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001792 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001793 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001794 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001795 options->gss_authentication = -1;
1796 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001797 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001798 options->kbd_interactive_authentication = -1;
1799 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001800 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001801 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001802 options->batch_mode = -1;
1803 options->check_host_ip = -1;
1804 options->strict_host_key_checking = -1;
1805 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001806 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001807 options->compression_level = -1;
1808 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001809 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001810 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001811 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001812 options->number_of_password_prompts = -1;
1813 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001814 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001815 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001816 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001817 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001818 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001819 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001820 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001821 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001822 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001823 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001824 options->jump_user = NULL;
1825 options->jump_host = NULL;
1826 options->jump_port = -1;
1827 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001828 options->user = NULL;
1829 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001830 options->num_system_hostfiles = 0;
1831 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001832 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001833 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001834 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001835 options->num_remote_forwards = 0;
Damien Millerfcd93202002-02-05 12:26:34 +11001836 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001837 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001838 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001839 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001840 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001841 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001842 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001843 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001844 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001845 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001846 options->server_alive_interval = -1;
1847 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001848 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001849 options->control_path = NULL;
1850 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001851 options->control_persist = -1;
1852 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001853 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001854 options->tun_open = -1;
1855 options->tun_local = -1;
1856 options->tun_remote = -1;
1857 options->local_command = NULL;
1858 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001859 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001860 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001861 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001862 options->ip_qos_interactive = -1;
1863 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001864 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001865 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001866 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001867 options->num_canonical_domains = 0;
1868 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001869 options->canonicalize_max_dots = -1;
1870 options->canonicalize_fallback_local = -1;
1871 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001872 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001873 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001874 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001875 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001876 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001877}
1878
Damien Miller5428f641999-11-25 11:54:57 +11001879/*
Damien Miller13f97b22014-02-24 15:57:55 +11001880 * A petite version of fill_default_options() that just fills the options
1881 * needed for hostname canonicalization to proceed.
1882 */
1883void
1884fill_default_options_for_canonicalization(Options *options)
1885{
1886 if (options->canonicalize_max_dots == -1)
1887 options->canonicalize_max_dots = 1;
1888 if (options->canonicalize_fallback_local == -1)
1889 options->canonicalize_fallback_local = 1;
1890 if (options->canonicalize_hostname == -1)
1891 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1892}
1893
1894/*
Damien Miller5428f641999-11-25 11:54:57 +11001895 * Called after processing other sources of option data, this fills those
1896 * options for which no value has been specified with their default values.
1897 */
Damien Miller4af51302000-04-16 11:18:38 +10001898void
Damien Miller95def091999-11-25 00:26:21 +11001899fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001900{
Damien Miller95def091999-11-25 00:26:21 +11001901 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001902 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001903 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001904 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001905 if (options->forward_x11_trusted == -1)
1906 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001907 if (options->forward_x11_timeout == -1)
1908 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001909 /*
1910 * stdio forwarding (-W) changes the default for these but we defer
1911 * setting the values so they can be overridden.
1912 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001913 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001914 options->exit_on_forward_failure =
1915 options->stdio_forward_host != NULL ? 1 : 0;
1916 if (options->clear_forwardings == -1)
1917 options->clear_forwardings =
1918 options->stdio_forward_host != NULL ? 1 : 0;
1919 if (options->clear_forwardings == 1)
1920 clear_forwardings(options);
1921
Damien Millerd3a18572000-06-07 19:55:44 +10001922 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001923 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001924 if (options->fwd_opts.gateway_ports == -1)
1925 options->fwd_opts.gateway_ports = 0;
1926 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1927 options->fwd_opts.streamlocal_bind_mask = 0177;
1928 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1929 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001930 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001931 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001932 if (options->rsa_authentication == -1)
1933 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001934 if (options->pubkey_authentication == -1)
1935 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001936 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001937 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001938 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001939 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001940 if (options->gss_deleg_creds == -1)
1941 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001942 if (options->password_authentication == -1)
1943 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001944 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001945 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001946 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001947 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001948 if (options->hostbased_authentication == -1)
1949 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001950 if (options->batch_mode == -1)
1951 options->batch_mode = 0;
1952 if (options->check_host_ip == -1)
1953 options->check_host_ip = 1;
1954 if (options->strict_host_key_checking == -1)
1955 options->strict_host_key_checking = 2; /* 2 is default */
1956 if (options->compression == -1)
1957 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001958 if (options->tcp_keep_alive == -1)
1959 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001960 if (options->compression_level == -1)
1961 options->compression_level = 6;
1962 if (options->port == -1)
1963 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001964 if (options->address_family == -1)
1965 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001966 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001967 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001968 if (options->number_of_password_prompts == -1)
1969 options->number_of_password_prompts = 3;
1970 /* Selected in ssh_login(). */
1971 if (options->cipher == -1)
1972 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001973 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001974 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001975 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001976 if (options->add_keys_to_agent == -1)
1977 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001978 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001979 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001980 add_identity_file(options, "~/",
1981 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001982 }
1983 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001984 add_identity_file(options, "~/",
1985 _PATH_SSH_CLIENT_ID_RSA, 0);
1986 add_identity_file(options, "~/",
1987 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001988#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001989 add_identity_file(options, "~/",
1990 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001991#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001992 add_identity_file(options, "~/",
1993 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001994 }
Damien Millereba71ba2000-04-29 23:57:08 +10001995 }
Damien Miller95def091999-11-25 00:26:21 +11001996 if (options->escape_char == -1)
1997 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001998 if (options->num_system_hostfiles == 0) {
1999 options->system_hostfiles[options->num_system_hostfiles++] =
2000 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2001 options->system_hostfiles[options->num_system_hostfiles++] =
2002 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2003 }
2004 if (options->num_user_hostfiles == 0) {
2005 options->user_hostfiles[options->num_user_hostfiles++] =
2006 xstrdup(_PATH_SSH_USER_HOSTFILE);
2007 options->user_hostfiles[options->num_user_hostfiles++] =
2008 xstrdup(_PATH_SSH_USER_HOSTFILE2);
2009 }
Damien Millerfcd93202002-02-05 12:26:34 +11002010 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002011 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002012 if (options->no_host_authentication_for_localhost == - 1)
2013 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002014 if (options->identities_only == -1)
2015 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002016 if (options->enable_ssh_keysign == -1)
2017 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002018 if (options->rekey_limit == -1)
2019 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002020 if (options->rekey_interval == -1)
2021 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002022 if (options->verify_host_key_dns == -1)
2023 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002024 if (options->server_alive_interval == -1)
2025 options->server_alive_interval = 0;
2026 if (options->server_alive_count_max == -1)
2027 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002028 if (options->control_master == -1)
2029 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002030 if (options->control_persist == -1) {
2031 options->control_persist = 0;
2032 options->control_persist_timeout = 0;
2033 }
Damien Millere1776152005-03-01 21:47:37 +11002034 if (options->hash_known_hosts == -1)
2035 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002036 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002037 options->tun_open = SSH_TUNMODE_NO;
2038 if (options->tun_local == -1)
2039 options->tun_local = SSH_TUNID_ANY;
2040 if (options->tun_remote == -1)
2041 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002042 if (options->permit_local_command == -1)
2043 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002044 if (options->visual_host_key == -1)
2045 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002046 if (options->ip_qos_interactive == -1)
2047 options->ip_qos_interactive = IPTOS_LOWDELAY;
2048 if (options->ip_qos_bulk == -1)
2049 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002050 if (options->request_tty == -1)
2051 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002052 if (options->proxy_use_fdpass == -1)
2053 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002054 if (options->canonicalize_max_dots == -1)
2055 options->canonicalize_max_dots = 1;
2056 if (options->canonicalize_fallback_local == -1)
2057 options->canonicalize_fallback_local = 1;
2058 if (options->canonicalize_hostname == -1)
2059 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002060 if (options->fingerprint_hash == -1)
2061 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002062 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002063 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002064 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2065 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2066 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2067 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2068 &options->hostbased_key_types) != 0 ||
2069 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2070 &options->pubkey_key_types) != 0)
2071 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002072
Damien Millere9fc72e2013-10-15 12:14:12 +11002073#define CLEAR_ON_NONE(v) \
2074 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002075 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002076 free(v); \
2077 v = NULL; \
2078 } \
2079 } while(0)
2080 CLEAR_ON_NONE(options->local_command);
2081 CLEAR_ON_NONE(options->proxy_command);
2082 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002083 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002084 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002085 /* options->user will be set in the main program if appropriate */
2086 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002087 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002088 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002089}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002090
Damien Miller7acefbb2014-07-18 14:11:24 +10002091struct fwdarg {
2092 char *arg;
2093 int ispath;
2094};
2095
2096/*
2097 * parse_fwd_field
2098 * parses the next field in a port forwarding specification.
2099 * sets fwd to the parsed field and advances p past the colon
2100 * or sets it to NULL at end of string.
2101 * returns 0 on success, else non-zero.
2102 */
2103static int
2104parse_fwd_field(char **p, struct fwdarg *fwd)
2105{
2106 char *ep, *cp = *p;
2107 int ispath = 0;
2108
2109 if (*cp == '\0') {
2110 *p = NULL;
2111 return -1; /* end of string */
2112 }
2113
2114 /*
2115 * A field escaped with square brackets is used literally.
2116 * XXX - allow ']' to be escaped via backslash?
2117 */
2118 if (*cp == '[') {
2119 /* find matching ']' */
2120 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2121 if (*ep == '/')
2122 ispath = 1;
2123 }
2124 /* no matching ']' or not at end of field. */
2125 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2126 return -1;
2127 /* NUL terminate the field and advance p past the colon */
2128 *ep++ = '\0';
2129 if (*ep != '\0')
2130 *ep++ = '\0';
2131 fwd->arg = cp + 1;
2132 fwd->ispath = ispath;
2133 *p = ep;
2134 return 0;
2135 }
2136
2137 for (cp = *p; *cp != '\0'; cp++) {
2138 switch (*cp) {
2139 case '\\':
2140 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002141 if (*cp == '\0')
2142 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002143 break;
2144 case '/':
2145 ispath = 1;
2146 break;
2147 case ':':
2148 *cp++ = '\0';
2149 goto done;
2150 }
2151 }
2152done:
2153 fwd->arg = *p;
2154 fwd->ispath = ispath;
2155 *p = cp;
2156 return 0;
2157}
2158
Damien Millerf91ee4c2005-03-01 21:24:33 +11002159/*
2160 * parse_forward
2161 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002162 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002163 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2164 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002165 * dynamicfwd == 1
2166 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002167 * returns number of arguments parsed or zero on error
2168 */
2169int
Damien Miller7acefbb2014-07-18 14:11:24 +10002170parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002171{
Damien Miller7acefbb2014-07-18 14:11:24 +10002172 struct fwdarg fwdargs[4];
2173 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002174 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002175
Damien Miller7acefbb2014-07-18 14:11:24 +10002176 memset(fwd, 0, sizeof(*fwd));
2177 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002178
2179 cp = p = xstrdup(fwdspec);
2180
2181 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002182 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002183 cp++;
2184
Damien Miller7acefbb2014-07-18 14:11:24 +10002185 for (i = 0; i < 4; ++i) {
2186 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002187 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002188 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002189
Damien Millerf4b39532008-11-03 19:28:21 +11002190 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002191 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002192 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002193 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002194
2195 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002196 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002197 if (fwdargs[0].ispath) {
2198 fwd->listen_path = xstrdup(fwdargs[0].arg);
2199 fwd->listen_port = PORT_STREAMLOCAL;
2200 } else {
2201 fwd->listen_host = NULL;
2202 fwd->listen_port = a2port(fwdargs[0].arg);
2203 }
Damien Millera699d952008-11-03 19:27:34 +11002204 fwd->connect_host = xstrdup("socks");
2205 break;
2206
2207 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002208 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2209 fwd->listen_path = xstrdup(fwdargs[0].arg);
2210 fwd->listen_port = PORT_STREAMLOCAL;
2211 fwd->connect_path = xstrdup(fwdargs[1].arg);
2212 fwd->connect_port = PORT_STREAMLOCAL;
2213 } else if (fwdargs[1].ispath) {
2214 fwd->listen_host = NULL;
2215 fwd->listen_port = a2port(fwdargs[0].arg);
2216 fwd->connect_path = xstrdup(fwdargs[1].arg);
2217 fwd->connect_port = PORT_STREAMLOCAL;
2218 } else {
2219 fwd->listen_host = xstrdup(fwdargs[0].arg);
2220 fwd->listen_port = a2port(fwdargs[1].arg);
2221 fwd->connect_host = xstrdup("socks");
2222 }
Damien Millera699d952008-11-03 19:27:34 +11002223 break;
2224
Damien Millerf91ee4c2005-03-01 21:24:33 +11002225 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002226 if (fwdargs[0].ispath) {
2227 fwd->listen_path = xstrdup(fwdargs[0].arg);
2228 fwd->listen_port = PORT_STREAMLOCAL;
2229 fwd->connect_host = xstrdup(fwdargs[1].arg);
2230 fwd->connect_port = a2port(fwdargs[2].arg);
2231 } else if (fwdargs[2].ispath) {
2232 fwd->listen_host = xstrdup(fwdargs[0].arg);
2233 fwd->listen_port = a2port(fwdargs[1].arg);
2234 fwd->connect_path = xstrdup(fwdargs[2].arg);
2235 fwd->connect_port = PORT_STREAMLOCAL;
2236 } else {
2237 fwd->listen_host = NULL;
2238 fwd->listen_port = a2port(fwdargs[0].arg);
2239 fwd->connect_host = xstrdup(fwdargs[1].arg);
2240 fwd->connect_port = a2port(fwdargs[2].arg);
2241 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002242 break;
2243
2244 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002245 fwd->listen_host = xstrdup(fwdargs[0].arg);
2246 fwd->listen_port = a2port(fwdargs[1].arg);
2247 fwd->connect_host = xstrdup(fwdargs[2].arg);
2248 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002249 break;
2250 default:
2251 i = 0; /* failure */
2252 }
2253
Darren Tuckera627d422013-06-02 07:31:17 +10002254 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002255
Damien Millera699d952008-11-03 19:27:34 +11002256 if (dynamicfwd) {
2257 if (!(i == 1 || i == 2))
2258 goto fail_free;
2259 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002260 if (!(i == 3 || i == 4)) {
2261 if (fwd->connect_path == NULL &&
2262 fwd->listen_path == NULL)
2263 goto fail_free;
2264 }
2265 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002266 goto fail_free;
2267 }
2268
Damien Miller7acefbb2014-07-18 14:11:24 +10002269 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2270 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002271 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002272 if (fwd->connect_host != NULL &&
2273 strlen(fwd->connect_host) >= NI_MAXHOST)
2274 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002275 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2276 if (fwd->connect_path != NULL &&
2277 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2278 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002279 if (fwd->listen_host != NULL &&
2280 strlen(fwd->listen_host) >= NI_MAXHOST)
2281 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002282 if (fwd->listen_path != NULL &&
2283 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2284 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002285
2286 return (i);
2287
2288 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002289 free(fwd->connect_host);
2290 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002291 free(fwd->connect_path);
2292 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002293 free(fwd->listen_host);
2294 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002295 free(fwd->listen_path);
2296 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002297 return (0);
2298}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002299
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002300int
2301parse_jump(const char *s, Options *o, int active)
2302{
2303 char *orig, *sdup, *cp;
2304 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002305 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002306
2307 active &= o->proxy_command == NULL && o->jump_host == NULL;
2308
2309 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002310 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002311 do {
2312 if ((cp = strrchr(sdup, ',')) == NULL)
2313 cp = sdup; /* last */
2314 else
2315 *cp++ = '\0';
2316
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002317 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002318 /* First argument and configuration is active */
2319 if (parse_user_host_port(cp, &user, &host, &port) != 0)
2320 goto out;
2321 } else {
2322 /* Subsequent argument or inactive configuration */
2323 if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2324 goto out;
2325 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002326 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002327 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002328 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002329 if (active) {
2330 o->jump_user = user;
2331 o->jump_host = host;
2332 o->jump_port = port;
2333 o->proxy_command = xstrdup("none");
2334 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002335 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2336 o->jump_extra = xstrdup(s);
2337 o->jump_extra[cp - s] = '\0';
2338 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002339 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002340 ret = 0;
2341 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002342 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002343 free(user);
2344 free(host);
2345 return ret;
2346}
2347
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002348/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2349static const char *
2350fmt_multistate_int(int val, const struct multistate *m)
2351{
2352 u_int i;
2353
2354 for (i = 0; m[i].key != NULL; i++) {
2355 if (m[i].value == val)
2356 return m[i].key;
2357 }
2358 return "UNKNOWN";
2359}
2360
2361static const char *
2362fmt_intarg(OpCodes code, int val)
2363{
2364 if (val == -1)
2365 return "unset";
2366 switch (code) {
2367 case oAddressFamily:
2368 return fmt_multistate_int(val, multistate_addressfamily);
2369 case oVerifyHostKeyDNS:
2370 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002371 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002372 return fmt_multistate_int(val, multistate_yesnoask);
2373 case oControlMaster:
2374 return fmt_multistate_int(val, multistate_controlmaster);
2375 case oTunnel:
2376 return fmt_multistate_int(val, multistate_tunnel);
2377 case oRequestTTY:
2378 return fmt_multistate_int(val, multistate_requesttty);
2379 case oCanonicalizeHostname:
2380 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002381 case oFingerprintHash:
2382 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002383 case oProtocol:
2384 switch (val) {
2385 case SSH_PROTO_1:
2386 return "1";
2387 case SSH_PROTO_2:
2388 return "2";
2389 case (SSH_PROTO_1|SSH_PROTO_2):
2390 return "2,1";
2391 default:
2392 return "UNKNOWN";
2393 }
2394 default:
2395 switch (val) {
2396 case 0:
2397 return "no";
2398 case 1:
2399 return "yes";
2400 default:
2401 return "UNKNOWN";
2402 }
2403 }
2404}
2405
2406static const char *
2407lookup_opcode_name(OpCodes code)
2408{
2409 u_int i;
2410
2411 for (i = 0; keywords[i].name != NULL; i++)
2412 if (keywords[i].opcode == code)
2413 return(keywords[i].name);
2414 return "UNKNOWN";
2415}
2416
2417static void
2418dump_cfg_int(OpCodes code, int val)
2419{
2420 printf("%s %d\n", lookup_opcode_name(code), val);
2421}
2422
2423static void
2424dump_cfg_fmtint(OpCodes code, int val)
2425{
2426 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2427}
2428
2429static void
2430dump_cfg_string(OpCodes code, const char *val)
2431{
2432 if (val == NULL)
2433 return;
2434 printf("%s %s\n", lookup_opcode_name(code), val);
2435}
2436
2437static void
2438dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2439{
2440 u_int i;
2441
2442 for (i = 0; i < count; i++)
2443 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2444}
2445
2446static void
2447dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2448{
2449 u_int i;
2450
2451 printf("%s", lookup_opcode_name(code));
2452 for (i = 0; i < count; i++)
2453 printf(" %s", vals[i]);
2454 printf("\n");
2455}
2456
2457static void
2458dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2459{
2460 const struct Forward *fwd;
2461 u_int i;
2462
2463 /* oDynamicForward */
2464 for (i = 0; i < count; i++) {
2465 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002466 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002467 strcmp(fwd->connect_host, "socks") != 0)
2468 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002469 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002470 strcmp(fwd->connect_host, "socks") == 0)
2471 continue;
2472 printf("%s", lookup_opcode_name(code));
2473 if (fwd->listen_port == PORT_STREAMLOCAL)
2474 printf(" %s", fwd->listen_path);
2475 else if (fwd->listen_host == NULL)
2476 printf(" %d", fwd->listen_port);
2477 else {
2478 printf(" [%s]:%d",
2479 fwd->listen_host, fwd->listen_port);
2480 }
2481 if (code != oDynamicForward) {
2482 if (fwd->connect_port == PORT_STREAMLOCAL)
2483 printf(" %s", fwd->connect_path);
2484 else if (fwd->connect_host == NULL)
2485 printf(" %d", fwd->connect_port);
2486 else {
2487 printf(" [%s]:%d",
2488 fwd->connect_host, fwd->connect_port);
2489 }
2490 }
2491 printf("\n");
2492 }
2493}
2494
2495void
2496dump_client_config(Options *o, const char *host)
2497{
2498 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002499 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002500
djm@openbsd.org60a92472015-08-21 23:53:08 +00002501 /* This is normally prepared in ssh_kex2 */
2502 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2503 fatal("%s: kex_assemble_names failed", __func__);
2504
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002505 /* Most interesting options first: user, host, port */
2506 dump_cfg_string(oUser, o->user);
2507 dump_cfg_string(oHostName, host);
2508 dump_cfg_int(oPort, o->port);
2509
2510 /* Flag options */
2511 dump_cfg_fmtint(oAddressFamily, o->address_family);
2512 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2513 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2514 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2515 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2516 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2517 dump_cfg_fmtint(oCompression, o->compression);
2518 dump_cfg_fmtint(oControlMaster, o->control_master);
2519 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002520 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002521 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002522 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002523 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2524 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2525 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2526 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2527#ifdef GSSAPI
2528 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2529 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2530#endif /* GSSAPI */
2531 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2532 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2533 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2534 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2535 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2536 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2537 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2538 dump_cfg_fmtint(oProtocol, o->protocol);
2539 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2540 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2541 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002542#ifdef WITH_RSA1
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002543 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2544 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002545#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002546 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2547 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2548 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2549 dump_cfg_fmtint(oTunnel, o->tun_open);
2550 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2551 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2552 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002553 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002554
2555 /* Integer options */
2556 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002557#ifdef WITH_SSH1
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002558 dump_cfg_int(oCompressionLevel, o->compression_level);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002559#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002560 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2561 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2562 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2563 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2564 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2565
2566 /* String options */
2567 dump_cfg_string(oBindAddress, o->bind_address);
2568 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2569 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002570 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002571 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002572 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002573 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002574 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2575 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2576 dump_cfg_string(oLocalCommand, o->local_command);
2577 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2578 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002579#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002580 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002581#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002582 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002583 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002584 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002585 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002586
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002587 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002588 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2589 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2590 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2591
2592 /* String array options */
2593 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2594 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2595 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2596 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2597 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2598
2599 /* Special cases */
2600
2601 /* oConnectTimeout */
2602 if (o->connection_timeout == -1)
2603 printf("connecttimeout none\n");
2604 else
2605 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2606
2607 /* oTunnelDevice */
2608 printf("tunneldevice");
2609 if (o->tun_local == SSH_TUNID_ANY)
2610 printf(" any");
2611 else
2612 printf(" %d", o->tun_local);
2613 if (o->tun_remote == SSH_TUNID_ANY)
2614 printf(":any");
2615 else
2616 printf(":%d", o->tun_remote);
2617 printf("\n");
2618
2619 /* oCanonicalizePermittedCNAMEs */
2620 if ( o->num_permitted_cnames > 0) {
2621 printf("canonicalizePermittedcnames");
2622 for (i = 0; i < o->num_permitted_cnames; i++) {
2623 printf(" %s:%s", o->permitted_cnames[i].source_list,
2624 o->permitted_cnames[i].target_list);
2625 }
2626 printf("\n");
2627 }
2628
2629 /* oCipher */
2630 if (o->cipher != SSH_CIPHER_NOT_SET)
2631 printf("Cipher %s\n", cipher_name(o->cipher));
2632
2633 /* oControlPersist */
2634 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2635 dump_cfg_fmtint(oControlPersist, o->control_persist);
2636 else
2637 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2638
2639 /* oEscapeChar */
2640 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2641 printf("escapechar none\n");
2642 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002643 vis(buf, o->escape_char, VIS_WHITE, 0);
2644 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002645 }
2646
2647 /* oIPQoS */
2648 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2649 printf("%s\n", iptos2str(o->ip_qos_bulk));
2650
2651 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002652 printf("rekeylimit %llu %d\n",
2653 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002654
2655 /* oStreamLocalBindMask */
2656 printf("streamlocalbindmask 0%o\n",
2657 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002658
2659 /* oProxyCommand / oProxyJump */
2660 if (o->jump_host == NULL)
2661 dump_cfg_string(oProxyCommand, o->proxy_command);
2662 else {
2663 /* Check for numeric addresses */
2664 i = strchr(o->jump_host, ':') != NULL ||
2665 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2666 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2667 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002668 /* optional additional jump spec */
2669 o->jump_extra == NULL ? "" : o->jump_extra,
2670 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002671 /* optional user */
2672 o->jump_user == NULL ? "" : o->jump_user,
2673 o->jump_user == NULL ? "" : "@",
2674 /* opening [ if hostname is numeric */
2675 i ? "[" : "",
2676 /* mandatory hostname */
2677 o->jump_host,
2678 /* closing ] if hostname is numeric */
2679 i ? "]" : "",
2680 /* optional port number */
2681 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002682 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002683 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002684}