blob: f80d1ccbc5fa9e137e93ab2822657263a747f4ec [file] [log] [blame]
Damien Miller7cc194f2014-02-04 11:12:56 +11001/* $OpenBSD: readconf.c,v 1.216 2014/01/29 06:18:35 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;
348
349 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
350 fatal("Too many identity files specified (max %d)",
351 SSH_MAX_IDENTITY_FILES);
352
353 if (dir == NULL) /* no dir, filename is absolute */
354 path = xstrdup(filename);
355 else
356 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
357
358 options->identity_file_userprovided[options->num_identity_files] =
359 userprovided;
360 options->identity_files[options->num_identity_files++] = path;
361}
362
Damien Miller194fd902013-10-15 12:13:05 +1100363int
364default_ssh_port(void)
365{
366 static int port;
367 struct servent *sp;
368
369 if (port == 0) {
370 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
371 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
372 }
373 return port;
374}
375
376/*
377 * Execute a command in a shell.
378 * Return its exit status or -1 on abnormal exit.
379 */
380static int
381execute_in_shell(const char *cmd)
382{
383 char *shell, *command_string;
384 pid_t pid;
385 int devnull, status;
386 extern uid_t original_real_uid;
387
388 if ((shell = getenv("SHELL")) == NULL)
389 shell = _PATH_BSHELL;
390
391 /*
392 * Use "exec" to avoid "sh -c" processes on some platforms
393 * (e.g. Solaris)
394 */
395 xasprintf(&command_string, "exec %s", cmd);
396
397 /* Need this to redirect subprocess stdin/out */
398 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
399 fatal("open(/dev/null): %s", strerror(errno));
400
401 debug("Executing command: '%.500s'", cmd);
402
403 /* Fork and execute the command. */
404 if ((pid = fork()) == 0) {
405 char *argv[4];
406
407 /* Child. Permanently give up superuser privileges. */
408 permanently_drop_suid(original_real_uid);
409
410 /* Redirect child stdin and stdout. Leave stderr */
411 if (dup2(devnull, STDIN_FILENO) == -1)
412 fatal("dup2: %s", strerror(errno));
413 if (dup2(devnull, STDOUT_FILENO) == -1)
414 fatal("dup2: %s", strerror(errno));
415 if (devnull > STDERR_FILENO)
416 close(devnull);
417 closefrom(STDERR_FILENO + 1);
418
419 argv[0] = shell;
420 argv[1] = "-c";
421 argv[2] = command_string;
422 argv[3] = NULL;
423
424 execv(argv[0], argv);
425 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
426 /* Die with signal to make this error apparent to parent. */
427 signal(SIGTERM, SIG_DFL);
428 kill(getpid(), SIGTERM);
429 _exit(1);
430 }
431 /* Parent. */
432 if (pid < 0)
433 fatal("%s: fork: %.100s", __func__, strerror(errno));
434
435 close(devnull);
436 free(command_string);
437
438 while (waitpid(pid, &status, 0) == -1) {
439 if (errno != EINTR && errno != EAGAIN)
440 fatal("%s: waitpid: %s", __func__, strerror(errno));
441 }
442 if (!WIFEXITED(status)) {
443 error("command '%.100s' exited abnormally", cmd);
444 return -1;
445 }
446 debug3("command returned status %d", WEXITSTATUS(status));
447 return WEXITSTATUS(status);
448}
449
450/*
451 * Parse and execute a Match directive.
452 */
453static int
454match_cfg_line(Options *options, char **condition, struct passwd *pw,
455 const char *host_arg, const char *filename, int linenum)
456{
Damien Miller084bcd22013-10-23 16:30:51 +1100457 char *arg, *attrib, *cmd, *cp = *condition, *host;
458 const char *ruser;
Damien Millercf31f382013-10-24 21:02:56 +1100459 int r, port, result = 1, attributes = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100460 size_t len;
461 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
462
463 /*
464 * Configuration is likely to be incomplete at this point so we
465 * must be prepared to use default values.
466 */
467 port = options->port <= 0 ? default_ssh_port() : options->port;
468 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100469 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100470 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100471 host = percent_expand(options->hostname,
472 "h", host_arg, (char *)NULL);
473 } else
474 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100475
476 debug3("checking match for '%s' host %s", cp, host);
477 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100478 attributes++;
479 if (strcasecmp(attrib, "all") == 0) {
480 if (attributes != 1 ||
481 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
482 error("'all' cannot be combined with other "
483 "Match attributes");
484 result = -1;
485 goto out;
486 }
487 *condition = cp;
488 result = 1;
489 goto out;
490 }
Damien Miller194fd902013-10-15 12:13:05 +1100491 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
492 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100493 result = -1;
494 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100495 }
496 len = strlen(arg);
497 if (strcasecmp(attrib, "host") == 0) {
498 if (match_hostname(host, arg, len) != 1)
499 result = 0;
500 else
501 debug("%.200s line %d: matched 'Host %.100s' ",
502 filename, linenum, host);
503 } else if (strcasecmp(attrib, "originalhost") == 0) {
504 if (match_hostname(host_arg, arg, len) != 1)
505 result = 0;
506 else
507 debug("%.200s line %d: matched "
508 "'OriginalHost %.100s' ",
509 filename, linenum, host_arg);
510 } else if (strcasecmp(attrib, "user") == 0) {
511 if (match_pattern_list(ruser, arg, len, 0) != 1)
512 result = 0;
513 else
514 debug("%.200s line %d: matched 'User %.100s' ",
515 filename, linenum, ruser);
516 } else if (strcasecmp(attrib, "localuser") == 0) {
517 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
518 result = 0;
519 else
520 debug("%.200s line %d: matched "
521 "'LocalUser %.100s' ",
522 filename, linenum, pw->pw_name);
Damien Miller8a04be72013-10-23 16:29:40 +1100523 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100524 if (gethostname(thishost, sizeof(thishost)) == -1)
525 fatal("gethostname: %s", strerror(errno));
526 strlcpy(shorthost, thishost, sizeof(shorthost));
527 shorthost[strcspn(thishost, ".")] = '\0';
528 snprintf(portstr, sizeof(portstr), "%d", port);
529
530 cmd = percent_expand(arg,
531 "L", shorthost,
532 "d", pw->pw_dir,
533 "h", host,
534 "l", thishost,
535 "n", host_arg,
536 "p", portstr,
537 "r", ruser,
538 "u", pw->pw_name,
539 (char *)NULL);
540 r = execute_in_shell(cmd);
541 if (r == -1) {
Damien Miller8a04be72013-10-23 16:29:40 +1100542 fatal("%.200s line %d: match exec '%.100s' "
Damien Miller194fd902013-10-15 12:13:05 +1100543 "error", filename, linenum, cmd);
544 } else if (r == 0) {
545 debug("%.200s line %d: matched "
Damien Miller8a04be72013-10-23 16:29:40 +1100546 "'exec \"%.100s\"' ",
Damien Miller194fd902013-10-15 12:13:05 +1100547 filename, linenum, cmd);
548 } else
549 result = 0;
550 free(cmd);
551 } else {
552 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100553 result = -1;
554 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100555 }
556 }
Damien Millercf31f382013-10-24 21:02:56 +1100557 if (attributes == 0) {
558 error("One or more attributes required for Match");
559 result = -1;
560 goto out;
561 }
Damien Miller194fd902013-10-15 12:13:05 +1100562 debug3("match %sfound", result ? "" : "not ");
563 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100564 out:
565 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100566 return result;
567}
568
Damien Miller0faf7472013-10-17 11:47:23 +1100569/* Check and prepare a domain name: removes trailing '.' and lowercases */
570static void
571valid_domain(char *name, const char *filename, int linenum)
572{
573 size_t i, l = strlen(name);
574 u_char c, last = '\0';
575
576 if (l == 0)
577 fatal("%s line %d: empty hostname suffix", filename, linenum);
578 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
579 fatal("%s line %d: hostname suffix \"%.100s\" "
580 "starts with invalid character", filename, linenum, name);
581 for (i = 0; i < l; i++) {
582 c = tolower((u_char)name[i]);
583 name[i] = (char)c;
584 if (last == '.' && c == '.')
585 fatal("%s line %d: hostname suffix \"%.100s\" contains "
586 "consecutive separators", filename, linenum, name);
587 if (c != '.' && c != '-' && !isalnum(c) &&
588 c != '_') /* technically invalid, but common */
589 fatal("%s line %d: hostname suffix \"%.100s\" contains "
590 "invalid characters", filename, linenum, name);
591 last = c;
592 }
593 if (name[l - 1] == '.')
594 name[l - 1] = '\0';
595}
596
Damien Miller5428f641999-11-25 11:54:57 +1100597/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000598 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100599 */
Damien Miller4af51302000-04-16 11:18:38 +1000600static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000601parse_token(const char *cp, const char *filename, int linenum,
602 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603{
Darren Tucker07636982013-05-16 20:30:03 +1000604 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller95def091999-11-25 00:26:21 +1100606 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000607 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100608 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000609 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
610 strlen(ignored_unknown), 1) == 1)
611 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000612 error("%s: line %d: Bad configuration option: %s",
613 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100614 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615}
616
Damien Millere9fc72e2013-10-15 12:14:12 +1100617/* Multistate option parsing */
618struct multistate {
619 char *key;
620 int value;
621};
622static const struct multistate multistate_flag[] = {
623 { "true", 1 },
624 { "false", 0 },
625 { "yes", 1 },
626 { "no", 0 },
627 { NULL, -1 }
628};
629static const struct multistate multistate_yesnoask[] = {
630 { "true", 1 },
631 { "false", 0 },
632 { "yes", 1 },
633 { "no", 0 },
634 { "ask", 2 },
635 { NULL, -1 }
636};
637static const struct multistate multistate_addressfamily[] = {
638 { "inet", AF_INET },
639 { "inet6", AF_INET6 },
640 { "any", AF_UNSPEC },
641 { NULL, -1 }
642};
643static const struct multistate multistate_controlmaster[] = {
644 { "true", SSHCTL_MASTER_YES },
645 { "yes", SSHCTL_MASTER_YES },
646 { "false", SSHCTL_MASTER_NO },
647 { "no", SSHCTL_MASTER_NO },
648 { "auto", SSHCTL_MASTER_AUTO },
649 { "ask", SSHCTL_MASTER_ASK },
650 { "autoask", SSHCTL_MASTER_AUTO_ASK },
651 { NULL, -1 }
652};
653static const struct multistate multistate_tunnel[] = {
654 { "ethernet", SSH_TUNMODE_ETHERNET },
655 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
656 { "true", SSH_TUNMODE_DEFAULT },
657 { "yes", SSH_TUNMODE_DEFAULT },
658 { "false", SSH_TUNMODE_NO },
659 { "no", SSH_TUNMODE_NO },
660 { NULL, -1 }
661};
662static const struct multistate multistate_requesttty[] = {
663 { "true", REQUEST_TTY_YES },
664 { "yes", REQUEST_TTY_YES },
665 { "false", REQUEST_TTY_NO },
666 { "no", REQUEST_TTY_NO },
667 { "force", REQUEST_TTY_FORCE },
668 { "auto", REQUEST_TTY_AUTO },
669 { NULL, -1 }
670};
Damien Miller38505592013-10-17 11:48:13 +1100671static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100672 { "true", SSH_CANONICALISE_YES },
673 { "false", SSH_CANONICALISE_NO },
674 { "yes", SSH_CANONICALISE_YES },
675 { "no", SSH_CANONICALISE_NO },
676 { "always", SSH_CANONICALISE_ALWAYS },
677 { NULL, -1 }
678};
Damien Millere9fc72e2013-10-15 12:14:12 +1100679
Damien Miller5428f641999-11-25 11:54:57 +1100680/*
681 * Processes a single option line as used in the configuration files. This
682 * only sets those values that have not already been set.
683 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100684#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100685int
Damien Miller194fd902013-10-15 12:13:05 +1100686process_config_line(Options *options, struct passwd *pw, const char *host,
687 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Damien Miller295ee632011-05-29 21:42:31 +1000689 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
690 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000691 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100692 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100693 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000694 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100695 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100696 Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100697 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100698 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699
Damien Miller194fd902013-10-15 12:13:05 +1100700 if (activep == NULL) { /* We are processing a command line directive */
701 cmdline = 1;
702 activep = &cmdline;
703 }
704
Damien Millerc652cac2003-05-14 13:40:54 +1000705 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100706 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000707 if (strchr(WHITESPACE, line[len]) == NULL)
708 break;
709 line[len] = '\0';
710 }
711
Damien Millerbe484b52000-07-15 14:14:16 +1000712 s = line;
713 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100714 if ((keyword = strdelim(&s)) == NULL)
715 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000716 /* Ignore leading whitespace. */
717 if (*keyword == '\0')
718 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000719 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100720 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000721 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100722 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723
Darren Tucker07636982013-05-16 20:30:03 +1000724 opcode = parse_token(keyword, filename, linenum,
725 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 switch (opcode) {
728 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100729 /* don't panic, but count bad options */
730 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100731 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000732 case oIgnoredUnknownOption:
733 debug("%s line %d: Ignored unknown option \"%s\"",
734 filename, linenum, keyword);
735 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000736 case oConnectTimeout:
737 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100738parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000739 arg = strdelim(&s);
740 if (!arg || *arg == '\0')
741 fatal("%s line %d: missing time value.",
742 filename, linenum);
743 if ((value = convtime(arg)) == -1)
744 fatal("%s line %d: invalid time value.",
745 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100746 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000747 *intptr = value;
748 break;
749
Damien Miller95def091999-11-25 00:26:21 +1100750 case oForwardAgent:
751 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100752 parse_flag:
753 multistate_ptr = multistate_flag;
754 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000755 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000756 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100757 fatal("%s line %d: missing argument.",
758 filename, linenum);
759 value = -1;
760 for (i = 0; multistate_ptr[i].key != NULL; i++) {
761 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
762 value = multistate_ptr[i].value;
763 break;
764 }
765 }
766 if (value == -1)
767 fatal("%s line %d: unsupported option \"%s\".",
768 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100769 if (*activep && *intptr == -1)
770 *intptr = value;
771 break;
772
773 case oForwardX11:
774 intptr = &options->forward_x11;
775 goto parse_flag;
776
Darren Tucker0a118da2003-10-15 15:54:32 +1000777 case oForwardX11Trusted:
778 intptr = &options->forward_x11_trusted;
779 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000780
781 case oForwardX11Timeout:
782 intptr = &options->forward_x11_timeout;
783 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000784
Damien Miller95def091999-11-25 00:26:21 +1100785 case oGatewayPorts:
786 intptr = &options->gateway_ports;
787 goto parse_flag;
788
Darren Tuckere7d4b192006-07-12 22:17:10 +1000789 case oExitOnForwardFailure:
790 intptr = &options->exit_on_forward_failure;
791 goto parse_flag;
792
Damien Miller95def091999-11-25 00:26:21 +1100793 case oUsePrivilegedPort:
794 intptr = &options->use_privileged_port;
795 goto parse_flag;
796
Damien Miller95def091999-11-25 00:26:21 +1100797 case oPasswordAuthentication:
798 intptr = &options->password_authentication;
799 goto parse_flag;
800
Damien Miller874d77b2000-10-14 16:23:11 +1100801 case oKbdInteractiveAuthentication:
802 intptr = &options->kbd_interactive_authentication;
803 goto parse_flag;
804
805 case oKbdInteractiveDevices:
806 charptr = &options->kbd_interactive_devices;
807 goto parse_string;
808
Damien Miller0bc1bd82000-11-13 22:57:25 +1100809 case oPubkeyAuthentication:
810 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000811 goto parse_flag;
812
Damien Miller95def091999-11-25 00:26:21 +1100813 case oRSAAuthentication:
814 intptr = &options->rsa_authentication;
815 goto parse_flag;
816
817 case oRhostsRSAAuthentication:
818 intptr = &options->rhosts_rsa_authentication;
819 goto parse_flag;
820
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000821 case oHostbasedAuthentication:
822 intptr = &options->hostbased_authentication;
823 goto parse_flag;
824
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000825 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000826 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100827 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000828
Darren Tucker0efd1552003-08-26 11:49:55 +1000829 case oGssAuthentication:
830 intptr = &options->gss_authentication;
831 goto parse_flag;
832
833 case oGssDelegateCreds:
834 intptr = &options->gss_deleg_creds;
835 goto parse_flag;
836
Damien Miller95def091999-11-25 00:26:21 +1100837 case oBatchMode:
838 intptr = &options->batch_mode;
839 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840
Damien Miller95def091999-11-25 00:26:21 +1100841 case oCheckHostIP:
842 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000843 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Miller37876e92003-05-15 10:19:46 +1000845 case oVerifyHostKeyDNS:
846 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100847 multistate_ptr = multistate_yesnoask;
848 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000849
Damien Miller95def091999-11-25 00:26:21 +1100850 case oStrictHostKeyChecking:
851 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100852 multistate_ptr = multistate_yesnoask;
853 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854
Damien Miller95def091999-11-25 00:26:21 +1100855 case oCompression:
856 intptr = &options->compression;
857 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858
Damien Miller12c150e2003-12-17 16:31:10 +1100859 case oTCPKeepAlive:
860 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100861 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000863 case oNoHostAuthenticationForLocalhost:
864 intptr = &options->no_host_authentication_for_localhost;
865 goto parse_flag;
866
Damien Miller95def091999-11-25 00:26:21 +1100867 case oNumberOfPasswordPrompts:
868 intptr = &options->number_of_password_prompts;
869 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
Damien Miller95def091999-11-25 00:26:21 +1100871 case oCompressionLevel:
872 intptr = &options->compression_level;
873 goto parse_int;
874
Damien Millera5539d22003-04-09 20:50:06 +1000875 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000876 arg = strdelim(&s);
877 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000878 fatal("%.200s line %d: Missing argument.", filename,
879 linenum);
880 if (strcmp(arg, "default") == 0) {
881 val64 = 0;
882 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000883 if (scan_scaled(arg, &val64) == -1)
884 fatal("%.200s line %d: Bad number '%s': %s",
885 filename, linenum, arg, strerror(errno));
886 /* check for too-large or too-small limits */
887 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000888 fatal("%.200s line %d: RekeyLimit too large",
889 filename, linenum);
890 if (val64 != 0 && val64 < 16)
891 fatal("%.200s line %d: RekeyLimit too small",
892 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000893 }
Damien Miller3dff1762008-02-10 22:25:52 +1100894 if (*activep && options->rekey_limit == -1)
895 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000896 if (s != NULL) { /* optional rekey interval present */
897 if (strcmp(s, "none") == 0) {
898 (void)strdelim(&s); /* discard */
899 break;
900 }
901 intptr = &options->rekey_interval;
902 goto parse_time;
903 }
Damien Millera5539d22003-04-09 20:50:06 +1000904 break;
905
Damien Miller95def091999-11-25 00:26:21 +1100906 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000907 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000908 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100909 fatal("%.200s line %d: Missing argument.", filename, linenum);
910 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100911 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000912 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100913 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100914 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100915 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100916 }
917 break;
918
Damien Millerd3a18572000-06-07 19:55:44 +1000919 case oXAuthLocation:
920 charptr=&options->xauth_location;
921 goto parse_string;
922
Damien Miller95def091999-11-25 00:26:21 +1100923 case oUser:
924 charptr = &options->user;
925parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000926 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000927 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000928 fatal("%.200s line %d: Missing argument.",
929 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100930 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000931 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100932 break;
933
934 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000935 cpptr = (char **)&options->system_hostfiles;
936 uintptr = &options->num_system_hostfiles;
937 max_entries = SSH_MAX_HOSTS_FILES;
938parse_char_array:
939 if (*activep && *uintptr == 0) {
940 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
941 if ((*uintptr) >= max_entries)
942 fatal("%s line %d: "
943 "too many authorized keys files.",
944 filename, linenum);
945 cpptr[(*uintptr)++] = xstrdup(arg);
946 }
947 }
948 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100949
950 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000951 cpptr = (char **)&options->user_hostfiles;
952 uintptr = &options->num_user_hostfiles;
953 max_entries = SSH_MAX_HOSTS_FILES;
954 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000955
Damien Miller95def091999-11-25 00:26:21 +1100956 case oHostName:
957 charptr = &options->hostname;
958 goto parse_string;
959
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000960 case oHostKeyAlias:
961 charptr = &options->host_key_alias;
962 goto parse_string;
963
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000964 case oPreferredAuthentications:
965 charptr = &options->preferred_authentications;
966 goto parse_string;
967
Ben Lindstrome0f88042001-04-30 13:06:24 +0000968 case oBindAddress:
969 charptr = &options->bind_address;
970 goto parse_string;
971
Damien Miller7ea845e2010-02-12 09:21:02 +1100972 case oPKCS11Provider:
973 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000974 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000975
Damien Miller95def091999-11-25 00:26:21 +1100976 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100977 charptr = &options->proxy_command;
978parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000979 if (s == NULL)
980 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100981 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100982 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100983 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100984 return 0;
985
986 case oPort:
987 intptr = &options->port;
988parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000989 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000990 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100991 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000992 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100993 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100994
995 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000996 value = strtol(arg, &endofnumber, 0);
997 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100998 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100999 if (*activep && *intptr == -1)
1000 *intptr = value;
1001 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 case oConnectionAttempts:
1004 intptr = &options->connection_attempts;
1005 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001006
Damien Miller95def091999-11-25 00:26:21 +11001007 case oCipher:
1008 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001009 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001010 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001011 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001012 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001013 if (value == -1)
1014 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001015 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001016 if (*activep && *intptr == -1)
1017 *intptr = value;
1018 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019
Damien Miller78928792000-04-12 20:17:38 +10001020 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001021 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001022 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001023 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001024 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001025 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001026 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001027 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001028 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001029 break;
1030
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001031 case oMacs:
1032 arg = strdelim(&s);
1033 if (!arg || *arg == '\0')
1034 fatal("%.200s line %d: Missing argument.", filename, linenum);
1035 if (!mac_valid(arg))
1036 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001037 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001038 if (*activep && options->macs == NULL)
1039 options->macs = xstrdup(arg);
1040 break;
1041
Damien Millerd5f62bf2010-09-24 22:11:14 +10001042 case oKexAlgorithms:
1043 arg = strdelim(&s);
1044 if (!arg || *arg == '\0')
1045 fatal("%.200s line %d: Missing argument.",
1046 filename, linenum);
1047 if (!kex_names_valid(arg))
1048 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1049 filename, linenum, arg ? arg : "<NONE>");
1050 if (*activep && options->kex_algorithms == NULL)
1051 options->kex_algorithms = xstrdup(arg);
1052 break;
1053
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001054 case oHostKeyAlgorithms:
1055 arg = strdelim(&s);
1056 if (!arg || *arg == '\0')
1057 fatal("%.200s line %d: Missing argument.", filename, linenum);
1058 if (!key_names_valid2(arg))
1059 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001060 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001061 if (*activep && options->hostkeyalgorithms == NULL)
1062 options->hostkeyalgorithms = xstrdup(arg);
1063 break;
1064
Damien Miller78928792000-04-12 20:17:38 +10001065 case oProtocol:
1066 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001067 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001068 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001069 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001070 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001071 if (value == SSH_PROTO_UNKNOWN)
1072 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001073 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001074 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1075 *intptr = value;
1076 break;
1077
Damien Miller95def091999-11-25 00:26:21 +11001078 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001079 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001080 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001081 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001082 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001083 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001084 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001085 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1086 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001087 break;
1088
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001089 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001090 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001091 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001092 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001093 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001094 fatal("%.200s line %d: Missing port argument.",
1095 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001096
Damien Millera699d952008-11-03 19:27:34 +11001097 if (opcode == oLocalForward ||
1098 opcode == oRemoteForward) {
1099 arg2 = strdelim(&s);
1100 if (arg2 == NULL || *arg2 == '\0')
1101 fatal("%.200s line %d: Missing target argument.",
1102 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001103
Damien Millera699d952008-11-03 19:27:34 +11001104 /* construct a string for parse_forward */
1105 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1106 } else if (opcode == oDynamicForward) {
1107 strlcpy(fwdarg, arg, sizeof(fwdarg));
1108 }
1109
1110 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001111 opcode == oDynamicForward ? 1 : 0,
1112 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001113 fatal("%.200s line %d: Bad forwarding specification.",
1114 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001115
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001116 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001117 if (opcode == oLocalForward ||
1118 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001119 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001120 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001121 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001122 }
Damien Miller95def091999-11-25 00:26:21 +11001123 break;
1124
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001125 case oClearAllForwardings:
1126 intptr = &options->clear_forwardings;
1127 goto parse_flag;
1128
Damien Miller95def091999-11-25 00:26:21 +11001129 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001130 if (cmdline)
1131 fatal("Host directive not supported as a command-line "
1132 "option");
Damien Miller95def091999-11-25 00:26:21 +11001133 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001134 arg2 = NULL;
1135 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1136 negated = *arg == '!';
1137 if (negated)
1138 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001139 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001140 if (negated) {
1141 debug("%.200s line %d: Skipping Host "
1142 "block because of negated match "
1143 "for %.100s", filename, linenum,
1144 arg);
1145 *activep = 0;
1146 break;
1147 }
1148 if (!*activep)
1149 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001150 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001151 }
Damien Millerfe924212011-05-15 08:44:45 +10001152 }
1153 if (*activep)
1154 debug("%.200s line %d: Applying options for %.100s",
1155 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001156 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001157 return 0;
1158
Damien Miller194fd902013-10-15 12:13:05 +11001159 case oMatch:
1160 if (cmdline)
1161 fatal("Host directive not supported as a command-line "
1162 "option");
1163 value = match_cfg_line(options, &s, pw, host,
1164 filename, linenum);
1165 if (value < 0)
1166 fatal("%.200s line %d: Bad Match condition", filename,
1167 linenum);
1168 *activep = value;
1169 break;
1170
Damien Miller95def091999-11-25 00:26:21 +11001171 case oEscapeChar:
1172 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001173 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001174 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001175 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001176 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001177 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1178 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001179 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001180 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001181 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001182 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001183 else {
1184 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001186 /* NOTREACHED */
1187 value = 0; /* Avoid compiler warning. */
1188 }
1189 if (*activep && *intptr == -1)
1190 *intptr = value;
1191 break;
1192
Damien Miller20a8f972003-05-18 20:50:30 +10001193 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001194 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001195 multistate_ptr = multistate_addressfamily;
1196 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001197
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001198 case oEnableSSHKeysign:
1199 intptr = &options->enable_ssh_keysign;
1200 goto parse_flag;
1201
Damien Millerbd394c32004-03-08 23:12:36 +11001202 case oIdentitiesOnly:
1203 intptr = &options->identities_only;
1204 goto parse_flag;
1205
Damien Miller509b0102003-12-17 16:33:10 +11001206 case oServerAliveInterval:
1207 intptr = &options->server_alive_interval;
1208 goto parse_time;
1209
1210 case oServerAliveCountMax:
1211 intptr = &options->server_alive_count_max;
1212 goto parse_int;
1213
Darren Tucker46bc0752004-05-02 22:11:30 +10001214 case oSendEnv:
1215 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1216 if (strchr(arg, '=') != NULL)
1217 fatal("%s line %d: Invalid environment name.",
1218 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001219 if (!*activep)
1220 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001221 if (options->num_send_env >= MAX_SEND_ENV)
1222 fatal("%s line %d: too many send env.",
1223 filename, linenum);
1224 options->send_env[options->num_send_env++] =
1225 xstrdup(arg);
1226 }
1227 break;
1228
Damien Miller0e220db2004-06-15 10:34:08 +10001229 case oControlPath:
1230 charptr = &options->control_path;
1231 goto parse_string;
1232
1233 case oControlMaster:
1234 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001235 multistate_ptr = multistate_controlmaster;
1236 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001237
Damien Millere11e1ea2010-08-03 16:04:46 +10001238 case oControlPersist:
1239 /* no/false/yes/true, or a time spec */
1240 intptr = &options->control_persist;
1241 arg = strdelim(&s);
1242 if (!arg || *arg == '\0')
1243 fatal("%.200s line %d: Missing ControlPersist"
1244 " argument.", filename, linenum);
1245 value = 0;
1246 value2 = 0; /* timeout */
1247 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1248 value = 0;
1249 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1250 value = 1;
1251 else if ((value2 = convtime(arg)) >= 0)
1252 value = 1;
1253 else
1254 fatal("%.200s line %d: Bad ControlPersist argument.",
1255 filename, linenum);
1256 if (*activep && *intptr == -1) {
1257 *intptr = value;
1258 options->control_persist_timeout = value2;
1259 }
1260 break;
1261
Damien Millere1776152005-03-01 21:47:37 +11001262 case oHashKnownHosts:
1263 intptr = &options->hash_known_hosts;
1264 goto parse_flag;
1265
Damien Millerd27b9472005-12-13 19:29:02 +11001266 case oTunnel:
1267 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001268 multistate_ptr = multistate_tunnel;
1269 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001270
1271 case oTunnelDevice:
1272 arg = strdelim(&s);
1273 if (!arg || *arg == '\0')
1274 fatal("%.200s line %d: Missing argument.", filename, linenum);
1275 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001276 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001277 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1278 if (*activep) {
1279 options->tun_local = value;
1280 options->tun_remote = value2;
1281 }
1282 break;
1283
1284 case oLocalCommand:
1285 charptr = &options->local_command;
1286 goto parse_command;
1287
1288 case oPermitLocalCommand:
1289 intptr = &options->permit_local_command;
1290 goto parse_flag;
1291
Damien Miller10288242008-06-30 00:04:03 +10001292 case oVisualHostKey:
1293 intptr = &options->visual_host_key;
1294 goto parse_flag;
1295
Damien Miller0dac6fb2010-11-20 15:19:38 +11001296 case oIPQoS:
1297 arg = strdelim(&s);
1298 if ((value = parse_ipqos(arg)) == -1)
1299 fatal("%s line %d: Bad IPQoS value: %s",
1300 filename, linenum, arg);
1301 arg = strdelim(&s);
1302 if (arg == NULL)
1303 value2 = value;
1304 else if ((value2 = parse_ipqos(arg)) == -1)
1305 fatal("%s line %d: Bad IPQoS value: %s",
1306 filename, linenum, arg);
1307 if (*activep) {
1308 options->ip_qos_interactive = value;
1309 options->ip_qos_bulk = value2;
1310 }
1311 break;
1312
Darren Tucker71e4d542009-07-06 07:12:27 +10001313 case oUseRoaming:
1314 intptr = &options->use_roaming;
1315 goto parse_flag;
1316
Damien Miller21771e22011-05-15 08:45:50 +10001317 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001318 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001319 multistate_ptr = multistate_requesttty;
1320 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001321
Darren Tucker07636982013-05-16 20:30:03 +10001322 case oIgnoreUnknown:
1323 charptr = &options->ignored_unknown;
1324 goto parse_string;
1325
Damien Miller1262b662013-08-21 02:44:24 +10001326 case oProxyUseFdpass:
1327 intptr = &options->proxy_use_fdpass;
1328 goto parse_flag;
1329
Damien Miller0faf7472013-10-17 11:47:23 +11001330 case oCanonicalDomains:
1331 value = options->num_canonical_domains != 0;
1332 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1333 valid_domain(arg, filename, linenum);
1334 if (!*activep || value)
1335 continue;
1336 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1337 fatal("%s line %d: too many hostname suffixes.",
1338 filename, linenum);
1339 options->canonical_domains[
1340 options->num_canonical_domains++] = xstrdup(arg);
1341 }
1342 break;
1343
Damien Miller38505592013-10-17 11:48:13 +11001344 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001345 value = options->num_permitted_cnames != 0;
1346 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1347 /* Either '*' for everything or 'list:list' */
1348 if (strcmp(arg, "*") == 0)
1349 arg2 = arg;
1350 else {
1351 lowercase(arg);
1352 if ((arg2 = strchr(arg, ':')) == NULL ||
1353 arg2[1] == '\0') {
1354 fatal("%s line %d: "
1355 "Invalid permitted CNAME \"%s\"",
1356 filename, linenum, arg);
1357 }
1358 *arg2 = '\0';
1359 arg2++;
1360 }
1361 if (!*activep || value)
1362 continue;
1363 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1364 fatal("%s line %d: too many permitted CNAMEs.",
1365 filename, linenum);
1366 cname = options->permitted_cnames +
1367 options->num_permitted_cnames++;
1368 cname->source_list = xstrdup(arg);
1369 cname->target_list = xstrdup(arg2);
1370 }
1371 break;
1372
Damien Miller38505592013-10-17 11:48:13 +11001373 case oCanonicalizeHostname:
1374 intptr = &options->canonicalize_hostname;
1375 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001376 goto parse_multistate;
1377
Damien Miller38505592013-10-17 11:48:13 +11001378 case oCanonicalizeMaxDots:
1379 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001380 goto parse_int;
1381
Damien Miller38505592013-10-17 11:48:13 +11001382 case oCanonicalizeFallbackLocal:
1383 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001384 goto parse_flag;
1385
Ben Lindstrom4daea862002-06-09 20:04:02 +00001386 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001387 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001388 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001389 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001390
Damien Millerf9b3feb2003-05-16 11:38:32 +10001391 case oUnsupported:
1392 error("%s line %d: Unsupported option \"%s\"",
1393 filename, linenum, keyword);
1394 return 0;
1395
Damien Miller95def091999-11-25 00:26:21 +11001396 default:
1397 fatal("process_config_line: Unimplemented opcode %d", opcode);
1398 }
1399
1400 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001401 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001402 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001403 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001404 }
Damien Miller95def091999-11-25 00:26:21 +11001405 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406}
1407
1408
Damien Miller5428f641999-11-25 11:54:57 +11001409/*
1410 * Reads the config file and modifies the options accordingly. Options
1411 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001412 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001413 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001415int
Damien Miller194fd902013-10-15 12:13:05 +11001416read_config_file(const char *filename, struct passwd *pw, const char *host,
1417 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418{
Damien Miller95def091999-11-25 00:26:21 +11001419 FILE *f;
1420 char line[1024];
1421 int active, linenum;
1422 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Damien Miller57a44762004-04-20 20:11:57 +10001424 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001425 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426
Darren Tuckeraefa3682013-04-05 11:18:35 +11001427 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001428 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001429
Damien Miller33793852004-06-15 10:27:55 +10001430 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001431 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001432 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001433 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001434 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001435 }
1436
Damien Miller95def091999-11-25 00:26:21 +11001437 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller5428f641999-11-25 11:54:57 +11001439 /*
1440 * Mark that we are now processing the options. This flag is turned
1441 * on/off by Host specifications.
1442 */
Damien Miller95def091999-11-25 00:26:21 +11001443 active = 1;
1444 linenum = 0;
1445 while (fgets(line, sizeof(line), f)) {
1446 /* Update line number counter. */
1447 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001448 if (process_config_line(options, pw, host, line, filename,
1449 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001450 bad_options++;
1451 }
1452 fclose(f);
1453 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001454 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001455 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001456 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457}
1458
Damien Miller5428f641999-11-25 11:54:57 +11001459/*
1460 * Initializes options to special values that indicate that they have not yet
1461 * been set. Read_config_file will only set options with this value. Options
1462 * are processed in the following order: command line, user config file,
1463 * system config file. Last, fill_default_options is called.
1464 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465
Damien Miller4af51302000-04-16 11:18:38 +10001466void
Damien Miller95def091999-11-25 00:26:21 +11001467initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468{
Damien Miller95def091999-11-25 00:26:21 +11001469 memset(options, 'X', sizeof(*options));
1470 options->forward_agent = -1;
1471 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001472 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001473 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001474 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001475 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001476 options->gateway_ports = -1;
1477 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001478 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001479 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001480 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001481 options->gss_authentication = -1;
1482 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001483 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001484 options->kbd_interactive_authentication = -1;
1485 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001486 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001487 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001488 options->batch_mode = -1;
1489 options->check_host_ip = -1;
1490 options->strict_host_key_checking = -1;
1491 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001492 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001493 options->compression_level = -1;
1494 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001495 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001496 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001497 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001498 options->number_of_password_prompts = -1;
1499 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001500 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001501 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001502 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001503 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001504 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001505 options->num_identity_files = 0;
1506 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001507 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001508 options->proxy_command = NULL;
1509 options->user = NULL;
1510 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001511 options->num_system_hostfiles = 0;
1512 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001513 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001514 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001515 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001516 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001517 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001518 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001519 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001520 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001521 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001522 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001523 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001524 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001525 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001526 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001527 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001528 options->server_alive_interval = -1;
1529 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001530 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001531 options->control_path = NULL;
1532 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001533 options->control_persist = -1;
1534 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001535 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001536 options->tun_open = -1;
1537 options->tun_local = -1;
1538 options->tun_remote = -1;
1539 options->local_command = NULL;
1540 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001541 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001542 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001543 options->ip_qos_interactive = -1;
1544 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001545 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001546 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001547 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001548 options->num_canonical_domains = 0;
1549 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001550 options->canonicalize_max_dots = -1;
1551 options->canonicalize_fallback_local = -1;
1552 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553}
1554
Damien Miller5428f641999-11-25 11:54:57 +11001555/*
1556 * Called after processing other sources of option data, this fills those
1557 * options for which no value has been specified with their default values.
1558 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001559
Damien Miller4af51302000-04-16 11:18:38 +10001560void
Damien Miller95def091999-11-25 00:26:21 +11001561fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562{
Damien Miller95def091999-11-25 00:26:21 +11001563 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001564 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001565 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001566 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001567 if (options->forward_x11_trusted == -1)
1568 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001569 if (options->forward_x11_timeout == -1)
1570 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001571 if (options->exit_on_forward_failure == -1)
1572 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001573 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001574 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001575 if (options->gateway_ports == -1)
1576 options->gateway_ports = 0;
1577 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001578 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001579 if (options->rsa_authentication == -1)
1580 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001581 if (options->pubkey_authentication == -1)
1582 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001583 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001584 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001585 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001586 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001587 if (options->gss_deleg_creds == -1)
1588 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001589 if (options->password_authentication == -1)
1590 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001591 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001592 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001593 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001594 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001595 if (options->hostbased_authentication == -1)
1596 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001597 if (options->batch_mode == -1)
1598 options->batch_mode = 0;
1599 if (options->check_host_ip == -1)
1600 options->check_host_ip = 1;
1601 if (options->strict_host_key_checking == -1)
1602 options->strict_host_key_checking = 2; /* 2 is default */
1603 if (options->compression == -1)
1604 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001605 if (options->tcp_keep_alive == -1)
1606 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001607 if (options->compression_level == -1)
1608 options->compression_level = 6;
1609 if (options->port == -1)
1610 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001611 if (options->address_family == -1)
1612 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001613 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001614 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001615 if (options->number_of_password_prompts == -1)
1616 options->number_of_password_prompts = 3;
1617 /* Selected in ssh_login(). */
1618 if (options->cipher == -1)
1619 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001620 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001621 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001622 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001623 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001624 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001625 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001626 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001627 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001628 add_identity_file(options, "~/",
1629 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001630 }
1631 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001632 add_identity_file(options, "~/",
1633 _PATH_SSH_CLIENT_ID_RSA, 0);
1634 add_identity_file(options, "~/",
1635 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001636#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001637 add_identity_file(options, "~/",
1638 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001639#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001640 add_identity_file(options, "~/",
1641 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001642 }
Damien Millereba71ba2000-04-29 23:57:08 +10001643 }
Damien Miller95def091999-11-25 00:26:21 +11001644 if (options->escape_char == -1)
1645 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001646 if (options->num_system_hostfiles == 0) {
1647 options->system_hostfiles[options->num_system_hostfiles++] =
1648 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1649 options->system_hostfiles[options->num_system_hostfiles++] =
1650 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1651 }
1652 if (options->num_user_hostfiles == 0) {
1653 options->user_hostfiles[options->num_user_hostfiles++] =
1654 xstrdup(_PATH_SSH_USER_HOSTFILE);
1655 options->user_hostfiles[options->num_user_hostfiles++] =
1656 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1657 }
Damien Millerfcd93202002-02-05 12:26:34 +11001658 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001659 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001660 if (options->clear_forwardings == 1)
1661 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001662 if (options->no_host_authentication_for_localhost == - 1)
1663 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001664 if (options->identities_only == -1)
1665 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001666 if (options->enable_ssh_keysign == -1)
1667 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001668 if (options->rekey_limit == -1)
1669 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001670 if (options->rekey_interval == -1)
1671 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001672 if (options->verify_host_key_dns == -1)
1673 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001674 if (options->server_alive_interval == -1)
1675 options->server_alive_interval = 0;
1676 if (options->server_alive_count_max == -1)
1677 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001678 if (options->control_master == -1)
1679 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001680 if (options->control_persist == -1) {
1681 options->control_persist = 0;
1682 options->control_persist_timeout = 0;
1683 }
Damien Millere1776152005-03-01 21:47:37 +11001684 if (options->hash_known_hosts == -1)
1685 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001686 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001687 options->tun_open = SSH_TUNMODE_NO;
1688 if (options->tun_local == -1)
1689 options->tun_local = SSH_TUNID_ANY;
1690 if (options->tun_remote == -1)
1691 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001692 if (options->permit_local_command == -1)
1693 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001694 if (options->use_roaming == -1)
1695 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001696 if (options->visual_host_key == -1)
1697 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001698 if (options->ip_qos_interactive == -1)
1699 options->ip_qos_interactive = IPTOS_LOWDELAY;
1700 if (options->ip_qos_bulk == -1)
1701 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001702 if (options->request_tty == -1)
1703 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001704 if (options->proxy_use_fdpass == -1)
1705 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001706 if (options->canonicalize_max_dots == -1)
1707 options->canonicalize_max_dots = 1;
1708 if (options->canonicalize_fallback_local == -1)
1709 options->canonicalize_fallback_local = 1;
1710 if (options->canonicalize_hostname == -1)
1711 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001712#define CLEAR_ON_NONE(v) \
1713 do { \
1714 if (v != NULL && strcasecmp(v, "none") == 0) { \
1715 free(v); \
1716 v = NULL; \
1717 } \
1718 } while(0)
1719 CLEAR_ON_NONE(options->local_command);
1720 CLEAR_ON_NONE(options->proxy_command);
1721 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001722 /* options->user will be set in the main program if appropriate */
1723 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001724 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001725 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001727
1728/*
1729 * parse_forward
1730 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001731 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001732 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001733 * dynamicfwd == 1
1734 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001735 * returns number of arguments parsed or zero on error
1736 */
1737int
Damien Miller4bf648f2009-02-14 16:28:21 +11001738parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001739{
1740 int i;
1741 char *p, *cp, *fwdarg[4];
1742
1743 memset(fwd, '\0', sizeof(*fwd));
1744
1745 cp = p = xstrdup(fwdspec);
1746
1747 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001748 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001749 cp++;
1750
1751 for (i = 0; i < 4; ++i)
1752 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1753 break;
1754
Damien Millerf4b39532008-11-03 19:28:21 +11001755 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001756 if (cp != NULL)
1757 i = 0; /* failure */
1758
1759 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001760 case 1:
1761 fwd->listen_host = NULL;
1762 fwd->listen_port = a2port(fwdarg[0]);
1763 fwd->connect_host = xstrdup("socks");
1764 break;
1765
1766 case 2:
1767 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1768 fwd->listen_port = a2port(fwdarg[1]);
1769 fwd->connect_host = xstrdup("socks");
1770 break;
1771
Damien Millerf91ee4c2005-03-01 21:24:33 +11001772 case 3:
1773 fwd->listen_host = NULL;
1774 fwd->listen_port = a2port(fwdarg[0]);
1775 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1776 fwd->connect_port = a2port(fwdarg[2]);
1777 break;
1778
1779 case 4:
1780 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1781 fwd->listen_port = a2port(fwdarg[1]);
1782 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1783 fwd->connect_port = a2port(fwdarg[3]);
1784 break;
1785 default:
1786 i = 0; /* failure */
1787 }
1788
Darren Tuckera627d422013-06-02 07:31:17 +10001789 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001790
Damien Millera699d952008-11-03 19:27:34 +11001791 if (dynamicfwd) {
1792 if (!(i == 1 || i == 2))
1793 goto fail_free;
1794 } else {
1795 if (!(i == 3 || i == 4))
1796 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001797 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001798 goto fail_free;
1799 }
1800
Damien Miller4bf648f2009-02-14 16:28:21 +11001801 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001802 goto fail_free;
1803
1804 if (fwd->connect_host != NULL &&
1805 strlen(fwd->connect_host) >= NI_MAXHOST)
1806 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001807 if (fwd->listen_host != NULL &&
1808 strlen(fwd->listen_host) >= NI_MAXHOST)
1809 goto fail_free;
1810
Damien Millerf91ee4c2005-03-01 21:24:33 +11001811
1812 return (i);
1813
1814 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001815 free(fwd->connect_host);
1816 fwd->connect_host = NULL;
1817 free(fwd->listen_host);
1818 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001819 return (0);
1820}