blob: 9c7e73d7d8a6addd24c2376a33eda058e33584db [file] [log] [blame]
Damien Miller5be9d9e2013-12-07 11:24:01 +11001/* $OpenBSD: readconf.c,v 1.215 2013/12/06 13:39:49 markus 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>
Darren Tuckera3357662014-01-18 00:03:57 +110030#ifdef HAVE_PATHS_H
31# include <paths.h>
32#endif
Damien Miller194fd902013-10-15 12:13:05 +110033#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100035#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100036#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100038#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100039#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100040#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100041#endif
Damien Millerc7b06362006-03-15 11:53:45 +110042
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100044#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100045#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
47#include "pathnames.h"
48#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "readconf.h"
51#include "match.h"
52#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100053#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000054#include "kex.h"
55#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110056#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58/* Format of the configuration file:
59
60 # Configuration data is parsed as follows:
61 # 1. command line options
62 # 2. user-specific file
63 # 3. system-wide file
64 # Any configuration value is only changed the first time it is set.
65 # Thus, host-specific definitions should be at the beginning of the
66 # configuration file, and defaults at the end.
67
68 # Host-specific declarations. These may override anything above. A single
69 # host may match multiple declarations; these are processed in the order
70 # that they are given in.
71
72 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000073 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
75 Host fake.com
76 HostName another.host.name.real.org
77 User blaah
78 Port 34289
79 ForwardX11 no
80 ForwardAgent no
81
82 Host books.com
83 RemoteForward 9999 shadows.cs.hut.fi:9999
84 Cipher 3des
85
86 Host fascist.blob.com
87 Port 23123
88 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 PasswordAuthentication no
90
91 Host puukko.hut.fi
92 User t35124p
93 ProxyCommand ssh-proxy %h %p
94
95 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000096 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98 Host *.su
99 Cipher none
100 PasswordAuthentication no
101
Damien Millerd27b9472005-12-13 19:29:02 +1100102 Host vpn.fake.com
103 Tunnel yes
104 TunnelDevice 3
105
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106 # Defaults for various options
107 Host *
108 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100109 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110 PasswordAuthentication yes
111 RSAAuthentication yes
112 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100114 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115 IdentityFile ~/.ssh/identity
116 Port 22
117 EscapeChar ~
118
119*/
120
121/* Keyword tokens. */
122
Damien Miller95def091999-11-25 00:26:21 +1100123typedef enum {
124 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100125 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000126 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
127 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000128 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000129 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100130 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100131 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100132 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
133 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100134 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000135 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000137 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000138 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100139 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000140 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000141 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000142 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100143 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000144 oSendEnv, oControlPath, oControlMaster, oControlPersist,
145 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100146 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100147 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Miller1262b662013-08-21 02:44:24 +1000148 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100149 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
150 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
Darren Tucker07636982013-05-16 20:30:03 +1000151 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152} OpCodes;
153
154/* Textual representations of the tokens. */
155
Damien Miller95def091999-11-25 00:26:21 +1100156static struct {
157 const char *name;
158 OpCodes opcode;
159} keywords[] = {
160 { "forwardagent", oForwardAgent },
161 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000162 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000163 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000164 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000165 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100166 { "gatewayports", oGatewayPorts },
167 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000168 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100169 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100170 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
171 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100172 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100173 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000174 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000175 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000176 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000177 { "challengeresponseauthentication", oChallengeResponseAuthentication },
178 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
179 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000180 { "kerberosauthentication", oUnsupported },
181 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000182 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000183#if defined(GSSAPI)
184 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000185 { "gssapidelegatecredentials", oGssDelegateCreds },
186#else
187 { "gssapiauthentication", oUnsupported },
188 { "gssapidelegatecredentials", oUnsupported },
189#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000190 { "fallbacktorsh", oDeprecated },
191 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100192 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100193 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100194 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000196 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100197 { "proxycommand", oProxyCommand },
198 { "port", oPort },
199 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000200 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000201 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000202 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "remoteforward", oRemoteForward },
204 { "localforward", oLocalForward },
205 { "user", oUser },
206 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100207 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000210 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100211 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000212 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100213 { "connectionattempts", oConnectionAttempts },
214 { "batchmode", oBatchMode },
215 { "checkhostip", oCheckHostIP },
216 { "stricthostkeychecking", oStrictHostKeyChecking },
217 { "compression", oCompression },
218 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100219 { "tcpkeepalive", oTCPKeepAlive },
220 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100221 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000223 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000224 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000225 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000226 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100227#ifdef ENABLE_PKCS11
228 { "smartcarddevice", oPKCS11Provider },
229 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000230#else
231 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100232 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000233#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100234 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000235 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000236 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100237 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000238 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000239 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000240 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100241 { "serveraliveinterval", oServerAliveInterval },
242 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000243 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000244 { "controlpath", oControlPath },
245 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000246 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100247 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100248 { "tunnel", oTunnel },
249 { "tunneldevice", oTunnelDevice },
250 { "localcommand", oLocalCommand },
251 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000252 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000253 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100254#ifdef JPAKE
255 { "zeroknowledgepasswordauthentication",
256 oZeroKnowledgePasswordAuthentication },
257#else
258 { "zeroknowledgepasswordauthentication", oUnsupported },
259#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000260 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100261 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000262 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000263 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100264 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100265 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
266 { "canonicalizehostname", oCanonicalizeHostname },
267 { "canonicalizemaxdots", oCanonicalizeMaxDots },
268 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Darren Tucker07636982013-05-16 20:30:03 +1000269 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100270
Ben Lindstrom65366a82001-12-06 16:32:47 +0000271 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100272};
273
Damien Miller5428f641999-11-25 11:54:57 +1100274/*
275 * Adds a local TCP/IP port forward to options. Never returns if there is an
276 * error.
277 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller4af51302000-04-16 11:18:38 +1000279void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281{
Damien Miller95def091999-11-25 00:26:21 +1100282 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000283#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100284 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000286 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100287#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000288 options->local_forwards = xrealloc(options->local_forwards,
289 options->num_local_forwards + 1,
290 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100291 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100292
Damien Miller1a0442f2008-11-05 16:30:06 +1100293 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100295 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Damien Miller5428f641999-11-25 11:54:57 +1100299/*
300 * Adds a remote TCP/IP port forward to options. Never returns if there is
301 * an error.
302 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller4af51302000-04-16 11:18:38 +1000304void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306{
Damien Miller95def091999-11-25 00:26:21 +1100307 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000308
309 options->remote_forwards = xrealloc(options->remote_forwards,
310 options->num_remote_forwards + 1,
311 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100312 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100313
Damien Miller1a0442f2008-11-05 16:30:06 +1100314 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100315 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100316 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100317 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100318 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000319 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320}
321
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000322static void
323clear_forwardings(Options *options)
324{
325 int i;
326
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000328 free(options->local_forwards[i].listen_host);
329 free(options->local_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100330 }
Damien Miller232cfb12010-06-26 09:50:30 +1000331 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000333 options->local_forwards = NULL;
334 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000335 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000337 free(options->remote_forwards[i].listen_host);
338 free(options->remote_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 }
Damien Miller232cfb12010-06-26 09:50:30 +1000340 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000341 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000342 options->remote_forwards = NULL;
343 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000344 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100345 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000346}
347
Darren Tucker19104782013-04-05 11:13:08 +1100348void
349add_identity_file(Options *options, const char *dir, const char *filename,
350 int userprovided)
351{
352 char *path;
353
354 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
355 fatal("Too many identity files specified (max %d)",
356 SSH_MAX_IDENTITY_FILES);
357
358 if (dir == NULL) /* no dir, filename is absolute */
359 path = xstrdup(filename);
360 else
361 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
362
363 options->identity_file_userprovided[options->num_identity_files] =
364 userprovided;
365 options->identity_files[options->num_identity_files++] = path;
366}
367
Damien Miller194fd902013-10-15 12:13:05 +1100368int
369default_ssh_port(void)
370{
371 static int port;
372 struct servent *sp;
373
374 if (port == 0) {
375 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
376 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
377 }
378 return port;
379}
380
381/*
382 * Execute a command in a shell.
383 * Return its exit status or -1 on abnormal exit.
384 */
385static int
386execute_in_shell(const char *cmd)
387{
388 char *shell, *command_string;
389 pid_t pid;
390 int devnull, status;
391 extern uid_t original_real_uid;
392
393 if ((shell = getenv("SHELL")) == NULL)
394 shell = _PATH_BSHELL;
395
396 /*
397 * Use "exec" to avoid "sh -c" processes on some platforms
398 * (e.g. Solaris)
399 */
400 xasprintf(&command_string, "exec %s", cmd);
401
402 /* Need this to redirect subprocess stdin/out */
403 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
404 fatal("open(/dev/null): %s", strerror(errno));
405
406 debug("Executing command: '%.500s'", cmd);
407
408 /* Fork and execute the command. */
409 if ((pid = fork()) == 0) {
410 char *argv[4];
411
412 /* Child. Permanently give up superuser privileges. */
413 permanently_drop_suid(original_real_uid);
414
415 /* Redirect child stdin and stdout. Leave stderr */
416 if (dup2(devnull, STDIN_FILENO) == -1)
417 fatal("dup2: %s", strerror(errno));
418 if (dup2(devnull, STDOUT_FILENO) == -1)
419 fatal("dup2: %s", strerror(errno));
420 if (devnull > STDERR_FILENO)
421 close(devnull);
422 closefrom(STDERR_FILENO + 1);
423
424 argv[0] = shell;
425 argv[1] = "-c";
426 argv[2] = command_string;
427 argv[3] = NULL;
428
429 execv(argv[0], argv);
430 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
431 /* Die with signal to make this error apparent to parent. */
432 signal(SIGTERM, SIG_DFL);
433 kill(getpid(), SIGTERM);
434 _exit(1);
435 }
436 /* Parent. */
437 if (pid < 0)
438 fatal("%s: fork: %.100s", __func__, strerror(errno));
439
440 close(devnull);
441 free(command_string);
442
443 while (waitpid(pid, &status, 0) == -1) {
444 if (errno != EINTR && errno != EAGAIN)
445 fatal("%s: waitpid: %s", __func__, strerror(errno));
446 }
447 if (!WIFEXITED(status)) {
448 error("command '%.100s' exited abnormally", cmd);
449 return -1;
450 }
451 debug3("command returned status %d", WEXITSTATUS(status));
452 return WEXITSTATUS(status);
453}
454
455/*
456 * Parse and execute a Match directive.
457 */
458static int
459match_cfg_line(Options *options, char **condition, struct passwd *pw,
460 const char *host_arg, const char *filename, int linenum)
461{
Damien Miller084bcd22013-10-23 16:30:51 +1100462 char *arg, *attrib, *cmd, *cp = *condition, *host;
463 const char *ruser;
Damien Millercf31f382013-10-24 21:02:56 +1100464 int r, port, result = 1, attributes = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100465 size_t len;
466 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
467
468 /*
469 * Configuration is likely to be incomplete at this point so we
470 * must be prepared to use default values.
471 */
472 port = options->port <= 0 ? default_ssh_port() : options->port;
473 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100474 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100475 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100476 host = percent_expand(options->hostname,
477 "h", host_arg, (char *)NULL);
478 } else
479 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100480
481 debug3("checking match for '%s' host %s", cp, host);
482 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100483 attributes++;
484 if (strcasecmp(attrib, "all") == 0) {
485 if (attributes != 1 ||
486 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
487 error("'all' cannot be combined with other "
488 "Match attributes");
489 result = -1;
490 goto out;
491 }
492 *condition = cp;
493 result = 1;
494 goto out;
495 }
Damien Miller194fd902013-10-15 12:13:05 +1100496 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
497 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100498 result = -1;
499 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100500 }
501 len = strlen(arg);
502 if (strcasecmp(attrib, "host") == 0) {
503 if (match_hostname(host, arg, len) != 1)
504 result = 0;
505 else
506 debug("%.200s line %d: matched 'Host %.100s' ",
507 filename, linenum, host);
508 } else if (strcasecmp(attrib, "originalhost") == 0) {
509 if (match_hostname(host_arg, arg, len) != 1)
510 result = 0;
511 else
512 debug("%.200s line %d: matched "
513 "'OriginalHost %.100s' ",
514 filename, linenum, host_arg);
515 } else if (strcasecmp(attrib, "user") == 0) {
516 if (match_pattern_list(ruser, arg, len, 0) != 1)
517 result = 0;
518 else
519 debug("%.200s line %d: matched 'User %.100s' ",
520 filename, linenum, ruser);
521 } else if (strcasecmp(attrib, "localuser") == 0) {
522 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
523 result = 0;
524 else
525 debug("%.200s line %d: matched "
526 "'LocalUser %.100s' ",
527 filename, linenum, pw->pw_name);
Damien Miller8a04be72013-10-23 16:29:40 +1100528 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100529 if (gethostname(thishost, sizeof(thishost)) == -1)
530 fatal("gethostname: %s", strerror(errno));
531 strlcpy(shorthost, thishost, sizeof(shorthost));
532 shorthost[strcspn(thishost, ".")] = '\0';
533 snprintf(portstr, sizeof(portstr), "%d", port);
534
535 cmd = percent_expand(arg,
536 "L", shorthost,
537 "d", pw->pw_dir,
538 "h", host,
539 "l", thishost,
540 "n", host_arg,
541 "p", portstr,
542 "r", ruser,
543 "u", pw->pw_name,
544 (char *)NULL);
545 r = execute_in_shell(cmd);
546 if (r == -1) {
Damien Miller8a04be72013-10-23 16:29:40 +1100547 fatal("%.200s line %d: match exec '%.100s' "
Damien Miller194fd902013-10-15 12:13:05 +1100548 "error", filename, linenum, cmd);
549 } else if (r == 0) {
550 debug("%.200s line %d: matched "
Damien Miller8a04be72013-10-23 16:29:40 +1100551 "'exec \"%.100s\"' ",
Damien Miller194fd902013-10-15 12:13:05 +1100552 filename, linenum, cmd);
553 } else
554 result = 0;
555 free(cmd);
556 } else {
557 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100558 result = -1;
559 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100560 }
561 }
Damien Millercf31f382013-10-24 21:02:56 +1100562 if (attributes == 0) {
563 error("One or more attributes required for Match");
564 result = -1;
565 goto out;
566 }
Damien Miller194fd902013-10-15 12:13:05 +1100567 debug3("match %sfound", result ? "" : "not ");
568 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100569 out:
570 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100571 return result;
572}
573
Damien Miller0faf7472013-10-17 11:47:23 +1100574/* Check and prepare a domain name: removes trailing '.' and lowercases */
575static void
576valid_domain(char *name, const char *filename, int linenum)
577{
578 size_t i, l = strlen(name);
579 u_char c, last = '\0';
580
581 if (l == 0)
582 fatal("%s line %d: empty hostname suffix", filename, linenum);
583 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
584 fatal("%s line %d: hostname suffix \"%.100s\" "
585 "starts with invalid character", filename, linenum, name);
586 for (i = 0; i < l; i++) {
587 c = tolower((u_char)name[i]);
588 name[i] = (char)c;
589 if (last == '.' && c == '.')
590 fatal("%s line %d: hostname suffix \"%.100s\" contains "
591 "consecutive separators", filename, linenum, name);
592 if (c != '.' && c != '-' && !isalnum(c) &&
593 c != '_') /* technically invalid, but common */
594 fatal("%s line %d: hostname suffix \"%.100s\" contains "
595 "invalid characters", filename, linenum, name);
596 last = c;
597 }
598 if (name[l - 1] == '.')
599 name[l - 1] = '\0';
600}
601
Damien Miller5428f641999-11-25 11:54:57 +1100602/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000603 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100604 */
Damien Miller4af51302000-04-16 11:18:38 +1000605static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000606parse_token(const char *cp, const char *filename, int linenum,
607 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608{
Darren Tucker07636982013-05-16 20:30:03 +1000609 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610
Damien Miller95def091999-11-25 00:26:21 +1100611 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000612 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100613 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000614 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
615 strlen(ignored_unknown), 1) == 1)
616 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000617 error("%s: line %d: Bad configuration option: %s",
618 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100619 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620}
621
Damien Millere9fc72e2013-10-15 12:14:12 +1100622/* Multistate option parsing */
623struct multistate {
624 char *key;
625 int value;
626};
627static const struct multistate multistate_flag[] = {
628 { "true", 1 },
629 { "false", 0 },
630 { "yes", 1 },
631 { "no", 0 },
632 { NULL, -1 }
633};
634static const struct multistate multistate_yesnoask[] = {
635 { "true", 1 },
636 { "false", 0 },
637 { "yes", 1 },
638 { "no", 0 },
639 { "ask", 2 },
640 { NULL, -1 }
641};
642static const struct multistate multistate_addressfamily[] = {
643 { "inet", AF_INET },
644 { "inet6", AF_INET6 },
645 { "any", AF_UNSPEC },
646 { NULL, -1 }
647};
648static const struct multistate multistate_controlmaster[] = {
649 { "true", SSHCTL_MASTER_YES },
650 { "yes", SSHCTL_MASTER_YES },
651 { "false", SSHCTL_MASTER_NO },
652 { "no", SSHCTL_MASTER_NO },
653 { "auto", SSHCTL_MASTER_AUTO },
654 { "ask", SSHCTL_MASTER_ASK },
655 { "autoask", SSHCTL_MASTER_AUTO_ASK },
656 { NULL, -1 }
657};
658static const struct multistate multistate_tunnel[] = {
659 { "ethernet", SSH_TUNMODE_ETHERNET },
660 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
661 { "true", SSH_TUNMODE_DEFAULT },
662 { "yes", SSH_TUNMODE_DEFAULT },
663 { "false", SSH_TUNMODE_NO },
664 { "no", SSH_TUNMODE_NO },
665 { NULL, -1 }
666};
667static const struct multistate multistate_requesttty[] = {
668 { "true", REQUEST_TTY_YES },
669 { "yes", REQUEST_TTY_YES },
670 { "false", REQUEST_TTY_NO },
671 { "no", REQUEST_TTY_NO },
672 { "force", REQUEST_TTY_FORCE },
673 { "auto", REQUEST_TTY_AUTO },
674 { NULL, -1 }
675};
Damien Miller38505592013-10-17 11:48:13 +1100676static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100677 { "true", SSH_CANONICALISE_YES },
678 { "false", SSH_CANONICALISE_NO },
679 { "yes", SSH_CANONICALISE_YES },
680 { "no", SSH_CANONICALISE_NO },
681 { "always", SSH_CANONICALISE_ALWAYS },
682 { NULL, -1 }
683};
Damien Millere9fc72e2013-10-15 12:14:12 +1100684
Damien Miller5428f641999-11-25 11:54:57 +1100685/*
686 * Processes a single option line as used in the configuration files. This
687 * only sets those values that have not already been set.
688 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100689#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100690int
Damien Miller194fd902013-10-15 12:13:05 +1100691process_config_line(Options *options, struct passwd *pw, const char *host,
692 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693{
Damien Miller295ee632011-05-29 21:42:31 +1000694 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
695 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000696 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100697 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100698 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000699 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100700 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701 Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100702 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100703 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller194fd902013-10-15 12:13:05 +1100705 if (activep == NULL) { /* We are processing a command line directive */
706 cmdline = 1;
707 activep = &cmdline;
708 }
709
Damien Millerc652cac2003-05-14 13:40:54 +1000710 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100711 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000712 if (strchr(WHITESPACE, line[len]) == NULL)
713 break;
714 line[len] = '\0';
715 }
716
Damien Millerbe484b52000-07-15 14:14:16 +1000717 s = line;
718 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100719 if ((keyword = strdelim(&s)) == NULL)
720 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000721 /* Ignore leading whitespace. */
722 if (*keyword == '\0')
723 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000724 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100725 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000726 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100727 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Darren Tucker07636982013-05-16 20:30:03 +1000729 opcode = parse_token(keyword, filename, linenum,
730 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller95def091999-11-25 00:26:21 +1100732 switch (opcode) {
733 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100734 /* don't panic, but count bad options */
735 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100736 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000737 case oIgnoredUnknownOption:
738 debug("%s line %d: Ignored unknown option \"%s\"",
739 filename, linenum, keyword);
740 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000741 case oConnectTimeout:
742 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100743parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000744 arg = strdelim(&s);
745 if (!arg || *arg == '\0')
746 fatal("%s line %d: missing time value.",
747 filename, linenum);
748 if ((value = convtime(arg)) == -1)
749 fatal("%s line %d: invalid time value.",
750 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100751 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000752 *intptr = value;
753 break;
754
Damien Miller95def091999-11-25 00:26:21 +1100755 case oForwardAgent:
756 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100757 parse_flag:
758 multistate_ptr = multistate_flag;
759 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000760 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000761 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100762 fatal("%s line %d: missing argument.",
763 filename, linenum);
764 value = -1;
765 for (i = 0; multistate_ptr[i].key != NULL; i++) {
766 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
767 value = multistate_ptr[i].value;
768 break;
769 }
770 }
771 if (value == -1)
772 fatal("%s line %d: unsupported option \"%s\".",
773 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100774 if (*activep && *intptr == -1)
775 *intptr = value;
776 break;
777
778 case oForwardX11:
779 intptr = &options->forward_x11;
780 goto parse_flag;
781
Darren Tucker0a118da2003-10-15 15:54:32 +1000782 case oForwardX11Trusted:
783 intptr = &options->forward_x11_trusted;
784 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000785
786 case oForwardX11Timeout:
787 intptr = &options->forward_x11_timeout;
788 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000789
Damien Miller95def091999-11-25 00:26:21 +1100790 case oGatewayPorts:
791 intptr = &options->gateway_ports;
792 goto parse_flag;
793
Darren Tuckere7d4b192006-07-12 22:17:10 +1000794 case oExitOnForwardFailure:
795 intptr = &options->exit_on_forward_failure;
796 goto parse_flag;
797
Damien Miller95def091999-11-25 00:26:21 +1100798 case oUsePrivilegedPort:
799 intptr = &options->use_privileged_port;
800 goto parse_flag;
801
Damien Miller95def091999-11-25 00:26:21 +1100802 case oPasswordAuthentication:
803 intptr = &options->password_authentication;
804 goto parse_flag;
805
Damien Miller01ed2272008-11-05 16:20:46 +1100806 case oZeroKnowledgePasswordAuthentication:
807 intptr = &options->zero_knowledge_password_authentication;
808 goto parse_flag;
809
Damien Miller874d77b2000-10-14 16:23:11 +1100810 case oKbdInteractiveAuthentication:
811 intptr = &options->kbd_interactive_authentication;
812 goto parse_flag;
813
814 case oKbdInteractiveDevices:
815 charptr = &options->kbd_interactive_devices;
816 goto parse_string;
817
Damien Miller0bc1bd82000-11-13 22:57:25 +1100818 case oPubkeyAuthentication:
819 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000820 goto parse_flag;
821
Damien Miller95def091999-11-25 00:26:21 +1100822 case oRSAAuthentication:
823 intptr = &options->rsa_authentication;
824 goto parse_flag;
825
826 case oRhostsRSAAuthentication:
827 intptr = &options->rhosts_rsa_authentication;
828 goto parse_flag;
829
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000830 case oHostbasedAuthentication:
831 intptr = &options->hostbased_authentication;
832 goto parse_flag;
833
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000834 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000835 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100836 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000837
Darren Tucker0efd1552003-08-26 11:49:55 +1000838 case oGssAuthentication:
839 intptr = &options->gss_authentication;
840 goto parse_flag;
841
842 case oGssDelegateCreds:
843 intptr = &options->gss_deleg_creds;
844 goto parse_flag;
845
Damien Miller95def091999-11-25 00:26:21 +1100846 case oBatchMode:
847 intptr = &options->batch_mode;
848 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849
Damien Miller95def091999-11-25 00:26:21 +1100850 case oCheckHostIP:
851 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000852 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Miller37876e92003-05-15 10:19:46 +1000854 case oVerifyHostKeyDNS:
855 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100856 multistate_ptr = multistate_yesnoask;
857 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000858
Damien Miller95def091999-11-25 00:26:21 +1100859 case oStrictHostKeyChecking:
860 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100861 multistate_ptr = multistate_yesnoask;
862 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863
Damien Miller95def091999-11-25 00:26:21 +1100864 case oCompression:
865 intptr = &options->compression;
866 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867
Damien Miller12c150e2003-12-17 16:31:10 +1100868 case oTCPKeepAlive:
869 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100870 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000872 case oNoHostAuthenticationForLocalhost:
873 intptr = &options->no_host_authentication_for_localhost;
874 goto parse_flag;
875
Damien Miller95def091999-11-25 00:26:21 +1100876 case oNumberOfPasswordPrompts:
877 intptr = &options->number_of_password_prompts;
878 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 case oCompressionLevel:
881 intptr = &options->compression_level;
882 goto parse_int;
883
Damien Millera5539d22003-04-09 20:50:06 +1000884 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000885 arg = strdelim(&s);
886 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000887 fatal("%.200s line %d: Missing argument.", filename,
888 linenum);
889 if (strcmp(arg, "default") == 0) {
890 val64 = 0;
891 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000892 if (scan_scaled(arg, &val64) == -1)
893 fatal("%.200s line %d: Bad number '%s': %s",
894 filename, linenum, arg, strerror(errno));
895 /* check for too-large or too-small limits */
896 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000897 fatal("%.200s line %d: RekeyLimit too large",
898 filename, linenum);
899 if (val64 != 0 && val64 < 16)
900 fatal("%.200s line %d: RekeyLimit too small",
901 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000902 }
Damien Miller3dff1762008-02-10 22:25:52 +1100903 if (*activep && options->rekey_limit == -1)
904 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000905 if (s != NULL) { /* optional rekey interval present */
906 if (strcmp(s, "none") == 0) {
907 (void)strdelim(&s); /* discard */
908 break;
909 }
910 intptr = &options->rekey_interval;
911 goto parse_time;
912 }
Damien Millera5539d22003-04-09 20:50:06 +1000913 break;
914
Damien Miller95def091999-11-25 00:26:21 +1100915 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000916 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000917 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100918 fatal("%.200s line %d: Missing argument.", filename, linenum);
919 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100920 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000921 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100922 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100923 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100924 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100925 }
926 break;
927
Damien Millerd3a18572000-06-07 19:55:44 +1000928 case oXAuthLocation:
929 charptr=&options->xauth_location;
930 goto parse_string;
931
Damien Miller95def091999-11-25 00:26:21 +1100932 case oUser:
933 charptr = &options->user;
934parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000935 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000936 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000937 fatal("%.200s line %d: Missing argument.",
938 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100939 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000940 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100941 break;
942
943 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000944 cpptr = (char **)&options->system_hostfiles;
945 uintptr = &options->num_system_hostfiles;
946 max_entries = SSH_MAX_HOSTS_FILES;
947parse_char_array:
948 if (*activep && *uintptr == 0) {
949 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
950 if ((*uintptr) >= max_entries)
951 fatal("%s line %d: "
952 "too many authorized keys files.",
953 filename, linenum);
954 cpptr[(*uintptr)++] = xstrdup(arg);
955 }
956 }
957 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100958
959 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000960 cpptr = (char **)&options->user_hostfiles;
961 uintptr = &options->num_user_hostfiles;
962 max_entries = SSH_MAX_HOSTS_FILES;
963 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000964
Damien Miller95def091999-11-25 00:26:21 +1100965 case oHostName:
966 charptr = &options->hostname;
967 goto parse_string;
968
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000969 case oHostKeyAlias:
970 charptr = &options->host_key_alias;
971 goto parse_string;
972
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000973 case oPreferredAuthentications:
974 charptr = &options->preferred_authentications;
975 goto parse_string;
976
Ben Lindstrome0f88042001-04-30 13:06:24 +0000977 case oBindAddress:
978 charptr = &options->bind_address;
979 goto parse_string;
980
Damien Miller7ea845e2010-02-12 09:21:02 +1100981 case oPKCS11Provider:
982 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000983 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000984
Damien Miller95def091999-11-25 00:26:21 +1100985 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100986 charptr = &options->proxy_command;
987parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000988 if (s == NULL)
989 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100990 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100991 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100992 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100993 return 0;
994
995 case oPort:
996 intptr = &options->port;
997parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000998 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000999 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001000 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001001 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001002 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001003
1004 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001005 value = strtol(arg, &endofnumber, 0);
1006 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001007 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001008 if (*activep && *intptr == -1)
1009 *intptr = value;
1010 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
Damien Miller95def091999-11-25 00:26:21 +11001012 case oConnectionAttempts:
1013 intptr = &options->connection_attempts;
1014 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001015
Damien Miller95def091999-11-25 00:26:21 +11001016 case oCipher:
1017 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001018 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001019 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001020 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001021 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001022 if (value == -1)
1023 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001024 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001025 if (*activep && *intptr == -1)
1026 *intptr = value;
1027 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Damien Miller78928792000-04-12 20:17:38 +10001029 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001030 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001031 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001032 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001033 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001034 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001035 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001036 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001037 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001038 break;
1039
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001040 case oMacs:
1041 arg = strdelim(&s);
1042 if (!arg || *arg == '\0')
1043 fatal("%.200s line %d: Missing argument.", filename, linenum);
1044 if (!mac_valid(arg))
1045 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001046 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001047 if (*activep && options->macs == NULL)
1048 options->macs = xstrdup(arg);
1049 break;
1050
Damien Millerd5f62bf2010-09-24 22:11:14 +10001051 case oKexAlgorithms:
1052 arg = strdelim(&s);
1053 if (!arg || *arg == '\0')
1054 fatal("%.200s line %d: Missing argument.",
1055 filename, linenum);
1056 if (!kex_names_valid(arg))
1057 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1058 filename, linenum, arg ? arg : "<NONE>");
1059 if (*activep && options->kex_algorithms == NULL)
1060 options->kex_algorithms = xstrdup(arg);
1061 break;
1062
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001063 case oHostKeyAlgorithms:
1064 arg = strdelim(&s);
1065 if (!arg || *arg == '\0')
1066 fatal("%.200s line %d: Missing argument.", filename, linenum);
1067 if (!key_names_valid2(arg))
1068 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001069 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001070 if (*activep && options->hostkeyalgorithms == NULL)
1071 options->hostkeyalgorithms = xstrdup(arg);
1072 break;
1073
Damien Miller78928792000-04-12 20:17:38 +10001074 case oProtocol:
1075 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001076 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001077 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001078 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001079 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001080 if (value == SSH_PROTO_UNKNOWN)
1081 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001082 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001083 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1084 *intptr = value;
1085 break;
1086
Damien Miller95def091999-11-25 00:26:21 +11001087 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001088 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001089 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001090 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001091 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001092 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001093 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001094 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1095 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001096 break;
1097
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001098 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001099 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001100 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001101 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001102 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001103 fatal("%.200s line %d: Missing port argument.",
1104 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001105
Damien Millera699d952008-11-03 19:27:34 +11001106 if (opcode == oLocalForward ||
1107 opcode == oRemoteForward) {
1108 arg2 = strdelim(&s);
1109 if (arg2 == NULL || *arg2 == '\0')
1110 fatal("%.200s line %d: Missing target argument.",
1111 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001112
Damien Millera699d952008-11-03 19:27:34 +11001113 /* construct a string for parse_forward */
1114 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1115 } else if (opcode == oDynamicForward) {
1116 strlcpy(fwdarg, arg, sizeof(fwdarg));
1117 }
1118
1119 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001120 opcode == oDynamicForward ? 1 : 0,
1121 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001122 fatal("%.200s line %d: Bad forwarding specification.",
1123 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001124
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001125 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001126 if (opcode == oLocalForward ||
1127 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001128 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001129 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001130 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001131 }
Damien Miller95def091999-11-25 00:26:21 +11001132 break;
1133
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001134 case oClearAllForwardings:
1135 intptr = &options->clear_forwardings;
1136 goto parse_flag;
1137
Damien Miller95def091999-11-25 00:26:21 +11001138 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001139 if (cmdline)
1140 fatal("Host directive not supported as a command-line "
1141 "option");
Damien Miller95def091999-11-25 00:26:21 +11001142 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001143 arg2 = NULL;
1144 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1145 negated = *arg == '!';
1146 if (negated)
1147 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001148 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001149 if (negated) {
1150 debug("%.200s line %d: Skipping Host "
1151 "block because of negated match "
1152 "for %.100s", filename, linenum,
1153 arg);
1154 *activep = 0;
1155 break;
1156 }
1157 if (!*activep)
1158 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001159 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001160 }
Damien Millerfe924212011-05-15 08:44:45 +10001161 }
1162 if (*activep)
1163 debug("%.200s line %d: Applying options for %.100s",
1164 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001165 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001166 return 0;
1167
Damien Miller194fd902013-10-15 12:13:05 +11001168 case oMatch:
1169 if (cmdline)
1170 fatal("Host directive not supported as a command-line "
1171 "option");
1172 value = match_cfg_line(options, &s, pw, host,
1173 filename, linenum);
1174 if (value < 0)
1175 fatal("%.200s line %d: Bad Match condition", filename,
1176 linenum);
1177 *activep = value;
1178 break;
1179
Damien Miller95def091999-11-25 00:26:21 +11001180 case oEscapeChar:
1181 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001182 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001183 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001184 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001185 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001186 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1187 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001188 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001189 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001190 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001191 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001192 else {
1193 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001194 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001195 /* NOTREACHED */
1196 value = 0; /* Avoid compiler warning. */
1197 }
1198 if (*activep && *intptr == -1)
1199 *intptr = value;
1200 break;
1201
Damien Miller20a8f972003-05-18 20:50:30 +10001202 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001203 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001204 multistate_ptr = multistate_addressfamily;
1205 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001206
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001207 case oEnableSSHKeysign:
1208 intptr = &options->enable_ssh_keysign;
1209 goto parse_flag;
1210
Damien Millerbd394c32004-03-08 23:12:36 +11001211 case oIdentitiesOnly:
1212 intptr = &options->identities_only;
1213 goto parse_flag;
1214
Damien Miller509b0102003-12-17 16:33:10 +11001215 case oServerAliveInterval:
1216 intptr = &options->server_alive_interval;
1217 goto parse_time;
1218
1219 case oServerAliveCountMax:
1220 intptr = &options->server_alive_count_max;
1221 goto parse_int;
1222
Darren Tucker46bc0752004-05-02 22:11:30 +10001223 case oSendEnv:
1224 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1225 if (strchr(arg, '=') != NULL)
1226 fatal("%s line %d: Invalid environment name.",
1227 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001228 if (!*activep)
1229 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001230 if (options->num_send_env >= MAX_SEND_ENV)
1231 fatal("%s line %d: too many send env.",
1232 filename, linenum);
1233 options->send_env[options->num_send_env++] =
1234 xstrdup(arg);
1235 }
1236 break;
1237
Damien Miller0e220db2004-06-15 10:34:08 +10001238 case oControlPath:
1239 charptr = &options->control_path;
1240 goto parse_string;
1241
1242 case oControlMaster:
1243 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001244 multistate_ptr = multistate_controlmaster;
1245 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001246
Damien Millere11e1ea2010-08-03 16:04:46 +10001247 case oControlPersist:
1248 /* no/false/yes/true, or a time spec */
1249 intptr = &options->control_persist;
1250 arg = strdelim(&s);
1251 if (!arg || *arg == '\0')
1252 fatal("%.200s line %d: Missing ControlPersist"
1253 " argument.", filename, linenum);
1254 value = 0;
1255 value2 = 0; /* timeout */
1256 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1257 value = 0;
1258 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1259 value = 1;
1260 else if ((value2 = convtime(arg)) >= 0)
1261 value = 1;
1262 else
1263 fatal("%.200s line %d: Bad ControlPersist argument.",
1264 filename, linenum);
1265 if (*activep && *intptr == -1) {
1266 *intptr = value;
1267 options->control_persist_timeout = value2;
1268 }
1269 break;
1270
Damien Millere1776152005-03-01 21:47:37 +11001271 case oHashKnownHosts:
1272 intptr = &options->hash_known_hosts;
1273 goto parse_flag;
1274
Damien Millerd27b9472005-12-13 19:29:02 +11001275 case oTunnel:
1276 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001277 multistate_ptr = multistate_tunnel;
1278 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001279
1280 case oTunnelDevice:
1281 arg = strdelim(&s);
1282 if (!arg || *arg == '\0')
1283 fatal("%.200s line %d: Missing argument.", filename, linenum);
1284 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001285 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001286 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1287 if (*activep) {
1288 options->tun_local = value;
1289 options->tun_remote = value2;
1290 }
1291 break;
1292
1293 case oLocalCommand:
1294 charptr = &options->local_command;
1295 goto parse_command;
1296
1297 case oPermitLocalCommand:
1298 intptr = &options->permit_local_command;
1299 goto parse_flag;
1300
Damien Miller10288242008-06-30 00:04:03 +10001301 case oVisualHostKey:
1302 intptr = &options->visual_host_key;
1303 goto parse_flag;
1304
Damien Miller0dac6fb2010-11-20 15:19:38 +11001305 case oIPQoS:
1306 arg = strdelim(&s);
1307 if ((value = parse_ipqos(arg)) == -1)
1308 fatal("%s line %d: Bad IPQoS value: %s",
1309 filename, linenum, arg);
1310 arg = strdelim(&s);
1311 if (arg == NULL)
1312 value2 = value;
1313 else if ((value2 = parse_ipqos(arg)) == -1)
1314 fatal("%s line %d: Bad IPQoS value: %s",
1315 filename, linenum, arg);
1316 if (*activep) {
1317 options->ip_qos_interactive = value;
1318 options->ip_qos_bulk = value2;
1319 }
1320 break;
1321
Darren Tucker71e4d542009-07-06 07:12:27 +10001322 case oUseRoaming:
1323 intptr = &options->use_roaming;
1324 goto parse_flag;
1325
Damien Miller21771e22011-05-15 08:45:50 +10001326 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001327 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001328 multistate_ptr = multistate_requesttty;
1329 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001330
Darren Tucker07636982013-05-16 20:30:03 +10001331 case oIgnoreUnknown:
1332 charptr = &options->ignored_unknown;
1333 goto parse_string;
1334
Damien Miller1262b662013-08-21 02:44:24 +10001335 case oProxyUseFdpass:
1336 intptr = &options->proxy_use_fdpass;
1337 goto parse_flag;
1338
Damien Miller0faf7472013-10-17 11:47:23 +11001339 case oCanonicalDomains:
1340 value = options->num_canonical_domains != 0;
1341 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1342 valid_domain(arg, filename, linenum);
1343 if (!*activep || value)
1344 continue;
1345 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1346 fatal("%s line %d: too many hostname suffixes.",
1347 filename, linenum);
1348 options->canonical_domains[
1349 options->num_canonical_domains++] = xstrdup(arg);
1350 }
1351 break;
1352
Damien Miller38505592013-10-17 11:48:13 +11001353 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001354 value = options->num_permitted_cnames != 0;
1355 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1356 /* Either '*' for everything or 'list:list' */
1357 if (strcmp(arg, "*") == 0)
1358 arg2 = arg;
1359 else {
1360 lowercase(arg);
1361 if ((arg2 = strchr(arg, ':')) == NULL ||
1362 arg2[1] == '\0') {
1363 fatal("%s line %d: "
1364 "Invalid permitted CNAME \"%s\"",
1365 filename, linenum, arg);
1366 }
1367 *arg2 = '\0';
1368 arg2++;
1369 }
1370 if (!*activep || value)
1371 continue;
1372 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1373 fatal("%s line %d: too many permitted CNAMEs.",
1374 filename, linenum);
1375 cname = options->permitted_cnames +
1376 options->num_permitted_cnames++;
1377 cname->source_list = xstrdup(arg);
1378 cname->target_list = xstrdup(arg2);
1379 }
1380 break;
1381
Damien Miller38505592013-10-17 11:48:13 +11001382 case oCanonicalizeHostname:
1383 intptr = &options->canonicalize_hostname;
1384 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001385 goto parse_multistate;
1386
Damien Miller38505592013-10-17 11:48:13 +11001387 case oCanonicalizeMaxDots:
1388 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001389 goto parse_int;
1390
Damien Miller38505592013-10-17 11:48:13 +11001391 case oCanonicalizeFallbackLocal:
1392 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001393 goto parse_flag;
1394
Ben Lindstrom4daea862002-06-09 20:04:02 +00001395 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001396 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001397 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001398 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001399
Damien Millerf9b3feb2003-05-16 11:38:32 +10001400 case oUnsupported:
1401 error("%s line %d: Unsupported option \"%s\"",
1402 filename, linenum, keyword);
1403 return 0;
1404
Damien Miller95def091999-11-25 00:26:21 +11001405 default:
1406 fatal("process_config_line: Unimplemented opcode %d", opcode);
1407 }
1408
1409 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001410 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001411 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001412 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001413 }
Damien Miller95def091999-11-25 00:26:21 +11001414 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415}
1416
1417
Damien Miller5428f641999-11-25 11:54:57 +11001418/*
1419 * Reads the config file and modifies the options accordingly. Options
1420 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001421 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001422 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001424int
Damien Miller194fd902013-10-15 12:13:05 +11001425read_config_file(const char *filename, struct passwd *pw, const char *host,
1426 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427{
Damien Miller95def091999-11-25 00:26:21 +11001428 FILE *f;
1429 char line[1024];
1430 int active, linenum;
1431 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Damien Miller57a44762004-04-20 20:11:57 +10001433 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001434 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Darren Tuckeraefa3682013-04-05 11:18:35 +11001436 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001437 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001438
Damien Miller33793852004-06-15 10:27:55 +10001439 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001440 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001441 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001442 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001443 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001444 }
1445
Damien Miller95def091999-11-25 00:26:21 +11001446 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller5428f641999-11-25 11:54:57 +11001448 /*
1449 * Mark that we are now processing the options. This flag is turned
1450 * on/off by Host specifications.
1451 */
Damien Miller95def091999-11-25 00:26:21 +11001452 active = 1;
1453 linenum = 0;
1454 while (fgets(line, sizeof(line), f)) {
1455 /* Update line number counter. */
1456 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001457 if (process_config_line(options, pw, host, line, filename,
1458 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001459 bad_options++;
1460 }
1461 fclose(f);
1462 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001463 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001464 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001465 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
Damien Miller5428f641999-11-25 11:54:57 +11001468/*
1469 * Initializes options to special values that indicate that they have not yet
1470 * been set. Read_config_file will only set options with this value. Options
1471 * are processed in the following order: command line, user config file,
1472 * system config file. Last, fill_default_options is called.
1473 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474
Damien Miller4af51302000-04-16 11:18:38 +10001475void
Damien Miller95def091999-11-25 00:26:21 +11001476initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477{
Damien Miller95def091999-11-25 00:26:21 +11001478 memset(options, 'X', sizeof(*options));
1479 options->forward_agent = -1;
1480 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001481 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001482 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001483 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001484 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001485 options->gateway_ports = -1;
1486 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001487 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001488 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001489 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001490 options->gss_authentication = -1;
1491 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001492 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001493 options->kbd_interactive_authentication = -1;
1494 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001495 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001496 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001497 options->batch_mode = -1;
1498 options->check_host_ip = -1;
1499 options->strict_host_key_checking = -1;
1500 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001501 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001502 options->compression_level = -1;
1503 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001504 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001505 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001506 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001507 options->number_of_password_prompts = -1;
1508 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001509 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001510 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001511 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001512 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001513 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001514 options->num_identity_files = 0;
1515 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001516 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001517 options->proxy_command = NULL;
1518 options->user = NULL;
1519 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001520 options->num_system_hostfiles = 0;
1521 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001522 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001523 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001524 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001525 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001526 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001527 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001528 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001529 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001530 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001531 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001532 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001533 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001534 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001535 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001536 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001537 options->server_alive_interval = -1;
1538 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001539 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001540 options->control_path = NULL;
1541 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001542 options->control_persist = -1;
1543 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001544 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001545 options->tun_open = -1;
1546 options->tun_local = -1;
1547 options->tun_remote = -1;
1548 options->local_command = NULL;
1549 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001550 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001551 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001552 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001553 options->ip_qos_interactive = -1;
1554 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001555 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001556 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001557 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001558 options->num_canonical_domains = 0;
1559 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001560 options->canonicalize_max_dots = -1;
1561 options->canonicalize_fallback_local = -1;
1562 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001563}
1564
Damien Miller5428f641999-11-25 11:54:57 +11001565/*
1566 * Called after processing other sources of option data, this fills those
1567 * options for which no value has been specified with their default values.
1568 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001569
Damien Miller4af51302000-04-16 11:18:38 +10001570void
Damien Miller95def091999-11-25 00:26:21 +11001571fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572{
Damien Miller95def091999-11-25 00:26:21 +11001573 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001574 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001575 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001576 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001577 if (options->forward_x11_trusted == -1)
1578 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001579 if (options->forward_x11_timeout == -1)
1580 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001581 if (options->exit_on_forward_failure == -1)
1582 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001583 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001584 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001585 if (options->gateway_ports == -1)
1586 options->gateway_ports = 0;
1587 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001588 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001589 if (options->rsa_authentication == -1)
1590 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001591 if (options->pubkey_authentication == -1)
1592 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001593 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001594 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001595 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001596 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001597 if (options->gss_deleg_creds == -1)
1598 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001599 if (options->password_authentication == -1)
1600 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001601 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001602 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001603 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001604 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001605 if (options->hostbased_authentication == -1)
1606 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001607 if (options->batch_mode == -1)
1608 options->batch_mode = 0;
1609 if (options->check_host_ip == -1)
1610 options->check_host_ip = 1;
1611 if (options->strict_host_key_checking == -1)
1612 options->strict_host_key_checking = 2; /* 2 is default */
1613 if (options->compression == -1)
1614 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001615 if (options->tcp_keep_alive == -1)
1616 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001617 if (options->compression_level == -1)
1618 options->compression_level = 6;
1619 if (options->port == -1)
1620 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001621 if (options->address_family == -1)
1622 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001623 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001624 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001625 if (options->number_of_password_prompts == -1)
1626 options->number_of_password_prompts = 3;
1627 /* Selected in ssh_login(). */
1628 if (options->cipher == -1)
1629 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001630 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001631 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001632 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001633 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001634 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001635 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001636 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001637 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001638 add_identity_file(options, "~/",
1639 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001640 }
1641 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001642 add_identity_file(options, "~/",
1643 _PATH_SSH_CLIENT_ID_RSA, 0);
1644 add_identity_file(options, "~/",
1645 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001646#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001647 add_identity_file(options, "~/",
1648 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001649#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001650 add_identity_file(options, "~/",
1651 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001652 }
Damien Millereba71ba2000-04-29 23:57:08 +10001653 }
Damien Miller95def091999-11-25 00:26:21 +11001654 if (options->escape_char == -1)
1655 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001656 if (options->num_system_hostfiles == 0) {
1657 options->system_hostfiles[options->num_system_hostfiles++] =
1658 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1659 options->system_hostfiles[options->num_system_hostfiles++] =
1660 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1661 }
1662 if (options->num_user_hostfiles == 0) {
1663 options->user_hostfiles[options->num_user_hostfiles++] =
1664 xstrdup(_PATH_SSH_USER_HOSTFILE);
1665 options->user_hostfiles[options->num_user_hostfiles++] =
1666 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1667 }
Damien Millerfcd93202002-02-05 12:26:34 +11001668 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001669 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001670 if (options->clear_forwardings == 1)
1671 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001672 if (options->no_host_authentication_for_localhost == - 1)
1673 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001674 if (options->identities_only == -1)
1675 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001676 if (options->enable_ssh_keysign == -1)
1677 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001678 if (options->rekey_limit == -1)
1679 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001680 if (options->rekey_interval == -1)
1681 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001682 if (options->verify_host_key_dns == -1)
1683 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001684 if (options->server_alive_interval == -1)
1685 options->server_alive_interval = 0;
1686 if (options->server_alive_count_max == -1)
1687 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001688 if (options->control_master == -1)
1689 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001690 if (options->control_persist == -1) {
1691 options->control_persist = 0;
1692 options->control_persist_timeout = 0;
1693 }
Damien Millere1776152005-03-01 21:47:37 +11001694 if (options->hash_known_hosts == -1)
1695 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001696 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001697 options->tun_open = SSH_TUNMODE_NO;
1698 if (options->tun_local == -1)
1699 options->tun_local = SSH_TUNID_ANY;
1700 if (options->tun_remote == -1)
1701 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001702 if (options->permit_local_command == -1)
1703 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001704 if (options->use_roaming == -1)
1705 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001706 if (options->visual_host_key == -1)
1707 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001708 if (options->zero_knowledge_password_authentication == -1)
1709 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001710 if (options->ip_qos_interactive == -1)
1711 options->ip_qos_interactive = IPTOS_LOWDELAY;
1712 if (options->ip_qos_bulk == -1)
1713 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001714 if (options->request_tty == -1)
1715 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001716 if (options->proxy_use_fdpass == -1)
1717 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001718 if (options->canonicalize_max_dots == -1)
1719 options->canonicalize_max_dots = 1;
1720 if (options->canonicalize_fallback_local == -1)
1721 options->canonicalize_fallback_local = 1;
1722 if (options->canonicalize_hostname == -1)
1723 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001724#define CLEAR_ON_NONE(v) \
1725 do { \
1726 if (v != NULL && strcasecmp(v, "none") == 0) { \
1727 free(v); \
1728 v = NULL; \
1729 } \
1730 } while(0)
1731 CLEAR_ON_NONE(options->local_command);
1732 CLEAR_ON_NONE(options->proxy_command);
1733 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001734 /* options->user will be set in the main program if appropriate */
1735 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001736 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001737 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001739
1740/*
1741 * parse_forward
1742 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001743 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001744 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001745 * dynamicfwd == 1
1746 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001747 * returns number of arguments parsed or zero on error
1748 */
1749int
Damien Miller4bf648f2009-02-14 16:28:21 +11001750parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001751{
1752 int i;
1753 char *p, *cp, *fwdarg[4];
1754
1755 memset(fwd, '\0', sizeof(*fwd));
1756
1757 cp = p = xstrdup(fwdspec);
1758
1759 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001760 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001761 cp++;
1762
1763 for (i = 0; i < 4; ++i)
1764 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1765 break;
1766
Damien Millerf4b39532008-11-03 19:28:21 +11001767 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001768 if (cp != NULL)
1769 i = 0; /* failure */
1770
1771 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001772 case 1:
1773 fwd->listen_host = NULL;
1774 fwd->listen_port = a2port(fwdarg[0]);
1775 fwd->connect_host = xstrdup("socks");
1776 break;
1777
1778 case 2:
1779 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1780 fwd->listen_port = a2port(fwdarg[1]);
1781 fwd->connect_host = xstrdup("socks");
1782 break;
1783
Damien Millerf91ee4c2005-03-01 21:24:33 +11001784 case 3:
1785 fwd->listen_host = NULL;
1786 fwd->listen_port = a2port(fwdarg[0]);
1787 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1788 fwd->connect_port = a2port(fwdarg[2]);
1789 break;
1790
1791 case 4:
1792 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1793 fwd->listen_port = a2port(fwdarg[1]);
1794 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1795 fwd->connect_port = a2port(fwdarg[3]);
1796 break;
1797 default:
1798 i = 0; /* failure */
1799 }
1800
Darren Tuckera627d422013-06-02 07:31:17 +10001801 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001802
Damien Millera699d952008-11-03 19:27:34 +11001803 if (dynamicfwd) {
1804 if (!(i == 1 || i == 2))
1805 goto fail_free;
1806 } else {
1807 if (!(i == 3 || i == 4))
1808 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001809 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001810 goto fail_free;
1811 }
1812
Damien Miller4bf648f2009-02-14 16:28:21 +11001813 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001814 goto fail_free;
1815
1816 if (fwd->connect_host != NULL &&
1817 strlen(fwd->connect_host) >= NI_MAXHOST)
1818 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001819 if (fwd->listen_host != NULL &&
1820 strlen(fwd->listen_host) >= NI_MAXHOST)
1821 goto fail_free;
1822
Damien Millerf91ee4c2005-03-01 21:24:33 +11001823
1824 return (i);
1825
1826 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001827 free(fwd->connect_host);
1828 fwd->connect_host = NULL;
1829 free(fwd->listen_host);
1830 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001831 return (0);
1832}