blob: a4ecf7a0b2cf155281ef9b087d01e8ba183265e0 [file] [log] [blame]
Damien Miller15271902014-05-15 13:47:56 +10001/* $OpenBSD: readconf.c,v 1.219 2014/04/23 12:42:34 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 Miller8ec8c3e2006-07-10 20:35:38 +100021
22#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110023#include <netinet/in_systm.h>
24#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110025#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Millerc7b06362006-03-15 11:53:45 +110027#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100028#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110029#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110031#ifdef HAVE_PATHS_H
32# include <paths.h>
33#endif
Damien Miller194fd902013-10-15 12:13:05 +110034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100039#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
Damien Millerc7b06362006-03-15 11:53:45 +110043
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100046#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "cipher.h"
48#include "pathnames.h"
49#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "readconf.h"
52#include "match.h"
53#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000055#include "kex.h"
56#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110057#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59/* Format of the configuration file:
60
61 # Configuration data is parsed as follows:
62 # 1. command line options
63 # 2. user-specific file
64 # 3. system-wide file
65 # Any configuration value is only changed the first time it is set.
66 # Thus, host-specific definitions should be at the beginning of the
67 # configuration file, and defaults at the end.
68
69 # Host-specific declarations. These may override anything above. A single
70 # host may match multiple declarations; these are processed in the order
71 # that they are given in.
72
73 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000074 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
76 Host fake.com
77 HostName another.host.name.real.org
78 User blaah
79 Port 34289
80 ForwardX11 no
81 ForwardAgent no
82
83 Host books.com
84 RemoteForward 9999 shadows.cs.hut.fi:9999
85 Cipher 3des
86
87 Host fascist.blob.com
88 Port 23123
89 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 PasswordAuthentication no
91
92 Host puukko.hut.fi
93 User t35124p
94 ProxyCommand ssh-proxy %h %p
95
96 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000097 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host *.su
100 Cipher none
101 PasswordAuthentication no
102
Damien Millerd27b9472005-12-13 19:29:02 +1100103 Host vpn.fake.com
104 Tunnel yes
105 TunnelDevice 3
106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107 # Defaults for various options
108 Host *
109 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111 PasswordAuthentication yes
112 RSAAuthentication yes
113 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100115 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 IdentityFile ~/.ssh/identity
117 Port 22
118 EscapeChar ~
119
120*/
121
122/* Keyword tokens. */
123
Damien Miller95def091999-11-25 00:26:21 +1100124typedef enum {
125 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100126 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000127 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
128 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000129 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000130 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100131 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100133 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
134 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100135 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000136 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000138 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000139 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100140 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000141 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000142 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000143 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100144 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000145 oSendEnv, oControlPath, oControlMaster, oControlPersist,
146 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100147 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100148 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000149 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100150 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
151 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
Darren Tucker07636982013-05-16 20:30:03 +1000152 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153} OpCodes;
154
155/* Textual representations of the tokens. */
156
Damien Miller95def091999-11-25 00:26:21 +1100157static struct {
158 const char *name;
159 OpCodes opcode;
160} keywords[] = {
161 { "forwardagent", oForwardAgent },
162 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000163 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000164 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000165 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000166 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100167 { "gatewayports", oGatewayPorts },
168 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000169 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100171 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
172 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000175 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000176 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000177 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000178 { "challengeresponseauthentication", oChallengeResponseAuthentication },
179 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
180 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000181 { "kerberosauthentication", oUnsupported },
182 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000183 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000184#if defined(GSSAPI)
185 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000186 { "gssapidelegatecredentials", oGssDelegateCreds },
187#else
188 { "gssapiauthentication", oUnsupported },
189 { "gssapidelegatecredentials", oUnsupported },
190#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000191 { "fallbacktorsh", oDeprecated },
192 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100194 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100195 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100196 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000197 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100198 { "proxycommand", oProxyCommand },
199 { "port", oPort },
200 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000201 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000202 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000203 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "remoteforward", oRemoteForward },
205 { "localforward", oLocalForward },
206 { "user", oUser },
207 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100208 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100210 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000211 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100212 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000213 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100214 { "connectionattempts", oConnectionAttempts },
215 { "batchmode", oBatchMode },
216 { "checkhostip", oCheckHostIP },
217 { "stricthostkeychecking", oStrictHostKeyChecking },
218 { "compression", oCompression },
219 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100220 { "tcpkeepalive", oTCPKeepAlive },
221 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100222 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100223 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000224 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000225 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000226 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000227 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100228#ifdef ENABLE_PKCS11
229 { "smartcarddevice", oPKCS11Provider },
230 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000231#else
232 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100233 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000234#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100235 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000236 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000237 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000239 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000240 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000241 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100242 { "serveraliveinterval", oServerAliveInterval },
243 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000244 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000245 { "controlpath", oControlPath },
246 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000247 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100248 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100249 { "tunnel", oTunnel },
250 { "tunneldevice", oTunnelDevice },
251 { "localcommand", oLocalCommand },
252 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000253 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000254 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000255 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100256 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000257 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000258 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100259 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100260 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
261 { "canonicalizehostname", oCanonicalizeHostname },
262 { "canonicalizemaxdots", oCanonicalizeMaxDots },
263 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Darren Tucker07636982013-05-16 20:30:03 +1000264 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100265
Ben Lindstrom65366a82001-12-06 16:32:47 +0000266 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100267};
268
Damien Miller5428f641999-11-25 11:54:57 +1100269/*
270 * Adds a local TCP/IP port forward to options. Never returns if there is an
271 * error.
272 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller4af51302000-04-16 11:18:38 +1000274void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Damien Miller95def091999-11-25 00:26:21 +1100277 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000278#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100279 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000281 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100282#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000283 options->local_forwards = xrealloc(options->local_forwards,
284 options->num_local_forwards + 1,
285 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100286 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287
Damien Miller1a0442f2008-11-05 16:30:06 +1100288 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100289 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100290 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Damien Miller5428f641999-11-25 11:54:57 +1100294/*
295 * Adds a remote TCP/IP port forward to options. Never returns if there is
296 * an error.
297 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller4af51302000-04-16 11:18:38 +1000299void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Damien Miller95def091999-11-25 00:26:21 +1100302 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000303
304 options->remote_forwards = xrealloc(options->remote_forwards,
305 options->num_remote_forwards + 1,
306 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100307 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308
Damien Miller1a0442f2008-11-05 16:30:06 +1100309 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100310 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100311 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100312 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100313 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000314 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315}
316
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000317static void
318clear_forwardings(Options *options)
319{
320 int i;
321
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(options->local_forwards[i].listen_host);
324 free(options->local_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325 }
Damien Miller232cfb12010-06-26 09:50:30 +1000326 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000327 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000328 options->local_forwards = NULL;
329 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000330 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100331 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(options->remote_forwards[i].listen_host);
333 free(options->remote_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 }
Damien Miller232cfb12010-06-26 09:50:30 +1000335 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000336 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000337 options->remote_forwards = NULL;
338 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000339 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100340 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000341}
342
Darren Tucker19104782013-04-05 11:13:08 +1100343void
344add_identity_file(Options *options, const char *dir, const char *filename,
345 int userprovided)
346{
347 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000348 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100349
350 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
351 fatal("Too many identity files specified (max %d)",
352 SSH_MAX_IDENTITY_FILES);
353
354 if (dir == NULL) /* no dir, filename is absolute */
355 path = xstrdup(filename);
356 else
357 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
358
Damien Miller15271902014-05-15 13:47:56 +1000359 /* Avoid registering duplicates */
360 for (i = 0; i < options->num_identity_files; i++) {
361 if (options->identity_file_userprovided[i] == userprovided &&
362 strcmp(options->identity_files[i], path) == 0) {
363 debug2("%s: ignoring duplicate key %s", __func__, path);
364 free(path);
365 return;
366 }
367 }
368
Darren Tucker19104782013-04-05 11:13:08 +1100369 options->identity_file_userprovided[options->num_identity_files] =
370 userprovided;
371 options->identity_files[options->num_identity_files++] = path;
372}
373
Damien Miller194fd902013-10-15 12:13:05 +1100374int
375default_ssh_port(void)
376{
377 static int port;
378 struct servent *sp;
379
380 if (port == 0) {
381 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
382 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
383 }
384 return port;
385}
386
387/*
388 * Execute a command in a shell.
389 * Return its exit status or -1 on abnormal exit.
390 */
391static int
392execute_in_shell(const char *cmd)
393{
394 char *shell, *command_string;
395 pid_t pid;
396 int devnull, status;
397 extern uid_t original_real_uid;
398
399 if ((shell = getenv("SHELL")) == NULL)
400 shell = _PATH_BSHELL;
401
402 /*
403 * Use "exec" to avoid "sh -c" processes on some platforms
404 * (e.g. Solaris)
405 */
406 xasprintf(&command_string, "exec %s", cmd);
407
408 /* Need this to redirect subprocess stdin/out */
409 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
410 fatal("open(/dev/null): %s", strerror(errno));
411
412 debug("Executing command: '%.500s'", cmd);
413
414 /* Fork and execute the command. */
415 if ((pid = fork()) == 0) {
416 char *argv[4];
417
418 /* Child. Permanently give up superuser privileges. */
419 permanently_drop_suid(original_real_uid);
420
421 /* Redirect child stdin and stdout. Leave stderr */
422 if (dup2(devnull, STDIN_FILENO) == -1)
423 fatal("dup2: %s", strerror(errno));
424 if (dup2(devnull, STDOUT_FILENO) == -1)
425 fatal("dup2: %s", strerror(errno));
426 if (devnull > STDERR_FILENO)
427 close(devnull);
428 closefrom(STDERR_FILENO + 1);
429
430 argv[0] = shell;
431 argv[1] = "-c";
432 argv[2] = command_string;
433 argv[3] = NULL;
434
435 execv(argv[0], argv);
436 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
437 /* Die with signal to make this error apparent to parent. */
438 signal(SIGTERM, SIG_DFL);
439 kill(getpid(), SIGTERM);
440 _exit(1);
441 }
442 /* Parent. */
443 if (pid < 0)
444 fatal("%s: fork: %.100s", __func__, strerror(errno));
445
446 close(devnull);
447 free(command_string);
448
449 while (waitpid(pid, &status, 0) == -1) {
450 if (errno != EINTR && errno != EAGAIN)
451 fatal("%s: waitpid: %s", __func__, strerror(errno));
452 }
453 if (!WIFEXITED(status)) {
454 error("command '%.100s' exited abnormally", cmd);
455 return -1;
456 }
457 debug3("command returned status %d", WEXITSTATUS(status));
458 return WEXITSTATUS(status);
459}
460
461/*
462 * Parse and execute a Match directive.
463 */
464static int
465match_cfg_line(Options *options, char **condition, struct passwd *pw,
466 const char *host_arg, const char *filename, int linenum)
467{
Damien Miller084bcd22013-10-23 16:30:51 +1100468 char *arg, *attrib, *cmd, *cp = *condition, *host;
469 const char *ruser;
Damien Millercf31f382013-10-24 21:02:56 +1100470 int r, port, result = 1, attributes = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100471 size_t len;
472 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
473
474 /*
475 * Configuration is likely to be incomplete at this point so we
476 * must be prepared to use default values.
477 */
478 port = options->port <= 0 ? default_ssh_port() : options->port;
479 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100480 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100481 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100482 host = percent_expand(options->hostname,
483 "h", host_arg, (char *)NULL);
484 } else
485 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100486
487 debug3("checking match for '%s' host %s", cp, host);
488 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100489 attributes++;
490 if (strcasecmp(attrib, "all") == 0) {
491 if (attributes != 1 ||
492 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
493 error("'all' cannot be combined with other "
494 "Match attributes");
495 result = -1;
496 goto out;
497 }
498 *condition = cp;
499 result = 1;
500 goto out;
501 }
Damien Miller194fd902013-10-15 12:13:05 +1100502 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
503 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100504 result = -1;
505 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100506 }
507 len = strlen(arg);
508 if (strcasecmp(attrib, "host") == 0) {
509 if (match_hostname(host, arg, len) != 1)
510 result = 0;
511 else
512 debug("%.200s line %d: matched 'Host %.100s' ",
513 filename, linenum, host);
514 } else if (strcasecmp(attrib, "originalhost") == 0) {
515 if (match_hostname(host_arg, arg, len) != 1)
516 result = 0;
517 else
518 debug("%.200s line %d: matched "
519 "'OriginalHost %.100s' ",
520 filename, linenum, host_arg);
521 } else if (strcasecmp(attrib, "user") == 0) {
522 if (match_pattern_list(ruser, arg, len, 0) != 1)
523 result = 0;
524 else
525 debug("%.200s line %d: matched 'User %.100s' ",
526 filename, linenum, ruser);
527 } else if (strcasecmp(attrib, "localuser") == 0) {
528 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
529 result = 0;
530 else
531 debug("%.200s line %d: matched "
532 "'LocalUser %.100s' ",
533 filename, linenum, pw->pw_name);
Damien Miller8a04be72013-10-23 16:29:40 +1100534 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100535 if (gethostname(thishost, sizeof(thishost)) == -1)
536 fatal("gethostname: %s", strerror(errno));
537 strlcpy(shorthost, thishost, sizeof(shorthost));
538 shorthost[strcspn(thishost, ".")] = '\0';
539 snprintf(portstr, sizeof(portstr), "%d", port);
540
541 cmd = percent_expand(arg,
542 "L", shorthost,
543 "d", pw->pw_dir,
544 "h", host,
545 "l", thishost,
546 "n", host_arg,
547 "p", portstr,
548 "r", ruser,
549 "u", pw->pw_name,
550 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100551 if (result != 1) {
552 /* skip execution if prior predicate failed */
553 debug("%.200s line %d: skipped exec \"%.100s\"",
Damien Miller194fd902013-10-15 12:13:05 +1100554 filename, linenum, cmd);
Damien Miller06287802014-02-24 15:56:45 +1100555 } else {
556 r = execute_in_shell(cmd);
557 if (r == -1) {
558 fatal("%.200s line %d: match exec "
559 "'%.100s' error", filename,
560 linenum, cmd);
561 } else if (r == 0) {
562 debug("%.200s line %d: matched "
563 "'exec \"%.100s\"'", filename,
564 linenum, cmd);
565 } else {
566 debug("%.200s line %d: no match "
567 "'exec \"%.100s\"'", filename,
568 linenum, cmd);
569 result = 0;
570 }
571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572 free(cmd);
573 } else {
574 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100575 result = -1;
576 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100577 }
578 }
Damien Millercf31f382013-10-24 21:02:56 +1100579 if (attributes == 0) {
580 error("One or more attributes required for Match");
581 result = -1;
582 goto out;
583 }
Damien Miller194fd902013-10-15 12:13:05 +1100584 debug3("match %sfound", result ? "" : "not ");
585 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100586 out:
587 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100588 return result;
589}
590
Damien Miller0faf7472013-10-17 11:47:23 +1100591/* Check and prepare a domain name: removes trailing '.' and lowercases */
592static void
593valid_domain(char *name, const char *filename, int linenum)
594{
595 size_t i, l = strlen(name);
596 u_char c, last = '\0';
597
598 if (l == 0)
599 fatal("%s line %d: empty hostname suffix", filename, linenum);
600 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
601 fatal("%s line %d: hostname suffix \"%.100s\" "
602 "starts with invalid character", filename, linenum, name);
603 for (i = 0; i < l; i++) {
604 c = tolower((u_char)name[i]);
605 name[i] = (char)c;
606 if (last == '.' && c == '.')
607 fatal("%s line %d: hostname suffix \"%.100s\" contains "
608 "consecutive separators", filename, linenum, name);
609 if (c != '.' && c != '-' && !isalnum(c) &&
610 c != '_') /* technically invalid, but common */
611 fatal("%s line %d: hostname suffix \"%.100s\" contains "
612 "invalid characters", filename, linenum, name);
613 last = c;
614 }
615 if (name[l - 1] == '.')
616 name[l - 1] = '\0';
617}
618
Damien Miller5428f641999-11-25 11:54:57 +1100619/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000620 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100621 */
Damien Miller4af51302000-04-16 11:18:38 +1000622static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000623parse_token(const char *cp, const char *filename, int linenum,
624 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625{
Darren Tucker07636982013-05-16 20:30:03 +1000626 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Damien Miller95def091999-11-25 00:26:21 +1100628 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000629 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100630 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000631 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
632 strlen(ignored_unknown), 1) == 1)
633 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000634 error("%s: line %d: Bad configuration option: %s",
635 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100636 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637}
638
Damien Millere9fc72e2013-10-15 12:14:12 +1100639/* Multistate option parsing */
640struct multistate {
641 char *key;
642 int value;
643};
644static const struct multistate multistate_flag[] = {
645 { "true", 1 },
646 { "false", 0 },
647 { "yes", 1 },
648 { "no", 0 },
649 { NULL, -1 }
650};
651static const struct multistate multistate_yesnoask[] = {
652 { "true", 1 },
653 { "false", 0 },
654 { "yes", 1 },
655 { "no", 0 },
656 { "ask", 2 },
657 { NULL, -1 }
658};
659static const struct multistate multistate_addressfamily[] = {
660 { "inet", AF_INET },
661 { "inet6", AF_INET6 },
662 { "any", AF_UNSPEC },
663 { NULL, -1 }
664};
665static const struct multistate multistate_controlmaster[] = {
666 { "true", SSHCTL_MASTER_YES },
667 { "yes", SSHCTL_MASTER_YES },
668 { "false", SSHCTL_MASTER_NO },
669 { "no", SSHCTL_MASTER_NO },
670 { "auto", SSHCTL_MASTER_AUTO },
671 { "ask", SSHCTL_MASTER_ASK },
672 { "autoask", SSHCTL_MASTER_AUTO_ASK },
673 { NULL, -1 }
674};
675static const struct multistate multistate_tunnel[] = {
676 { "ethernet", SSH_TUNMODE_ETHERNET },
677 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
678 { "true", SSH_TUNMODE_DEFAULT },
679 { "yes", SSH_TUNMODE_DEFAULT },
680 { "false", SSH_TUNMODE_NO },
681 { "no", SSH_TUNMODE_NO },
682 { NULL, -1 }
683};
684static const struct multistate multistate_requesttty[] = {
685 { "true", REQUEST_TTY_YES },
686 { "yes", REQUEST_TTY_YES },
687 { "false", REQUEST_TTY_NO },
688 { "no", REQUEST_TTY_NO },
689 { "force", REQUEST_TTY_FORCE },
690 { "auto", REQUEST_TTY_AUTO },
691 { NULL, -1 }
692};
Damien Miller38505592013-10-17 11:48:13 +1100693static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100694 { "true", SSH_CANONICALISE_YES },
695 { "false", SSH_CANONICALISE_NO },
696 { "yes", SSH_CANONICALISE_YES },
697 { "no", SSH_CANONICALISE_NO },
698 { "always", SSH_CANONICALISE_ALWAYS },
699 { NULL, -1 }
700};
Damien Millere9fc72e2013-10-15 12:14:12 +1100701
Damien Miller5428f641999-11-25 11:54:57 +1100702/*
703 * Processes a single option line as used in the configuration files. This
704 * only sets those values that have not already been set.
705 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100706#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100707int
Damien Miller194fd902013-10-15 12:13:05 +1100708process_config_line(Options *options, struct passwd *pw, const char *host,
709 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710{
Damien Miller295ee632011-05-29 21:42:31 +1000711 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
712 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000713 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100714 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100715 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000716 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100717 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100718 Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100719 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100720 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller194fd902013-10-15 12:13:05 +1100722 if (activep == NULL) { /* We are processing a command line directive */
723 cmdline = 1;
724 activep = &cmdline;
725 }
726
Damien Millerc652cac2003-05-14 13:40:54 +1000727 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100728 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000729 if (strchr(WHITESPACE, line[len]) == NULL)
730 break;
731 line[len] = '\0';
732 }
733
Damien Millerbe484b52000-07-15 14:14:16 +1000734 s = line;
735 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100736 if ((keyword = strdelim(&s)) == NULL)
737 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000738 /* Ignore leading whitespace. */
739 if (*keyword == '\0')
740 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000741 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100742 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000743 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100744 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745
Darren Tucker07636982013-05-16 20:30:03 +1000746 opcode = parse_token(keyword, filename, linenum,
747 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748
Damien Miller95def091999-11-25 00:26:21 +1100749 switch (opcode) {
750 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100751 /* don't panic, but count bad options */
752 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100753 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000754 case oIgnoredUnknownOption:
755 debug("%s line %d: Ignored unknown option \"%s\"",
756 filename, linenum, keyword);
757 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000758 case oConnectTimeout:
759 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100760parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000761 arg = strdelim(&s);
762 if (!arg || *arg == '\0')
763 fatal("%s line %d: missing time value.",
764 filename, linenum);
765 if ((value = convtime(arg)) == -1)
766 fatal("%s line %d: invalid time value.",
767 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100768 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000769 *intptr = value;
770 break;
771
Damien Miller95def091999-11-25 00:26:21 +1100772 case oForwardAgent:
773 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100774 parse_flag:
775 multistate_ptr = multistate_flag;
776 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000777 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000778 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100779 fatal("%s line %d: missing argument.",
780 filename, linenum);
781 value = -1;
782 for (i = 0; multistate_ptr[i].key != NULL; i++) {
783 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
784 value = multistate_ptr[i].value;
785 break;
786 }
787 }
788 if (value == -1)
789 fatal("%s line %d: unsupported option \"%s\".",
790 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100791 if (*activep && *intptr == -1)
792 *intptr = value;
793 break;
794
795 case oForwardX11:
796 intptr = &options->forward_x11;
797 goto parse_flag;
798
Darren Tucker0a118da2003-10-15 15:54:32 +1000799 case oForwardX11Trusted:
800 intptr = &options->forward_x11_trusted;
801 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000802
803 case oForwardX11Timeout:
804 intptr = &options->forward_x11_timeout;
805 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 case oGatewayPorts:
808 intptr = &options->gateway_ports;
809 goto parse_flag;
810
Darren Tuckere7d4b192006-07-12 22:17:10 +1000811 case oExitOnForwardFailure:
812 intptr = &options->exit_on_forward_failure;
813 goto parse_flag;
814
Damien Miller95def091999-11-25 00:26:21 +1100815 case oUsePrivilegedPort:
816 intptr = &options->use_privileged_port;
817 goto parse_flag;
818
Damien Miller95def091999-11-25 00:26:21 +1100819 case oPasswordAuthentication:
820 intptr = &options->password_authentication;
821 goto parse_flag;
822
Damien Miller874d77b2000-10-14 16:23:11 +1100823 case oKbdInteractiveAuthentication:
824 intptr = &options->kbd_interactive_authentication;
825 goto parse_flag;
826
827 case oKbdInteractiveDevices:
828 charptr = &options->kbd_interactive_devices;
829 goto parse_string;
830
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831 case oPubkeyAuthentication:
832 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000833 goto parse_flag;
834
Damien Miller95def091999-11-25 00:26:21 +1100835 case oRSAAuthentication:
836 intptr = &options->rsa_authentication;
837 goto parse_flag;
838
839 case oRhostsRSAAuthentication:
840 intptr = &options->rhosts_rsa_authentication;
841 goto parse_flag;
842
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000843 case oHostbasedAuthentication:
844 intptr = &options->hostbased_authentication;
845 goto parse_flag;
846
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000847 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000848 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100849 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000850
Darren Tucker0efd1552003-08-26 11:49:55 +1000851 case oGssAuthentication:
852 intptr = &options->gss_authentication;
853 goto parse_flag;
854
855 case oGssDelegateCreds:
856 intptr = &options->gss_deleg_creds;
857 goto parse_flag;
858
Damien Miller95def091999-11-25 00:26:21 +1100859 case oBatchMode:
860 intptr = &options->batch_mode;
861 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862
Damien Miller95def091999-11-25 00:26:21 +1100863 case oCheckHostIP:
864 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000865 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Damien Miller37876e92003-05-15 10:19:46 +1000867 case oVerifyHostKeyDNS:
868 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100869 multistate_ptr = multistate_yesnoask;
870 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000871
Damien Miller95def091999-11-25 00:26:21 +1100872 case oStrictHostKeyChecking:
873 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100874 multistate_ptr = multistate_yesnoask;
875 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876
Damien Miller95def091999-11-25 00:26:21 +1100877 case oCompression:
878 intptr = &options->compression;
879 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Miller12c150e2003-12-17 16:31:10 +1100881 case oTCPKeepAlive:
882 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100883 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000885 case oNoHostAuthenticationForLocalhost:
886 intptr = &options->no_host_authentication_for_localhost;
887 goto parse_flag;
888
Damien Miller95def091999-11-25 00:26:21 +1100889 case oNumberOfPasswordPrompts:
890 intptr = &options->number_of_password_prompts;
891 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 case oCompressionLevel:
894 intptr = &options->compression_level;
895 goto parse_int;
896
Damien Millera5539d22003-04-09 20:50:06 +1000897 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000898 arg = strdelim(&s);
899 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000900 fatal("%.200s line %d: Missing argument.", filename,
901 linenum);
902 if (strcmp(arg, "default") == 0) {
903 val64 = 0;
904 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000905 if (scan_scaled(arg, &val64) == -1)
906 fatal("%.200s line %d: Bad number '%s': %s",
907 filename, linenum, arg, strerror(errno));
908 /* check for too-large or too-small limits */
909 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000910 fatal("%.200s line %d: RekeyLimit too large",
911 filename, linenum);
912 if (val64 != 0 && val64 < 16)
913 fatal("%.200s line %d: RekeyLimit too small",
914 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000915 }
Damien Miller3dff1762008-02-10 22:25:52 +1100916 if (*activep && options->rekey_limit == -1)
917 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000918 if (s != NULL) { /* optional rekey interval present */
919 if (strcmp(s, "none") == 0) {
920 (void)strdelim(&s); /* discard */
921 break;
922 }
923 intptr = &options->rekey_interval;
924 goto parse_time;
925 }
Damien Millera5539d22003-04-09 20:50:06 +1000926 break;
927
Damien Miller95def091999-11-25 00:26:21 +1100928 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000929 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000930 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100931 fatal("%.200s line %d: Missing argument.", filename, linenum);
932 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100933 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000934 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100935 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100936 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100937 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100938 }
939 break;
940
Damien Millerd3a18572000-06-07 19:55:44 +1000941 case oXAuthLocation:
942 charptr=&options->xauth_location;
943 goto parse_string;
944
Damien Miller95def091999-11-25 00:26:21 +1100945 case oUser:
946 charptr = &options->user;
947parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000948 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000949 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000950 fatal("%.200s line %d: Missing argument.",
951 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100952 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000953 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100954 break;
955
956 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000957 cpptr = (char **)&options->system_hostfiles;
958 uintptr = &options->num_system_hostfiles;
959 max_entries = SSH_MAX_HOSTS_FILES;
960parse_char_array:
961 if (*activep && *uintptr == 0) {
962 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
963 if ((*uintptr) >= max_entries)
964 fatal("%s line %d: "
965 "too many authorized keys files.",
966 filename, linenum);
967 cpptr[(*uintptr)++] = xstrdup(arg);
968 }
969 }
970 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100971
972 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000973 cpptr = (char **)&options->user_hostfiles;
974 uintptr = &options->num_user_hostfiles;
975 max_entries = SSH_MAX_HOSTS_FILES;
976 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000977
Damien Miller95def091999-11-25 00:26:21 +1100978 case oHostName:
979 charptr = &options->hostname;
980 goto parse_string;
981
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000982 case oHostKeyAlias:
983 charptr = &options->host_key_alias;
984 goto parse_string;
985
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000986 case oPreferredAuthentications:
987 charptr = &options->preferred_authentications;
988 goto parse_string;
989
Ben Lindstrome0f88042001-04-30 13:06:24 +0000990 case oBindAddress:
991 charptr = &options->bind_address;
992 goto parse_string;
993
Damien Miller7ea845e2010-02-12 09:21:02 +1100994 case oPKCS11Provider:
995 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000996 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000997
Damien Miller95def091999-11-25 00:26:21 +1100998 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100999 charptr = &options->proxy_command;
1000parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001001 if (s == NULL)
1002 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001003 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001004 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001005 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001006 return 0;
1007
1008 case oPort:
1009 intptr = &options->port;
1010parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001011 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001012 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001013 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001014 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001015 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001016
1017 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001018 value = strtol(arg, &endofnumber, 0);
1019 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001020 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001021 if (*activep && *intptr == -1)
1022 *intptr = value;
1023 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Miller95def091999-11-25 00:26:21 +11001025 case oConnectionAttempts:
1026 intptr = &options->connection_attempts;
1027 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001028
Damien Miller95def091999-11-25 00:26:21 +11001029 case oCipher:
1030 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001031 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001032 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001033 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001034 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001035 if (value == -1)
1036 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001037 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001038 if (*activep && *intptr == -1)
1039 *intptr = value;
1040 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller78928792000-04-12 20:17:38 +10001042 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001043 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001044 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001045 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001046 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001047 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001048 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001049 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001050 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001051 break;
1052
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001053 case oMacs:
1054 arg = strdelim(&s);
1055 if (!arg || *arg == '\0')
1056 fatal("%.200s line %d: Missing argument.", filename, linenum);
1057 if (!mac_valid(arg))
1058 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001059 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001060 if (*activep && options->macs == NULL)
1061 options->macs = xstrdup(arg);
1062 break;
1063
Damien Millerd5f62bf2010-09-24 22:11:14 +10001064 case oKexAlgorithms:
1065 arg = strdelim(&s);
1066 if (!arg || *arg == '\0')
1067 fatal("%.200s line %d: Missing argument.",
1068 filename, linenum);
1069 if (!kex_names_valid(arg))
1070 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1071 filename, linenum, arg ? arg : "<NONE>");
1072 if (*activep && options->kex_algorithms == NULL)
1073 options->kex_algorithms = xstrdup(arg);
1074 break;
1075
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001076 case oHostKeyAlgorithms:
1077 arg = strdelim(&s);
1078 if (!arg || *arg == '\0')
1079 fatal("%.200s line %d: Missing argument.", filename, linenum);
1080 if (!key_names_valid2(arg))
1081 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001082 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001083 if (*activep && options->hostkeyalgorithms == NULL)
1084 options->hostkeyalgorithms = xstrdup(arg);
1085 break;
1086
Damien Miller78928792000-04-12 20:17:38 +10001087 case oProtocol:
1088 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001089 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001090 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001091 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001092 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001093 if (value == SSH_PROTO_UNKNOWN)
1094 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001095 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001096 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1097 *intptr = value;
1098 break;
1099
Damien Miller95def091999-11-25 00:26:21 +11001100 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001101 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001102 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001103 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001104 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001105 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001106 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001107 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1108 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001109 break;
1110
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001111 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001112 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001113 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001114 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001115 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001116 fatal("%.200s line %d: Missing port argument.",
1117 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001118
Damien Millera699d952008-11-03 19:27:34 +11001119 if (opcode == oLocalForward ||
1120 opcode == oRemoteForward) {
1121 arg2 = strdelim(&s);
1122 if (arg2 == NULL || *arg2 == '\0')
1123 fatal("%.200s line %d: Missing target argument.",
1124 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001125
Damien Millera699d952008-11-03 19:27:34 +11001126 /* construct a string for parse_forward */
1127 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1128 } else if (opcode == oDynamicForward) {
1129 strlcpy(fwdarg, arg, sizeof(fwdarg));
1130 }
1131
1132 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001133 opcode == oDynamicForward ? 1 : 0,
1134 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001135 fatal("%.200s line %d: Bad forwarding specification.",
1136 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001137
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001138 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001139 if (opcode == oLocalForward ||
1140 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001141 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001142 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001143 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001144 }
Damien Miller95def091999-11-25 00:26:21 +11001145 break;
1146
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001147 case oClearAllForwardings:
1148 intptr = &options->clear_forwardings;
1149 goto parse_flag;
1150
Damien Miller95def091999-11-25 00:26:21 +11001151 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001152 if (cmdline)
1153 fatal("Host directive not supported as a command-line "
1154 "option");
Damien Miller95def091999-11-25 00:26:21 +11001155 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001156 arg2 = NULL;
1157 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1158 negated = *arg == '!';
1159 if (negated)
1160 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001161 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001162 if (negated) {
1163 debug("%.200s line %d: Skipping Host "
1164 "block because of negated match "
1165 "for %.100s", filename, linenum,
1166 arg);
1167 *activep = 0;
1168 break;
1169 }
1170 if (!*activep)
1171 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001172 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001173 }
Damien Millerfe924212011-05-15 08:44:45 +10001174 }
1175 if (*activep)
1176 debug("%.200s line %d: Applying options for %.100s",
1177 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001178 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001179 return 0;
1180
Damien Miller194fd902013-10-15 12:13:05 +11001181 case oMatch:
1182 if (cmdline)
1183 fatal("Host directive not supported as a command-line "
1184 "option");
1185 value = match_cfg_line(options, &s, pw, host,
1186 filename, linenum);
1187 if (value < 0)
1188 fatal("%.200s line %d: Bad Match condition", filename,
1189 linenum);
1190 *activep = value;
1191 break;
1192
Damien Miller95def091999-11-25 00:26:21 +11001193 case oEscapeChar:
1194 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001195 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001196 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001197 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001198 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001199 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1200 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001201 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001202 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001203 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001204 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001205 else {
1206 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001207 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001208 /* NOTREACHED */
1209 value = 0; /* Avoid compiler warning. */
1210 }
1211 if (*activep && *intptr == -1)
1212 *intptr = value;
1213 break;
1214
Damien Miller20a8f972003-05-18 20:50:30 +10001215 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001216 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001217 multistate_ptr = multistate_addressfamily;
1218 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001219
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001220 case oEnableSSHKeysign:
1221 intptr = &options->enable_ssh_keysign;
1222 goto parse_flag;
1223
Damien Millerbd394c32004-03-08 23:12:36 +11001224 case oIdentitiesOnly:
1225 intptr = &options->identities_only;
1226 goto parse_flag;
1227
Damien Miller509b0102003-12-17 16:33:10 +11001228 case oServerAliveInterval:
1229 intptr = &options->server_alive_interval;
1230 goto parse_time;
1231
1232 case oServerAliveCountMax:
1233 intptr = &options->server_alive_count_max;
1234 goto parse_int;
1235
Darren Tucker46bc0752004-05-02 22:11:30 +10001236 case oSendEnv:
1237 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1238 if (strchr(arg, '=') != NULL)
1239 fatal("%s line %d: Invalid environment name.",
1240 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001241 if (!*activep)
1242 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001243 if (options->num_send_env >= MAX_SEND_ENV)
1244 fatal("%s line %d: too many send env.",
1245 filename, linenum);
1246 options->send_env[options->num_send_env++] =
1247 xstrdup(arg);
1248 }
1249 break;
1250
Damien Miller0e220db2004-06-15 10:34:08 +10001251 case oControlPath:
1252 charptr = &options->control_path;
1253 goto parse_string;
1254
1255 case oControlMaster:
1256 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001257 multistate_ptr = multistate_controlmaster;
1258 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001259
Damien Millere11e1ea2010-08-03 16:04:46 +10001260 case oControlPersist:
1261 /* no/false/yes/true, or a time spec */
1262 intptr = &options->control_persist;
1263 arg = strdelim(&s);
1264 if (!arg || *arg == '\0')
1265 fatal("%.200s line %d: Missing ControlPersist"
1266 " argument.", filename, linenum);
1267 value = 0;
1268 value2 = 0; /* timeout */
1269 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1270 value = 0;
1271 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1272 value = 1;
1273 else if ((value2 = convtime(arg)) >= 0)
1274 value = 1;
1275 else
1276 fatal("%.200s line %d: Bad ControlPersist argument.",
1277 filename, linenum);
1278 if (*activep && *intptr == -1) {
1279 *intptr = value;
1280 options->control_persist_timeout = value2;
1281 }
1282 break;
1283
Damien Millere1776152005-03-01 21:47:37 +11001284 case oHashKnownHosts:
1285 intptr = &options->hash_known_hosts;
1286 goto parse_flag;
1287
Damien Millerd27b9472005-12-13 19:29:02 +11001288 case oTunnel:
1289 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001290 multistate_ptr = multistate_tunnel;
1291 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001292
1293 case oTunnelDevice:
1294 arg = strdelim(&s);
1295 if (!arg || *arg == '\0')
1296 fatal("%.200s line %d: Missing argument.", filename, linenum);
1297 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001298 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001299 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1300 if (*activep) {
1301 options->tun_local = value;
1302 options->tun_remote = value2;
1303 }
1304 break;
1305
1306 case oLocalCommand:
1307 charptr = &options->local_command;
1308 goto parse_command;
1309
1310 case oPermitLocalCommand:
1311 intptr = &options->permit_local_command;
1312 goto parse_flag;
1313
Damien Miller10288242008-06-30 00:04:03 +10001314 case oVisualHostKey:
1315 intptr = &options->visual_host_key;
1316 goto parse_flag;
1317
Damien Miller0dac6fb2010-11-20 15:19:38 +11001318 case oIPQoS:
1319 arg = strdelim(&s);
1320 if ((value = parse_ipqos(arg)) == -1)
1321 fatal("%s line %d: Bad IPQoS value: %s",
1322 filename, linenum, arg);
1323 arg = strdelim(&s);
1324 if (arg == NULL)
1325 value2 = value;
1326 else if ((value2 = parse_ipqos(arg)) == -1)
1327 fatal("%s line %d: Bad IPQoS value: %s",
1328 filename, linenum, arg);
1329 if (*activep) {
1330 options->ip_qos_interactive = value;
1331 options->ip_qos_bulk = value2;
1332 }
1333 break;
1334
Darren Tucker71e4d542009-07-06 07:12:27 +10001335 case oUseRoaming:
1336 intptr = &options->use_roaming;
1337 goto parse_flag;
1338
Damien Miller21771e22011-05-15 08:45:50 +10001339 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001340 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001341 multistate_ptr = multistate_requesttty;
1342 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001343
Darren Tucker07636982013-05-16 20:30:03 +10001344 case oIgnoreUnknown:
1345 charptr = &options->ignored_unknown;
1346 goto parse_string;
1347
Damien Miller1262b662013-08-21 02:44:24 +10001348 case oProxyUseFdpass:
1349 intptr = &options->proxy_use_fdpass;
1350 goto parse_flag;
1351
Damien Miller0faf7472013-10-17 11:47:23 +11001352 case oCanonicalDomains:
1353 value = options->num_canonical_domains != 0;
1354 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1355 valid_domain(arg, filename, linenum);
1356 if (!*activep || value)
1357 continue;
1358 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1359 fatal("%s line %d: too many hostname suffixes.",
1360 filename, linenum);
1361 options->canonical_domains[
1362 options->num_canonical_domains++] = xstrdup(arg);
1363 }
1364 break;
1365
Damien Miller38505592013-10-17 11:48:13 +11001366 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001367 value = options->num_permitted_cnames != 0;
1368 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1369 /* Either '*' for everything or 'list:list' */
1370 if (strcmp(arg, "*") == 0)
1371 arg2 = arg;
1372 else {
1373 lowercase(arg);
1374 if ((arg2 = strchr(arg, ':')) == NULL ||
1375 arg2[1] == '\0') {
1376 fatal("%s line %d: "
1377 "Invalid permitted CNAME \"%s\"",
1378 filename, linenum, arg);
1379 }
1380 *arg2 = '\0';
1381 arg2++;
1382 }
1383 if (!*activep || value)
1384 continue;
1385 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1386 fatal("%s line %d: too many permitted CNAMEs.",
1387 filename, linenum);
1388 cname = options->permitted_cnames +
1389 options->num_permitted_cnames++;
1390 cname->source_list = xstrdup(arg);
1391 cname->target_list = xstrdup(arg2);
1392 }
1393 break;
1394
Damien Miller38505592013-10-17 11:48:13 +11001395 case oCanonicalizeHostname:
1396 intptr = &options->canonicalize_hostname;
1397 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001398 goto parse_multistate;
1399
Damien Miller38505592013-10-17 11:48:13 +11001400 case oCanonicalizeMaxDots:
1401 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001402 goto parse_int;
1403
Damien Miller38505592013-10-17 11:48:13 +11001404 case oCanonicalizeFallbackLocal:
1405 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001406 goto parse_flag;
1407
Ben Lindstrom4daea862002-06-09 20:04:02 +00001408 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001409 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001410 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001411 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001412
Damien Millerf9b3feb2003-05-16 11:38:32 +10001413 case oUnsupported:
1414 error("%s line %d: Unsupported option \"%s\"",
1415 filename, linenum, keyword);
1416 return 0;
1417
Damien Miller95def091999-11-25 00:26:21 +11001418 default:
1419 fatal("process_config_line: Unimplemented opcode %d", opcode);
1420 }
1421
1422 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001423 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001424 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001425 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001426 }
Damien Miller95def091999-11-25 00:26:21 +11001427 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428}
1429
1430
Damien Miller5428f641999-11-25 11:54:57 +11001431/*
1432 * Reads the config file and modifies the options accordingly. Options
1433 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001434 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001435 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001437int
Damien Miller194fd902013-10-15 12:13:05 +11001438read_config_file(const char *filename, struct passwd *pw, const char *host,
1439 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440{
Damien Miller95def091999-11-25 00:26:21 +11001441 FILE *f;
1442 char line[1024];
1443 int active, linenum;
1444 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller57a44762004-04-20 20:11:57 +10001446 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001447 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448
Darren Tuckeraefa3682013-04-05 11:18:35 +11001449 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001450 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001451
Damien Miller33793852004-06-15 10:27:55 +10001452 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001453 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001454 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001455 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001456 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001457 }
1458
Damien Miller95def091999-11-25 00:26:21 +11001459 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460
Damien Miller5428f641999-11-25 11:54:57 +11001461 /*
1462 * Mark that we are now processing the options. This flag is turned
1463 * on/off by Host specifications.
1464 */
Damien Miller95def091999-11-25 00:26:21 +11001465 active = 1;
1466 linenum = 0;
1467 while (fgets(line, sizeof(line), f)) {
1468 /* Update line number counter. */
1469 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001470 if (process_config_line(options, pw, host, line, filename,
1471 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001472 bad_options++;
1473 }
1474 fclose(f);
1475 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001476 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001477 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001478 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479}
1480
Damien Miller13f97b22014-02-24 15:57:55 +11001481/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1482int
1483option_clear_or_none(const char *o)
1484{
1485 return o == NULL || strcasecmp(o, "none") == 0;
1486}
1487
Damien Miller5428f641999-11-25 11:54:57 +11001488/*
1489 * Initializes options to special values that indicate that they have not yet
1490 * been set. Read_config_file will only set options with this value. Options
1491 * are processed in the following order: command line, user config file,
1492 * system config file. Last, fill_default_options is called.
1493 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller4af51302000-04-16 11:18:38 +10001495void
Damien Miller95def091999-11-25 00:26:21 +11001496initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497{
Damien Miller95def091999-11-25 00:26:21 +11001498 memset(options, 'X', sizeof(*options));
1499 options->forward_agent = -1;
1500 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001501 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001502 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001503 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001504 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001505 options->gateway_ports = -1;
1506 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001507 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001508 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001509 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001510 options->gss_authentication = -1;
1511 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001512 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001513 options->kbd_interactive_authentication = -1;
1514 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001515 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001516 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001517 options->batch_mode = -1;
1518 options->check_host_ip = -1;
1519 options->strict_host_key_checking = -1;
1520 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001521 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001522 options->compression_level = -1;
1523 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001524 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001525 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001526 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001527 options->number_of_password_prompts = -1;
1528 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001529 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001530 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001531 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001532 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001533 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001534 options->num_identity_files = 0;
1535 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001536 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001537 options->proxy_command = NULL;
1538 options->user = NULL;
1539 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001540 options->num_system_hostfiles = 0;
1541 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001542 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001543 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001544 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001545 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001546 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001547 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001548 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001549 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001550 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001551 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001552 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001553 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001554 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001555 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001556 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001557 options->server_alive_interval = -1;
1558 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001559 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001560 options->control_path = NULL;
1561 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001562 options->control_persist = -1;
1563 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001564 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001565 options->tun_open = -1;
1566 options->tun_local = -1;
1567 options->tun_remote = -1;
1568 options->local_command = NULL;
1569 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001570 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001571 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001572 options->ip_qos_interactive = -1;
1573 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001574 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001575 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001576 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001577 options->num_canonical_domains = 0;
1578 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001579 options->canonicalize_max_dots = -1;
1580 options->canonicalize_fallback_local = -1;
1581 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582}
1583
Damien Miller5428f641999-11-25 11:54:57 +11001584/*
Damien Miller13f97b22014-02-24 15:57:55 +11001585 * A petite version of fill_default_options() that just fills the options
1586 * needed for hostname canonicalization to proceed.
1587 */
1588void
1589fill_default_options_for_canonicalization(Options *options)
1590{
1591 if (options->canonicalize_max_dots == -1)
1592 options->canonicalize_max_dots = 1;
1593 if (options->canonicalize_fallback_local == -1)
1594 options->canonicalize_fallback_local = 1;
1595 if (options->canonicalize_hostname == -1)
1596 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1597}
1598
1599/*
Damien Miller5428f641999-11-25 11:54:57 +11001600 * Called after processing other sources of option data, this fills those
1601 * options for which no value has been specified with their default values.
1602 */
Damien Miller4af51302000-04-16 11:18:38 +10001603void
Damien Miller95def091999-11-25 00:26:21 +11001604fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605{
Damien Miller95def091999-11-25 00:26:21 +11001606 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001607 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001608 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001609 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001610 if (options->forward_x11_trusted == -1)
1611 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001612 if (options->forward_x11_timeout == -1)
1613 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001614 if (options->exit_on_forward_failure == -1)
1615 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001616 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001617 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001618 if (options->gateway_ports == -1)
1619 options->gateway_ports = 0;
1620 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001621 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001622 if (options->rsa_authentication == -1)
1623 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001624 if (options->pubkey_authentication == -1)
1625 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001626 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001627 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001628 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001629 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001630 if (options->gss_deleg_creds == -1)
1631 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001632 if (options->password_authentication == -1)
1633 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001634 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001635 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001636 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001637 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001638 if (options->hostbased_authentication == -1)
1639 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001640 if (options->batch_mode == -1)
1641 options->batch_mode = 0;
1642 if (options->check_host_ip == -1)
1643 options->check_host_ip = 1;
1644 if (options->strict_host_key_checking == -1)
1645 options->strict_host_key_checking = 2; /* 2 is default */
1646 if (options->compression == -1)
1647 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001648 if (options->tcp_keep_alive == -1)
1649 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001650 if (options->compression_level == -1)
1651 options->compression_level = 6;
1652 if (options->port == -1)
1653 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001654 if (options->address_family == -1)
1655 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001656 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001657 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001658 if (options->number_of_password_prompts == -1)
1659 options->number_of_password_prompts = 3;
1660 /* Selected in ssh_login(). */
1661 if (options->cipher == -1)
1662 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001663 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001664 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001665 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001666 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001667 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001668 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001669 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001670 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001671 add_identity_file(options, "~/",
1672 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001673 }
1674 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001675 add_identity_file(options, "~/",
1676 _PATH_SSH_CLIENT_ID_RSA, 0);
1677 add_identity_file(options, "~/",
1678 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001679#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001680 add_identity_file(options, "~/",
1681 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001682#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001683 add_identity_file(options, "~/",
1684 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685 }
Damien Millereba71ba2000-04-29 23:57:08 +10001686 }
Damien Miller95def091999-11-25 00:26:21 +11001687 if (options->escape_char == -1)
1688 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001689 if (options->num_system_hostfiles == 0) {
1690 options->system_hostfiles[options->num_system_hostfiles++] =
1691 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1692 options->system_hostfiles[options->num_system_hostfiles++] =
1693 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1694 }
1695 if (options->num_user_hostfiles == 0) {
1696 options->user_hostfiles[options->num_user_hostfiles++] =
1697 xstrdup(_PATH_SSH_USER_HOSTFILE);
1698 options->user_hostfiles[options->num_user_hostfiles++] =
1699 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1700 }
Damien Millerfcd93202002-02-05 12:26:34 +11001701 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001702 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001703 if (options->clear_forwardings == 1)
1704 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001705 if (options->no_host_authentication_for_localhost == - 1)
1706 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001707 if (options->identities_only == -1)
1708 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001709 if (options->enable_ssh_keysign == -1)
1710 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001711 if (options->rekey_limit == -1)
1712 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001713 if (options->rekey_interval == -1)
1714 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001715 if (options->verify_host_key_dns == -1)
1716 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001717 if (options->server_alive_interval == -1)
1718 options->server_alive_interval = 0;
1719 if (options->server_alive_count_max == -1)
1720 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001721 if (options->control_master == -1)
1722 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001723 if (options->control_persist == -1) {
1724 options->control_persist = 0;
1725 options->control_persist_timeout = 0;
1726 }
Damien Millere1776152005-03-01 21:47:37 +11001727 if (options->hash_known_hosts == -1)
1728 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001729 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001730 options->tun_open = SSH_TUNMODE_NO;
1731 if (options->tun_local == -1)
1732 options->tun_local = SSH_TUNID_ANY;
1733 if (options->tun_remote == -1)
1734 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001735 if (options->permit_local_command == -1)
1736 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001737 if (options->use_roaming == -1)
1738 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001739 if (options->visual_host_key == -1)
1740 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001741 if (options->ip_qos_interactive == -1)
1742 options->ip_qos_interactive = IPTOS_LOWDELAY;
1743 if (options->ip_qos_bulk == -1)
1744 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001745 if (options->request_tty == -1)
1746 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001747 if (options->proxy_use_fdpass == -1)
1748 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001749 if (options->canonicalize_max_dots == -1)
1750 options->canonicalize_max_dots = 1;
1751 if (options->canonicalize_fallback_local == -1)
1752 options->canonicalize_fallback_local = 1;
1753 if (options->canonicalize_hostname == -1)
1754 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001755#define CLEAR_ON_NONE(v) \
1756 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001757 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001758 free(v); \
1759 v = NULL; \
1760 } \
1761 } while(0)
1762 CLEAR_ON_NONE(options->local_command);
1763 CLEAR_ON_NONE(options->proxy_command);
1764 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001765 /* options->user will be set in the main program if appropriate */
1766 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001767 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001768 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001769}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001770
1771/*
1772 * parse_forward
1773 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001774 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001775 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001776 * dynamicfwd == 1
1777 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001778 * returns number of arguments parsed or zero on error
1779 */
1780int
Damien Miller4bf648f2009-02-14 16:28:21 +11001781parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001782{
1783 int i;
1784 char *p, *cp, *fwdarg[4];
1785
1786 memset(fwd, '\0', sizeof(*fwd));
1787
1788 cp = p = xstrdup(fwdspec);
1789
1790 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001791 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001792 cp++;
1793
1794 for (i = 0; i < 4; ++i)
1795 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1796 break;
1797
Damien Millerf4b39532008-11-03 19:28:21 +11001798 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001799 if (cp != NULL)
1800 i = 0; /* failure */
1801
1802 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001803 case 1:
1804 fwd->listen_host = NULL;
1805 fwd->listen_port = a2port(fwdarg[0]);
1806 fwd->connect_host = xstrdup("socks");
1807 break;
1808
1809 case 2:
1810 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1811 fwd->listen_port = a2port(fwdarg[1]);
1812 fwd->connect_host = xstrdup("socks");
1813 break;
1814
Damien Millerf91ee4c2005-03-01 21:24:33 +11001815 case 3:
1816 fwd->listen_host = NULL;
1817 fwd->listen_port = a2port(fwdarg[0]);
1818 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1819 fwd->connect_port = a2port(fwdarg[2]);
1820 break;
1821
1822 case 4:
1823 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1824 fwd->listen_port = a2port(fwdarg[1]);
1825 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1826 fwd->connect_port = a2port(fwdarg[3]);
1827 break;
1828 default:
1829 i = 0; /* failure */
1830 }
1831
Darren Tuckera627d422013-06-02 07:31:17 +10001832 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001833
Damien Millera699d952008-11-03 19:27:34 +11001834 if (dynamicfwd) {
1835 if (!(i == 1 || i == 2))
1836 goto fail_free;
1837 } else {
1838 if (!(i == 3 || i == 4))
1839 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001840 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001841 goto fail_free;
1842 }
1843
Damien Miller4bf648f2009-02-14 16:28:21 +11001844 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001845 goto fail_free;
1846
1847 if (fwd->connect_host != NULL &&
1848 strlen(fwd->connect_host) >= NI_MAXHOST)
1849 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001850 if (fwd->listen_host != NULL &&
1851 strlen(fwd->listen_host) >= NI_MAXHOST)
1852 goto fail_free;
1853
Damien Millerf91ee4c2005-03-01 21:24:33 +11001854
1855 return (i);
1856
1857 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001858 free(fwd->connect_host);
1859 fwd->connect_host = NULL;
1860 free(fwd->listen_host);
1861 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001862 return (0);
1863}