blob: c177202b1a944e670ee2ba83a072dff9afe267ee [file] [log] [blame]
djm@openbsd.org286f5a72016-07-22 03:35:11 +00001/* $OpenBSD: readconf.c,v 1.259 2016/07/22 03:35:11 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
96 Cipher 3des
97
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
111 Cipher none
112 PasswordAuthentication no
113
Damien Millerd27b9472005-12-13 19:29:02 +1100114 Host vpn.fake.com
115 Tunnel yes
116 TunnelDevice 3
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 # Defaults for various options
119 Host *
120 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 PasswordAuthentication yes
123 RSAAuthentication yes
124 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100126 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127 IdentityFile ~/.ssh/identity
128 Port 22
129 EscapeChar ~
130
131*/
132
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000133static int read_config_file_depth(const char *filename, struct passwd *pw,
134 const char *host, const char *original_host, Options *options,
135 int flags, int *activep, int depth);
136static int process_config_line_depth(Options *options, struct passwd *pw,
137 const char *host, const char *original_host, char *line,
138 const char *filename, int linenum, int *activep, int flags, int depth);
139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Keyword tokens. */
141
Damien Miller95def091999-11-25 00:26:21 +1100142typedef enum {
143 oBadOption,
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000144 oHost, oMatch, oInclude,
Damien Miller1ab6a512010-06-26 10:02:24 +1000145 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
146 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000147 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000148 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100149 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000150 oCertificateFile, oAddKeysToAgent, oIdentityAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100151 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100152 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
153 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100154 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000155 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000156 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000157 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000158 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100159 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000161 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100163 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000164 oSendEnv, oControlPath, oControlMaster, oControlPersist,
165 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100166 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000167 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000168 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100169 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
170 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000171 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000172 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000173 oPubkeyAcceptedKeyTypes, oProxyJump,
Darren Tucker07636982013-05-16 20:30:03 +1000174 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175} OpCodes;
176
177/* Textual representations of the tokens. */
178
Damien Miller95def091999-11-25 00:26:21 +1100179static struct {
180 const char *name;
181 OpCodes opcode;
182} keywords[] = {
183 { "forwardagent", oForwardAgent },
184 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000185 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000186 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000187 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000188 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100189 { "gatewayports", oGatewayPorts },
190 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000191 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100192 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100193 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
194 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000197 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000198 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000199 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000200 { "challengeresponseauthentication", oChallengeResponseAuthentication },
201 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
202 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000203 { "kerberosauthentication", oUnsupported },
204 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000205 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000206#if defined(GSSAPI)
207 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000208 { "gssapidelegatecredentials", oGssDelegateCreds },
209#else
210 { "gssapiauthentication", oUnsupported },
211 { "gssapidelegatecredentials", oUnsupported },
212#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000213 { "fallbacktorsh", oDeprecated },
214 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100215 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100216 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100217 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000218 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000219 { "addkeystoagent", oAddKeysToAgent },
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +0000220 { "identityagent", oIdentityAgent },
Damien Miller95def091999-11-25 00:26:21 +1100221 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000222 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100223 { "proxycommand", oProxyCommand },
224 { "port", oPort },
225 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000226 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000227 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000228 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100229 { "remoteforward", oRemoteForward },
230 { "localforward", oLocalForward },
231 { "user", oUser },
232 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100233 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100234 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100235 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000236 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100237 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000238 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100239 { "connectionattempts", oConnectionAttempts },
240 { "batchmode", oBatchMode },
241 { "checkhostip", oCheckHostIP },
242 { "stricthostkeychecking", oStrictHostKeyChecking },
243 { "compression", oCompression },
244 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100245 { "tcpkeepalive", oTCPKeepAlive },
246 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100247 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100248 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000249 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000250 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000251 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000252 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100253#ifdef ENABLE_PKCS11
254 { "smartcarddevice", oPKCS11Provider },
255 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000256#else
257 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100258 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000259#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100260 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000261 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000262 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000264 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000265 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000266 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100267 { "serveraliveinterval", oServerAliveInterval },
268 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000269 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000270 { "controlpath", oControlPath },
271 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000272 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100273 { "hashknownhosts", oHashKnownHosts },
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000274 { "include", oInclude },
Damien Millerd27b9472005-12-13 19:29:02 +1100275 { "tunnel", oTunnel },
276 { "tunneldevice", oTunnelDevice },
277 { "localcommand", oLocalCommand },
278 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000279 { "visualhostkey", oVisualHostKey },
markus@openbsd.orga3068632016-01-14 16:17:39 +0000280 { "useroaming", oDeprecated },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000281 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100282 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000283 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000284 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100285 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100286 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
287 { "canonicalizehostname", oCanonicalizeHostname },
288 { "canonicalizemaxdots", oCanonicalizeMaxDots },
289 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000290 { "streamlocalbindmask", oStreamLocalBindMask },
291 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000292 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000293 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000294 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000295 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000296 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000297 { "ignoreunknown", oIgnoreUnknown },
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000298 { "proxyjump", oProxyJump },
Damien Miller01ed2272008-11-05 16:20:46 +1100299
Ben Lindstrom65366a82001-12-06 16:32:47 +0000300 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100301};
302
Damien Miller5428f641999-11-25 11:54:57 +1100303/*
304 * Adds a local TCP/IP port forward to options. Never returns if there is an
305 * error.
306 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
Damien Miller4af51302000-04-16 11:18:38 +1000308void
Damien Miller7acefbb2014-07-18 14:11:24 +1000309add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310{
Damien Miller7acefbb2014-07-18 14:11:24 +1000311 struct Forward *fwd;
Damien Miller95def091999-11-25 00:26:21 +1100312 extern uid_t original_real_uid;
Darren Tucker5f41f032016-04-08 21:14:13 +1000313 int i;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000314
Damien Miller7acefbb2014-07-18 14:11:24 +1000315 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
316 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000317 fatal("Privileged ports can only be forwarded by root.");
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000318 /* Don't add duplicates */
319 for (i = 0; i < options->num_local_forwards; i++) {
320 if (forward_equals(newfwd, options->local_forwards + i))
321 return;
322 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000323 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000324 options->num_local_forwards + 1,
325 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100326 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327
Damien Miller1a0442f2008-11-05 16:30:06 +1100328 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100329 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000330 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100331 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100332 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000333 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334}
335
Damien Miller5428f641999-11-25 11:54:57 +1100336/*
337 * Adds a remote TCP/IP port forward to options. Never returns if there is
338 * an error.
339 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller4af51302000-04-16 11:18:38 +1000341void
Damien Miller7acefbb2014-07-18 14:11:24 +1000342add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343{
Damien Miller7acefbb2014-07-18 14:11:24 +1000344 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000345 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000346
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000347 /* Don't add duplicates */
348 for (i = 0; i < options->num_remote_forwards; i++) {
349 if (forward_equals(newfwd, options->remote_forwards + i))
350 return;
351 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000352 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000353 options->num_remote_forwards + 1,
354 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100355 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100356
Damien Miller1a0442f2008-11-05 16:30:06 +1100357 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100358 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000359 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100360 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100361 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000362 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100363 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000364 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365}
366
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000367static void
368clear_forwardings(Options *options)
369{
370 int i;
371
Damien Millerf91ee4c2005-03-01 21:24:33 +1100372 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000373 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000374 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000375 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000376 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100377 }
Damien Miller232cfb12010-06-26 09:50:30 +1000378 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000380 options->local_forwards = NULL;
381 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000382 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100383 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000385 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000387 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100388 }
Damien Miller232cfb12010-06-26 09:50:30 +1000389 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000390 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000391 options->remote_forwards = NULL;
392 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000393 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100394 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000395}
396
Darren Tucker19104782013-04-05 11:13:08 +1100397void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000398add_certificate_file(Options *options, const char *path, int userprovided)
399{
400 int i;
401
402 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
403 fatal("Too many certificate files specified (max %d)",
404 SSH_MAX_CERTIFICATE_FILES);
405
406 /* Avoid registering duplicates */
407 for (i = 0; i < options->num_certificate_files; i++) {
408 if (options->certificate_file_userprovided[i] == userprovided &&
409 strcmp(options->certificate_files[i], path) == 0) {
410 debug2("%s: ignoring duplicate key %s", __func__, path);
411 return;
412 }
413 }
414
415 options->certificate_file_userprovided[options->num_certificate_files] =
416 userprovided;
417 options->certificate_files[options->num_certificate_files++] =
418 xstrdup(path);
419}
420
421void
Darren Tucker19104782013-04-05 11:13:08 +1100422add_identity_file(Options *options, const char *dir, const char *filename,
423 int userprovided)
424{
425 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000426 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100427
428 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
429 fatal("Too many identity files specified (max %d)",
430 SSH_MAX_IDENTITY_FILES);
431
432 if (dir == NULL) /* no dir, filename is absolute */
433 path = xstrdup(filename);
434 else
435 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
436
Damien Miller15271902014-05-15 13:47:56 +1000437 /* Avoid registering duplicates */
438 for (i = 0; i < options->num_identity_files; i++) {
439 if (options->identity_file_userprovided[i] == userprovided &&
440 strcmp(options->identity_files[i], path) == 0) {
441 debug2("%s: ignoring duplicate key %s", __func__, path);
442 free(path);
443 return;
444 }
445 }
446
Darren Tucker19104782013-04-05 11:13:08 +1100447 options->identity_file_userprovided[options->num_identity_files] =
448 userprovided;
449 options->identity_files[options->num_identity_files++] = path;
450}
451
Damien Miller194fd902013-10-15 12:13:05 +1100452int
453default_ssh_port(void)
454{
455 static int port;
456 struct servent *sp;
457
458 if (port == 0) {
459 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
460 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
461 }
462 return port;
463}
464
465/*
466 * Execute a command in a shell.
467 * Return its exit status or -1 on abnormal exit.
468 */
469static int
470execute_in_shell(const char *cmd)
471{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000472 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100473 pid_t pid;
474 int devnull, status;
475 extern uid_t original_real_uid;
476
477 if ((shell = getenv("SHELL")) == NULL)
478 shell = _PATH_BSHELL;
479
Damien Miller194fd902013-10-15 12:13:05 +1100480 /* Need this to redirect subprocess stdin/out */
481 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
482 fatal("open(/dev/null): %s", strerror(errno));
483
484 debug("Executing command: '%.500s'", cmd);
485
486 /* Fork and execute the command. */
487 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000488 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100489
490 /* Child. Permanently give up superuser privileges. */
491 permanently_drop_suid(original_real_uid);
492
493 /* Redirect child stdin and stdout. Leave stderr */
494 if (dup2(devnull, STDIN_FILENO) == -1)
495 fatal("dup2: %s", strerror(errno));
496 if (dup2(devnull, STDOUT_FILENO) == -1)
497 fatal("dup2: %s", strerror(errno));
498 if (devnull > STDERR_FILENO)
499 close(devnull);
500 closefrom(STDERR_FILENO + 1);
501
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000502 argv[0] = shell;
503 argv[1] = "-c";
504 argv[2] = xstrdup(cmd);
505 argv[3] = NULL;
506
Damien Miller194fd902013-10-15 12:13:05 +1100507 execv(argv[0], argv);
508 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
509 /* Die with signal to make this error apparent to parent. */
510 signal(SIGTERM, SIG_DFL);
511 kill(getpid(), SIGTERM);
512 _exit(1);
513 }
514 /* Parent. */
515 if (pid < 0)
516 fatal("%s: fork: %.100s", __func__, strerror(errno));
517
518 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100519
520 while (waitpid(pid, &status, 0) == -1) {
521 if (errno != EINTR && errno != EAGAIN)
522 fatal("%s: waitpid: %s", __func__, strerror(errno));
523 }
524 if (!WIFEXITED(status)) {
525 error("command '%.100s' exited abnormally", cmd);
526 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000527 }
Damien Miller194fd902013-10-15 12:13:05 +1100528 debug3("command returned status %d", WEXITSTATUS(status));
529 return WEXITSTATUS(status);
530}
531
532/*
533 * Parse and execute a Match directive.
534 */
535static int
536match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000537 const char *host_arg, const char *original_host, int post_canon,
538 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100539{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000540 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100541 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000542 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100543 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
544
545 /*
546 * Configuration is likely to be incomplete at this point so we
547 * must be prepared to use default values.
548 */
549 port = options->port <= 0 ? default_ssh_port() : options->port;
550 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000551 if (post_canon) {
552 host = xstrdup(options->hostname);
553 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100554 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100555 host = percent_expand(options->hostname,
556 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000557 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100558 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000559 }
Damien Miller194fd902013-10-15 12:13:05 +1100560
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000561 debug2("checking match for '%s' host %s originally %s",
562 cp, host, original_host);
563 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
564 criteria = NULL;
565 this_result = 1;
566 if ((negate = attrib[0] == '!'))
567 attrib++;
568 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100569 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000570 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100571 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000572 error("%.200s line %d: '%s' cannot be combined "
573 "with other Match attributes",
574 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100575 result = -1;
576 goto out;
577 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000578 if (result)
579 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100580 goto out;
581 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 attributes++;
583 if (strcasecmp(attrib, "canonical") == 0) {
584 r = !!post_canon; /* force bitmask member to boolean */
585 if (r == (negate ? 1 : 0))
586 this_result = result = 0;
587 debug3("%.200s line %d: %smatched '%s'",
588 filename, linenum,
589 this_result ? "" : "not ", oattrib);
590 continue;
591 }
592 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100593 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
594 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100595 result = -1;
596 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100597 }
Damien Miller194fd902013-10-15 12:13:05 +1100598 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000599 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000600 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000601 if (r == (negate ? 1 : 0))
602 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100603 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000604 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000605 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000606 if (r == (negate ? 1 : 0))
607 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100608 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000609 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000610 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 if (r == (negate ? 1 : 0))
612 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100613 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000614 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000615 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 if (r == (negate ? 1 : 0))
617 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100618 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100619 if (gethostname(thishost, sizeof(thishost)) == -1)
620 fatal("gethostname: %s", strerror(errno));
621 strlcpy(shorthost, thishost, sizeof(shorthost));
622 shorthost[strcspn(thishost, ".")] = '\0';
623 snprintf(portstr, sizeof(portstr), "%d", port);
624
625 cmd = percent_expand(arg,
626 "L", shorthost,
627 "d", pw->pw_dir,
628 "h", host,
629 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000630 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100631 "p", portstr,
632 "r", ruser,
633 "u", pw->pw_name,
634 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100635 if (result != 1) {
636 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000637 debug3("%.200s line %d: skipped exec "
638 "\"%.100s\"", filename, linenum, cmd);
639 free(cmd);
640 continue;
Damien Miller06287802014-02-24 15:56:45 +1100641 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000642 r = execute_in_shell(cmd);
643 if (r == -1) {
644 fatal("%.200s line %d: match exec "
645 "'%.100s' error", filename,
646 linenum, cmd);
647 }
648 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100649 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000650 /* Force exit status to boolean */
651 r = r == 0;
652 if (r == (negate ? 1 : 0))
653 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100654 } else {
655 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100656 result = -1;
657 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100658 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000659 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
660 filename, linenum, this_result ? "": "not ",
661 oattrib, criteria);
662 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100663 }
Damien Millercf31f382013-10-24 21:02:56 +1100664 if (attributes == 0) {
665 error("One or more attributes required for Match");
666 result = -1;
667 goto out;
668 }
Damien Miller084bcd22013-10-23 16:30:51 +1100669 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000670 if (result != -1)
671 debug2("match %sfound", result ? "" : "not ");
672 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100673 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100674 return result;
675}
676
Damien Miller0faf7472013-10-17 11:47:23 +1100677/* Check and prepare a domain name: removes trailing '.' and lowercases */
678static void
679valid_domain(char *name, const char *filename, int linenum)
680{
681 size_t i, l = strlen(name);
682 u_char c, last = '\0';
683
684 if (l == 0)
685 fatal("%s line %d: empty hostname suffix", filename, linenum);
686 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
687 fatal("%s line %d: hostname suffix \"%.100s\" "
688 "starts with invalid character", filename, linenum, name);
689 for (i = 0; i < l; i++) {
690 c = tolower((u_char)name[i]);
691 name[i] = (char)c;
692 if (last == '.' && c == '.')
693 fatal("%s line %d: hostname suffix \"%.100s\" contains "
694 "consecutive separators", filename, linenum, name);
695 if (c != '.' && c != '-' && !isalnum(c) &&
696 c != '_') /* technically invalid, but common */
697 fatal("%s line %d: hostname suffix \"%.100s\" contains "
698 "invalid characters", filename, linenum, name);
699 last = c;
700 }
701 if (name[l - 1] == '.')
702 name[l - 1] = '\0';
703}
704
Damien Miller5428f641999-11-25 11:54:57 +1100705/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000706 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100707 */
Damien Miller4af51302000-04-16 11:18:38 +1000708static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000709parse_token(const char *cp, const char *filename, int linenum,
710 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711{
Darren Tucker07636982013-05-16 20:30:03 +1000712 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713
Damien Miller95def091999-11-25 00:26:21 +1100714 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000715 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100716 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000717 if (ignored_unknown != NULL &&
718 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000719 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000720 error("%s: line %d: Bad configuration option: %s",
721 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100722 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723}
724
Damien Millere9fc72e2013-10-15 12:14:12 +1100725/* Multistate option parsing */
726struct multistate {
727 char *key;
728 int value;
729};
730static const struct multistate multistate_flag[] = {
731 { "true", 1 },
732 { "false", 0 },
733 { "yes", 1 },
734 { "no", 0 },
735 { NULL, -1 }
736};
737static const struct multistate multistate_yesnoask[] = {
738 { "true", 1 },
739 { "false", 0 },
740 { "yes", 1 },
741 { "no", 0 },
742 { "ask", 2 },
743 { NULL, -1 }
744};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000745static const struct multistate multistate_yesnoaskconfirm[] = {
746 { "true", 1 },
747 { "false", 0 },
748 { "yes", 1 },
749 { "no", 0 },
750 { "ask", 2 },
751 { "confirm", 3 },
752 { NULL, -1 }
753};
Damien Millere9fc72e2013-10-15 12:14:12 +1100754static const struct multistate multistate_addressfamily[] = {
755 { "inet", AF_INET },
756 { "inet6", AF_INET6 },
757 { "any", AF_UNSPEC },
758 { NULL, -1 }
759};
760static const struct multistate multistate_controlmaster[] = {
761 { "true", SSHCTL_MASTER_YES },
762 { "yes", SSHCTL_MASTER_YES },
763 { "false", SSHCTL_MASTER_NO },
764 { "no", SSHCTL_MASTER_NO },
765 { "auto", SSHCTL_MASTER_AUTO },
766 { "ask", SSHCTL_MASTER_ASK },
767 { "autoask", SSHCTL_MASTER_AUTO_ASK },
768 { NULL, -1 }
769};
770static const struct multistate multistate_tunnel[] = {
771 { "ethernet", SSH_TUNMODE_ETHERNET },
772 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
773 { "true", SSH_TUNMODE_DEFAULT },
774 { "yes", SSH_TUNMODE_DEFAULT },
775 { "false", SSH_TUNMODE_NO },
776 { "no", SSH_TUNMODE_NO },
777 { NULL, -1 }
778};
779static const struct multistate multistate_requesttty[] = {
780 { "true", REQUEST_TTY_YES },
781 { "yes", REQUEST_TTY_YES },
782 { "false", REQUEST_TTY_NO },
783 { "no", REQUEST_TTY_NO },
784 { "force", REQUEST_TTY_FORCE },
785 { "auto", REQUEST_TTY_AUTO },
786 { NULL, -1 }
787};
Damien Miller38505592013-10-17 11:48:13 +1100788static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100789 { "true", SSH_CANONICALISE_YES },
790 { "false", SSH_CANONICALISE_NO },
791 { "yes", SSH_CANONICALISE_YES },
792 { "no", SSH_CANONICALISE_NO },
793 { "always", SSH_CANONICALISE_ALWAYS },
794 { NULL, -1 }
795};
Damien Millere9fc72e2013-10-15 12:14:12 +1100796
Damien Miller5428f641999-11-25 11:54:57 +1100797/*
798 * Processes a single option line as used in the configuration files. This
799 * only sets those values that have not already been set.
800 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100801int
Damien Miller194fd902013-10-15 12:13:05 +1100802process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000803 const char *original_host, char *line, const char *filename,
804 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000806 return process_config_line_depth(options, pw, host, original_host,
807 line, filename, linenum, activep, flags, 0);
808}
809
810#define WHITESPACE " \t\r\n"
811static int
812process_config_line_depth(Options *options, struct passwd *pw, const char *host,
813 const char *original_host, char *line, const char *filename,
814 int linenum, int *activep, int flags, int depth)
815{
Damien Miller295ee632011-05-29 21:42:31 +1000816 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
817 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000818 u_int i, *uintptr, max_entries = 0;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000819 int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100820 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000821 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100822 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000823 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100824 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100825 struct allowed_cname *cname;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +0000826 glob_t gl;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827
Damien Miller194fd902013-10-15 12:13:05 +1100828 if (activep == NULL) { /* We are processing a command line directive */
829 cmdline = 1;
830 activep = &cmdline;
831 }
832
Damien Millerc652cac2003-05-14 13:40:54 +1000833 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000834 if ((len = strlen(line)) == 0)
835 return 0;
836 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000837 if (strchr(WHITESPACE, line[len]) == NULL)
838 break;
839 line[len] = '\0';
840 }
841
Damien Millerbe484b52000-07-15 14:14:16 +1000842 s = line;
843 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100844 if ((keyword = strdelim(&s)) == NULL)
845 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000846 /* Ignore leading whitespace. */
847 if (*keyword == '\0')
848 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000849 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100850 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000851 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100852 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Darren Tucker07636982013-05-16 20:30:03 +1000854 opcode = parse_token(keyword, filename, linenum,
855 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller95def091999-11-25 00:26:21 +1100857 switch (opcode) {
858 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100859 /* don't panic, but count bad options */
860 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100861 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000862 case oIgnoredUnknownOption:
863 debug("%s line %d: Ignored unknown option \"%s\"",
864 filename, linenum, keyword);
865 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000866 case oConnectTimeout:
867 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100868parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000869 arg = strdelim(&s);
870 if (!arg || *arg == '\0')
871 fatal("%s line %d: missing time value.",
872 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000873 if (strcmp(arg, "none") == 0)
874 value = -1;
875 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000876 fatal("%s line %d: invalid time value.",
877 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100878 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000879 *intptr = value;
880 break;
881
Damien Miller95def091999-11-25 00:26:21 +1100882 case oForwardAgent:
883 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100884 parse_flag:
885 multistate_ptr = multistate_flag;
886 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000887 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000888 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100889 fatal("%s line %d: missing argument.",
890 filename, linenum);
891 value = -1;
892 for (i = 0; multistate_ptr[i].key != NULL; i++) {
893 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
894 value = multistate_ptr[i].value;
895 break;
896 }
897 }
898 if (value == -1)
899 fatal("%s line %d: unsupported option \"%s\".",
900 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100901 if (*activep && *intptr == -1)
902 *intptr = value;
903 break;
904
905 case oForwardX11:
906 intptr = &options->forward_x11;
907 goto parse_flag;
908
Darren Tucker0a118da2003-10-15 15:54:32 +1000909 case oForwardX11Trusted:
910 intptr = &options->forward_x11_trusted;
911 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000912
Damien Miller1ab6a512010-06-26 10:02:24 +1000913 case oForwardX11Timeout:
914 intptr = &options->forward_x11_timeout;
915 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000916
Damien Miller95def091999-11-25 00:26:21 +1100917 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000918 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100919 goto parse_flag;
920
Darren Tuckere7d4b192006-07-12 22:17:10 +1000921 case oExitOnForwardFailure:
922 intptr = &options->exit_on_forward_failure;
923 goto parse_flag;
924
Damien Miller95def091999-11-25 00:26:21 +1100925 case oUsePrivilegedPort:
926 intptr = &options->use_privileged_port;
927 goto parse_flag;
928
Damien Miller95def091999-11-25 00:26:21 +1100929 case oPasswordAuthentication:
930 intptr = &options->password_authentication;
931 goto parse_flag;
932
Damien Miller874d77b2000-10-14 16:23:11 +1100933 case oKbdInteractiveAuthentication:
934 intptr = &options->kbd_interactive_authentication;
935 goto parse_flag;
936
937 case oKbdInteractiveDevices:
938 charptr = &options->kbd_interactive_devices;
939 goto parse_string;
940
Damien Miller0bc1bd82000-11-13 22:57:25 +1100941 case oPubkeyAuthentication:
942 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000943 goto parse_flag;
944
Damien Miller95def091999-11-25 00:26:21 +1100945 case oRSAAuthentication:
946 intptr = &options->rsa_authentication;
947 goto parse_flag;
948
949 case oRhostsRSAAuthentication:
950 intptr = &options->rhosts_rsa_authentication;
951 goto parse_flag;
952
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000953 case oHostbasedAuthentication:
954 intptr = &options->hostbased_authentication;
955 goto parse_flag;
956
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000957 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000958 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100959 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000960
Darren Tucker0efd1552003-08-26 11:49:55 +1000961 case oGssAuthentication:
962 intptr = &options->gss_authentication;
963 goto parse_flag;
964
965 case oGssDelegateCreds:
966 intptr = &options->gss_deleg_creds;
967 goto parse_flag;
968
Damien Miller95def091999-11-25 00:26:21 +1100969 case oBatchMode:
970 intptr = &options->batch_mode;
971 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 case oCheckHostIP:
974 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000975 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller37876e92003-05-15 10:19:46 +1000977 case oVerifyHostKeyDNS:
978 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100979 multistate_ptr = multistate_yesnoask;
980 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 case oStrictHostKeyChecking:
983 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100984 multistate_ptr = multistate_yesnoask;
985 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 case oCompression:
988 intptr = &options->compression;
989 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller12c150e2003-12-17 16:31:10 +1100991 case oTCPKeepAlive:
992 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100993 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000995 case oNoHostAuthenticationForLocalhost:
996 intptr = &options->no_host_authentication_for_localhost;
997 goto parse_flag;
998
Damien Miller95def091999-11-25 00:26:21 +1100999 case oNumberOfPasswordPrompts:
1000 intptr = &options->number_of_password_prompts;
1001 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 case oCompressionLevel:
1004 intptr = &options->compression_level;
1005 goto parse_int;
1006
Damien Millera5539d22003-04-09 20:50:06 +10001007 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +10001008 arg = strdelim(&s);
1009 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001010 fatal("%.200s line %d: Missing argument.", filename,
1011 linenum);
1012 if (strcmp(arg, "default") == 0) {
1013 val64 = 0;
1014 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001015 if (scan_scaled(arg, &val64) == -1)
1016 fatal("%.200s line %d: Bad number '%s': %s",
1017 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001018 if (val64 != 0 && val64 < 16)
1019 fatal("%.200s line %d: RekeyLimit too small",
1020 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +10001021 }
Damien Miller3dff1762008-02-10 22:25:52 +11001022 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001023 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001024 if (s != NULL) { /* optional rekey interval present */
1025 if (strcmp(s, "none") == 0) {
1026 (void)strdelim(&s); /* discard */
1027 break;
1028 }
1029 intptr = &options->rekey_interval;
1030 goto parse_time;
1031 }
Damien Millera5539d22003-04-09 20:50:06 +10001032 break;
1033
Damien Miller95def091999-11-25 00:26:21 +11001034 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001035 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001036 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001037 fatal("%.200s line %d: Missing argument.", filename, linenum);
1038 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001039 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001040 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001041 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001042 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001043 add_identity_file(options, NULL,
1044 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001045 }
1046 break;
1047
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001048 case oCertificateFile:
1049 arg = strdelim(&s);
1050 if (!arg || *arg == '\0')
1051 fatal("%.200s line %d: Missing argument.",
1052 filename, linenum);
1053 if (*activep) {
1054 intptr = &options->num_certificate_files;
1055 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1056 fatal("%.200s line %d: Too many certificate "
1057 "files specified (max %d).",
1058 filename, linenum,
1059 SSH_MAX_CERTIFICATE_FILES);
1060 }
1061 add_certificate_file(options, arg,
1062 flags & SSHCONF_USERCONF);
1063 }
1064 break;
1065
Damien Millerd3a18572000-06-07 19:55:44 +10001066 case oXAuthLocation:
1067 charptr=&options->xauth_location;
1068 goto parse_string;
1069
Damien Miller95def091999-11-25 00:26:21 +11001070 case oUser:
1071 charptr = &options->user;
1072parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001073 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001074 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001075 fatal("%.200s line %d: Missing argument.",
1076 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001077 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001078 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001079 break;
1080
1081 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001082 cpptr = (char **)&options->system_hostfiles;
1083 uintptr = &options->num_system_hostfiles;
1084 max_entries = SSH_MAX_HOSTS_FILES;
1085parse_char_array:
1086 if (*activep && *uintptr == 0) {
1087 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1088 if ((*uintptr) >= max_entries)
1089 fatal("%s line %d: "
1090 "too many authorized keys files.",
1091 filename, linenum);
1092 cpptr[(*uintptr)++] = xstrdup(arg);
1093 }
1094 }
1095 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001096
1097 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001098 cpptr = (char **)&options->user_hostfiles;
1099 uintptr = &options->num_user_hostfiles;
1100 max_entries = SSH_MAX_HOSTS_FILES;
1101 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001102
Damien Miller95def091999-11-25 00:26:21 +11001103 case oHostName:
1104 charptr = &options->hostname;
1105 goto parse_string;
1106
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001107 case oHostKeyAlias:
1108 charptr = &options->host_key_alias;
1109 goto parse_string;
1110
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001111 case oPreferredAuthentications:
1112 charptr = &options->preferred_authentications;
1113 goto parse_string;
1114
Ben Lindstrome0f88042001-04-30 13:06:24 +00001115 case oBindAddress:
1116 charptr = &options->bind_address;
1117 goto parse_string;
1118
Damien Miller7ea845e2010-02-12 09:21:02 +11001119 case oPKCS11Provider:
1120 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001121 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001122
Damien Miller95def091999-11-25 00:26:21 +11001123 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001124 charptr = &options->proxy_command;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001125 /* Ignore ProxyCommand if ProxyJump already specified */
1126 if (options->jump_host != NULL)
1127 charptr = &options->jump_host; /* Skip below */
Damien Millerd27b9472005-12-13 19:29:02 +11001128parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001129 if (s == NULL)
1130 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001131 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001132 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001133 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001134 return 0;
1135
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001136 case oProxyJump:
1137 if (s == NULL) {
1138 fatal("%.200s line %d: Missing argument.",
1139 filename, linenum);
1140 }
1141 len = strspn(s, WHITESPACE "=");
1142 if (parse_jump(s + len, options, *activep) == -1) {
1143 fatal("%.200s line %d: Invalid ProxyJump \"%s\"",
1144 filename, linenum, s + len);
1145 }
1146 return 0;
1147
Damien Miller95def091999-11-25 00:26:21 +11001148 case oPort:
1149 intptr = &options->port;
1150parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001151 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001152 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001153 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001154 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001155 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001156
1157 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001158 value = strtol(arg, &endofnumber, 0);
1159 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001160 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001161 if (*activep && *intptr == -1)
1162 *intptr = value;
1163 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164
Damien Miller95def091999-11-25 00:26:21 +11001165 case oConnectionAttempts:
1166 intptr = &options->connection_attempts;
1167 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001168
Damien Miller95def091999-11-25 00:26:21 +11001169 case oCipher:
1170 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001171 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001172 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001173 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001174 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001175 if (value == -1)
1176 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001177 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001178 if (*activep && *intptr == -1)
1179 *intptr = value;
1180 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181
Damien Miller78928792000-04-12 20:17:38 +10001182 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001183 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001184 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001185 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001186 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001187 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001188 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001189 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001190 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001191 break;
1192
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001193 case oMacs:
1194 arg = strdelim(&s);
1195 if (!arg || *arg == '\0')
1196 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001197 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001198 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001199 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001200 if (*activep && options->macs == NULL)
1201 options->macs = xstrdup(arg);
1202 break;
1203
Damien Millerd5f62bf2010-09-24 22:11:14 +10001204 case oKexAlgorithms:
1205 arg = strdelim(&s);
1206 if (!arg || *arg == '\0')
1207 fatal("%.200s line %d: Missing argument.",
1208 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001209 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001210 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1211 filename, linenum, arg ? arg : "<NONE>");
1212 if (*activep && options->kex_algorithms == NULL)
1213 options->kex_algorithms = xstrdup(arg);
1214 break;
1215
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001216 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001217 charptr = &options->hostkeyalgorithms;
1218parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001219 arg = strdelim(&s);
1220 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001221 fatal("%.200s line %d: Missing argument.",
1222 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001223 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001224 fatal("%s line %d: Bad key types '%s'.",
1225 filename, linenum, arg ? arg : "<NONE>");
1226 if (*activep && *charptr == NULL)
1227 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001228 break;
1229
Damien Miller78928792000-04-12 20:17:38 +10001230 case oProtocol:
1231 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001232 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001233 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001234 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001235 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001236 if (value == SSH_PROTO_UNKNOWN)
1237 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001238 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001239 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1240 *intptr = value;
1241 break;
1242
Damien Miller95def091999-11-25 00:26:21 +11001243 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001244 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001245 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001246 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001247 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001248 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001249 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001250 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1251 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001252 break;
1253
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001254 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001255 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001256 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001257 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001258 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001259 fatal("%.200s line %d: Missing port argument.",
1260 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001261
Damien Millera699d952008-11-03 19:27:34 +11001262 if (opcode == oLocalForward ||
1263 opcode == oRemoteForward) {
1264 arg2 = strdelim(&s);
1265 if (arg2 == NULL || *arg2 == '\0')
1266 fatal("%.200s line %d: Missing target argument.",
1267 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001268
Damien Millera699d952008-11-03 19:27:34 +11001269 /* construct a string for parse_forward */
1270 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1271 } else if (opcode == oDynamicForward) {
1272 strlcpy(fwdarg, arg, sizeof(fwdarg));
1273 }
1274
1275 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001276 opcode == oDynamicForward ? 1 : 0,
1277 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001278 fatal("%.200s line %d: Bad forwarding specification.",
1279 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001280
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001281 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001282 if (opcode == oLocalForward ||
1283 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001284 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001285 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001286 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001287 }
Damien Miller95def091999-11-25 00:26:21 +11001288 break;
1289
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001290 case oClearAllForwardings:
1291 intptr = &options->clear_forwardings;
1292 goto parse_flag;
1293
Damien Miller95def091999-11-25 00:26:21 +11001294 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001295 if (cmdline)
1296 fatal("Host directive not supported as a command-line "
1297 "option");
Damien Miller95def091999-11-25 00:26:21 +11001298 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001299 arg2 = NULL;
1300 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001301 if ((flags & SSHCONF_NEVERMATCH) != 0)
1302 break;
Damien Millerfe924212011-05-15 08:44:45 +10001303 negated = *arg == '!';
1304 if (negated)
1305 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001306 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001307 if (negated) {
1308 debug("%.200s line %d: Skipping Host "
1309 "block because of negated match "
1310 "for %.100s", filename, linenum,
1311 arg);
1312 *activep = 0;
1313 break;
1314 }
1315 if (!*activep)
1316 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001317 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001318 }
Damien Millerfe924212011-05-15 08:44:45 +10001319 }
1320 if (*activep)
1321 debug("%.200s line %d: Applying options for %.100s",
1322 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001323 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001324 return 0;
1325
Damien Miller194fd902013-10-15 12:13:05 +11001326 case oMatch:
1327 if (cmdline)
1328 fatal("Host directive not supported as a command-line "
1329 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001330 value = match_cfg_line(options, &s, pw, host, original_host,
1331 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001332 if (value < 0)
1333 fatal("%.200s line %d: Bad Match condition", filename,
1334 linenum);
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001335 *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
Damien Miller194fd902013-10-15 12:13:05 +11001336 break;
1337
Damien Miller95def091999-11-25 00:26:21 +11001338 case oEscapeChar:
1339 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001340 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001341 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001342 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001343 if (strcmp(arg, "none") == 0)
1344 value = SSH_ESCAPECHAR_NONE;
1345 else if (arg[1] == '\0')
1346 value = (u_char) arg[0];
1347 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001348 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1349 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001350 else {
1351 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001352 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001353 /* NOTREACHED */
1354 value = 0; /* Avoid compiler warning. */
1355 }
1356 if (*activep && *intptr == -1)
1357 *intptr = value;
1358 break;
1359
Damien Miller20a8f972003-05-18 20:50:30 +10001360 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001361 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001362 multistate_ptr = multistate_addressfamily;
1363 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001364
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001365 case oEnableSSHKeysign:
1366 intptr = &options->enable_ssh_keysign;
1367 goto parse_flag;
1368
Damien Millerbd394c32004-03-08 23:12:36 +11001369 case oIdentitiesOnly:
1370 intptr = &options->identities_only;
1371 goto parse_flag;
1372
Damien Miller509b0102003-12-17 16:33:10 +11001373 case oServerAliveInterval:
1374 intptr = &options->server_alive_interval;
1375 goto parse_time;
1376
1377 case oServerAliveCountMax:
1378 intptr = &options->server_alive_count_max;
1379 goto parse_int;
1380
Darren Tucker46bc0752004-05-02 22:11:30 +10001381 case oSendEnv:
1382 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1383 if (strchr(arg, '=') != NULL)
1384 fatal("%s line %d: Invalid environment name.",
1385 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001386 if (!*activep)
1387 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001388 if (options->num_send_env >= MAX_SEND_ENV)
1389 fatal("%s line %d: too many send env.",
1390 filename, linenum);
1391 options->send_env[options->num_send_env++] =
1392 xstrdup(arg);
1393 }
1394 break;
1395
Damien Miller0e220db2004-06-15 10:34:08 +10001396 case oControlPath:
1397 charptr = &options->control_path;
1398 goto parse_string;
1399
1400 case oControlMaster:
1401 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001402 multistate_ptr = multistate_controlmaster;
1403 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001404
Damien Millere11e1ea2010-08-03 16:04:46 +10001405 case oControlPersist:
1406 /* no/false/yes/true, or a time spec */
1407 intptr = &options->control_persist;
1408 arg = strdelim(&s);
1409 if (!arg || *arg == '\0')
1410 fatal("%.200s line %d: Missing ControlPersist"
1411 " argument.", filename, linenum);
1412 value = 0;
1413 value2 = 0; /* timeout */
1414 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1415 value = 0;
1416 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1417 value = 1;
1418 else if ((value2 = convtime(arg)) >= 0)
1419 value = 1;
1420 else
1421 fatal("%.200s line %d: Bad ControlPersist argument.",
1422 filename, linenum);
1423 if (*activep && *intptr == -1) {
1424 *intptr = value;
1425 options->control_persist_timeout = value2;
1426 }
1427 break;
1428
Damien Millere1776152005-03-01 21:47:37 +11001429 case oHashKnownHosts:
1430 intptr = &options->hash_known_hosts;
1431 goto parse_flag;
1432
Damien Millerd27b9472005-12-13 19:29:02 +11001433 case oTunnel:
1434 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001435 multistate_ptr = multistate_tunnel;
1436 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001437
1438 case oTunnelDevice:
1439 arg = strdelim(&s);
1440 if (!arg || *arg == '\0')
1441 fatal("%.200s line %d: Missing argument.", filename, linenum);
1442 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001443 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001444 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1445 if (*activep) {
1446 options->tun_local = value;
1447 options->tun_remote = value2;
1448 }
1449 break;
1450
1451 case oLocalCommand:
1452 charptr = &options->local_command;
1453 goto parse_command;
1454
1455 case oPermitLocalCommand:
1456 intptr = &options->permit_local_command;
1457 goto parse_flag;
1458
Damien Miller10288242008-06-30 00:04:03 +10001459 case oVisualHostKey:
1460 intptr = &options->visual_host_key;
1461 goto parse_flag;
1462
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001463 case oInclude:
1464 if (cmdline)
1465 fatal("Include directive not supported as a "
1466 "command-line option");
1467 value = 0;
1468 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1469 /*
1470 * Ensure all paths are anchored. User configuration
1471 * files may begin with '~/' but system configurations
1472 * must not. If the path is relative, then treat it
1473 * as living in ~/.ssh for user configurations or
1474 * /etc/ssh for system ones.
1475 */
1476 if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0)
1477 fatal("%.200s line %d: bad include path %s.",
1478 filename, linenum, arg);
1479 if (*arg != '/' && *arg != '~') {
1480 xasprintf(&arg2, "%s/%s",
1481 (flags & SSHCONF_USERCONF) ?
1482 "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1483 } else
1484 arg2 = xstrdup(arg);
1485 memset(&gl, 0, sizeof(gl));
1486 r = glob(arg2, GLOB_TILDE, NULL, &gl);
1487 if (r == GLOB_NOMATCH) {
1488 debug("%.200s line %d: include %s matched no "
1489 "files",filename, linenum, arg2);
1490 continue;
1491 } else if (r != 0 || gl.gl_pathc < 0)
1492 fatal("%.200s line %d: glob failed for %s.",
1493 filename, linenum, arg2);
1494 free(arg2);
1495 oactive = *activep;
1496 for (i = 0; i < (u_int)gl.gl_pathc; i++) {
1497 debug3("%.200s line %d: Including file %s "
1498 "depth %d%s", filename, linenum,
1499 gl.gl_pathv[i], depth,
1500 oactive ? "" : " (parse only)");
1501 r = read_config_file_depth(gl.gl_pathv[i],
1502 pw, host, original_host, options,
1503 flags | SSHCONF_CHECKPERM |
1504 (oactive ? 0 : SSHCONF_NEVERMATCH),
1505 activep, depth + 1);
1506 /*
1507 * don't let Match in includes clobber the
1508 * containing file's Match state.
1509 */
1510 *activep = oactive;
1511 if (r != 1)
1512 value = -1;
1513 }
1514 globfree(&gl);
1515 }
1516 if (value != 0)
1517 return value;
1518 break;
1519
Damien Miller0dac6fb2010-11-20 15:19:38 +11001520 case oIPQoS:
1521 arg = strdelim(&s);
1522 if ((value = parse_ipqos(arg)) == -1)
1523 fatal("%s line %d: Bad IPQoS value: %s",
1524 filename, linenum, arg);
1525 arg = strdelim(&s);
1526 if (arg == NULL)
1527 value2 = value;
1528 else if ((value2 = parse_ipqos(arg)) == -1)
1529 fatal("%s line %d: Bad IPQoS value: %s",
1530 filename, linenum, arg);
1531 if (*activep) {
1532 options->ip_qos_interactive = value;
1533 options->ip_qos_bulk = value2;
1534 }
1535 break;
1536
Damien Miller21771e22011-05-15 08:45:50 +10001537 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001538 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001539 multistate_ptr = multistate_requesttty;
1540 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001541
Darren Tucker07636982013-05-16 20:30:03 +10001542 case oIgnoreUnknown:
1543 charptr = &options->ignored_unknown;
1544 goto parse_string;
1545
Damien Miller1262b662013-08-21 02:44:24 +10001546 case oProxyUseFdpass:
1547 intptr = &options->proxy_use_fdpass;
1548 goto parse_flag;
1549
Damien Miller0faf7472013-10-17 11:47:23 +11001550 case oCanonicalDomains:
1551 value = options->num_canonical_domains != 0;
1552 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1553 valid_domain(arg, filename, linenum);
1554 if (!*activep || value)
1555 continue;
1556 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1557 fatal("%s line %d: too many hostname suffixes.",
1558 filename, linenum);
1559 options->canonical_domains[
1560 options->num_canonical_domains++] = xstrdup(arg);
1561 }
1562 break;
1563
Damien Miller38505592013-10-17 11:48:13 +11001564 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001565 value = options->num_permitted_cnames != 0;
1566 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1567 /* Either '*' for everything or 'list:list' */
1568 if (strcmp(arg, "*") == 0)
1569 arg2 = arg;
1570 else {
1571 lowercase(arg);
1572 if ((arg2 = strchr(arg, ':')) == NULL ||
1573 arg2[1] == '\0') {
1574 fatal("%s line %d: "
1575 "Invalid permitted CNAME \"%s\"",
1576 filename, linenum, arg);
1577 }
1578 *arg2 = '\0';
1579 arg2++;
1580 }
1581 if (!*activep || value)
1582 continue;
1583 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1584 fatal("%s line %d: too many permitted CNAMEs.",
1585 filename, linenum);
1586 cname = options->permitted_cnames +
1587 options->num_permitted_cnames++;
1588 cname->source_list = xstrdup(arg);
1589 cname->target_list = xstrdup(arg2);
1590 }
1591 break;
1592
Damien Miller38505592013-10-17 11:48:13 +11001593 case oCanonicalizeHostname:
1594 intptr = &options->canonicalize_hostname;
1595 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001596 goto parse_multistate;
1597
Damien Miller38505592013-10-17 11:48:13 +11001598 case oCanonicalizeMaxDots:
1599 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001600 goto parse_int;
1601
Damien Miller38505592013-10-17 11:48:13 +11001602 case oCanonicalizeFallbackLocal:
1603 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001604 goto parse_flag;
1605
Damien Miller7acefbb2014-07-18 14:11:24 +10001606 case oStreamLocalBindMask:
1607 arg = strdelim(&s);
1608 if (!arg || *arg == '\0')
1609 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1610 /* Parse mode in octal format */
1611 value = strtol(arg, &endofnumber, 8);
1612 if (arg == endofnumber || value < 0 || value > 0777)
1613 fatal("%.200s line %d: Bad mask.", filename, linenum);
1614 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1615 break;
1616
1617 case oStreamLocalBindUnlink:
1618 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1619 goto parse_flag;
1620
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001621 case oRevokedHostKeys:
1622 charptr = &options->revoked_host_keys;
1623 goto parse_string;
1624
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001625 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001626 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001627 arg = strdelim(&s);
1628 if (!arg || *arg == '\0')
1629 fatal("%.200s line %d: Missing argument.",
1630 filename, linenum);
1631 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1632 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1633 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001634 if (*activep && *intptr == -1)
1635 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001636 break;
1637
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001638 case oUpdateHostkeys:
1639 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001640 multistate_ptr = multistate_yesnoask;
1641 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001642
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001643 case oHostbasedKeyTypes:
1644 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001645 goto parse_keytypes;
1646
1647 case oPubkeyAcceptedKeyTypes:
1648 charptr = &options->pubkey_key_types;
1649 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001650
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001651 case oAddKeysToAgent:
1652 intptr = &options->add_keys_to_agent;
1653 multistate_ptr = multistate_yesnoaskconfirm;
1654 goto parse_multistate;
1655
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001656 case oIdentityAgent:
1657 charptr = &options->identity_agent;
1658 goto parse_string;
1659
Ben Lindstrom4daea862002-06-09 20:04:02 +00001660 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001661 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001662 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001663 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001664
Damien Millerf9b3feb2003-05-16 11:38:32 +10001665 case oUnsupported:
1666 error("%s line %d: Unsupported option \"%s\"",
1667 filename, linenum, keyword);
1668 return 0;
1669
Damien Miller95def091999-11-25 00:26:21 +11001670 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001671 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001672 }
1673
1674 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001675 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001676 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001677 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001678 }
Damien Miller95def091999-11-25 00:26:21 +11001679 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001680}
1681
Damien Miller5428f641999-11-25 11:54:57 +11001682/*
1683 * Reads the config file and modifies the options accordingly. Options
1684 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001685 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001686 */
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001687int
Damien Miller194fd902013-10-15 12:13:05 +11001688read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001689 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690{
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001691 int active = 1;
1692
1693 return read_config_file_depth(filename, pw, host, original_host,
1694 options, flags, &active, 0);
1695}
1696
1697#define READCONF_MAX_DEPTH 16
1698static int
1699read_config_file_depth(const char *filename, struct passwd *pw,
1700 const char *host, const char *original_host, Options *options,
1701 int flags, int *activep, int depth)
1702{
Damien Miller95def091999-11-25 00:26:21 +11001703 FILE *f;
1704 char line[1024];
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001705 int linenum;
Damien Miller95def091999-11-25 00:26:21 +11001706 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001707
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001708 if (depth < 0 || depth > READCONF_MAX_DEPTH)
1709 fatal("Too many recursive configuration includes");
1710
Damien Miller57a44762004-04-20 20:11:57 +10001711 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001712 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001713
Darren Tuckeraefa3682013-04-05 11:18:35 +11001714 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001715 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001716
Damien Miller33793852004-06-15 10:27:55 +10001717 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001718 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001719 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001720 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001721 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001722 }
1723
Damien Miller95def091999-11-25 00:26:21 +11001724 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001725
Damien Miller5428f641999-11-25 11:54:57 +11001726 /*
1727 * Mark that we are now processing the options. This flag is turned
1728 * on/off by Host specifications.
1729 */
Damien Miller95def091999-11-25 00:26:21 +11001730 linenum = 0;
1731 while (fgets(line, sizeof(line), f)) {
1732 /* Update line number counter. */
1733 linenum++;
djm@openbsd.orgdc7990b2016-04-15 00:30:19 +00001734 if (process_config_line_depth(options, pw, host, original_host,
1735 line, filename, linenum, activep, flags, depth) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001736 bad_options++;
1737 }
1738 fclose(f);
1739 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001740 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001741 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001742 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001743}
1744
Damien Miller13f97b22014-02-24 15:57:55 +11001745/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1746int
1747option_clear_or_none(const char *o)
1748{
1749 return o == NULL || strcasecmp(o, "none") == 0;
1750}
1751
Damien Miller5428f641999-11-25 11:54:57 +11001752/*
1753 * Initializes options to special values that indicate that they have not yet
1754 * been set. Read_config_file will only set options with this value. Options
1755 * are processed in the following order: command line, user config file,
1756 * system config file. Last, fill_default_options is called.
1757 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758
Damien Miller4af51302000-04-16 11:18:38 +10001759void
Damien Miller95def091999-11-25 00:26:21 +11001760initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001761{
Damien Miller95def091999-11-25 00:26:21 +11001762 memset(options, 'X', sizeof(*options));
1763 options->forward_agent = -1;
1764 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001765 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001766 options->forward_x11_timeout = -1;
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001767 options->stdio_forward_host = NULL;
1768 options->stdio_forward_port = 0;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001769 options->clear_forwardings = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001770 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001771 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001772 options->fwd_opts.gateway_ports = -1;
1773 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1774 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001775 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001776 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001777 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001778 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001779 options->gss_authentication = -1;
1780 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001781 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001782 options->kbd_interactive_authentication = -1;
1783 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001784 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001785 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001786 options->batch_mode = -1;
1787 options->check_host_ip = -1;
1788 options->strict_host_key_checking = -1;
1789 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001790 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001791 options->compression_level = -1;
1792 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001793 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001794 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001795 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001796 options->number_of_password_prompts = -1;
1797 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001798 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001799 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001800 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001801 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001802 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001803 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001804 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001805 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001806 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001807 options->proxy_command = NULL;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001808 options->jump_user = NULL;
1809 options->jump_host = NULL;
1810 options->jump_port = -1;
1811 options->jump_extra = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001812 options->user = NULL;
1813 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001814 options->num_system_hostfiles = 0;
1815 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001816 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001817 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001818 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001819 options->num_remote_forwards = 0;
Damien Millerfcd93202002-02-05 12:26:34 +11001820 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001821 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001822 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001823 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001824 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001825 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001826 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001827 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001828 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001829 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001830 options->server_alive_interval = -1;
1831 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001832 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001833 options->control_path = NULL;
1834 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001835 options->control_persist = -1;
1836 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001837 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001838 options->tun_open = -1;
1839 options->tun_local = -1;
1840 options->tun_remote = -1;
1841 options->local_command = NULL;
1842 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001843 options->add_keys_to_agent = -1;
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001844 options->identity_agent = NULL;
Damien Miller10288242008-06-30 00:04:03 +10001845 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001846 options->ip_qos_interactive = -1;
1847 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001848 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001849 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001850 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001851 options->num_canonical_domains = 0;
1852 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001853 options->canonicalize_max_dots = -1;
1854 options->canonicalize_fallback_local = -1;
1855 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001856 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001857 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001858 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001859 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001860 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861}
1862
Damien Miller5428f641999-11-25 11:54:57 +11001863/*
Damien Miller13f97b22014-02-24 15:57:55 +11001864 * A petite version of fill_default_options() that just fills the options
1865 * needed for hostname canonicalization to proceed.
1866 */
1867void
1868fill_default_options_for_canonicalization(Options *options)
1869{
1870 if (options->canonicalize_max_dots == -1)
1871 options->canonicalize_max_dots = 1;
1872 if (options->canonicalize_fallback_local == -1)
1873 options->canonicalize_fallback_local = 1;
1874 if (options->canonicalize_hostname == -1)
1875 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1876}
1877
1878/*
Damien Miller5428f641999-11-25 11:54:57 +11001879 * Called after processing other sources of option data, this fills those
1880 * options for which no value has been specified with their default values.
1881 */
Damien Miller4af51302000-04-16 11:18:38 +10001882void
Damien Miller95def091999-11-25 00:26:21 +11001883fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001884{
Damien Miller95def091999-11-25 00:26:21 +11001885 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001886 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001887 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001888 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001889 if (options->forward_x11_trusted == -1)
1890 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001891 if (options->forward_x11_timeout == -1)
1892 options->forward_x11_timeout = 1200;
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001893 /*
1894 * stdio forwarding (-W) changes the default for these but we defer
1895 * setting the values so they can be overridden.
1896 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10001897 if (options->exit_on_forward_failure == -1)
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00001898 options->exit_on_forward_failure =
1899 options->stdio_forward_host != NULL ? 1 : 0;
1900 if (options->clear_forwardings == -1)
1901 options->clear_forwardings =
1902 options->stdio_forward_host != NULL ? 1 : 0;
1903 if (options->clear_forwardings == 1)
1904 clear_forwardings(options);
1905
Damien Millerd3a18572000-06-07 19:55:44 +10001906 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001907 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001908 if (options->fwd_opts.gateway_ports == -1)
1909 options->fwd_opts.gateway_ports = 0;
1910 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1911 options->fwd_opts.streamlocal_bind_mask = 0177;
1912 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1913 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001914 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001915 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001916 if (options->rsa_authentication == -1)
1917 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001918 if (options->pubkey_authentication == -1)
1919 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001920 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001921 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001922 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001923 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001924 if (options->gss_deleg_creds == -1)
1925 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001926 if (options->password_authentication == -1)
1927 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001928 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001929 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001930 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001931 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001932 if (options->hostbased_authentication == -1)
1933 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001934 if (options->batch_mode == -1)
1935 options->batch_mode = 0;
1936 if (options->check_host_ip == -1)
1937 options->check_host_ip = 1;
1938 if (options->strict_host_key_checking == -1)
1939 options->strict_host_key_checking = 2; /* 2 is default */
1940 if (options->compression == -1)
1941 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001942 if (options->tcp_keep_alive == -1)
1943 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001944 if (options->compression_level == -1)
1945 options->compression_level = 6;
1946 if (options->port == -1)
1947 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001948 if (options->address_family == -1)
1949 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001950 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001951 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001952 if (options->number_of_password_prompts == -1)
1953 options->number_of_password_prompts = 3;
1954 /* Selected in ssh_login(). */
1955 if (options->cipher == -1)
1956 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001957 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001958 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001959 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001960 if (options->add_keys_to_agent == -1)
1961 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001962 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001963 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001964 add_identity_file(options, "~/",
1965 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001966 }
1967 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001968 add_identity_file(options, "~/",
1969 _PATH_SSH_CLIENT_ID_RSA, 0);
1970 add_identity_file(options, "~/",
1971 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001972#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001973 add_identity_file(options, "~/",
1974 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001975#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001976 add_identity_file(options, "~/",
1977 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001978 }
Damien Millereba71ba2000-04-29 23:57:08 +10001979 }
Damien Miller95def091999-11-25 00:26:21 +11001980 if (options->escape_char == -1)
1981 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001982 if (options->num_system_hostfiles == 0) {
1983 options->system_hostfiles[options->num_system_hostfiles++] =
1984 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1985 options->system_hostfiles[options->num_system_hostfiles++] =
1986 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1987 }
1988 if (options->num_user_hostfiles == 0) {
1989 options->user_hostfiles[options->num_user_hostfiles++] =
1990 xstrdup(_PATH_SSH_USER_HOSTFILE);
1991 options->user_hostfiles[options->num_user_hostfiles++] =
1992 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1993 }
Damien Millerfcd93202002-02-05 12:26:34 +11001994 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001995 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001996 if (options->no_host_authentication_for_localhost == - 1)
1997 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001998 if (options->identities_only == -1)
1999 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00002000 if (options->enable_ssh_keysign == -1)
2001 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10002002 if (options->rekey_limit == -1)
2003 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002004 if (options->rekey_interval == -1)
2005 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10002006 if (options->verify_host_key_dns == -1)
2007 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11002008 if (options->server_alive_interval == -1)
2009 options->server_alive_interval = 0;
2010 if (options->server_alive_count_max == -1)
2011 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10002012 if (options->control_master == -1)
2013 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002014 if (options->control_persist == -1) {
2015 options->control_persist = 0;
2016 options->control_persist_timeout = 0;
2017 }
Damien Millere1776152005-03-01 21:47:37 +11002018 if (options->hash_known_hosts == -1)
2019 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11002020 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11002021 options->tun_open = SSH_TUNMODE_NO;
2022 if (options->tun_local == -1)
2023 options->tun_local = SSH_TUNID_ANY;
2024 if (options->tun_remote == -1)
2025 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11002026 if (options->permit_local_command == -1)
2027 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10002028 if (options->visual_host_key == -1)
2029 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11002030 if (options->ip_qos_interactive == -1)
2031 options->ip_qos_interactive = IPTOS_LOWDELAY;
2032 if (options->ip_qos_bulk == -1)
2033 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10002034 if (options->request_tty == -1)
2035 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10002036 if (options->proxy_use_fdpass == -1)
2037 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11002038 if (options->canonicalize_max_dots == -1)
2039 options->canonicalize_max_dots = 1;
2040 if (options->canonicalize_fallback_local == -1)
2041 options->canonicalize_fallback_local = 1;
2042 if (options->canonicalize_hostname == -1)
2043 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002044 if (options->fingerprint_hash == -1)
2045 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002046 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00002047 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002048 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
2049 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
2050 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
2051 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2052 &options->hostbased_key_types) != 0 ||
2053 kex_assemble_names(KEX_DEFAULT_PK_ALG,
2054 &options->pubkey_key_types) != 0)
2055 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002056
Damien Millere9fc72e2013-10-15 12:14:12 +11002057#define CLEAR_ON_NONE(v) \
2058 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11002059 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11002060 free(v); \
2061 v = NULL; \
2062 } \
2063 } while(0)
2064 CLEAR_ON_NONE(options->local_command);
2065 CLEAR_ON_NONE(options->proxy_command);
2066 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002067 CLEAR_ON_NONE(options->revoked_host_keys);
markus@openbsd.org1a75d142016-05-04 14:29:58 +00002068 /* options->identity_agent distinguishes NULL from 'none' */
Damien Miller95def091999-11-25 00:26:21 +11002069 /* options->user will be set in the main program if appropriate */
2070 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00002071 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002072 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002073}
Damien Millerf91ee4c2005-03-01 21:24:33 +11002074
Damien Miller7acefbb2014-07-18 14:11:24 +10002075struct fwdarg {
2076 char *arg;
2077 int ispath;
2078};
2079
2080/*
2081 * parse_fwd_field
2082 * parses the next field in a port forwarding specification.
2083 * sets fwd to the parsed field and advances p past the colon
2084 * or sets it to NULL at end of string.
2085 * returns 0 on success, else non-zero.
2086 */
2087static int
2088parse_fwd_field(char **p, struct fwdarg *fwd)
2089{
2090 char *ep, *cp = *p;
2091 int ispath = 0;
2092
2093 if (*cp == '\0') {
2094 *p = NULL;
2095 return -1; /* end of string */
2096 }
2097
2098 /*
2099 * A field escaped with square brackets is used literally.
2100 * XXX - allow ']' to be escaped via backslash?
2101 */
2102 if (*cp == '[') {
2103 /* find matching ']' */
2104 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2105 if (*ep == '/')
2106 ispath = 1;
2107 }
2108 /* no matching ']' or not at end of field. */
2109 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2110 return -1;
2111 /* NUL terminate the field and advance p past the colon */
2112 *ep++ = '\0';
2113 if (*ep != '\0')
2114 *ep++ = '\0';
2115 fwd->arg = cp + 1;
2116 fwd->ispath = ispath;
2117 *p = ep;
2118 return 0;
2119 }
2120
2121 for (cp = *p; *cp != '\0'; cp++) {
2122 switch (*cp) {
2123 case '\\':
2124 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00002125 if (*cp == '\0')
2126 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002127 break;
2128 case '/':
2129 ispath = 1;
2130 break;
2131 case ':':
2132 *cp++ = '\0';
2133 goto done;
2134 }
2135 }
2136done:
2137 fwd->arg = *p;
2138 fwd->ispath = ispath;
2139 *p = cp;
2140 return 0;
2141}
2142
Damien Millerf91ee4c2005-03-01 21:24:33 +11002143/*
2144 * parse_forward
2145 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002146 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002147 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2148 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002149 * dynamicfwd == 1
2150 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002151 * returns number of arguments parsed or zero on error
2152 */
2153int
Damien Miller7acefbb2014-07-18 14:11:24 +10002154parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002155{
Damien Miller7acefbb2014-07-18 14:11:24 +10002156 struct fwdarg fwdargs[4];
2157 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002158 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002159
Damien Miller7acefbb2014-07-18 14:11:24 +10002160 memset(fwd, 0, sizeof(*fwd));
2161 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002162
2163 cp = p = xstrdup(fwdspec);
2164
2165 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002166 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002167 cp++;
2168
Damien Miller7acefbb2014-07-18 14:11:24 +10002169 for (i = 0; i < 4; ++i) {
2170 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002171 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002172 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002173
Damien Millerf4b39532008-11-03 19:28:21 +11002174 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002175 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002176 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002177 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002178
2179 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002180 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002181 if (fwdargs[0].ispath) {
2182 fwd->listen_path = xstrdup(fwdargs[0].arg);
2183 fwd->listen_port = PORT_STREAMLOCAL;
2184 } else {
2185 fwd->listen_host = NULL;
2186 fwd->listen_port = a2port(fwdargs[0].arg);
2187 }
Damien Millera699d952008-11-03 19:27:34 +11002188 fwd->connect_host = xstrdup("socks");
2189 break;
2190
2191 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002192 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2193 fwd->listen_path = xstrdup(fwdargs[0].arg);
2194 fwd->listen_port = PORT_STREAMLOCAL;
2195 fwd->connect_path = xstrdup(fwdargs[1].arg);
2196 fwd->connect_port = PORT_STREAMLOCAL;
2197 } else if (fwdargs[1].ispath) {
2198 fwd->listen_host = NULL;
2199 fwd->listen_port = a2port(fwdargs[0].arg);
2200 fwd->connect_path = xstrdup(fwdargs[1].arg);
2201 fwd->connect_port = PORT_STREAMLOCAL;
2202 } else {
2203 fwd->listen_host = xstrdup(fwdargs[0].arg);
2204 fwd->listen_port = a2port(fwdargs[1].arg);
2205 fwd->connect_host = xstrdup("socks");
2206 }
Damien Millera699d952008-11-03 19:27:34 +11002207 break;
2208
Damien Millerf91ee4c2005-03-01 21:24:33 +11002209 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002210 if (fwdargs[0].ispath) {
2211 fwd->listen_path = xstrdup(fwdargs[0].arg);
2212 fwd->listen_port = PORT_STREAMLOCAL;
2213 fwd->connect_host = xstrdup(fwdargs[1].arg);
2214 fwd->connect_port = a2port(fwdargs[2].arg);
2215 } else if (fwdargs[2].ispath) {
2216 fwd->listen_host = xstrdup(fwdargs[0].arg);
2217 fwd->listen_port = a2port(fwdargs[1].arg);
2218 fwd->connect_path = xstrdup(fwdargs[2].arg);
2219 fwd->connect_port = PORT_STREAMLOCAL;
2220 } else {
2221 fwd->listen_host = NULL;
2222 fwd->listen_port = a2port(fwdargs[0].arg);
2223 fwd->connect_host = xstrdup(fwdargs[1].arg);
2224 fwd->connect_port = a2port(fwdargs[2].arg);
2225 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002226 break;
2227
2228 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002229 fwd->listen_host = xstrdup(fwdargs[0].arg);
2230 fwd->listen_port = a2port(fwdargs[1].arg);
2231 fwd->connect_host = xstrdup(fwdargs[2].arg);
2232 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002233 break;
2234 default:
2235 i = 0; /* failure */
2236 }
2237
Darren Tuckera627d422013-06-02 07:31:17 +10002238 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002239
Damien Millera699d952008-11-03 19:27:34 +11002240 if (dynamicfwd) {
2241 if (!(i == 1 || i == 2))
2242 goto fail_free;
2243 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002244 if (!(i == 3 || i == 4)) {
2245 if (fwd->connect_path == NULL &&
2246 fwd->listen_path == NULL)
2247 goto fail_free;
2248 }
2249 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002250 goto fail_free;
2251 }
2252
Damien Miller7acefbb2014-07-18 14:11:24 +10002253 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2254 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002255 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002256 if (fwd->connect_host != NULL &&
2257 strlen(fwd->connect_host) >= NI_MAXHOST)
2258 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002259 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2260 if (fwd->connect_path != NULL &&
2261 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2262 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002263 if (fwd->listen_host != NULL &&
2264 strlen(fwd->listen_host) >= NI_MAXHOST)
2265 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002266 if (fwd->listen_path != NULL &&
2267 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2268 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002269
2270 return (i);
2271
2272 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002273 free(fwd->connect_host);
2274 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002275 free(fwd->connect_path);
2276 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002277 free(fwd->listen_host);
2278 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002279 free(fwd->listen_path);
2280 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002281 return (0);
2282}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002283
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002284int
2285parse_jump(const char *s, Options *o, int active)
2286{
2287 char *orig, *sdup, *cp;
2288 char *host = NULL, *user = NULL;
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002289 int ret = -1, port = -1, first;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002290
2291 active &= o->proxy_command == NULL && o->jump_host == NULL;
2292
2293 orig = sdup = xstrdup(s);
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002294 first = active;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002295 do {
2296 if ((cp = strrchr(sdup, ',')) == NULL)
2297 cp = sdup; /* last */
2298 else
2299 *cp++ = '\0';
2300
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002301 if (first) {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002302 /* First argument and configuration is active */
2303 if (parse_user_host_port(cp, &user, &host, &port) != 0)
2304 goto out;
2305 } else {
2306 /* Subsequent argument or inactive configuration */
2307 if (parse_user_host_port(cp, NULL, NULL, NULL) != 0)
2308 goto out;
2309 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002310 first = 0; /* only check syntax for subsequent hosts */
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002311 } while (cp != sdup);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002312 /* success */
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002313 if (active) {
2314 o->jump_user = user;
2315 o->jump_host = host;
2316 o->jump_port = port;
2317 o->proxy_command = xstrdup("none");
2318 user = host = NULL;
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002319 if ((cp = strrchr(s, ',')) != NULL && cp != s) {
2320 o->jump_extra = xstrdup(s);
2321 o->jump_extra[cp - s] = '\0';
2322 }
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002323 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002324 ret = 0;
2325 out:
naddy@openbsd.org324583e2016-07-20 10:45:27 +00002326 free(orig);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002327 free(user);
2328 free(host);
2329 return ret;
2330}
2331
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002332/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2333static const char *
2334fmt_multistate_int(int val, const struct multistate *m)
2335{
2336 u_int i;
2337
2338 for (i = 0; m[i].key != NULL; i++) {
2339 if (m[i].value == val)
2340 return m[i].key;
2341 }
2342 return "UNKNOWN";
2343}
2344
2345static const char *
2346fmt_intarg(OpCodes code, int val)
2347{
2348 if (val == -1)
2349 return "unset";
2350 switch (code) {
2351 case oAddressFamily:
2352 return fmt_multistate_int(val, multistate_addressfamily);
2353 case oVerifyHostKeyDNS:
2354 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002355 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002356 return fmt_multistate_int(val, multistate_yesnoask);
2357 case oControlMaster:
2358 return fmt_multistate_int(val, multistate_controlmaster);
2359 case oTunnel:
2360 return fmt_multistate_int(val, multistate_tunnel);
2361 case oRequestTTY:
2362 return fmt_multistate_int(val, multistate_requesttty);
2363 case oCanonicalizeHostname:
2364 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002365 case oFingerprintHash:
2366 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002367 case oProtocol:
2368 switch (val) {
2369 case SSH_PROTO_1:
2370 return "1";
2371 case SSH_PROTO_2:
2372 return "2";
2373 case (SSH_PROTO_1|SSH_PROTO_2):
2374 return "2,1";
2375 default:
2376 return "UNKNOWN";
2377 }
2378 default:
2379 switch (val) {
2380 case 0:
2381 return "no";
2382 case 1:
2383 return "yes";
2384 default:
2385 return "UNKNOWN";
2386 }
2387 }
2388}
2389
2390static const char *
2391lookup_opcode_name(OpCodes code)
2392{
2393 u_int i;
2394
2395 for (i = 0; keywords[i].name != NULL; i++)
2396 if (keywords[i].opcode == code)
2397 return(keywords[i].name);
2398 return "UNKNOWN";
2399}
2400
2401static void
2402dump_cfg_int(OpCodes code, int val)
2403{
2404 printf("%s %d\n", lookup_opcode_name(code), val);
2405}
2406
2407static void
2408dump_cfg_fmtint(OpCodes code, int val)
2409{
2410 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2411}
2412
2413static void
2414dump_cfg_string(OpCodes code, const char *val)
2415{
2416 if (val == NULL)
2417 return;
2418 printf("%s %s\n", lookup_opcode_name(code), val);
2419}
2420
2421static void
2422dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2423{
2424 u_int i;
2425
2426 for (i = 0; i < count; i++)
2427 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2428}
2429
2430static void
2431dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2432{
2433 u_int i;
2434
2435 printf("%s", lookup_opcode_name(code));
2436 for (i = 0; i < count; i++)
2437 printf(" %s", vals[i]);
2438 printf("\n");
2439}
2440
2441static void
2442dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2443{
2444 const struct Forward *fwd;
2445 u_int i;
2446
2447 /* oDynamicForward */
2448 for (i = 0; i < count; i++) {
2449 fwd = &fwds[i];
2450 if (code == oDynamicForward &&
2451 strcmp(fwd->connect_host, "socks") != 0)
2452 continue;
2453 if (code == oLocalForward &&
2454 strcmp(fwd->connect_host, "socks") == 0)
2455 continue;
2456 printf("%s", lookup_opcode_name(code));
2457 if (fwd->listen_port == PORT_STREAMLOCAL)
2458 printf(" %s", fwd->listen_path);
2459 else if (fwd->listen_host == NULL)
2460 printf(" %d", fwd->listen_port);
2461 else {
2462 printf(" [%s]:%d",
2463 fwd->listen_host, fwd->listen_port);
2464 }
2465 if (code != oDynamicForward) {
2466 if (fwd->connect_port == PORT_STREAMLOCAL)
2467 printf(" %s", fwd->connect_path);
2468 else if (fwd->connect_host == NULL)
2469 printf(" %d", fwd->connect_port);
2470 else {
2471 printf(" [%s]:%d",
2472 fwd->connect_host, fwd->connect_port);
2473 }
2474 }
2475 printf("\n");
2476 }
2477}
2478
2479void
2480dump_client_config(Options *o, const char *host)
2481{
2482 int i;
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002483 char buf[8];
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002484
djm@openbsd.org60a92472015-08-21 23:53:08 +00002485 /* This is normally prepared in ssh_kex2 */
2486 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2487 fatal("%s: kex_assemble_names failed", __func__);
2488
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002489 /* Most interesting options first: user, host, port */
2490 dump_cfg_string(oUser, o->user);
2491 dump_cfg_string(oHostName, host);
2492 dump_cfg_int(oPort, o->port);
2493
2494 /* Flag options */
2495 dump_cfg_fmtint(oAddressFamily, o->address_family);
2496 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2497 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2498 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2499 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2500 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2501 dump_cfg_fmtint(oCompression, o->compression);
2502 dump_cfg_fmtint(oControlMaster, o->control_master);
2503 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
dtucker@openbsd.org0cb2f4c2016-06-03 04:09:38 +00002504 dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002505 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002506 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002507 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2508 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2509 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2510 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2511#ifdef GSSAPI
2512 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2513 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2514#endif /* GSSAPI */
2515 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2516 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2517 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2518 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2519 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2520 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2521 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2522 dump_cfg_fmtint(oProtocol, o->protocol);
2523 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2524 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2525 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2526 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2527 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2528 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2529 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2530 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2531 dump_cfg_fmtint(oTunnel, o->tun_open);
2532 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2533 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2534 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002535 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002536
2537 /* Integer options */
2538 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2539 dump_cfg_int(oCompressionLevel, o->compression_level);
2540 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2541 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2542 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2543 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2544 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2545
2546 /* String options */
2547 dump_cfg_string(oBindAddress, o->bind_address);
2548 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2549 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002550 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002551 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002552 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00002553 dump_cfg_string(oIdentityAgent, o->identity_agent);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002554 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2555 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2556 dump_cfg_string(oLocalCommand, o->local_command);
2557 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2558 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2559 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2560 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002561 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002562 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002563 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002564
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002565 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002566 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2567 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2568 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2569
2570 /* String array options */
2571 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2572 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2573 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2574 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2575 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2576
2577 /* Special cases */
2578
2579 /* oConnectTimeout */
2580 if (o->connection_timeout == -1)
2581 printf("connecttimeout none\n");
2582 else
2583 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2584
2585 /* oTunnelDevice */
2586 printf("tunneldevice");
2587 if (o->tun_local == SSH_TUNID_ANY)
2588 printf(" any");
2589 else
2590 printf(" %d", o->tun_local);
2591 if (o->tun_remote == SSH_TUNID_ANY)
2592 printf(":any");
2593 else
2594 printf(":%d", o->tun_remote);
2595 printf("\n");
2596
2597 /* oCanonicalizePermittedCNAMEs */
2598 if ( o->num_permitted_cnames > 0) {
2599 printf("canonicalizePermittedcnames");
2600 for (i = 0; i < o->num_permitted_cnames; i++) {
2601 printf(" %s:%s", o->permitted_cnames[i].source_list,
2602 o->permitted_cnames[i].target_list);
2603 }
2604 printf("\n");
2605 }
2606
2607 /* oCipher */
2608 if (o->cipher != SSH_CIPHER_NOT_SET)
2609 printf("Cipher %s\n", cipher_name(o->cipher));
2610
2611 /* oControlPersist */
2612 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2613 dump_cfg_fmtint(oControlPersist, o->control_persist);
2614 else
2615 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2616
2617 /* oEscapeChar */
2618 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2619 printf("escapechar none\n");
2620 else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002621 vis(buf, o->escape_char, VIS_WHITE, 0);
2622 printf("escapechar %s\n", buf);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002623 }
2624
2625 /* oIPQoS */
2626 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2627 printf("%s\n", iptos2str(o->ip_qos_bulk));
2628
2629 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002630 printf("rekeylimit %llu %d\n",
2631 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002632
2633 /* oStreamLocalBindMask */
2634 printf("streamlocalbindmask 0%o\n",
2635 o->fwd_opts.streamlocal_bind_mask);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002636
2637 /* oProxyCommand / oProxyJump */
2638 if (o->jump_host == NULL)
2639 dump_cfg_string(oProxyCommand, o->proxy_command);
2640 else {
2641 /* Check for numeric addresses */
2642 i = strchr(o->jump_host, ':') != NULL ||
2643 strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
2644 snprintf(buf, sizeof(buf), "%d", o->jump_port);
2645 printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002646 /* optional additional jump spec */
2647 o->jump_extra == NULL ? "" : o->jump_extra,
2648 o->jump_extra == NULL ? "" : ",",
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002649 /* optional user */
2650 o->jump_user == NULL ? "" : o->jump_user,
2651 o->jump_user == NULL ? "" : "@",
2652 /* opening [ if hostname is numeric */
2653 i ? "[" : "",
2654 /* mandatory hostname */
2655 o->jump_host,
2656 /* closing ] if hostname is numeric */
2657 i ? "]" : "",
2658 /* optional port number */
2659 o->jump_port <= 0 ? "" : ":",
djm@openbsd.org286f5a72016-07-22 03:35:11 +00002660 o->jump_port <= 0 ? "" : buf);
djm@openbsd.orged877ef2016-07-15 00:24:30 +00002661 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002662}