blob: f7b912ef6480b4bec73b954b4750a7a4f1b3c6e7 [file] [log] [blame]
Damien Miller194fd902013-10-15 12:13:05 +11001/* $OpenBSD: readconf.c,v 1.206 2013/10/14 22:22:02 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>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Damien Millerc7b06362006-03-15 11:53:45 +110026#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100027#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110028#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100029#include <netdb.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <paths.h>
31#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100037#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100038#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100039#endif
Damien Millerc7b06362006-03-15 11:53:45 +110040
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100043#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "cipher.h"
45#include "pathnames.h"
46#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "readconf.h"
49#include "match.h"
50#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100051#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000052#include "kex.h"
53#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110054#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
56/* Format of the configuration file:
57
58 # Configuration data is parsed as follows:
59 # 1. command line options
60 # 2. user-specific file
61 # 3. system-wide file
62 # Any configuration value is only changed the first time it is set.
63 # Thus, host-specific definitions should be at the beginning of the
64 # configuration file, and defaults at the end.
65
66 # Host-specific declarations. These may override anything above. A single
67 # host may match multiple declarations; these are processed in the order
68 # that they are given in.
69
70 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000071 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
73 Host fake.com
74 HostName another.host.name.real.org
75 User blaah
76 Port 34289
77 ForwardX11 no
78 ForwardAgent no
79
80 Host books.com
81 RemoteForward 9999 shadows.cs.hut.fi:9999
82 Cipher 3des
83
84 Host fascist.blob.com
85 Port 23123
86 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087 PasswordAuthentication no
88
89 Host puukko.hut.fi
90 User t35124p
91 ProxyCommand ssh-proxy %h %p
92
93 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000094 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96 Host *.su
97 Cipher none
98 PasswordAuthentication no
99
Damien Millerd27b9472005-12-13 19:29:02 +1100100 Host vpn.fake.com
101 Tunnel yes
102 TunnelDevice 3
103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104 # Defaults for various options
105 Host *
106 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100107 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108 PasswordAuthentication yes
109 RSAAuthentication yes
110 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100112 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 IdentityFile ~/.ssh/identity
114 Port 22
115 EscapeChar ~
116
117*/
118
119/* Keyword tokens. */
120
Damien Miller95def091999-11-25 00:26:21 +1100121typedef enum {
122 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100123 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000124 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
125 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000126 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000127 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100128 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100129 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100130 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
131 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100132 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000133 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100134 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000135 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000136 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100137 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000138 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000139 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000140 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100141 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000142 oSendEnv, oControlPath, oControlMaster, oControlPersist,
143 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100144 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100145 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Miller1262b662013-08-21 02:44:24 +1000146 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Darren Tucker07636982013-05-16 20:30:03 +1000147 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148} OpCodes;
149
150/* Textual representations of the tokens. */
151
Damien Miller95def091999-11-25 00:26:21 +1100152static struct {
153 const char *name;
154 OpCodes opcode;
155} keywords[] = {
156 { "forwardagent", oForwardAgent },
157 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000158 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000159 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000160 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000161 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100162 { "gatewayports", oGatewayPorts },
163 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000164 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100166 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
167 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100168 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000170 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000171 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000172 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000173 { "challengeresponseauthentication", oChallengeResponseAuthentication },
174 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
175 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000176 { "kerberosauthentication", oUnsupported },
177 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000178 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000179#if defined(GSSAPI)
180 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000181 { "gssapidelegatecredentials", oGssDelegateCreds },
182#else
183 { "gssapiauthentication", oUnsupported },
184 { "gssapidelegatecredentials", oUnsupported },
185#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000186 { "fallbacktorsh", oDeprecated },
187 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100188 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100189 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100190 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100191 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000192 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "proxycommand", oProxyCommand },
194 { "port", oPort },
195 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000196 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000197 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000198 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100199 { "remoteforward", oRemoteForward },
200 { "localforward", oLocalForward },
201 { "user", oUser },
202 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100203 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100205 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000206 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100207 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000208 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "connectionattempts", oConnectionAttempts },
210 { "batchmode", oBatchMode },
211 { "checkhostip", oCheckHostIP },
212 { "stricthostkeychecking", oStrictHostKeyChecking },
213 { "compression", oCompression },
214 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100215 { "tcpkeepalive", oTCPKeepAlive },
216 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100217 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100218 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000219 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000220 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000221 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000222 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100223#ifdef ENABLE_PKCS11
224 { "smartcarddevice", oPKCS11Provider },
225 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000226#else
227 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100228 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000229#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100230 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000231 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000232 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100233 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000234 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000235 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000236 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100237 { "serveraliveinterval", oServerAliveInterval },
238 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000239 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000240 { "controlpath", oControlPath },
241 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000242 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100243 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100244 { "tunnel", oTunnel },
245 { "tunneldevice", oTunnelDevice },
246 { "localcommand", oLocalCommand },
247 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000248 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000249 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100250#ifdef JPAKE
251 { "zeroknowledgepasswordauthentication",
252 oZeroKnowledgePasswordAuthentication },
253#else
254 { "zeroknowledgepasswordauthentication", oUnsupported },
255#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000256 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100257 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000258 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000259 { "proxyusefdpass", oProxyUseFdpass },
Darren Tucker07636982013-05-16 20:30:03 +1000260 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100261
Ben Lindstrom65366a82001-12-06 16:32:47 +0000262 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100263};
264
Damien Miller5428f641999-11-25 11:54:57 +1100265/*
266 * Adds a local TCP/IP port forward to options. Never returns if there is an
267 * error.
268 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller4af51302000-04-16 11:18:38 +1000270void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272{
Damien Miller95def091999-11-25 00:26:21 +1100273 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000274#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100275 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100276 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000277 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100278#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000279 options->local_forwards = xrealloc(options->local_forwards,
280 options->num_local_forwards + 1,
281 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100282 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283
Damien Miller1a0442f2008-11-05 16:30:06 +1100284 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100286 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288}
289
Damien Miller5428f641999-11-25 11:54:57 +1100290/*
291 * Adds a remote TCP/IP port forward to options. Never returns if there is
292 * an error.
293 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Damien Miller4af51302000-04-16 11:18:38 +1000295void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297{
Damien Miller95def091999-11-25 00:26:21 +1100298 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000299
300 options->remote_forwards = xrealloc(options->remote_forwards,
301 options->num_remote_forwards + 1,
302 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100303 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304
Damien Miller1a0442f2008-11-05 16:30:06 +1100305 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100306 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100307 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100309 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000310 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000313static void
314clear_forwardings(Options *options)
315{
316 int i;
317
Damien Millerf91ee4c2005-03-01 21:24:33 +1100318 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000319 free(options->local_forwards[i].listen_host);
320 free(options->local_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100321 }
Damien Miller232cfb12010-06-26 09:50:30 +1000322 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000324 options->local_forwards = NULL;
325 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000326 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000328 free(options->remote_forwards[i].listen_host);
329 free(options->remote_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100330 }
Damien Miller232cfb12010-06-26 09:50:30 +1000331 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000333 options->remote_forwards = NULL;
334 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000335 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100336 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000337}
338
Darren Tucker19104782013-04-05 11:13:08 +1100339void
340add_identity_file(Options *options, const char *dir, const char *filename,
341 int userprovided)
342{
343 char *path;
344
345 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
346 fatal("Too many identity files specified (max %d)",
347 SSH_MAX_IDENTITY_FILES);
348
349 if (dir == NULL) /* no dir, filename is absolute */
350 path = xstrdup(filename);
351 else
352 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
353
354 options->identity_file_userprovided[options->num_identity_files] =
355 userprovided;
356 options->identity_files[options->num_identity_files++] = path;
357}
358
Damien Miller194fd902013-10-15 12:13:05 +1100359int
360default_ssh_port(void)
361{
362 static int port;
363 struct servent *sp;
364
365 if (port == 0) {
366 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
367 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
368 }
369 return port;
370}
371
372/*
373 * Execute a command in a shell.
374 * Return its exit status or -1 on abnormal exit.
375 */
376static int
377execute_in_shell(const char *cmd)
378{
379 char *shell, *command_string;
380 pid_t pid;
381 int devnull, status;
382 extern uid_t original_real_uid;
383
384 if ((shell = getenv("SHELL")) == NULL)
385 shell = _PATH_BSHELL;
386
387 /*
388 * Use "exec" to avoid "sh -c" processes on some platforms
389 * (e.g. Solaris)
390 */
391 xasprintf(&command_string, "exec %s", cmd);
392
393 /* Need this to redirect subprocess stdin/out */
394 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
395 fatal("open(/dev/null): %s", strerror(errno));
396
397 debug("Executing command: '%.500s'", cmd);
398
399 /* Fork and execute the command. */
400 if ((pid = fork()) == 0) {
401 char *argv[4];
402
403 /* Child. Permanently give up superuser privileges. */
404 permanently_drop_suid(original_real_uid);
405
406 /* Redirect child stdin and stdout. Leave stderr */
407 if (dup2(devnull, STDIN_FILENO) == -1)
408 fatal("dup2: %s", strerror(errno));
409 if (dup2(devnull, STDOUT_FILENO) == -1)
410 fatal("dup2: %s", strerror(errno));
411 if (devnull > STDERR_FILENO)
412 close(devnull);
413 closefrom(STDERR_FILENO + 1);
414
415 argv[0] = shell;
416 argv[1] = "-c";
417 argv[2] = command_string;
418 argv[3] = NULL;
419
420 execv(argv[0], argv);
421 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
422 /* Die with signal to make this error apparent to parent. */
423 signal(SIGTERM, SIG_DFL);
424 kill(getpid(), SIGTERM);
425 _exit(1);
426 }
427 /* Parent. */
428 if (pid < 0)
429 fatal("%s: fork: %.100s", __func__, strerror(errno));
430
431 close(devnull);
432 free(command_string);
433
434 while (waitpid(pid, &status, 0) == -1) {
435 if (errno != EINTR && errno != EAGAIN)
436 fatal("%s: waitpid: %s", __func__, strerror(errno));
437 }
438 if (!WIFEXITED(status)) {
439 error("command '%.100s' exited abnormally", cmd);
440 return -1;
441 }
442 debug3("command returned status %d", WEXITSTATUS(status));
443 return WEXITSTATUS(status);
444}
445
446/*
447 * Parse and execute a Match directive.
448 */
449static int
450match_cfg_line(Options *options, char **condition, struct passwd *pw,
451 const char *host_arg, const char *filename, int linenum)
452{
453 char *arg, *attrib, *cmd, *cp = *condition;
454 const char *ruser, *host;
455 int r, port, result = 1;
456 size_t len;
457 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
458
459 /*
460 * Configuration is likely to be incomplete at this point so we
461 * must be prepared to use default values.
462 */
463 port = options->port <= 0 ? default_ssh_port() : options->port;
464 ruser = options->user == NULL ? pw->pw_name : options->user;
465 host = options->hostname == NULL ? host_arg : options->hostname;
466
467 debug3("checking match for '%s' host %s", cp, host);
468 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
469 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
470 error("Missing Match criteria for %s", attrib);
471 return -1;
472 }
473 len = strlen(arg);
474 if (strcasecmp(attrib, "host") == 0) {
475 if (match_hostname(host, arg, len) != 1)
476 result = 0;
477 else
478 debug("%.200s line %d: matched 'Host %.100s' ",
479 filename, linenum, host);
480 } else if (strcasecmp(attrib, "originalhost") == 0) {
481 if (match_hostname(host_arg, arg, len) != 1)
482 result = 0;
483 else
484 debug("%.200s line %d: matched "
485 "'OriginalHost %.100s' ",
486 filename, linenum, host_arg);
487 } else if (strcasecmp(attrib, "user") == 0) {
488 if (match_pattern_list(ruser, arg, len, 0) != 1)
489 result = 0;
490 else
491 debug("%.200s line %d: matched 'User %.100s' ",
492 filename, linenum, ruser);
493 } else if (strcasecmp(attrib, "localuser") == 0) {
494 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
495 result = 0;
496 else
497 debug("%.200s line %d: matched "
498 "'LocalUser %.100s' ",
499 filename, linenum, pw->pw_name);
500 } else if (strcasecmp(attrib, "command") == 0) {
501 if (gethostname(thishost, sizeof(thishost)) == -1)
502 fatal("gethostname: %s", strerror(errno));
503 strlcpy(shorthost, thishost, sizeof(shorthost));
504 shorthost[strcspn(thishost, ".")] = '\0';
505 snprintf(portstr, sizeof(portstr), "%d", port);
506
507 cmd = percent_expand(arg,
508 "L", shorthost,
509 "d", pw->pw_dir,
510 "h", host,
511 "l", thishost,
512 "n", host_arg,
513 "p", portstr,
514 "r", ruser,
515 "u", pw->pw_name,
516 (char *)NULL);
517 r = execute_in_shell(cmd);
518 if (r == -1) {
519 fatal("%.200s line %d: match command '%.100s' "
520 "error", filename, linenum, cmd);
521 } else if (r == 0) {
522 debug("%.200s line %d: matched "
523 "'Command \"%.100s\"' ",
524 filename, linenum, cmd);
525 } else
526 result = 0;
527 free(cmd);
528 } else {
529 error("Unsupported Match attribute %s", attrib);
530 return -1;
531 }
532 }
533 debug3("match %sfound", result ? "" : "not ");
534 *condition = cp;
535 return result;
536}
537
Damien Miller5428f641999-11-25 11:54:57 +1100538/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000539 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100540 */
Damien Miller4af51302000-04-16 11:18:38 +1000541static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000542parse_token(const char *cp, const char *filename, int linenum,
543 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544{
Darren Tucker07636982013-05-16 20:30:03 +1000545 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller95def091999-11-25 00:26:21 +1100547 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000548 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100549 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000550 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
551 strlen(ignored_unknown), 1) == 1)
552 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000553 error("%s: line %d: Bad configuration option: %s",
554 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100555 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556}
557
Damien Miller5428f641999-11-25 11:54:57 +1100558/*
559 * Processes a single option line as used in the configuration files. This
560 * only sets those values that have not already been set.
561 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100562#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100563int
Damien Miller194fd902013-10-15 12:13:05 +1100564process_config_line(Options *options, struct passwd *pw, const char *host,
565 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566{
Damien Miller295ee632011-05-29 21:42:31 +1000567 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
568 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000569 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100570 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100571 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000572 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100573 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100574 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller194fd902013-10-15 12:13:05 +1100576 if (activep == NULL) { /* We are processing a command line directive */
577 cmdline = 1;
578 activep = &cmdline;
579 }
580
Damien Millerc652cac2003-05-14 13:40:54 +1000581 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100582 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000583 if (strchr(WHITESPACE, line[len]) == NULL)
584 break;
585 line[len] = '\0';
586 }
587
Damien Millerbe484b52000-07-15 14:14:16 +1000588 s = line;
589 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100590 if ((keyword = strdelim(&s)) == NULL)
591 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000592 /* Ignore leading whitespace. */
593 if (*keyword == '\0')
594 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000595 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100596 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000597 /* Match lowercase keyword */
598 for (i = 0; i < strlen(keyword); i++)
599 keyword[i] = tolower(keyword[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
Darren Tucker07636982013-05-16 20:30:03 +1000601 opcode = parse_token(keyword, filename, linenum,
602 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 switch (opcode) {
605 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100606 /* don't panic, but count bad options */
607 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100608 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000609 case oIgnoredUnknownOption:
610 debug("%s line %d: Ignored unknown option \"%s\"",
611 filename, linenum, keyword);
612 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000613 case oConnectTimeout:
614 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100615parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000616 arg = strdelim(&s);
617 if (!arg || *arg == '\0')
618 fatal("%s line %d: missing time value.",
619 filename, linenum);
620 if ((value = convtime(arg)) == -1)
621 fatal("%s line %d: invalid time value.",
622 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100623 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000624 *intptr = value;
625 break;
626
Damien Miller95def091999-11-25 00:26:21 +1100627 case oForwardAgent:
628 intptr = &options->forward_agent;
629parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000630 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000631 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100632 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
633 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000634 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100635 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000636 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100637 value = 0;
638 else
639 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
640 if (*activep && *intptr == -1)
641 *intptr = value;
642 break;
643
644 case oForwardX11:
645 intptr = &options->forward_x11;
646 goto parse_flag;
647
Darren Tucker0a118da2003-10-15 15:54:32 +1000648 case oForwardX11Trusted:
649 intptr = &options->forward_x11_trusted;
650 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000651
652 case oForwardX11Timeout:
653 intptr = &options->forward_x11_timeout;
654 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656 case oGatewayPorts:
657 intptr = &options->gateway_ports;
658 goto parse_flag;
659
Darren Tuckere7d4b192006-07-12 22:17:10 +1000660 case oExitOnForwardFailure:
661 intptr = &options->exit_on_forward_failure;
662 goto parse_flag;
663
Damien Miller95def091999-11-25 00:26:21 +1100664 case oUsePrivilegedPort:
665 intptr = &options->use_privileged_port;
666 goto parse_flag;
667
Damien Miller95def091999-11-25 00:26:21 +1100668 case oPasswordAuthentication:
669 intptr = &options->password_authentication;
670 goto parse_flag;
671
Damien Miller01ed2272008-11-05 16:20:46 +1100672 case oZeroKnowledgePasswordAuthentication:
673 intptr = &options->zero_knowledge_password_authentication;
674 goto parse_flag;
675
Damien Miller874d77b2000-10-14 16:23:11 +1100676 case oKbdInteractiveAuthentication:
677 intptr = &options->kbd_interactive_authentication;
678 goto parse_flag;
679
680 case oKbdInteractiveDevices:
681 charptr = &options->kbd_interactive_devices;
682 goto parse_string;
683
Damien Miller0bc1bd82000-11-13 22:57:25 +1100684 case oPubkeyAuthentication:
685 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000686 goto parse_flag;
687
Damien Miller95def091999-11-25 00:26:21 +1100688 case oRSAAuthentication:
689 intptr = &options->rsa_authentication;
690 goto parse_flag;
691
692 case oRhostsRSAAuthentication:
693 intptr = &options->rhosts_rsa_authentication;
694 goto parse_flag;
695
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000696 case oHostbasedAuthentication:
697 intptr = &options->hostbased_authentication;
698 goto parse_flag;
699
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000700 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000701 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100702 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000703
Darren Tucker0efd1552003-08-26 11:49:55 +1000704 case oGssAuthentication:
705 intptr = &options->gss_authentication;
706 goto parse_flag;
707
708 case oGssDelegateCreds:
709 intptr = &options->gss_deleg_creds;
710 goto parse_flag;
711
Damien Miller95def091999-11-25 00:26:21 +1100712 case oBatchMode:
713 intptr = &options->batch_mode;
714 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller95def091999-11-25 00:26:21 +1100716 case oCheckHostIP:
717 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000718 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller37876e92003-05-15 10:19:46 +1000720 case oVerifyHostKeyDNS:
721 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100722 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000723
Damien Miller95def091999-11-25 00:26:21 +1100724 case oStrictHostKeyChecking:
725 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100726parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000727 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000728 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000729 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100730 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100731 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000732 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100733 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000734 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100735 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000736 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100737 value = 2;
738 else
739 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
740 if (*activep && *intptr == -1)
741 *intptr = value;
742 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743
Damien Miller95def091999-11-25 00:26:21 +1100744 case oCompression:
745 intptr = &options->compression;
746 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Damien Miller12c150e2003-12-17 16:31:10 +1100748 case oTCPKeepAlive:
749 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100750 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000752 case oNoHostAuthenticationForLocalhost:
753 intptr = &options->no_host_authentication_for_localhost;
754 goto parse_flag;
755
Damien Miller95def091999-11-25 00:26:21 +1100756 case oNumberOfPasswordPrompts:
757 intptr = &options->number_of_password_prompts;
758 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller95def091999-11-25 00:26:21 +1100760 case oCompressionLevel:
761 intptr = &options->compression_level;
762 goto parse_int;
763
Damien Millera5539d22003-04-09 20:50:06 +1000764 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000765 arg = strdelim(&s);
766 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000767 fatal("%.200s line %d: Missing argument.", filename,
768 linenum);
769 if (strcmp(arg, "default") == 0) {
770 val64 = 0;
771 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000772 if (scan_scaled(arg, &val64) == -1)
773 fatal("%.200s line %d: Bad number '%s': %s",
774 filename, linenum, arg, strerror(errno));
775 /* check for too-large or too-small limits */
776 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000777 fatal("%.200s line %d: RekeyLimit too large",
778 filename, linenum);
779 if (val64 != 0 && val64 < 16)
780 fatal("%.200s line %d: RekeyLimit too small",
781 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000782 }
Damien Miller3dff1762008-02-10 22:25:52 +1100783 if (*activep && options->rekey_limit == -1)
784 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000785 if (s != NULL) { /* optional rekey interval present */
786 if (strcmp(s, "none") == 0) {
787 (void)strdelim(&s); /* discard */
788 break;
789 }
790 intptr = &options->rekey_interval;
791 goto parse_time;
792 }
Damien Millera5539d22003-04-09 20:50:06 +1000793 break;
794
Damien Miller95def091999-11-25 00:26:21 +1100795 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000796 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000797 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100798 fatal("%.200s line %d: Missing argument.", filename, linenum);
799 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100800 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000801 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100802 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100803 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100804 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100805 }
806 break;
807
Damien Millerd3a18572000-06-07 19:55:44 +1000808 case oXAuthLocation:
809 charptr=&options->xauth_location;
810 goto parse_string;
811
Damien Miller95def091999-11-25 00:26:21 +1100812 case oUser:
813 charptr = &options->user;
814parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000815 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000816 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000817 fatal("%.200s line %d: Missing argument.",
818 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100819 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000820 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100821 break;
822
823 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000824 cpptr = (char **)&options->system_hostfiles;
825 uintptr = &options->num_system_hostfiles;
826 max_entries = SSH_MAX_HOSTS_FILES;
827parse_char_array:
828 if (*activep && *uintptr == 0) {
829 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
830 if ((*uintptr) >= max_entries)
831 fatal("%s line %d: "
832 "too many authorized keys files.",
833 filename, linenum);
834 cpptr[(*uintptr)++] = xstrdup(arg);
835 }
836 }
837 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100838
839 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000840 cpptr = (char **)&options->user_hostfiles;
841 uintptr = &options->num_user_hostfiles;
842 max_entries = SSH_MAX_HOSTS_FILES;
843 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000844
Damien Miller95def091999-11-25 00:26:21 +1100845 case oHostName:
846 charptr = &options->hostname;
847 goto parse_string;
848
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000849 case oHostKeyAlias:
850 charptr = &options->host_key_alias;
851 goto parse_string;
852
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000853 case oPreferredAuthentications:
854 charptr = &options->preferred_authentications;
855 goto parse_string;
856
Ben Lindstrome0f88042001-04-30 13:06:24 +0000857 case oBindAddress:
858 charptr = &options->bind_address;
859 goto parse_string;
860
Damien Miller7ea845e2010-02-12 09:21:02 +1100861 case oPKCS11Provider:
862 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000863 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000864
Damien Miller95def091999-11-25 00:26:21 +1100865 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100866 charptr = &options->proxy_command;
867parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000868 if (s == NULL)
869 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100870 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100871 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100872 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100873 return 0;
874
875 case oPort:
876 intptr = &options->port;
877parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000878 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000879 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100880 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000881 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100882 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100883
884 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000885 value = strtol(arg, &endofnumber, 0);
886 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100887 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100888 if (*activep && *intptr == -1)
889 *intptr = value;
890 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller95def091999-11-25 00:26:21 +1100892 case oConnectionAttempts:
893 intptr = &options->connection_attempts;
894 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100895
Damien Miller95def091999-11-25 00:26:21 +1100896 case oCipher:
897 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000898 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000899 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000900 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000901 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100902 if (value == -1)
903 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100904 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100905 if (*activep && *intptr == -1)
906 *intptr = value;
907 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908
Damien Miller78928792000-04-12 20:17:38 +1000909 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000910 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000911 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000912 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000913 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000914 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100915 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000916 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000917 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000918 break;
919
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000920 case oMacs:
921 arg = strdelim(&s);
922 if (!arg || *arg == '\0')
923 fatal("%.200s line %d: Missing argument.", filename, linenum);
924 if (!mac_valid(arg))
925 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100926 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000927 if (*activep && options->macs == NULL)
928 options->macs = xstrdup(arg);
929 break;
930
Damien Millerd5f62bf2010-09-24 22:11:14 +1000931 case oKexAlgorithms:
932 arg = strdelim(&s);
933 if (!arg || *arg == '\0')
934 fatal("%.200s line %d: Missing argument.",
935 filename, linenum);
936 if (!kex_names_valid(arg))
937 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
938 filename, linenum, arg ? arg : "<NONE>");
939 if (*activep && options->kex_algorithms == NULL)
940 options->kex_algorithms = xstrdup(arg);
941 break;
942
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000943 case oHostKeyAlgorithms:
944 arg = strdelim(&s);
945 if (!arg || *arg == '\0')
946 fatal("%.200s line %d: Missing argument.", filename, linenum);
947 if (!key_names_valid2(arg))
948 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100949 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000950 if (*activep && options->hostkeyalgorithms == NULL)
951 options->hostkeyalgorithms = xstrdup(arg);
952 break;
953
Damien Miller78928792000-04-12 20:17:38 +1000954 case oProtocol:
955 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000956 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000957 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000958 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000959 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000960 if (value == SSH_PROTO_UNKNOWN)
961 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100962 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000963 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
964 *intptr = value;
965 break;
966
Damien Miller95def091999-11-25 00:26:21 +1100967 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100968 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000969 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000970 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100971 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000972 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100973 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100974 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
975 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100976 break;
977
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000978 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100979 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100980 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000981 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100982 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000983 fatal("%.200s line %d: Missing port argument.",
984 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100985
Damien Millera699d952008-11-03 19:27:34 +1100986 if (opcode == oLocalForward ||
987 opcode == oRemoteForward) {
988 arg2 = strdelim(&s);
989 if (arg2 == NULL || *arg2 == '\0')
990 fatal("%.200s line %d: Missing target argument.",
991 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100992
Damien Millera699d952008-11-03 19:27:34 +1100993 /* construct a string for parse_forward */
994 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
995 } else if (opcode == oDynamicForward) {
996 strlcpy(fwdarg, arg, sizeof(fwdarg));
997 }
998
999 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001000 opcode == oDynamicForward ? 1 : 0,
1001 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001002 fatal("%.200s line %d: Bad forwarding specification.",
1003 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001004
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001005 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001006 if (opcode == oLocalForward ||
1007 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001008 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001009 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001010 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001011 }
Damien Miller95def091999-11-25 00:26:21 +11001012 break;
1013
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001014 case oClearAllForwardings:
1015 intptr = &options->clear_forwardings;
1016 goto parse_flag;
1017
Damien Miller95def091999-11-25 00:26:21 +11001018 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001019 if (cmdline)
1020 fatal("Host directive not supported as a command-line "
1021 "option");
Damien Miller95def091999-11-25 00:26:21 +11001022 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001023 arg2 = NULL;
1024 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1025 negated = *arg == '!';
1026 if (negated)
1027 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001028 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001029 if (negated) {
1030 debug("%.200s line %d: Skipping Host "
1031 "block because of negated match "
1032 "for %.100s", filename, linenum,
1033 arg);
1034 *activep = 0;
1035 break;
1036 }
1037 if (!*activep)
1038 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001039 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001040 }
Damien Millerfe924212011-05-15 08:44:45 +10001041 }
1042 if (*activep)
1043 debug("%.200s line %d: Applying options for %.100s",
1044 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001045 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001046 return 0;
1047
Damien Miller194fd902013-10-15 12:13:05 +11001048 case oMatch:
1049 if (cmdline)
1050 fatal("Host directive not supported as a command-line "
1051 "option");
1052 value = match_cfg_line(options, &s, pw, host,
1053 filename, linenum);
1054 if (value < 0)
1055 fatal("%.200s line %d: Bad Match condition", filename,
1056 linenum);
1057 *activep = value;
1058 break;
1059
Damien Miller95def091999-11-25 00:26:21 +11001060 case oEscapeChar:
1061 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001062 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001063 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001064 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001065 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001066 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1067 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001068 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001069 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001070 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001071 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001072 else {
1073 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001074 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001075 /* NOTREACHED */
1076 value = 0; /* Avoid compiler warning. */
1077 }
1078 if (*activep && *intptr == -1)
1079 *intptr = value;
1080 break;
1081
Damien Miller20a8f972003-05-18 20:50:30 +10001082 case oAddressFamily:
1083 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +10001084 if (!arg || *arg == '\0')
1085 fatal("%s line %d: missing address family.",
1086 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001087 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +10001088 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001089 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +10001090 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001091 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +10001092 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001093 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +10001094 else
1095 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001096 if (*activep && *intptr == -1)
1097 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +10001098 break;
1099
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001100 case oEnableSSHKeysign:
1101 intptr = &options->enable_ssh_keysign;
1102 goto parse_flag;
1103
Damien Millerbd394c32004-03-08 23:12:36 +11001104 case oIdentitiesOnly:
1105 intptr = &options->identities_only;
1106 goto parse_flag;
1107
Damien Miller509b0102003-12-17 16:33:10 +11001108 case oServerAliveInterval:
1109 intptr = &options->server_alive_interval;
1110 goto parse_time;
1111
1112 case oServerAliveCountMax:
1113 intptr = &options->server_alive_count_max;
1114 goto parse_int;
1115
Darren Tucker46bc0752004-05-02 22:11:30 +10001116 case oSendEnv:
1117 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1118 if (strchr(arg, '=') != NULL)
1119 fatal("%s line %d: Invalid environment name.",
1120 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001121 if (!*activep)
1122 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001123 if (options->num_send_env >= MAX_SEND_ENV)
1124 fatal("%s line %d: too many send env.",
1125 filename, linenum);
1126 options->send_env[options->num_send_env++] =
1127 xstrdup(arg);
1128 }
1129 break;
1130
Damien Miller0e220db2004-06-15 10:34:08 +10001131 case oControlPath:
1132 charptr = &options->control_path;
1133 goto parse_string;
1134
1135 case oControlMaster:
1136 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +10001137 arg = strdelim(&s);
1138 if (!arg || *arg == '\0')
1139 fatal("%.200s line %d: Missing ControlMaster argument.",
1140 filename, linenum);
1141 value = 0; /* To avoid compiler warning... */
1142 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1143 value = SSHCTL_MASTER_YES;
1144 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1145 value = SSHCTL_MASTER_NO;
1146 else if (strcmp(arg, "auto") == 0)
1147 value = SSHCTL_MASTER_AUTO;
1148 else if (strcmp(arg, "ask") == 0)
1149 value = SSHCTL_MASTER_ASK;
1150 else if (strcmp(arg, "autoask") == 0)
1151 value = SSHCTL_MASTER_AUTO_ASK;
1152 else
1153 fatal("%.200s line %d: Bad ControlMaster argument.",
1154 filename, linenum);
1155 if (*activep && *intptr == -1)
1156 *intptr = value;
1157 break;
Damien Miller0e220db2004-06-15 10:34:08 +10001158
Damien Millere11e1ea2010-08-03 16:04:46 +10001159 case oControlPersist:
1160 /* no/false/yes/true, or a time spec */
1161 intptr = &options->control_persist;
1162 arg = strdelim(&s);
1163 if (!arg || *arg == '\0')
1164 fatal("%.200s line %d: Missing ControlPersist"
1165 " argument.", filename, linenum);
1166 value = 0;
1167 value2 = 0; /* timeout */
1168 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1169 value = 0;
1170 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1171 value = 1;
1172 else if ((value2 = convtime(arg)) >= 0)
1173 value = 1;
1174 else
1175 fatal("%.200s line %d: Bad ControlPersist argument.",
1176 filename, linenum);
1177 if (*activep && *intptr == -1) {
1178 *intptr = value;
1179 options->control_persist_timeout = value2;
1180 }
1181 break;
1182
Damien Millere1776152005-03-01 21:47:37 +11001183 case oHashKnownHosts:
1184 intptr = &options->hash_known_hosts;
1185 goto parse_flag;
1186
Damien Millerd27b9472005-12-13 19:29:02 +11001187 case oTunnel:
1188 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +11001189 arg = strdelim(&s);
1190 if (!arg || *arg == '\0')
1191 fatal("%s line %d: Missing yes/point-to-point/"
1192 "ethernet/no argument.", filename, linenum);
1193 value = 0; /* silence compiler */
1194 if (strcasecmp(arg, "ethernet") == 0)
1195 value = SSH_TUNMODE_ETHERNET;
1196 else if (strcasecmp(arg, "point-to-point") == 0)
1197 value = SSH_TUNMODE_POINTOPOINT;
1198 else if (strcasecmp(arg, "yes") == 0)
1199 value = SSH_TUNMODE_DEFAULT;
1200 else if (strcasecmp(arg, "no") == 0)
1201 value = SSH_TUNMODE_NO;
1202 else
1203 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1204 "no argument: %s", filename, linenum, arg);
1205 if (*activep)
1206 *intptr = value;
1207 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001208
1209 case oTunnelDevice:
1210 arg = strdelim(&s);
1211 if (!arg || *arg == '\0')
1212 fatal("%.200s line %d: Missing argument.", filename, linenum);
1213 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001214 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001215 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1216 if (*activep) {
1217 options->tun_local = value;
1218 options->tun_remote = value2;
1219 }
1220 break;
1221
1222 case oLocalCommand:
1223 charptr = &options->local_command;
1224 goto parse_command;
1225
1226 case oPermitLocalCommand:
1227 intptr = &options->permit_local_command;
1228 goto parse_flag;
1229
Damien Miller10288242008-06-30 00:04:03 +10001230 case oVisualHostKey:
1231 intptr = &options->visual_host_key;
1232 goto parse_flag;
1233
Damien Miller0dac6fb2010-11-20 15:19:38 +11001234 case oIPQoS:
1235 arg = strdelim(&s);
1236 if ((value = parse_ipqos(arg)) == -1)
1237 fatal("%s line %d: Bad IPQoS value: %s",
1238 filename, linenum, arg);
1239 arg = strdelim(&s);
1240 if (arg == NULL)
1241 value2 = value;
1242 else if ((value2 = parse_ipqos(arg)) == -1)
1243 fatal("%s line %d: Bad IPQoS value: %s",
1244 filename, linenum, arg);
1245 if (*activep) {
1246 options->ip_qos_interactive = value;
1247 options->ip_qos_bulk = value2;
1248 }
1249 break;
1250
Darren Tucker71e4d542009-07-06 07:12:27 +10001251 case oUseRoaming:
1252 intptr = &options->use_roaming;
1253 goto parse_flag;
1254
Damien Miller21771e22011-05-15 08:45:50 +10001255 case oRequestTTY:
1256 arg = strdelim(&s);
1257 if (!arg || *arg == '\0')
1258 fatal("%s line %d: missing argument.",
1259 filename, linenum);
1260 intptr = &options->request_tty;
1261 if (strcasecmp(arg, "yes") == 0)
1262 value = REQUEST_TTY_YES;
1263 else if (strcasecmp(arg, "no") == 0)
1264 value = REQUEST_TTY_NO;
1265 else if (strcasecmp(arg, "force") == 0)
1266 value = REQUEST_TTY_FORCE;
1267 else if (strcasecmp(arg, "auto") == 0)
1268 value = REQUEST_TTY_AUTO;
1269 else
1270 fatal("Unsupported RequestTTY \"%s\"", arg);
1271 if (*activep && *intptr == -1)
1272 *intptr = value;
1273 break;
1274
Darren Tucker07636982013-05-16 20:30:03 +10001275 case oIgnoreUnknown:
1276 charptr = &options->ignored_unknown;
1277 goto parse_string;
1278
Damien Miller1262b662013-08-21 02:44:24 +10001279 case oProxyUseFdpass:
1280 intptr = &options->proxy_use_fdpass;
1281 goto parse_flag;
1282
Ben Lindstrom4daea862002-06-09 20:04:02 +00001283 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001284 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001285 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001286 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001287
Damien Millerf9b3feb2003-05-16 11:38:32 +10001288 case oUnsupported:
1289 error("%s line %d: Unsupported option \"%s\"",
1290 filename, linenum, keyword);
1291 return 0;
1292
Damien Miller95def091999-11-25 00:26:21 +11001293 default:
1294 fatal("process_config_line: Unimplemented opcode %d", opcode);
1295 }
1296
1297 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001298 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001299 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001300 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001301 }
Damien Miller95def091999-11-25 00:26:21 +11001302 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303}
1304
1305
Damien Miller5428f641999-11-25 11:54:57 +11001306/*
1307 * Reads the config file and modifies the options accordingly. Options
1308 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001309 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001310 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001312int
Damien Miller194fd902013-10-15 12:13:05 +11001313read_config_file(const char *filename, struct passwd *pw, const char *host,
1314 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315{
Damien Miller95def091999-11-25 00:26:21 +11001316 FILE *f;
1317 char line[1024];
1318 int active, linenum;
1319 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
Damien Miller57a44762004-04-20 20:11:57 +10001321 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001322 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323
Darren Tuckeraefa3682013-04-05 11:18:35 +11001324 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001325 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001326
Damien Miller33793852004-06-15 10:27:55 +10001327 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001328 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001329 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001330 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001331 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001332 }
1333
Damien Miller95def091999-11-25 00:26:21 +11001334 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller5428f641999-11-25 11:54:57 +11001336 /*
1337 * Mark that we are now processing the options. This flag is turned
1338 * on/off by Host specifications.
1339 */
Damien Miller95def091999-11-25 00:26:21 +11001340 active = 1;
1341 linenum = 0;
1342 while (fgets(line, sizeof(line), f)) {
1343 /* Update line number counter. */
1344 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001345 if (process_config_line(options, pw, host, line, filename,
1346 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001347 bad_options++;
1348 }
1349 fclose(f);
1350 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001351 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001352 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001353 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354}
1355
Damien Miller5428f641999-11-25 11:54:57 +11001356/*
1357 * Initializes options to special values that indicate that they have not yet
1358 * been set. Read_config_file will only set options with this value. Options
1359 * are processed in the following order: command line, user config file,
1360 * system config file. Last, fill_default_options is called.
1361 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
Damien Miller4af51302000-04-16 11:18:38 +10001363void
Damien Miller95def091999-11-25 00:26:21 +11001364initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365{
Damien Miller95def091999-11-25 00:26:21 +11001366 memset(options, 'X', sizeof(*options));
1367 options->forward_agent = -1;
1368 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001369 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001370 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001371 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001372 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001373 options->gateway_ports = -1;
1374 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001375 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001376 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001377 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001378 options->gss_authentication = -1;
1379 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001380 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001381 options->kbd_interactive_authentication = -1;
1382 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001383 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001384 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001385 options->batch_mode = -1;
1386 options->check_host_ip = -1;
1387 options->strict_host_key_checking = -1;
1388 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001389 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001390 options->compression_level = -1;
1391 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001392 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001393 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001394 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001395 options->number_of_password_prompts = -1;
1396 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001397 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001398 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001399 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001400 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001401 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001402 options->num_identity_files = 0;
1403 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001404 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001405 options->proxy_command = NULL;
1406 options->user = NULL;
1407 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001408 options->num_system_hostfiles = 0;
1409 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001410 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001411 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001412 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001413 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001414 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001415 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001416 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001417 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001418 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001419 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001420 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001421 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001422 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001423 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001424 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001425 options->server_alive_interval = -1;
1426 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001427 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001428 options->control_path = NULL;
1429 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001430 options->control_persist = -1;
1431 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001432 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001433 options->tun_open = -1;
1434 options->tun_local = -1;
1435 options->tun_remote = -1;
1436 options->local_command = NULL;
1437 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001438 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001439 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001440 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001441 options->ip_qos_interactive = -1;
1442 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001443 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001444 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001445 options->ignored_unknown = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
Damien Miller5428f641999-11-25 11:54:57 +11001448/*
1449 * Called after processing other sources of option data, this fills those
1450 * options for which no value has been specified with their default values.
1451 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452
Damien Miller4af51302000-04-16 11:18:38 +10001453void
Damien Miller95def091999-11-25 00:26:21 +11001454fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455{
Damien Miller95def091999-11-25 00:26:21 +11001456 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001457 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001458 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001459 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001460 if (options->forward_x11_trusted == -1)
1461 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001462 if (options->forward_x11_timeout == -1)
1463 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001464 if (options->exit_on_forward_failure == -1)
1465 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001466 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001467 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001468 if (options->gateway_ports == -1)
1469 options->gateway_ports = 0;
1470 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001471 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001472 if (options->rsa_authentication == -1)
1473 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001474 if (options->pubkey_authentication == -1)
1475 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001476 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001477 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001478 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001479 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001480 if (options->gss_deleg_creds == -1)
1481 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001482 if (options->password_authentication == -1)
1483 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001484 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001485 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001486 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001487 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001488 if (options->hostbased_authentication == -1)
1489 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001490 if (options->batch_mode == -1)
1491 options->batch_mode = 0;
1492 if (options->check_host_ip == -1)
1493 options->check_host_ip = 1;
1494 if (options->strict_host_key_checking == -1)
1495 options->strict_host_key_checking = 2; /* 2 is default */
1496 if (options->compression == -1)
1497 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001498 if (options->tcp_keep_alive == -1)
1499 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001500 if (options->compression_level == -1)
1501 options->compression_level = 6;
1502 if (options->port == -1)
1503 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001504 if (options->address_family == -1)
1505 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001506 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001507 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001508 if (options->number_of_password_prompts == -1)
1509 options->number_of_password_prompts = 3;
1510 /* Selected in ssh_login(). */
1511 if (options->cipher == -1)
1512 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001513 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001514 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001515 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001516 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001517 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001518 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001519 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001520 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001521 add_identity_file(options, "~/",
1522 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001523 }
1524 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001525 add_identity_file(options, "~/",
1526 _PATH_SSH_CLIENT_ID_RSA, 0);
1527 add_identity_file(options, "~/",
1528 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001529#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001530 add_identity_file(options, "~/",
1531 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001532#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001533 }
Damien Millereba71ba2000-04-29 23:57:08 +10001534 }
Damien Miller95def091999-11-25 00:26:21 +11001535 if (options->escape_char == -1)
1536 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001537 if (options->num_system_hostfiles == 0) {
1538 options->system_hostfiles[options->num_system_hostfiles++] =
1539 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1540 options->system_hostfiles[options->num_system_hostfiles++] =
1541 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1542 }
1543 if (options->num_user_hostfiles == 0) {
1544 options->user_hostfiles[options->num_user_hostfiles++] =
1545 xstrdup(_PATH_SSH_USER_HOSTFILE);
1546 options->user_hostfiles[options->num_user_hostfiles++] =
1547 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1548 }
Damien Millerfcd93202002-02-05 12:26:34 +11001549 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001550 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001551 if (options->clear_forwardings == 1)
1552 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001553 if (options->no_host_authentication_for_localhost == - 1)
1554 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001555 if (options->identities_only == -1)
1556 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001557 if (options->enable_ssh_keysign == -1)
1558 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001559 if (options->rekey_limit == -1)
1560 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001561 if (options->rekey_interval == -1)
1562 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001563 if (options->verify_host_key_dns == -1)
1564 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001565 if (options->server_alive_interval == -1)
1566 options->server_alive_interval = 0;
1567 if (options->server_alive_count_max == -1)
1568 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001569 if (options->control_master == -1)
1570 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001571 if (options->control_persist == -1) {
1572 options->control_persist = 0;
1573 options->control_persist_timeout = 0;
1574 }
Damien Millere1776152005-03-01 21:47:37 +11001575 if (options->hash_known_hosts == -1)
1576 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001577 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001578 options->tun_open = SSH_TUNMODE_NO;
1579 if (options->tun_local == -1)
1580 options->tun_local = SSH_TUNID_ANY;
1581 if (options->tun_remote == -1)
1582 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001583 if (options->permit_local_command == -1)
1584 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001585 if (options->use_roaming == -1)
1586 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001587 if (options->visual_host_key == -1)
1588 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001589 if (options->zero_knowledge_password_authentication == -1)
1590 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001591 if (options->ip_qos_interactive == -1)
1592 options->ip_qos_interactive = IPTOS_LOWDELAY;
1593 if (options->ip_qos_bulk == -1)
1594 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001595 if (options->request_tty == -1)
1596 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001597 if (options->proxy_use_fdpass == -1)
1598 options->proxy_use_fdpass = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001599 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001600 /* options->proxy_command should not be set by default */
1601 /* options->user will be set in the main program if appropriate */
1602 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001603 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001604 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001606
1607/*
1608 * parse_forward
1609 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001610 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001611 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001612 * dynamicfwd == 1
1613 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001614 * returns number of arguments parsed or zero on error
1615 */
1616int
Damien Miller4bf648f2009-02-14 16:28:21 +11001617parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001618{
1619 int i;
1620 char *p, *cp, *fwdarg[4];
1621
1622 memset(fwd, '\0', sizeof(*fwd));
1623
1624 cp = p = xstrdup(fwdspec);
1625
1626 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001627 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001628 cp++;
1629
1630 for (i = 0; i < 4; ++i)
1631 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1632 break;
1633
Damien Millerf4b39532008-11-03 19:28:21 +11001634 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001635 if (cp != NULL)
1636 i = 0; /* failure */
1637
1638 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001639 case 1:
1640 fwd->listen_host = NULL;
1641 fwd->listen_port = a2port(fwdarg[0]);
1642 fwd->connect_host = xstrdup("socks");
1643 break;
1644
1645 case 2:
1646 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1647 fwd->listen_port = a2port(fwdarg[1]);
1648 fwd->connect_host = xstrdup("socks");
1649 break;
1650
Damien Millerf91ee4c2005-03-01 21:24:33 +11001651 case 3:
1652 fwd->listen_host = NULL;
1653 fwd->listen_port = a2port(fwdarg[0]);
1654 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1655 fwd->connect_port = a2port(fwdarg[2]);
1656 break;
1657
1658 case 4:
1659 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1660 fwd->listen_port = a2port(fwdarg[1]);
1661 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1662 fwd->connect_port = a2port(fwdarg[3]);
1663 break;
1664 default:
1665 i = 0; /* failure */
1666 }
1667
Darren Tuckera627d422013-06-02 07:31:17 +10001668 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001669
Damien Millera699d952008-11-03 19:27:34 +11001670 if (dynamicfwd) {
1671 if (!(i == 1 || i == 2))
1672 goto fail_free;
1673 } else {
1674 if (!(i == 3 || i == 4))
1675 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001676 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001677 goto fail_free;
1678 }
1679
Damien Miller4bf648f2009-02-14 16:28:21 +11001680 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001681 goto fail_free;
1682
1683 if (fwd->connect_host != NULL &&
1684 strlen(fwd->connect_host) >= NI_MAXHOST)
1685 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001686 if (fwd->listen_host != NULL &&
1687 strlen(fwd->listen_host) >= NI_MAXHOST)
1688 goto fail_free;
1689
Damien Millerf91ee4c2005-03-01 21:24:33 +11001690
1691 return (i);
1692
1693 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001694 free(fwd->connect_host);
1695 fwd->connect_host = NULL;
1696 free(fwd->listen_host);
1697 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001698 return (0);
1699}