blob: 963c648b97345af7b1d43ba80ec334bed754837b [file] [log] [blame]
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001/* $OpenBSD: readconf.c,v 1.273 2017/04/30 23:11:45 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,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000155 oUsePrivilegedPort, oLogFacility, oLogLevel, oCiphers, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
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,
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000174 oIgnore, 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 */
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000184 { "protocol", oIgnore }, /* NB. silently ignored */
djm@openbsd.org381a2612017-01-30 00:38:50 +0000185 { "fallbacktorsh", oDeprecated },
186 { "globalknownhostsfile2", oDeprecated },
187 { "rhostsauthentication", oDeprecated },
188 { "userknownhostsfile2", oDeprecated },
189 { "useroaming", oDeprecated },
190 { "usersh", oDeprecated },
191
192 /* Unsupported options */
193 { "afstokenpassing", oUnsupported },
194 { "kerberosauthentication", oUnsupported },
195 { "kerberostgtpassing", oUnsupported },
196
197 /* Sometimes-unsupported options */
198#if defined(GSSAPI)
199 { "gssapiauthentication", oGssAuthentication },
200 { "gssapidelegatecredentials", oGssDelegateCreds },
201# else
202 { "gssapiauthentication", oUnsupported },
203 { "gssapidelegatecredentials", oUnsupported },
204#endif
205#ifdef ENABLE_PKCS11
206 { "smartcarddevice", oPKCS11Provider },
207 { "pkcs11provider", oPKCS11Provider },
208# else
209 { "smartcarddevice", oUnsupported },
210 { "pkcs11provider", oUnsupported },
211#endif
djm@openbsd.org381a2612017-01-30 00:38:50 +0000212 { "rsaauthentication", oUnsupported },
213 { "rhostsrsaauthentication", oUnsupported },
214 { "compressionlevel", oUnsupported },
djm@openbsd.org381a2612017-01-30 00:38:50 +0000215
Damien Miller95def091999-11-25 00:26:21 +1100216 { "forwardagent", oForwardAgent },
217 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000218 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000219 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000220 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000221 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "gatewayports", oGatewayPorts },
223 { "useprivilegedport", oUsePrivilegedPort },
Damien Miller95def091999-11-25 00:26:21 +1100224 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100225 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
226 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100227 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000228 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstromd69dab32001-04-12 23:36:05 +0000229 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000230 { "challengeresponseauthentication", oChallengeResponseAuthentication },
231 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
232 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100233 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100234 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100235 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000236 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000237 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000238 { "identityagent", oIdentityAgent },
Damien Miller95def091999-11-25 00:26:21 +1100239 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000240 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100241 { "proxycommand", oProxyCommand },
242 { "port", oPort },
243 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000244 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000245 { "macs", oMacs },
Damien Miller95def091999-11-25 00:26:21 +1100246 { "remoteforward", oRemoteForward },
247 { "localforward", oLocalForward },
248 { "user", oUser },
249 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100250 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100251 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100252 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100253 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller95def091999-11-25 00:26:21 +1100254 { "connectionattempts", oConnectionAttempts },
255 { "batchmode", oBatchMode },
256 { "checkhostip", oCheckHostIP },
257 { "stricthostkeychecking", oStrictHostKeyChecking },
258 { "compression", oCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100259 { "tcpkeepalive", oTCPKeepAlive },
260 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100261 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000262 { "syslogfacility", oLogFacility },
Damien Miller95def091999-11-25 00:26:21 +1100263 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000264 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000265 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000266 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000267 { "bindaddress", oBindAddress },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100268 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000269 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000270 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100271 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000272 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000273 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000274 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100275 { "serveraliveinterval", oServerAliveInterval },
276 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000277 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000278 { "controlpath", oControlPath },
279 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000280 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100281 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000282 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100283 { "tunnel", oTunnel },
284 { "tunneldevice", oTunnelDevice },
285 { "localcommand", oLocalCommand },
286 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000287 { "visualhostkey", oVisualHostKey },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000288 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100289 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000290 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000291 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100292 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100293 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
294 { "canonicalizehostname", oCanonicalizeHostname },
295 { "canonicalizemaxdots", oCanonicalizeMaxDots },
296 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000297 { "streamlocalbindmask", oStreamLocalBindMask },
298 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000299 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000300 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000301 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000302 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000303 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000304 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000305 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100306
Ben Lindstrom65366a82001-12-06 16:32:47 +0000307 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100308};
309
Damien Miller5428f641999-11-25 11:54:57 +1100310/*
311 * Adds a local TCP/IP port forward to options. Never returns if there is an
312 * error.
313 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller4af51302000-04-16 11:18:38 +1000315void
Damien Miller7acefbb2014-07-18 14:11:24 +1000316add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317{
Damien Miller7acefbb2014-07-18 14:11:24 +1000318 struct Forward *fwd;
Damien Miller95def091999-11-25 00:26:21 +1100319 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000320 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000321
jsg@openbsd.org79d078e2016-10-25 04:08:13 +0000322 if (!bind_permitted(newfwd->listen_port, original_real_uid) &&
Damien Miller7acefbb2014-07-18 14:11:24 +1000323 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000324 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000325 /* Don't add duplicates */
326 for (i = 0; i < options->num_local_forwards; i++) {
327 if (forward_equals(newfwd, options->local_forwards + i))
328 return;
329 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000330 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000331 options->num_local_forwards + 1,
332 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100333 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334
Damien Miller1a0442f2008-11-05 16:30:06 +1100335 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000337 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100338 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
Damien Miller5428f641999-11-25 11:54:57 +1100343/*
344 * Adds a remote TCP/IP port forward to options. Never returns if there is
345 * an error.
346 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller4af51302000-04-16 11:18:38 +1000348void
Damien Miller7acefbb2014-07-18 14:11:24 +1000349add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Miller7acefbb2014-07-18 14:11:24 +1000351 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000352 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000353
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000354 /* Don't add duplicates */
355 for (i = 0; i < options->num_remote_forwards; i++) {
356 if (forward_equals(newfwd, options->remote_forwards + i))
357 return;
358 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000359 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000360 options->num_remote_forwards + 1,
361 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100362 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100363
Damien Miller1a0442f2008-11-05 16:30:06 +1100364 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000366 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100367 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000369 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100370 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000371 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000374static void
375clear_forwardings(Options *options)
376{
377 int i;
378
Damien Millerf91ee4c2005-03-01 21:24:33 +1100379 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000380 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000381 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000383 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100384 }
Damien Miller232cfb12010-06-26 09:50:30 +1000385 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000387 options->local_forwards = NULL;
388 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000389 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100390 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000392 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000394 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100395 }
Damien Miller232cfb12010-06-26 09:50:30 +1000396 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000398 options->remote_forwards = NULL;
399 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000400 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100401 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402}
403
Darren Tucker19104782013-04-05 11:13:08 +1100404void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000405add_certificate_file(Options *options, const char *path, int userprovided)
406{
407 int i;
408
409 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
410 fatal("Too many certificate files specified (max %d)",
411 SSH_MAX_CERTIFICATE_FILES);
412
413 /* Avoid registering duplicates */
414 for (i = 0; i < options->num_certificate_files; i++) {
415 if (options->certificate_file_userprovided[i] == userprovided &&
416 strcmp(options->certificate_files[i], path) == 0) {
417 debug2("%s: ignoring duplicate key %s", __func__, path);
418 return;
419 }
420 }
421
422 options->certificate_file_userprovided[options->num_certificate_files] =
423 userprovided;
424 options->certificate_files[options->num_certificate_files++] =
425 xstrdup(path);
426}
427
428void
Darren Tucker19104782013-04-05 11:13:08 +1100429add_identity_file(Options *options, const char *dir, const char *filename,
430 int userprovided)
431{
432 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000433 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100434
435 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
436 fatal("Too many identity files specified (max %d)",
437 SSH_MAX_IDENTITY_FILES);
438
439 if (dir == NULL) /* no dir, filename is absolute */
440 path = xstrdup(filename);
441 else
442 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
443
Damien Miller15271902014-05-15 13:47:56 +1000444 /* Avoid registering duplicates */
445 for (i = 0; i < options->num_identity_files; i++) {
446 if (options->identity_file_userprovided[i] == userprovided &&
447 strcmp(options->identity_files[i], path) == 0) {
448 debug2("%s: ignoring duplicate key %s", __func__, path);
449 free(path);
450 return;
451 }
452 }
453
Darren Tucker19104782013-04-05 11:13:08 +1100454 options->identity_file_userprovided[options->num_identity_files] =
455 userprovided;
456 options->identity_files[options->num_identity_files++] = path;
457}
458
Damien Miller194fd902013-10-15 12:13:05 +1100459int
460default_ssh_port(void)
461{
462 static int port;
463 struct servent *sp;
464
465 if (port == 0) {
466 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
467 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
468 }
469 return port;
470}
471
472/*
473 * Execute a command in a shell.
474 * Return its exit status or -1 on abnormal exit.
475 */
476static int
477execute_in_shell(const char *cmd)
478{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000479 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100480 pid_t pid;
481 int devnull, status;
482 extern uid_t original_real_uid;
483
484 if ((shell = getenv("SHELL")) == NULL)
485 shell = _PATH_BSHELL;
486
Damien Miller194fd902013-10-15 12:13:05 +1100487 /* Need this to redirect subprocess stdin/out */
488 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
489 fatal("open(/dev/null): %s", strerror(errno));
490
491 debug("Executing command: '%.500s'", cmd);
492
493 /* Fork and execute the command. */
494 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000495 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100496
497 /* Child. Permanently give up superuser privileges. */
498 permanently_drop_suid(original_real_uid);
499
500 /* Redirect child stdin and stdout. Leave stderr */
501 if (dup2(devnull, STDIN_FILENO) == -1)
502 fatal("dup2: %s", strerror(errno));
503 if (dup2(devnull, STDOUT_FILENO) == -1)
504 fatal("dup2: %s", strerror(errno));
505 if (devnull > STDERR_FILENO)
506 close(devnull);
507 closefrom(STDERR_FILENO + 1);
508
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000509 argv[0] = shell;
510 argv[1] = "-c";
511 argv[2] = xstrdup(cmd);
512 argv[3] = NULL;
513
Damien Miller194fd902013-10-15 12:13:05 +1100514 execv(argv[0], argv);
515 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
516 /* Die with signal to make this error apparent to parent. */
517 signal(SIGTERM, SIG_DFL);
518 kill(getpid(), SIGTERM);
519 _exit(1);
520 }
521 /* Parent. */
522 if (pid < 0)
523 fatal("%s: fork: %.100s", __func__, strerror(errno));
524
525 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100526
527 while (waitpid(pid, &status, 0) == -1) {
528 if (errno != EINTR && errno != EAGAIN)
529 fatal("%s: waitpid: %s", __func__, strerror(errno));
530 }
531 if (!WIFEXITED(status)) {
532 error("command '%.100s' exited abnormally", cmd);
533 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000534 }
Damien Miller194fd902013-10-15 12:13:05 +1100535 debug3("command returned status %d", WEXITSTATUS(status));
536 return WEXITSTATUS(status);
537}
538
539/*
540 * Parse and execute a Match directive.
541 */
542static int
543match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000544 const char *host_arg, const char *original_host, int post_canon,
545 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100546{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000547 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100548 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100550 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
551
552 /*
553 * Configuration is likely to be incomplete at this point so we
554 * must be prepared to use default values.
555 */
556 port = options->port <= 0 ? default_ssh_port() : options->port;
557 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000558 if (post_canon) {
559 host = xstrdup(options->hostname);
560 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100561 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100562 host = percent_expand(options->hostname,
563 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000564 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100565 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000566 }
Damien Miller194fd902013-10-15 12:13:05 +1100567
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000568 debug2("checking match for '%s' host %s originally %s",
569 cp, host, original_host);
570 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
571 criteria = NULL;
572 this_result = 1;
573 if ((negate = attrib[0] == '!'))
574 attrib++;
575 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100576 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000577 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100578 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 error("%.200s line %d: '%s' cannot be combined "
580 "with other Match attributes",
581 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100582 result = -1;
583 goto out;
584 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000585 if (result)
586 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100587 goto out;
588 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000589 attributes++;
590 if (strcasecmp(attrib, "canonical") == 0) {
591 r = !!post_canon; /* force bitmask member to boolean */
592 if (r == (negate ? 1 : 0))
593 this_result = result = 0;
594 debug3("%.200s line %d: %smatched '%s'",
595 filename, linenum,
596 this_result ? "" : "not ", oattrib);
597 continue;
598 }
599 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100600 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
601 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100602 result = -1;
603 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100604 }
Damien Miller194fd902013-10-15 12:13:05 +1100605 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000606 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000607 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 if (r == (negate ? 1 : 0))
609 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100610 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000612 r = match_hostname(original_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, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000617 r = match_pattern_list(ruser, arg, 0) == 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, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000622 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000623 if (r == (negate ? 1 : 0))
624 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100625 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100626 if (gethostname(thishost, sizeof(thishost)) == -1)
627 fatal("gethostname: %s", strerror(errno));
628 strlcpy(shorthost, thishost, sizeof(shorthost));
629 shorthost[strcspn(thishost, ".")] = '\0';
630 snprintf(portstr, sizeof(portstr), "%d", port);
631
632 cmd = percent_expand(arg,
633 "L", shorthost,
634 "d", pw->pw_dir,
635 "h", host,
636 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000637 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100638 "p", portstr,
639 "r", ruser,
640 "u", pw->pw_name,
641 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100642 if (result != 1) {
643 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000644 debug3("%.200s line %d: skipped exec "
645 "\"%.100s\"", filename, linenum, cmd);
646 free(cmd);
647 continue;
Damien Miller06287802014-02-24 15:56:45 +1100648 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000649 r = execute_in_shell(cmd);
650 if (r == -1) {
651 fatal("%.200s line %d: match exec "
652 "'%.100s' error", filename,
653 linenum, cmd);
654 }
655 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100656 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000657 /* Force exit status to boolean */
658 r = r == 0;
659 if (r == (negate ? 1 : 0))
660 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100661 } else {
662 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100663 result = -1;
664 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100665 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000666 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
667 filename, linenum, this_result ? "": "not ",
668 oattrib, criteria);
669 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100670 }
Damien Millercf31f382013-10-24 21:02:56 +1100671 if (attributes == 0) {
672 error("One or more attributes required for Match");
673 result = -1;
674 goto out;
675 }
Damien Miller084bcd22013-10-23 16:30:51 +1100676 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000677 if (result != -1)
678 debug2("match %sfound", result ? "" : "not ");
679 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100680 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100681 return result;
682}
683
Damien Miller0faf7472013-10-17 11:47:23 +1100684/* Check and prepare a domain name: removes trailing '.' and lowercases */
685static void
686valid_domain(char *name, const char *filename, int linenum)
687{
688 size_t i, l = strlen(name);
689 u_char c, last = '\0';
690
691 if (l == 0)
692 fatal("%s line %d: empty hostname suffix", filename, linenum);
693 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
694 fatal("%s line %d: hostname suffix \"%.100s\" "
695 "starts with invalid character", filename, linenum, name);
696 for (i = 0; i < l; i++) {
697 c = tolower((u_char)name[i]);
698 name[i] = (char)c;
699 if (last == '.' && c == '.')
700 fatal("%s line %d: hostname suffix \"%.100s\" contains "
701 "consecutive separators", filename, linenum, name);
702 if (c != '.' && c != '-' && !isalnum(c) &&
703 c != '_') /* technically invalid, but common */
704 fatal("%s line %d: hostname suffix \"%.100s\" contains "
705 "invalid characters", filename, linenum, name);
706 last = c;
707 }
708 if (name[l - 1] == '.')
709 name[l - 1] = '\0';
710}
711
Damien Miller5428f641999-11-25 11:54:57 +1100712/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000713 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100714 */
Damien Miller4af51302000-04-16 11:18:38 +1000715static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000716parse_token(const char *cp, const char *filename, int linenum,
717 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718{
Darren Tucker07636982013-05-16 20:30:03 +1000719 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000722 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100723 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000724 if (ignored_unknown != NULL &&
725 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000726 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000727 error("%s: line %d: Bad configuration option: %s",
728 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100729 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730}
731
Damien Millere9fc72e2013-10-15 12:14:12 +1100732/* Multistate option parsing */
733struct multistate {
734 char *key;
735 int value;
736};
737static const struct multistate multistate_flag[] = {
738 { "true", 1 },
739 { "false", 0 },
740 { "yes", 1 },
741 { "no", 0 },
742 { NULL, -1 }
743};
744static const struct multistate multistate_yesnoask[] = {
745 { "true", 1 },
746 { "false", 0 },
747 { "yes", 1 },
748 { "no", 0 },
749 { "ask", 2 },
750 { NULL, -1 }
751};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000752static const struct multistate multistate_yesnoaskconfirm[] = {
753 { "true", 1 },
754 { "false", 0 },
755 { "yes", 1 },
756 { "no", 0 },
757 { "ask", 2 },
758 { "confirm", 3 },
759 { NULL, -1 }
760};
Damien Millere9fc72e2013-10-15 12:14:12 +1100761static const struct multistate multistate_addressfamily[] = {
762 { "inet", AF_INET },
763 { "inet6", AF_INET6 },
764 { "any", AF_UNSPEC },
765 { NULL, -1 }
766};
767static const struct multistate multistate_controlmaster[] = {
768 { "true", SSHCTL_MASTER_YES },
769 { "yes", SSHCTL_MASTER_YES },
770 { "false", SSHCTL_MASTER_NO },
771 { "no", SSHCTL_MASTER_NO },
772 { "auto", SSHCTL_MASTER_AUTO },
773 { "ask", SSHCTL_MASTER_ASK },
774 { "autoask", SSHCTL_MASTER_AUTO_ASK },
775 { NULL, -1 }
776};
777static const struct multistate multistate_tunnel[] = {
778 { "ethernet", SSH_TUNMODE_ETHERNET },
779 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
780 { "true", SSH_TUNMODE_DEFAULT },
781 { "yes", SSH_TUNMODE_DEFAULT },
782 { "false", SSH_TUNMODE_NO },
783 { "no", SSH_TUNMODE_NO },
784 { NULL, -1 }
785};
786static const struct multistate multistate_requesttty[] = {
787 { "true", REQUEST_TTY_YES },
788 { "yes", REQUEST_TTY_YES },
789 { "false", REQUEST_TTY_NO },
790 { "no", REQUEST_TTY_NO },
791 { "force", REQUEST_TTY_FORCE },
792 { "auto", REQUEST_TTY_AUTO },
793 { NULL, -1 }
794};
Damien Miller38505592013-10-17 11:48:13 +1100795static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100796 { "true", SSH_CANONICALISE_YES },
797 { "false", SSH_CANONICALISE_NO },
798 { "yes", SSH_CANONICALISE_YES },
799 { "no", SSH_CANONICALISE_NO },
800 { "always", SSH_CANONICALISE_ALWAYS },
801 { NULL, -1 }
802};
Damien Millere9fc72e2013-10-15 12:14:12 +1100803
Damien Miller5428f641999-11-25 11:54:57 +1100804/*
805 * Processes a single option line as used in the configuration files. This
806 * only sets those values that have not already been set.
807 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100808int
Damien Miller194fd902013-10-15 12:13:05 +1100809process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000810 const char *original_host, char *line, const char *filename,
811 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000813 return process_config_line_depth(options, pw, host, original_host,
814 line, filename, linenum, activep, flags, 0);
815}
816
817#define WHITESPACE " \t\r\n"
818static int
819process_config_line_depth(Options *options, struct passwd *pw, const char *host,
820 const char *original_host, char *line, const char *filename,
821 int linenum, int *activep, int flags, int depth)
822{
Damien Miller295ee632011-05-29 21:42:31 +1000823 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
824 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000825 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000826 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100827 LogLevel *log_level_ptr;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +0000828 SyslogFacility *log_facility_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000829 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100830 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000831 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100832 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100833 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000834 glob_t gl;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835
Damien Miller194fd902013-10-15 12:13:05 +1100836 if (activep == NULL) { /* We are processing a command line directive */
837 cmdline = 1;
838 activep = &cmdline;
839 }
840
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000841 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000842 if ((len = strlen(line)) == 0)
843 return 0;
844 for (len--; len > 0; len--) {
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +0000845 if (strchr(WHITESPACE "\f", line[len]) == NULL)
Damien Millerc652cac2003-05-14 13:40:54 +1000846 break;
847 line[len] = '\0';
848 }
849
Damien Millerbe484b52000-07-15 14:14:16 +1000850 s = line;
851 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100852 if ((keyword = strdelim(&s)) == NULL)
853 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000854 /* Ignore leading whitespace. */
855 if (*keyword == '\0')
856 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000857 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100858 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000859 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100860 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861
Darren Tucker07636982013-05-16 20:30:03 +1000862 opcode = parse_token(keyword, filename, linenum,
863 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
Damien Miller95def091999-11-25 00:26:21 +1100865 switch (opcode) {
866 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100867 /* don't panic, but count bad options */
868 return -1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000869 case oIgnore:
870 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000871 case oIgnoredUnknownOption:
872 debug("%s line %d: Ignored unknown option \"%s\"",
873 filename, linenum, keyword);
874 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000875 case oConnectTimeout:
876 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100877parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000878 arg = strdelim(&s);
879 if (!arg || *arg == '\0')
880 fatal("%s line %d: missing time value.",
881 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000882 if (strcmp(arg, "none") == 0)
883 value = -1;
884 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000885 fatal("%s line %d: invalid time value.",
886 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100887 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000888 *intptr = value;
889 break;
890
Damien Miller95def091999-11-25 00:26:21 +1100891 case oForwardAgent:
892 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100893 parse_flag:
894 multistate_ptr = multistate_flag;
895 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000896 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000897 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100898 fatal("%s line %d: missing argument.",
899 filename, linenum);
900 value = -1;
901 for (i = 0; multistate_ptr[i].key != NULL; i++) {
902 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
903 value = multistate_ptr[i].value;
904 break;
905 }
906 }
907 if (value == -1)
908 fatal("%s line %d: unsupported option \"%s\".",
909 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100910 if (*activep && *intptr == -1)
911 *intptr = value;
912 break;
913
914 case oForwardX11:
915 intptr = &options->forward_x11;
916 goto parse_flag;
917
Darren Tucker0a118da2003-10-15 15:54:32 +1000918 case oForwardX11Trusted:
919 intptr = &options->forward_x11_trusted;
920 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000921
Damien Miller1ab6a512010-06-26 10:02:24 +1000922 case oForwardX11Timeout:
923 intptr = &options->forward_x11_timeout;
924 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000925
Damien Miller95def091999-11-25 00:26:21 +1100926 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000927 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100928 goto parse_flag;
929
Darren Tuckere7d4b192006-07-12 22:17:10 +1000930 case oExitOnForwardFailure:
931 intptr = &options->exit_on_forward_failure;
932 goto parse_flag;
933
Damien Miller95def091999-11-25 00:26:21 +1100934 case oUsePrivilegedPort:
935 intptr = &options->use_privileged_port;
936 goto parse_flag;
937
Damien Miller95def091999-11-25 00:26:21 +1100938 case oPasswordAuthentication:
939 intptr = &options->password_authentication;
940 goto parse_flag;
941
Damien Miller874d77b2000-10-14 16:23:11 +1100942 case oKbdInteractiveAuthentication:
943 intptr = &options->kbd_interactive_authentication;
944 goto parse_flag;
945
946 case oKbdInteractiveDevices:
947 charptr = &options->kbd_interactive_devices;
948 goto parse_string;
949
Damien Miller0bc1bd82000-11-13 22:57:25 +1100950 case oPubkeyAuthentication:
951 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000952 goto parse_flag;
953
Damien Miller95def091999-11-25 00:26:21 +1100954 case oRSAAuthentication:
955 intptr = &options->rsa_authentication;
956 goto parse_flag;
957
958 case oRhostsRSAAuthentication:
959 intptr = &options->rhosts_rsa_authentication;
960 goto parse_flag;
961
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000962 case oHostbasedAuthentication:
963 intptr = &options->hostbased_authentication;
964 goto parse_flag;
965
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000966 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000967 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100968 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000969
Darren Tucker0efd1552003-08-26 11:49:55 +1000970 case oGssAuthentication:
971 intptr = &options->gss_authentication;
972 goto parse_flag;
973
974 case oGssDelegateCreds:
975 intptr = &options->gss_deleg_creds;
976 goto parse_flag;
977
Damien Miller95def091999-11-25 00:26:21 +1100978 case oBatchMode:
979 intptr = &options->batch_mode;
980 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 case oCheckHostIP:
983 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000984 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985
Damien Miller37876e92003-05-15 10:19:46 +1000986 case oVerifyHostKeyDNS:
987 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100988 multistate_ptr = multistate_yesnoask;
989 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000990
Damien Miller95def091999-11-25 00:26:21 +1100991 case oStrictHostKeyChecking:
992 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100993 multistate_ptr = multistate_yesnoask;
994 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995
Damien Miller95def091999-11-25 00:26:21 +1100996 case oCompression:
997 intptr = &options->compression;
998 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999
Damien Miller12c150e2003-12-17 16:31:10 +11001000 case oTCPKeepAlive:
1001 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +11001002 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001004 case oNoHostAuthenticationForLocalhost:
1005 intptr = &options->no_host_authentication_for_localhost;
1006 goto parse_flag;
1007
Damien Miller95def091999-11-25 00:26:21 +11001008 case oNumberOfPasswordPrompts:
1009 intptr = &options->number_of_password_prompts;
1010 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
Damien Miller95def091999-11-25 00:26:21 +11001012 case oCompressionLevel:
1013 intptr = &options->compression_level;
1014 goto parse_int;
1015
Damien Millera5539d22003-04-09 20:50:06 +10001016 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001017 arg = strdelim(&s);
1018 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001019 fatal("%.200s line %d: Missing argument.", filename,
1020 linenum);
1021 if (strcmp(arg, "default") == 0) {
1022 val64 = 0;
1023 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001024 if (scan_scaled(arg, &val64) == -1)
1025 fatal("%.200s line %d: Bad number '%s': %s",
1026 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001027 if (val64 != 0 && val64 < 16)
1028 fatal("%.200s line %d: RekeyLimit too small",
1029 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001030 }
Damien Miller3dff1762008-02-10 22:25:52 +11001031 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001032 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001033 if (s != NULL) { /* optional rekey interval present */
1034 if (strcmp(s, "none") == 0) {
1035 (void)strdelim(&s); /* discard */
1036 break;
1037 }
1038 intptr = &options->rekey_interval;
1039 goto parse_time;
1040 }
Damien Millera5539d22003-04-09 20:50:06 +10001041 break;
1042
Damien Miller95def091999-11-25 00:26:21 +11001043 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001044 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001045 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001046 fatal("%.200s line %d: Missing argument.", filename, linenum);
1047 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001048 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001049 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001050 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001051 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001052 add_identity_file(options, NULL,
1053 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001054 }
1055 break;
1056
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001057 case oCertificateFile:
1058 arg = strdelim(&s);
1059 if (!arg || *arg == '\0')
1060 fatal("%.200s line %d: Missing argument.",
1061 filename, linenum);
1062 if (*activep) {
1063 intptr = &options->num_certificate_files;
1064 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1065 fatal("%.200s line %d: Too many certificate "
1066 "files specified (max %d).",
1067 filename, linenum,
1068 SSH_MAX_CERTIFICATE_FILES);
1069 }
1070 add_certificate_file(options, arg,
1071 flags & SSHCONF_USERCONF);
1072 }
1073 break;
1074
Damien Millerd3a18572000-06-07 19:55:44 +10001075 case oXAuthLocation:
1076 charptr=&options->xauth_location;
1077 goto parse_string;
1078
Damien Miller95def091999-11-25 00:26:21 +11001079 case oUser:
1080 charptr = &options->user;
1081parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001082 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001083 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001084 fatal("%.200s line %d: Missing argument.",
1085 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001086 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001087 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001088 break;
1089
1090 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001091 cpptr = (char **)&options->system_hostfiles;
1092 uintptr = &options->num_system_hostfiles;
1093 max_entries = SSH_MAX_HOSTS_FILES;
1094parse_char_array:
1095 if (*activep && *uintptr == 0) {
1096 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1097 if ((*uintptr) >= max_entries)
1098 fatal("%s line %d: "
1099 "too many authorized keys files.",
1100 filename, linenum);
1101 cpptr[(*uintptr)++] = xstrdup(arg);
1102 }
1103 }
1104 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001105
1106 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001107 cpptr = (char **)&options->user_hostfiles;
1108 uintptr = &options->num_user_hostfiles;
1109 max_entries = SSH_MAX_HOSTS_FILES;
1110 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001111
Damien Miller95def091999-11-25 00:26:21 +11001112 case oHostName:
1113 charptr = &options->hostname;
1114 goto parse_string;
1115
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001116 case oHostKeyAlias:
1117 charptr = &options->host_key_alias;
1118 goto parse_string;
1119
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001120 case oPreferredAuthentications:
1121 charptr = &options->preferred_authentications;
1122 goto parse_string;
1123
Ben Lindstrome0f88042001-04-30 13:06:24 +00001124 case oBindAddress:
1125 charptr = &options->bind_address;
1126 goto parse_string;
1127
Damien Miller7ea845e2010-02-12 09:21:02 +11001128 case oPKCS11Provider:
1129 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001130 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001131
Damien Miller95def091999-11-25 00:26:21 +11001132 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001133 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001134 /* Ignore ProxyCommand if ProxyJump already specified */
1135 if (options->jump_host != NULL)
1136 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001137parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001138 if (s == NULL)
1139 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001140 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001141 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001142 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001143 return 0;
1144
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001145 case oProxyJump:
1146 if (s == NULL) {
1147 fatal("%.200s line %d: Missing argument.",
1148 filename, linenum);
1149 }
1150 len = strspn(s, WHITESPACE "=");
1151 if (parse_jump(s + len, options, *activep) == -1) {
1152 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1153 filename, linenum, s + len);
1154 }
1155 return 0;
1156
Damien Miller95def091999-11-25 00:26:21 +11001157 case oPort:
1158 intptr = &options->port;
1159parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001160 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001161 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001162 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001163 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001164 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001165
1166 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001167 value = strtol(arg, &endofnumber, 0);
1168 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001169 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001170 if (*activep && *intptr == -1)
1171 *intptr = value;
1172 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173
Damien Miller95def091999-11-25 00:26:21 +11001174 case oConnectionAttempts:
1175 intptr = &options->connection_attempts;
1176 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001177
Damien Miller95def091999-11-25 00:26:21 +11001178 case oCipher:
1179 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001180 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001181 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001182 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001183 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001184 if (value == -1)
1185 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001186 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001187 if (*activep && *intptr == -1)
1188 *intptr = value;
1189 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190
Damien Miller78928792000-04-12 20:17:38 +10001191 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001192 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001193 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001194 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001195 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001196 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001197 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001198 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001199 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001200 break;
1201
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001202 case oMacs:
1203 arg = strdelim(&s);
1204 if (!arg || *arg == '\0')
1205 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001206 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001207 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001208 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001209 if (*activep && options->macs == NULL)
1210 options->macs = xstrdup(arg);
1211 break;
1212
Damien Millerd5f62bf2010-09-24 22:11:14 +10001213 case oKexAlgorithms:
1214 arg = strdelim(&s);
1215 if (!arg || *arg == '\0')
1216 fatal("%.200s line %d: Missing argument.",
1217 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001218 if (*arg != '-' &&
1219 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001220 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1221 filename, linenum, arg ? arg : "<NONE>");
1222 if (*activep && options->kex_algorithms == NULL)
1223 options->kex_algorithms = xstrdup(arg);
1224 break;
1225
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001226 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001227 charptr = &options->hostkeyalgorithms;
1228parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001229 arg = strdelim(&s);
1230 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001231 fatal("%.200s line %d: Missing argument.",
1232 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001233 if (*arg != '-' &&
1234 !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 Miller95def091999-11-25 00:26:21 +11001241 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001242 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001243 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001244 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001245 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001246 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001247 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001248 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1249 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001250 break;
1251
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001252 case oLogFacility:
1253 log_facility_ptr = &options->log_facility;
1254 arg = strdelim(&s);
1255 value = log_facility_number(arg);
1256 if (value == SYSLOG_FACILITY_NOT_SET)
1257 fatal("%.200s line %d: unsupported log facility '%s'",
1258 filename, linenum, arg ? arg : "<NONE>");
1259 if (*log_facility_ptr == -1)
1260 *log_facility_ptr = (SyslogFacility) value;
1261 break;
1262
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001263 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001264 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001265 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001266 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001267 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001268 fatal("%.200s line %d: Missing port argument.",
1269 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001270
Damien Millera699d952008-11-03 19:27:34 +11001271 if (opcode == oLocalForward ||
1272 opcode == oRemoteForward) {
1273 arg2 = strdelim(&s);
1274 if (arg2 == NULL || *arg2 == '\0')
1275 fatal("%.200s line %d: Missing target argument.",
1276 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001277
Damien Millera699d952008-11-03 19:27:34 +11001278 /* construct a string for parse_forward */
1279 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1280 } else if (opcode == oDynamicForward) {
1281 strlcpy(fwdarg, arg, sizeof(fwdarg));
1282 }
1283
1284 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001285 opcode == oDynamicForward ? 1 : 0,
1286 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001287 fatal("%.200s line %d: Bad forwarding specification.",
1288 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001289
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001290 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001291 if (opcode == oLocalForward ||
1292 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001293 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001294 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001295 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001296 }
Damien Miller95def091999-11-25 00:26:21 +11001297 break;
1298
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001299 case oClearAllForwardings:
1300 intptr = &options->clear_forwardings;
1301 goto parse_flag;
1302
Damien Miller95def091999-11-25 00:26:21 +11001303 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001304 if (cmdline)
1305 fatal("Host directive not supported as a command-line "
1306 "option");
Damien Miller95def091999-11-25 00:26:21 +11001307 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001308 arg2 = NULL;
1309 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001310 if ((flags & SSHCONF_NEVERMATCH) != 0)
1311 break;
Damien Millerfe924212011-05-15 08:44:45 +10001312 negated = *arg == '!';
1313 if (negated)
1314 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001315 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001316 if (negated) {
1317 debug("%.200s line %d: Skipping Host "
1318 "block because of negated match "
1319 "for %.100s", filename, linenum,
1320 arg);
1321 *activep = 0;
1322 break;
1323 }
1324 if (!*activep)
1325 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001326 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001327 }
Damien Millerfe924212011-05-15 08:44:45 +10001328 }
1329 if (*activep)
1330 debug("%.200s line %d: Applying options for %.100s",
1331 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001332 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001333 return 0;
1334
Damien Miller194fd902013-10-15 12:13:05 +11001335 case oMatch:
1336 if (cmdline)
1337 fatal("Host directive not supported as a command-line "
1338 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001339 value = match_cfg_line(options, &s, pw, host, original_host,
1340 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001341 if (value < 0)
1342 fatal("%.200s line %d: Bad Match condition", filename,
1343 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001344 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001345 break;
1346
Damien Miller95def091999-11-25 00:26:21 +11001347 case oEscapeChar:
1348 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001349 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001350 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001351 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001352 if (strcmp(arg, "none") == 0)
1353 value = SSH_ESCAPECHAR_NONE;
1354 else if (arg[1] == '\0')
1355 value = (u_char) arg[0];
1356 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001357 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1358 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001359 else {
1360 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001361 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001362 /* NOTREACHED */
1363 value = 0; /* Avoid compiler warning. */
1364 }
1365 if (*activep && *intptr == -1)
1366 *intptr = value;
1367 break;
1368
Damien Miller20a8f972003-05-18 20:50:30 +10001369 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001370 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001371 multistate_ptr = multistate_addressfamily;
1372 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001373
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001374 case oEnableSSHKeysign:
1375 intptr = &options->enable_ssh_keysign;
1376 goto parse_flag;
1377
Damien Millerbd394c32004-03-08 23:12:36 +11001378 case oIdentitiesOnly:
1379 intptr = &options->identities_only;
1380 goto parse_flag;
1381
Damien Miller509b0102003-12-17 16:33:10 +11001382 case oServerAliveInterval:
1383 intptr = &options->server_alive_interval;
1384 goto parse_time;
1385
1386 case oServerAliveCountMax:
1387 intptr = &options->server_alive_count_max;
1388 goto parse_int;
1389
Darren Tucker46bc0752004-05-02 22:11:30 +10001390 case oSendEnv:
1391 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1392 if (strchr(arg, '=') != NULL)
1393 fatal("%s line %d: Invalid environment name.",
1394 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001395 if (!*activep)
1396 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001397 if (options->num_send_env >= MAX_SEND_ENV)
1398 fatal("%s line %d: too many send env.",
1399 filename, linenum);
1400 options->send_env[options->num_send_env++] =
1401 xstrdup(arg);
1402 }
1403 break;
1404
Damien Miller0e220db2004-06-15 10:34:08 +10001405 case oControlPath:
1406 charptr = &options->control_path;
1407 goto parse_string;
1408
1409 case oControlMaster:
1410 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001411 multistate_ptr = multistate_controlmaster;
1412 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001413
Damien Millere11e1ea2010-08-03 16:04:46 +10001414 case oControlPersist:
1415 /* no/false/yes/true, or a time spec */
1416 intptr = &options->control_persist;
1417 arg = strdelim(&s);
1418 if (!arg || *arg == '\0')
1419 fatal("%.200s line %d: Missing ControlPersist"
1420 " argument.", filename, linenum);
1421 value = 0;
1422 value2 = 0; /* timeout */
1423 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1424 value = 0;
1425 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1426 value = 1;
1427 else if ((value2 = convtime(arg)) >= 0)
1428 value = 1;
1429 else
1430 fatal("%.200s line %d: Bad ControlPersist argument.",
1431 filename, linenum);
1432 if (*activep && *intptr == -1) {
1433 *intptr = value;
1434 options->control_persist_timeout = value2;
1435 }
1436 break;
1437
Damien Millere1776152005-03-01 21:47:37 +11001438 case oHashKnownHosts:
1439 intptr = &options->hash_known_hosts;
1440 goto parse_flag;
1441
Damien Millerd27b9472005-12-13 19:29:02 +11001442 case oTunnel:
1443 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001444 multistate_ptr = multistate_tunnel;
1445 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001446
1447 case oTunnelDevice:
1448 arg = strdelim(&s);
1449 if (!arg || *arg == '\0')
1450 fatal("%.200s line %d: Missing argument.", filename, linenum);
1451 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001452 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001453 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1454 if (*activep) {
1455 options->tun_local = value;
1456 options->tun_remote = value2;
1457 }
1458 break;
1459
1460 case oLocalCommand:
1461 charptr = &options->local_command;
1462 goto parse_command;
1463
1464 case oPermitLocalCommand:
1465 intptr = &options->permit_local_command;
1466 goto parse_flag;
1467
Damien Miller10288242008-06-30 00:04:03 +10001468 case oVisualHostKey:
1469 intptr = &options->visual_host_key;
1470 goto parse_flag;
1471
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001472 case oInclude:
1473 if (cmdline)
1474 fatal("Include directive not supported as a "
1475 "command-line option");
1476 value = 0;
1477 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1478 /*
1479 * Ensure all paths are anchored. User configuration
1480 * files may begin with '~/' but system configurations
1481 * must not. If the path is relative, then treat it
1482 * as living in ~/.ssh for user configurations or
1483 * /etc/ssh for system ones.
1484 */
1485 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1486 fatal("%.200s line %d: bad include path %s.",
1487 filename, linenum, arg);
1488 if (*arg != '/' && *arg != '~') {
1489 xasprintf(&arg2, "%s/%s",
1490 (flags & SSHCONF_USERCONF) ?
1491 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1492 } else
1493 arg2 = xstrdup(arg);
1494 memset(&gl, 0, sizeof(gl));
1495 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1496 if (r == GLOB_NOMATCH) {
1497 debug("%.200s line %d: include %s matched no "
1498 "files",filename, linenum, arg2);
dtucker@openbsd.orgf6edbe92017-03-10 03:24:48 +00001499 free(arg2);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001500 continue;
1501 } else if (r != 0 || gl.gl_pathc < 0)
1502 fatal("%.200s line %d: glob failed for %s.",
1503 filename, linenum, arg2);
1504 free(arg2);
1505 oactive = *activep;
1506 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1507 debug3("%.200s line %d: Including file %s "
1508 "depth %d%s", filename, linenum,
1509 gl.gl_pathv[i], depth,
1510 oactive ? "" : " (parse only)");
1511 r = read_config_file_depth(gl.gl_pathv[i],
1512 pw, host, original_host, options,
1513 flags | SSHCONF_CHECKPERM |
1514 (oactive ? 0 : SSHCONF_NEVERMATCH),
1515 activep, depth + 1);
djm@openbsd.orgb64077f2017-01-06 09:27:52 +00001516 if (r != 1 && errno != ENOENT) {
djm@openbsd.org5e820e92017-01-06 03:53:58 +00001517 fatal("Can't open user config file "
1518 "%.100s: %.100s", gl.gl_pathv[i],
1519 strerror(errno));
1520 }
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001521 /*
1522 * don't let Match in includes clobber the
1523 * containing file's Match state.
1524 */
1525 *activep = oactive;
1526 if (r != 1)
1527 value = -1;
1528 }
1529 globfree(&gl);
1530 }
1531 if (value != 0)
1532 return value;
1533 break;
1534
Damien Miller0dac6fb2010-11-20 15:19:38 +11001535 case oIPQoS:
1536 arg = strdelim(&s);
1537 if ((value = parse_ipqos(arg)) == -1)
1538 fatal("%s line %d: Bad IPQoS value: %s",
1539 filename, linenum, arg);
1540 arg = strdelim(&s);
1541 if (arg == NULL)
1542 value2 = value;
1543 else if ((value2 = parse_ipqos(arg)) == -1)
1544 fatal("%s line %d: Bad IPQoS value: %s",
1545 filename, linenum, arg);
1546 if (*activep) {
1547 options->ip_qos_interactive = value;
1548 options->ip_qos_bulk = value2;
1549 }
1550 break;
1551
Damien Miller21771e22011-05-15 08:45:50 +10001552 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001553 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001554 multistate_ptr = multistate_requesttty;
1555 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001556
Darren Tucker07636982013-05-16 20:30:03 +10001557 case oIgnoreUnknown:
1558 charptr = &options->ignored_unknown;
1559 goto parse_string;
1560
Damien Miller1262b662013-08-21 02:44:24 +10001561 case oProxyUseFdpass:
1562 intptr = &options->proxy_use_fdpass;
1563 goto parse_flag;
1564
Damien Miller0faf7472013-10-17 11:47:23 +11001565 case oCanonicalDomains:
1566 value = options->num_canonical_domains != 0;
1567 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1568 valid_domain(arg, filename, linenum);
1569 if (!*activep || value)
1570 continue;
1571 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1572 fatal("%s line %d: too many hostname suffixes.",
1573 filename, linenum);
1574 options->canonical_domains[
1575 options->num_canonical_domains++] = xstrdup(arg);
1576 }
1577 break;
1578
Damien Miller38505592013-10-17 11:48:13 +11001579 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001580 value = options->num_permitted_cnames != 0;
1581 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1582 /* Either '*' for everything or 'list:list' */
1583 if (strcmp(arg, "*") == 0)
1584 arg2 = arg;
1585 else {
1586 lowercase(arg);
1587 if ((arg2 = strchr(arg, ':')) == NULL ||
1588 arg2[1] == '\0') {
1589 fatal("%s line %d: "
1590 "Invalid permitted CNAME \"%s\"",
1591 filename, linenum, arg);
1592 }
1593 *arg2 = '\0';
1594 arg2++;
1595 }
1596 if (!*activep || value)
1597 continue;
1598 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1599 fatal("%s line %d: too many permitted CNAMEs.",
1600 filename, linenum);
1601 cname = options->permitted_cnames +
1602 options->num_permitted_cnames++;
1603 cname->source_list = xstrdup(arg);
1604 cname->target_list = xstrdup(arg2);
1605 }
1606 break;
1607
Damien Miller38505592013-10-17 11:48:13 +11001608 case oCanonicalizeHostname:
1609 intptr = &options->canonicalize_hostname;
1610 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001611 goto parse_multistate;
1612
Damien Miller38505592013-10-17 11:48:13 +11001613 case oCanonicalizeMaxDots:
1614 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001615 goto parse_int;
1616
Damien Miller38505592013-10-17 11:48:13 +11001617 case oCanonicalizeFallbackLocal:
1618 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001619 goto parse_flag;
1620
Damien Miller7acefbb2014-07-18 14:11:24 +10001621 case oStreamLocalBindMask:
1622 arg = strdelim(&s);
1623 if (!arg || *arg == '\0')
1624 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1625 /* Parse mode in octal format */
1626 value = strtol(arg, &endofnumber, 8);
1627 if (arg == endofnumber || value < 0 || value > 0777)
1628 fatal("%.200s line %d: Bad mask.", filename, linenum);
1629 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1630 break;
1631
1632 case oStreamLocalBindUnlink:
1633 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1634 goto parse_flag;
1635
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001636 case oRevokedHostKeys:
1637 charptr = &options->revoked_host_keys;
1638 goto parse_string;
1639
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001640 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001641 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001642 arg = strdelim(&s);
1643 if (!arg || *arg == '\0')
1644 fatal("%.200s line %d: Missing argument.",
1645 filename, linenum);
1646 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1647 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1648 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001649 if (*activep && *intptr == -1)
1650 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001651 break;
1652
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001653 case oUpdateHostkeys:
1654 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001655 multistate_ptr = multistate_yesnoask;
1656 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001657
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001658 case oHostbasedKeyTypes:
1659 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001660 goto parse_keytypes;
1661
1662 case oPubkeyAcceptedKeyTypes:
1663 charptr = &options->pubkey_key_types;
1664 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001665
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001666 case oAddKeysToAgent:
1667 intptr = &options->add_keys_to_agent;
1668 multistate_ptr = multistate_yesnoaskconfirm;
1669 goto parse_multistate;
1670
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001671 case oIdentityAgent:
1672 charptr = &options->identity_agent;
1673 goto parse_string;
1674
Ben Lindstrom4daea862002-06-09 20:04:02 +00001675 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001676 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001677 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001678 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001679
Damien Millerf9b3feb2003-05-16 11:38:32 +10001680 case oUnsupported:
1681 error("%s line %d: Unsupported option \"%s\"",
1682 filename, linenum, keyword);
1683 return 0;
1684
Damien Miller95def091999-11-25 00:26:21 +11001685 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001686 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001687 }
1688
1689 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001690 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001691 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001692 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001693 }
Damien Miller95def091999-11-25 00:26:21 +11001694 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695}
1696
Damien Miller5428f641999-11-25 11:54:57 +11001697/*
1698 * Reads the config file and modifies the options accordingly. Options
1699 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001700 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001701 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001702int
Damien Miller194fd902013-10-15 12:13:05 +11001703read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001704 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001705{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001706 int active = 1;
1707
1708 return read_config_file_depth(filename, pw, host, original_host,
1709 options, flags, &active, 0);
1710}
1711
1712#define READCONF_MAX_DEPTH 16
1713static int
1714read_config_file_depth(const char *filename, struct passwd *pw,
1715 const char *host, const char *original_host, Options *options,
1716 int flags, int *activep, int depth)
1717{
Damien Miller95def091999-11-25 00:26:21 +11001718 FILE *f;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001719 char line[4096];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001720 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001721 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001722
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001723 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1724 fatal("Too many recursive configuration includes");
1725
Damien Miller57a44762004-04-20 20:11:57 +10001726 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001727 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728
Darren Tuckeraefa3682013-04-05 11:18:35 +11001729 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001730 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001731
Damien Miller33793852004-06-15 10:27:55 +10001732 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001733 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001734 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001735 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001736 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001737 }
1738
Damien Miller95def091999-11-25 00:26:21 +11001739 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740
Damien Miller5428f641999-11-25 11:54:57 +11001741 /*
1742 * Mark that we are now processing the options. This flag is turned
1743 * on/off by Host specifications.
1744 */
Damien Miller95def091999-11-25 00:26:21 +11001745 linenum = 0;
1746 while (fgets(line, sizeof(line), f)) {
1747 /* Update line number counter. */
1748 linenum++;
djm@openbsd.org61b8ef62017-03-10 04:27:32 +00001749 if (strlen(line) == sizeof(line) - 1)
1750 fatal("%s line %d too long", filename, linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001751 if (process_config_line_depth(options, pw, host, original_host,
1752 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001753 bad_options++;
1754 }
1755 fclose(f);
1756 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001757 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001758 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001759 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760}
1761
Damien Miller13f97b22014-02-24 15:57:55 +11001762/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1763int
1764option_clear_or_none(const char *o)
1765{
1766 return o == NULL || strcasecmp(o, "none") == 0;
1767}
1768
Damien Miller5428f641999-11-25 11:54:57 +11001769/*
1770 * Initializes options to special values that indicate that they have not yet
1771 * been set. Read_config_file will only set options with this value. Options
1772 * are processed in the following order: command line, user config file,
1773 * system config file. Last, fill_default_options is called.
1774 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001775
Damien Miller4af51302000-04-16 11:18:38 +10001776void
Damien Miller95def091999-11-25 00:26:21 +11001777initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001778{
Damien Miller95def091999-11-25 00:26:21 +11001779 memset(options, 'X', sizeof(*options));
1780 options->forward_agent = -1;
1781 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001782 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001783 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001784 options->stdio_forward_host = NULL;
1785 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001786 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001787 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001788 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001789 options->fwd_opts.gateway_ports = -1;
1790 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1791 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001792 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001793 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001794 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001795 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001796 options->gss_authentication = -1;
1797 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001798 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001799 options->kbd_interactive_authentication = -1;
1800 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001801 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001802 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001803 options->batch_mode = -1;
1804 options->check_host_ip = -1;
1805 options->strict_host_key_checking = -1;
1806 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001807 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001808 options->compression_level = -1;
1809 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001810 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001811 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001812 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001813 options->number_of_password_prompts = -1;
1814 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001815 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001816 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001817 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001818 options->hostkeyalgorithms = NULL;
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;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001836 options->log_facility = SYSLOG_FACILITY_NOT_SET;
Damien Millerfcd93202002-02-05 12:26:34 +11001837 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001838 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001839 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001840 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001841 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001842 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001843 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001844 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001845 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001846 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001847 options->server_alive_interval = -1;
1848 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001849 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001850 options->control_path = NULL;
1851 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001852 options->control_persist = -1;
1853 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001854 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001855 options->tun_open = -1;
1856 options->tun_local = -1;
1857 options->tun_remote = -1;
1858 options->local_command = NULL;
1859 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001860 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001861 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001862 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001863 options->ip_qos_interactive = -1;
1864 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001865 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001866 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001867 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001868 options->num_canonical_domains = 0;
1869 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001870 options->canonicalize_max_dots = -1;
1871 options->canonicalize_fallback_local = -1;
1872 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001873 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001874 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001875 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001876 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001877 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878}
1879
Damien Miller5428f641999-11-25 11:54:57 +11001880/*
Damien Miller13f97b22014-02-24 15:57:55 +11001881 * A petite version of fill_default_options() that just fills the options
1882 * needed for hostname canonicalization to proceed.
1883 */
1884void
1885fill_default_options_for_canonicalization(Options *options)
1886{
1887 if (options->canonicalize_max_dots == -1)
1888 options->canonicalize_max_dots = 1;
1889 if (options->canonicalize_fallback_local == -1)
1890 options->canonicalize_fallback_local = 1;
1891 if (options->canonicalize_hostname == -1)
1892 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1893}
1894
1895/*
Damien Miller5428f641999-11-25 11:54:57 +11001896 * Called after processing other sources of option data, this fills those
1897 * options for which no value has been specified with their default values.
1898 */
Damien Miller4af51302000-04-16 11:18:38 +10001899void
Damien Miller95def091999-11-25 00:26:21 +11001900fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001901{
Damien Miller95def091999-11-25 00:26:21 +11001902 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001903 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001904 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001905 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001906 if (options->forward_x11_trusted == -1)
1907 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001908 if (options->forward_x11_timeout == -1)
1909 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001910 /*
1911 * stdio forwarding (-W) changes the default for these but we defer
1912 * setting the values so they can be overridden.
1913 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001914 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001915 options->exit_on_forward_failure =
1916 options->stdio_forward_host != NULL ? 1 : 0;
1917 if (options->clear_forwardings == -1)
1918 options->clear_forwardings =
1919 options->stdio_forward_host != NULL ? 1 : 0;
1920 if (options->clear_forwardings == 1)
1921 clear_forwardings(options);
1922
Damien Millerd3a18572000-06-07 19:55:44 +10001923 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001924 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001925 if (options->fwd_opts.gateway_ports == -1)
1926 options->fwd_opts.gateway_ports = 0;
1927 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1928 options->fwd_opts.streamlocal_bind_mask = 0177;
1929 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1930 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001931 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001932 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001933 if (options->rsa_authentication == -1)
1934 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001935 if (options->pubkey_authentication == -1)
1936 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001937 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001938 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001939 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001940 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001941 if (options->gss_deleg_creds == -1)
1942 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001943 if (options->password_authentication == -1)
1944 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001945 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001946 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001947 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001948 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001949 if (options->hostbased_authentication == -1)
1950 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001951 if (options->batch_mode == -1)
1952 options->batch_mode = 0;
1953 if (options->check_host_ip == -1)
1954 options->check_host_ip = 1;
1955 if (options->strict_host_key_checking == -1)
1956 options->strict_host_key_checking = 2; /* 2 is default */
1957 if (options->compression == -1)
1958 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001959 if (options->tcp_keep_alive == -1)
1960 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001961 if (options->compression_level == -1)
1962 options->compression_level = 6;
1963 if (options->port == -1)
1964 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001965 if (options->address_family == -1)
1966 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001967 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001968 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001969 if (options->number_of_password_prompts == -1)
1970 options->number_of_password_prompts = 3;
1971 /* Selected in ssh_login(). */
1972 if (options->cipher == -1)
1973 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001974 /* options->hostkeyalgorithms, default set in myproposals.h */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001975 if (options->add_keys_to_agent == -1)
1976 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001977 if (options->num_identity_files == 0) {
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001978 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
1979 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001980#ifdef OPENSSL_HAS_ECC
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001981 add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001982#endif
djm@openbsd.org99f95ba2017-04-30 23:11:45 +00001983 add_identity_file(options, "~/",
1984 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Millereba71ba2000-04-29 23:57:08 +10001985 }
Damien Miller95def091999-11-25 00:26:21 +11001986 if (options->escape_char == -1)
1987 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001988 if (options->num_system_hostfiles == 0) {
1989 options->system_hostfiles[options->num_system_hostfiles++] =
1990 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1991 options->system_hostfiles[options->num_system_hostfiles++] =
1992 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1993 }
1994 if (options->num_user_hostfiles == 0) {
1995 options->user_hostfiles[options->num_user_hostfiles++] =
1996 xstrdup(_PATH_SSH_USER_HOSTFILE);
1997 options->user_hostfiles[options->num_user_hostfiles++] =
1998 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1999 }
Damien Millerfcd93202002-02-05 12:26:34 +11002000 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002001 options->log_level = SYSLOG_LEVEL_INFO;
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00002002 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2003 options->log_facility = SYSLOG_FACILITY_USER;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00002004 if (options->no_host_authentication_for_localhost == - 1)
2005 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11002006 if (options->identities_only == -1)
2007 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002008 if (options->enable_ssh_keysign == -1)
2009 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002010 if (options->rekey_limit == -1)
2011 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002012 if (options->rekey_interval == -1)
2013 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002014 if (options->verify_host_key_dns == -1)
2015 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002016 if (options->server_alive_interval == -1)
2017 options->server_alive_interval = 0;
2018 if (options->server_alive_count_max == -1)
2019 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002020 if (options->control_master == -1)
2021 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002022 if (options->control_persist == -1) {
2023 options->control_persist = 0;
2024 options->control_persist_timeout = 0;
2025 }
Damien Millere1776152005-03-01 21:47:37 +11002026 if (options->hash_known_hosts == -1)
2027 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002028 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002029 options->tun_open = SSH_TUNMODE_NO;
2030 if (options->tun_local == -1)
2031 options->tun_local = SSH_TUNID_ANY;
2032 if (options->tun_remote == -1)
2033 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002034 if (options->permit_local_command == -1)
2035 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002036 if (options->visual_host_key == -1)
2037 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002038 if (options->ip_qos_interactive == -1)
2039 options->ip_qos_interactive = IPTOS_LOWDELAY;
2040 if (options->ip_qos_bulk == -1)
2041 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002042 if (options->request_tty == -1)
2043 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002044 if (options->proxy_use_fdpass == -1)
2045 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002046 if (options->canonicalize_max_dots == -1)
2047 options->canonicalize_max_dots = 1;
2048 if (options->canonicalize_fallback_local == -1)
2049 options->canonicalize_fallback_local = 1;
2050 if (options->canonicalize_hostname == -1)
2051 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002052 if (options->fingerprint_hash == -1)
2053 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002054 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002055 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002056 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2057 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2058 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2059 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2060 &options->hostbased_key_types) != 0 ||
2061 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2062 &options->pubkey_key_types) != 0)
2063 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002064
Damien Millere9fc72e2013-10-15 12:14:12 +11002065#define CLEAR_ON_NONE(v) \
2066 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002067 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002068 free(v); \
2069 v = NULL; \
2070 } \
2071 } while(0)
2072 CLEAR_ON_NONE(options->local_command);
2073 CLEAR_ON_NONE(options->proxy_command);
2074 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002075 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002076 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002077 /* options->user will be set in the main program if appropriate */
2078 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002079 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002080 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002081}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002082
Damien Miller7acefbb2014-07-18 14:11:24 +10002083struct fwdarg {
2084 char *arg;
2085 int ispath;
2086};
2087
2088/*
2089 * parse_fwd_field
2090 * parses the next field in a port forwarding specification.
2091 * sets fwd to the parsed field and advances p past the colon
2092 * or sets it to NULL at end of string.
2093 * returns 0 on success, else non-zero.
2094 */
2095static int
2096parse_fwd_field(char **p, struct fwdarg *fwd)
2097{
2098 char *ep, *cp = *p;
2099 int ispath = 0;
2100
2101 if (*cp == '\0') {
2102 *p = NULL;
2103 return -1; /* end of string */
2104 }
2105
2106 /*
2107 * A field escaped with square brackets is used literally.
2108 * XXX - allow ']' to be escaped via backslash?
2109 */
2110 if (*cp == '[') {
2111 /* find matching ']' */
2112 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2113 if (*ep == '/')
2114 ispath = 1;
2115 }
2116 /* no matching ']' or not at end of field. */
2117 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2118 return -1;
2119 /* NUL terminate the field and advance p past the colon */
2120 *ep++ = '\0';
2121 if (*ep != '\0')
2122 *ep++ = '\0';
2123 fwd->arg = cp + 1;
2124 fwd->ispath = ispath;
2125 *p = ep;
2126 return 0;
2127 }
2128
2129 for (cp = *p; *cp != '\0'; cp++) {
2130 switch (*cp) {
2131 case '\\':
2132 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002133 if (*cp == '\0')
2134 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002135 break;
2136 case '/':
2137 ispath = 1;
2138 break;
2139 case ':':
2140 *cp++ = '\0';
2141 goto done;
2142 }
2143 }
2144done:
2145 fwd->arg = *p;
2146 fwd->ispath = ispath;
2147 *p = cp;
2148 return 0;
2149}
2150
Damien Millerf91ee4c2005-03-01 21:24:33 +11002151/*
2152 * parse_forward
2153 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002154 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002155 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2156 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002157 * dynamicfwd == 1
2158 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002159 * returns number of arguments parsed or zero on error
2160 */
2161int
Damien Miller7acefbb2014-07-18 14:11:24 +10002162parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002163{
Damien Miller7acefbb2014-07-18 14:11:24 +10002164 struct fwdarg fwdargs[4];
2165 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002166 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002167
Damien Miller7acefbb2014-07-18 14:11:24 +10002168 memset(fwd, 0, sizeof(*fwd));
2169 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002170
2171 cp = p = xstrdup(fwdspec);
2172
2173 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002174 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002175 cp++;
2176
Damien Miller7acefbb2014-07-18 14:11:24 +10002177 for (i = 0; i < 4; ++i) {
2178 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002179 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002180 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002181
Damien Millerf4b39532008-11-03 19:28:21 +11002182 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002183 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002184 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002185 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002186
2187 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002188 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002189 if (fwdargs[0].ispath) {
2190 fwd->listen_path = xstrdup(fwdargs[0].arg);
2191 fwd->listen_port = PORT_STREAMLOCAL;
2192 } else {
2193 fwd->listen_host = NULL;
2194 fwd->listen_port = a2port(fwdargs[0].arg);
2195 }
Damien Millera699d952008-11-03 19:27:34 +11002196 fwd->connect_host = xstrdup("socks");
2197 break;
2198
2199 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002200 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2201 fwd->listen_path = xstrdup(fwdargs[0].arg);
2202 fwd->listen_port = PORT_STREAMLOCAL;
2203 fwd->connect_path = xstrdup(fwdargs[1].arg);
2204 fwd->connect_port = PORT_STREAMLOCAL;
2205 } else if (fwdargs[1].ispath) {
2206 fwd->listen_host = NULL;
2207 fwd->listen_port = a2port(fwdargs[0].arg);
2208 fwd->connect_path = xstrdup(fwdargs[1].arg);
2209 fwd->connect_port = PORT_STREAMLOCAL;
2210 } else {
2211 fwd->listen_host = xstrdup(fwdargs[0].arg);
2212 fwd->listen_port = a2port(fwdargs[1].arg);
2213 fwd->connect_host = xstrdup("socks");
2214 }
Damien Millera699d952008-11-03 19:27:34 +11002215 break;
2216
Damien Millerf91ee4c2005-03-01 21:24:33 +11002217 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002218 if (fwdargs[0].ispath) {
2219 fwd->listen_path = xstrdup(fwdargs[0].arg);
2220 fwd->listen_port = PORT_STREAMLOCAL;
2221 fwd->connect_host = xstrdup(fwdargs[1].arg);
2222 fwd->connect_port = a2port(fwdargs[2].arg);
2223 } else if (fwdargs[2].ispath) {
2224 fwd->listen_host = xstrdup(fwdargs[0].arg);
2225 fwd->listen_port = a2port(fwdargs[1].arg);
2226 fwd->connect_path = xstrdup(fwdargs[2].arg);
2227 fwd->connect_port = PORT_STREAMLOCAL;
2228 } else {
2229 fwd->listen_host = NULL;
2230 fwd->listen_port = a2port(fwdargs[0].arg);
2231 fwd->connect_host = xstrdup(fwdargs[1].arg);
2232 fwd->connect_port = a2port(fwdargs[2].arg);
2233 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002234 break;
2235
2236 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002237 fwd->listen_host = xstrdup(fwdargs[0].arg);
2238 fwd->listen_port = a2port(fwdargs[1].arg);
2239 fwd->connect_host = xstrdup(fwdargs[2].arg);
2240 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002241 break;
2242 default:
2243 i = 0; /* failure */
2244 }
2245
Darren Tuckera627d422013-06-02 07:31:17 +10002246 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002247
Damien Millera699d952008-11-03 19:27:34 +11002248 if (dynamicfwd) {
2249 if (!(i == 1 || i == 2))
2250 goto fail_free;
2251 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002252 if (!(i == 3 || i == 4)) {
2253 if (fwd->connect_path == NULL &&
2254 fwd->listen_path == NULL)
2255 goto fail_free;
2256 }
2257 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002258 goto fail_free;
2259 }
2260
Damien Miller7acefbb2014-07-18 14:11:24 +10002261 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2262 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002263 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002264 if (fwd->connect_host != NULL &&
2265 strlen(fwd->connect_host) >= NI_MAXHOST)
2266 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002267 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2268 if (fwd->connect_path != NULL &&
2269 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2270 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002271 if (fwd->listen_host != NULL &&
2272 strlen(fwd->listen_host) >= NI_MAXHOST)
2273 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002274 if (fwd->listen_path != NULL &&
2275 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2276 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002277
2278 return (i);
2279
2280 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002281 free(fwd->connect_host);
2282 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002283 free(fwd->connect_path);
2284 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002285 free(fwd->listen_host);
2286 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002287 free(fwd->listen_path);
2288 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002289 return (0);
2290}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002291
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002292int
2293parse_jump(const char *s, Options *o, int active)
2294{
2295 char *orig, *sdup, *cp;
2296 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002297 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002298
2299 active &= o->proxy_command == NULL && o->jump_host == NULL;
2300
2301 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002302 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002303 do {
2304 if ((cp = strrchr(sdup, ',')) == NULL)
2305 cp = sdup; /* last */
2306 else
2307 *cp++ = '\0';
2308
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002309 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002310 /* First argument and configuration is active */
2311 if (parse_user_host_port(cp, &user, &host, &port) != 0)
2312 goto out;
2313 } else {
2314 /* Subsequent argument or inactive configuration */
2315 if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2316 goto out;
2317 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002318 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002319 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002320 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002321 if (active) {
2322 o->jump_user = user;
2323 o->jump_host = host;
2324 o->jump_port = port;
2325 o->proxy_command = xstrdup("none");
2326 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002327 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2328 o->jump_extra = xstrdup(s);
2329 o->jump_extra[cp - s] = '\0';
2330 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002331 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002332 ret = 0;
2333 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002334 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002335 free(user);
2336 free(host);
2337 return ret;
2338}
2339
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002340/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2341static const char *
2342fmt_multistate_int(int val, const struct multistate *m)
2343{
2344 u_int i;
2345
2346 for (i = 0; m[i].key != NULL; i++) {
2347 if (m[i].value == val)
2348 return m[i].key;
2349 }
2350 return "UNKNOWN";
2351}
2352
2353static const char *
2354fmt_intarg(OpCodes code, int val)
2355{
2356 if (val == -1)
2357 return "unset";
2358 switch (code) {
2359 case oAddressFamily:
2360 return fmt_multistate_int(val, multistate_addressfamily);
2361 case oVerifyHostKeyDNS:
2362 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002363 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002364 return fmt_multistate_int(val, multistate_yesnoask);
2365 case oControlMaster:
2366 return fmt_multistate_int(val, multistate_controlmaster);
2367 case oTunnel:
2368 return fmt_multistate_int(val, multistate_tunnel);
2369 case oRequestTTY:
2370 return fmt_multistate_int(val, multistate_requesttty);
2371 case oCanonicalizeHostname:
2372 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002373 case oFingerprintHash:
2374 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002375 default:
2376 switch (val) {
2377 case 0:
2378 return "no";
2379 case 1:
2380 return "yes";
2381 default:
2382 return "UNKNOWN";
2383 }
2384 }
2385}
2386
2387static const char *
2388lookup_opcode_name(OpCodes code)
2389{
2390 u_int i;
2391
2392 for (i = 0; keywords[i].name != NULL; i++)
2393 if (keywords[i].opcode == code)
2394 return(keywords[i].name);
2395 return "UNKNOWN";
2396}
2397
2398static void
2399dump_cfg_int(OpCodes code, int val)
2400{
2401 printf("%s %d\n", lookup_opcode_name(code), val);
2402}
2403
2404static void
2405dump_cfg_fmtint(OpCodes code, int val)
2406{
2407 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2408}
2409
2410static void
2411dump_cfg_string(OpCodes code, const char *val)
2412{
2413 if (val == NULL)
2414 return;
2415 printf("%s %s\n", lookup_opcode_name(code), val);
2416}
2417
2418static void
2419dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2420{
2421 u_int i;
2422
2423 for (i = 0; i < count; i++)
2424 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2425}
2426
2427static void
2428dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2429{
2430 u_int i;
2431
2432 printf("%s", lookup_opcode_name(code));
2433 for (i = 0; i < count; i++)
2434 printf(" %s", vals[i]);
2435 printf("\n");
2436}
2437
2438static void
2439dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2440{
2441 const struct Forward *fwd;
2442 u_int i;
2443
2444 /* oDynamicForward */
2445 for (i = 0; i < count; i++) {
2446 fwd = &fwds[i];
djm@openbsd.org4833d012017-01-30 00:34:01 +00002447 if (code == oDynamicForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002448 strcmp(fwd->connect_host, "socks") != 0)
2449 continue;
djm@openbsd.org4833d012017-01-30 00:34:01 +00002450 if (code == oLocalForward && fwd->connect_host != NULL &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002451 strcmp(fwd->connect_host, "socks") == 0)
2452 continue;
2453 printf("%s", lookup_opcode_name(code));
2454 if (fwd->listen_port == PORT_STREAMLOCAL)
2455 printf(" %s", fwd->listen_path);
2456 else if (fwd->listen_host == NULL)
2457 printf(" %d", fwd->listen_port);
2458 else {
2459 printf(" [%s]:%d",
2460 fwd->listen_host, fwd->listen_port);
2461 }
2462 if (code != oDynamicForward) {
2463 if (fwd->connect_port == PORT_STREAMLOCAL)
2464 printf(" %s", fwd->connect_path);
2465 else if (fwd->connect_host == NULL)
2466 printf(" %d", fwd->connect_port);
2467 else {
2468 printf(" [%s]:%d",
2469 fwd->connect_host, fwd->connect_port);
2470 }
2471 }
2472 printf("\n");
2473 }
2474}
2475
2476void
2477dump_client_config(Options *o, const char *host)
2478{
2479 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002480 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002481
djm@openbsd.org60a92472015-08-21 23:53:08 +00002482 /* This is normally prepared in ssh_kex2 */
2483 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2484 fatal("%s: kex_assemble_names failed", __func__);
2485
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002486 /* Most interesting options first: user, host, port */
2487 dump_cfg_string(oUser, o->user);
2488 dump_cfg_string(oHostName, host);
2489 dump_cfg_int(oPort, o->port);
2490
2491 /* Flag options */
2492 dump_cfg_fmtint(oAddressFamily, o->address_family);
2493 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2494 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2495 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2496 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2497 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2498 dump_cfg_fmtint(oCompression, o->compression);
2499 dump_cfg_fmtint(oControlMaster, o->control_master);
2500 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002501 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002502 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002503 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002504 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2505 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2506 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2507 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2508#ifdef GSSAPI
2509 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2510 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2511#endif /* GSSAPI */
2512 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2513 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2514 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2515 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2516 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2517 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2518 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002519 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2520 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2521 dump_cfg_fmtint(oRequestTTY, o->request_tty);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002522#ifdef WITH_RSA1
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002523 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2524 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002525#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002526 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2527 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2528 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2529 dump_cfg_fmtint(oTunnel, o->tun_open);
2530 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2531 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2532 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002533 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002534
2535 /* Integer options */
2536 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002537 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2538 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2539 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2540 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2541 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2542
2543 /* String options */
2544 dump_cfg_string(oBindAddress, o->bind_address);
2545 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2546 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002547 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002548 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002549 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002550 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002551 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2552 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2553 dump_cfg_string(oLocalCommand, o->local_command);
2554 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2555 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002556#ifdef ENABLE_PKCS11
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002557 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
djm@openbsd.org381a2612017-01-30 00:38:50 +00002558#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002559 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002560 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002561 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002562 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002563
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002564 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002565 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2566 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2567 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2568
2569 /* String array options */
2570 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2571 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2572 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2573 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2574 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2575
2576 /* Special cases */
2577
2578 /* oConnectTimeout */
2579 if (o->connection_timeout == -1)
2580 printf("connecttimeout none\n");
2581 else
2582 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2583
2584 /* oTunnelDevice */
2585 printf("tunneldevice");
2586 if (o->tun_local == SSH_TUNID_ANY)
2587 printf(" any");
2588 else
2589 printf(" %d", o->tun_local);
2590 if (o->tun_remote == SSH_TUNID_ANY)
2591 printf(":any");
2592 else
2593 printf(":%d", o->tun_remote);
2594 printf("\n");
2595
2596 /* oCanonicalizePermittedCNAMEs */
2597 if ( o->num_permitted_cnames > 0) {
2598 printf("canonicalizePermittedcnames");
2599 for (i = 0; i < o->num_permitted_cnames; i++) {
2600 printf(" %s:%s", o->permitted_cnames[i].source_list,
2601 o->permitted_cnames[i].target_list);
2602 }
2603 printf("\n");
2604 }
2605
2606 /* oCipher */
2607 if (o->cipher != SSH_CIPHER_NOT_SET)
2608 printf("Cipher %s\n", cipher_name(o->cipher));
2609
2610 /* oControlPersist */
2611 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2612 dump_cfg_fmtint(oControlPersist, o->control_persist);
2613 else
2614 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2615
2616 /* oEscapeChar */
2617 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2618 printf("escapechar none\n");
2619 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002620 vis(buf, o->escape_char, VIS_WHITE, 0);
2621 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002622 }
2623
2624 /* oIPQoS */
2625 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2626 printf("%s\n", iptos2str(o->ip_qos_bulk));
2627
2628 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002629 printf("rekeylimit %llu %d\n",
2630 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002631
2632 /* oStreamLocalBindMask */
2633 printf("streamlocalbindmask 0%o\n",
2634 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002635
2636 /* oProxyCommand / oProxyJump */
2637 if (o->jump_host == NULL)
2638 dump_cfg_string(oProxyCommand, o->proxy_command);
2639 else {
2640 /* Check for numeric addresses */
2641 i = strchr(o->jump_host, ':') != NULL ||
2642 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2643 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2644 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002645 /* optional additional jump spec */
2646 o->jump_extra == NULL ? "" : o->jump_extra,
2647 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002648 /* optional user */
2649 o->jump_user == NULL ? "" : o->jump_user,
2650 o->jump_user == NULL ? "" : "@",
2651 /* opening [ if hostname is numeric */
2652 i ? "[" : "",
2653 /* mandatory hostname */
2654 o->jump_host,
2655 /* closing ] if hostname is numeric */
2656 i ? "]" : "",
2657 /* optional port number */
2658 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002659 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002660 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002661}