blob: f186667868b248e0cb2f4b84781ed132a61bd510 [file] [log] [blame]
Damien Millereff5cad2013-10-23 16:31:10 +11001/* $OpenBSD: readconf.c,v 1.212 2013/10/23 03:05:19 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,
Damien Miller38505592013-10-17 11:48:13 +1100147 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
148 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
Darren Tucker07636982013-05-16 20:30:03 +1000149 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150} OpCodes;
151
152/* Textual representations of the tokens. */
153
Damien Miller95def091999-11-25 00:26:21 +1100154static struct {
155 const char *name;
156 OpCodes opcode;
157} keywords[] = {
158 { "forwardagent", oForwardAgent },
159 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000160 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000161 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000162 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000163 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100164 { "gatewayports", oGatewayPorts },
165 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000166 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100167 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100168 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
169 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000172 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000173 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000174 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000175 { "challengeresponseauthentication", oChallengeResponseAuthentication },
176 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
177 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000178 { "kerberosauthentication", oUnsupported },
179 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000180 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000181#if defined(GSSAPI)
182 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000183 { "gssapidelegatecredentials", oGssDelegateCreds },
184#else
185 { "gssapiauthentication", oUnsupported },
186 { "gssapidelegatecredentials", oUnsupported },
187#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000188 { "fallbacktorsh", oDeprecated },
189 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100191 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100192 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000194 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "proxycommand", oProxyCommand },
196 { "port", oPort },
197 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000198 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000199 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000200 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100201 { "remoteforward", oRemoteForward },
202 { "localforward", oLocalForward },
203 { "user", oUser },
204 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100205 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100206 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100207 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000208 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100209 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000210 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100211 { "connectionattempts", oConnectionAttempts },
212 { "batchmode", oBatchMode },
213 { "checkhostip", oCheckHostIP },
214 { "stricthostkeychecking", oStrictHostKeyChecking },
215 { "compression", oCompression },
216 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100217 { "tcpkeepalive", oTCPKeepAlive },
218 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100219 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100220 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000221 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000222 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000223 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000224 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100225#ifdef ENABLE_PKCS11
226 { "smartcarddevice", oPKCS11Provider },
227 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000228#else
229 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100230 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000231#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100232 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000233 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000234 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100235 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000236 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000237 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000238 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100239 { "serveraliveinterval", oServerAliveInterval },
240 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000241 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000242 { "controlpath", oControlPath },
243 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000244 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100245 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100246 { "tunnel", oTunnel },
247 { "tunneldevice", oTunnelDevice },
248 { "localcommand", oLocalCommand },
249 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000250 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000251 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100252#ifdef JPAKE
253 { "zeroknowledgepasswordauthentication",
254 oZeroKnowledgePasswordAuthentication },
255#else
256 { "zeroknowledgepasswordauthentication", oUnsupported },
257#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000258 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100259 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000260 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000261 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100262 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100263 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
264 { "canonicalizehostname", oCanonicalizeHostname },
265 { "canonicalizemaxdots", oCanonicalizeMaxDots },
266 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Darren Tucker07636982013-05-16 20:30:03 +1000267 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100268
Ben Lindstrom65366a82001-12-06 16:32:47 +0000269 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100270};
271
Damien Miller5428f641999-11-25 11:54:57 +1100272/*
273 * Adds a local TCP/IP port forward to options. Never returns if there is an
274 * error.
275 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller4af51302000-04-16 11:18:38 +1000277void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
Damien Miller95def091999-11-25 00:26:21 +1100280 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000281#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100282 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000284 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100285#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000286 options->local_forwards = xrealloc(options->local_forwards,
287 options->num_local_forwards + 1,
288 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100289 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290
Damien Miller1a0442f2008-11-05 16:30:06 +1100291 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100292 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100293 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
296
Damien Miller5428f641999-11-25 11:54:57 +1100297/*
298 * Adds a remote TCP/IP port forward to options. Never returns if there is
299 * an error.
300 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Damien Miller4af51302000-04-16 11:18:38 +1000302void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100303add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304{
Damien Miller95def091999-11-25 00:26:21 +1100305 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000306
307 options->remote_forwards = xrealloc(options->remote_forwards,
308 options->num_remote_forwards + 1,
309 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100310 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100311
Damien Miller1a0442f2008-11-05 16:30:06 +1100312 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100313 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100314 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100315 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100316 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000317 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318}
319
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000320static void
321clear_forwardings(Options *options)
322{
323 int i;
324
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000326 free(options->local_forwards[i].listen_host);
327 free(options->local_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100328 }
Damien Miller232cfb12010-06-26 09:50:30 +1000329 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000330 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000331 options->local_forwards = NULL;
332 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000333 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000335 free(options->remote_forwards[i].listen_host);
336 free(options->remote_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337 }
Damien Miller232cfb12010-06-26 09:50:30 +1000338 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000339 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000340 options->remote_forwards = NULL;
341 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000342 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100343 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000344}
345
Darren Tucker19104782013-04-05 11:13:08 +1100346void
347add_identity_file(Options *options, const char *dir, const char *filename,
348 int userprovided)
349{
350 char *path;
351
352 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
353 fatal("Too many identity files specified (max %d)",
354 SSH_MAX_IDENTITY_FILES);
355
356 if (dir == NULL) /* no dir, filename is absolute */
357 path = xstrdup(filename);
358 else
359 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
360
361 options->identity_file_userprovided[options->num_identity_files] =
362 userprovided;
363 options->identity_files[options->num_identity_files++] = path;
364}
365
Damien Miller194fd902013-10-15 12:13:05 +1100366int
367default_ssh_port(void)
368{
369 static int port;
370 struct servent *sp;
371
372 if (port == 0) {
373 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
374 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
375 }
376 return port;
377}
378
379/*
380 * Execute a command in a shell.
381 * Return its exit status or -1 on abnormal exit.
382 */
383static int
384execute_in_shell(const char *cmd)
385{
386 char *shell, *command_string;
387 pid_t pid;
388 int devnull, status;
389 extern uid_t original_real_uid;
390
391 if ((shell = getenv("SHELL")) == NULL)
392 shell = _PATH_BSHELL;
393
394 /*
395 * Use "exec" to avoid "sh -c" processes on some platforms
396 * (e.g. Solaris)
397 */
398 xasprintf(&command_string, "exec %s", cmd);
399
400 /* Need this to redirect subprocess stdin/out */
401 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
402 fatal("open(/dev/null): %s", strerror(errno));
403
404 debug("Executing command: '%.500s'", cmd);
405
406 /* Fork and execute the command. */
407 if ((pid = fork()) == 0) {
408 char *argv[4];
409
410 /* Child. Permanently give up superuser privileges. */
411 permanently_drop_suid(original_real_uid);
412
413 /* Redirect child stdin and stdout. Leave stderr */
414 if (dup2(devnull, STDIN_FILENO) == -1)
415 fatal("dup2: %s", strerror(errno));
416 if (dup2(devnull, STDOUT_FILENO) == -1)
417 fatal("dup2: %s", strerror(errno));
418 if (devnull > STDERR_FILENO)
419 close(devnull);
420 closefrom(STDERR_FILENO + 1);
421
422 argv[0] = shell;
423 argv[1] = "-c";
424 argv[2] = command_string;
425 argv[3] = NULL;
426
427 execv(argv[0], argv);
428 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
429 /* Die with signal to make this error apparent to parent. */
430 signal(SIGTERM, SIG_DFL);
431 kill(getpid(), SIGTERM);
432 _exit(1);
433 }
434 /* Parent. */
435 if (pid < 0)
436 fatal("%s: fork: %.100s", __func__, strerror(errno));
437
438 close(devnull);
439 free(command_string);
440
441 while (waitpid(pid, &status, 0) == -1) {
442 if (errno != EINTR && errno != EAGAIN)
443 fatal("%s: waitpid: %s", __func__, strerror(errno));
444 }
445 if (!WIFEXITED(status)) {
446 error("command '%.100s' exited abnormally", cmd);
447 return -1;
448 }
449 debug3("command returned status %d", WEXITSTATUS(status));
450 return WEXITSTATUS(status);
451}
452
453/*
454 * Parse and execute a Match directive.
455 */
456static int
457match_cfg_line(Options *options, char **condition, struct passwd *pw,
458 const char *host_arg, const char *filename, int linenum)
459{
Damien Miller084bcd22013-10-23 16:30:51 +1100460 char *arg, *attrib, *cmd, *cp = *condition, *host;
461 const char *ruser;
Damien Miller194fd902013-10-15 12:13:05 +1100462 int r, port, result = 1;
463 size_t len;
464 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
465
466 /*
467 * Configuration is likely to be incomplete at this point so we
468 * must be prepared to use default values.
469 */
470 port = options->port <= 0 ? default_ssh_port() : options->port;
471 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100472 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100473 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100474 host = percent_expand(options->hostname,
475 "h", host_arg, (char *)NULL);
476 } else
477 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100478
479 debug3("checking match for '%s' host %s", cp, host);
480 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
481 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
482 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100483 result = -1;
484 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100485 }
486 len = strlen(arg);
487 if (strcasecmp(attrib, "host") == 0) {
488 if (match_hostname(host, arg, len) != 1)
489 result = 0;
490 else
491 debug("%.200s line %d: matched 'Host %.100s' ",
492 filename, linenum, host);
493 } else if (strcasecmp(attrib, "originalhost") == 0) {
494 if (match_hostname(host_arg, arg, len) != 1)
495 result = 0;
496 else
497 debug("%.200s line %d: matched "
498 "'OriginalHost %.100s' ",
499 filename, linenum, host_arg);
500 } else if (strcasecmp(attrib, "user") == 0) {
501 if (match_pattern_list(ruser, arg, len, 0) != 1)
502 result = 0;
503 else
504 debug("%.200s line %d: matched 'User %.100s' ",
505 filename, linenum, ruser);
506 } else if (strcasecmp(attrib, "localuser") == 0) {
507 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
508 result = 0;
509 else
510 debug("%.200s line %d: matched "
511 "'LocalUser %.100s' ",
512 filename, linenum, pw->pw_name);
Damien Miller8a04be72013-10-23 16:29:40 +1100513 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100514 if (gethostname(thishost, sizeof(thishost)) == -1)
515 fatal("gethostname: %s", strerror(errno));
516 strlcpy(shorthost, thishost, sizeof(shorthost));
517 shorthost[strcspn(thishost, ".")] = '\0';
518 snprintf(portstr, sizeof(portstr), "%d", port);
519
520 cmd = percent_expand(arg,
521 "L", shorthost,
522 "d", pw->pw_dir,
523 "h", host,
524 "l", thishost,
525 "n", host_arg,
526 "p", portstr,
527 "r", ruser,
528 "u", pw->pw_name,
529 (char *)NULL);
530 r = execute_in_shell(cmd);
531 if (r == -1) {
Damien Miller8a04be72013-10-23 16:29:40 +1100532 fatal("%.200s line %d: match exec '%.100s' "
Damien Miller194fd902013-10-15 12:13:05 +1100533 "error", filename, linenum, cmd);
534 } else if (r == 0) {
535 debug("%.200s line %d: matched "
Damien Miller8a04be72013-10-23 16:29:40 +1100536 "'exec \"%.100s\"' ",
Damien Miller194fd902013-10-15 12:13:05 +1100537 filename, linenum, cmd);
538 } else
539 result = 0;
540 free(cmd);
541 } else {
542 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100543 result = -1;
544 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100545 }
546 }
547 debug3("match %sfound", result ? "" : "not ");
548 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100549 out:
550 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100551 return result;
552}
553
Damien Miller0faf7472013-10-17 11:47:23 +1100554/* Check and prepare a domain name: removes trailing '.' and lowercases */
555static void
556valid_domain(char *name, const char *filename, int linenum)
557{
558 size_t i, l = strlen(name);
559 u_char c, last = '\0';
560
561 if (l == 0)
562 fatal("%s line %d: empty hostname suffix", filename, linenum);
563 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
564 fatal("%s line %d: hostname suffix \"%.100s\" "
565 "starts with invalid character", filename, linenum, name);
566 for (i = 0; i < l; i++) {
567 c = tolower((u_char)name[i]);
568 name[i] = (char)c;
569 if (last == '.' && c == '.')
570 fatal("%s line %d: hostname suffix \"%.100s\" contains "
571 "consecutive separators", filename, linenum, name);
572 if (c != '.' && c != '-' && !isalnum(c) &&
573 c != '_') /* technically invalid, but common */
574 fatal("%s line %d: hostname suffix \"%.100s\" contains "
575 "invalid characters", filename, linenum, name);
576 last = c;
577 }
578 if (name[l - 1] == '.')
579 name[l - 1] = '\0';
580}
581
Damien Miller5428f641999-11-25 11:54:57 +1100582/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000583 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100584 */
Damien Miller4af51302000-04-16 11:18:38 +1000585static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000586parse_token(const char *cp, const char *filename, int linenum,
587 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588{
Darren Tucker07636982013-05-16 20:30:03 +1000589 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller95def091999-11-25 00:26:21 +1100591 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000592 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100593 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000594 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
595 strlen(ignored_unknown), 1) == 1)
596 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000597 error("%s: line %d: Bad configuration option: %s",
598 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100599 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600}
601
Damien Millere9fc72e2013-10-15 12:14:12 +1100602/* Multistate option parsing */
603struct multistate {
604 char *key;
605 int value;
606};
607static const struct multistate multistate_flag[] = {
608 { "true", 1 },
609 { "false", 0 },
610 { "yes", 1 },
611 { "no", 0 },
612 { NULL, -1 }
613};
614static const struct multistate multistate_yesnoask[] = {
615 { "true", 1 },
616 { "false", 0 },
617 { "yes", 1 },
618 { "no", 0 },
619 { "ask", 2 },
620 { NULL, -1 }
621};
622static const struct multistate multistate_addressfamily[] = {
623 { "inet", AF_INET },
624 { "inet6", AF_INET6 },
625 { "any", AF_UNSPEC },
626 { NULL, -1 }
627};
628static const struct multistate multistate_controlmaster[] = {
629 { "true", SSHCTL_MASTER_YES },
630 { "yes", SSHCTL_MASTER_YES },
631 { "false", SSHCTL_MASTER_NO },
632 { "no", SSHCTL_MASTER_NO },
633 { "auto", SSHCTL_MASTER_AUTO },
634 { "ask", SSHCTL_MASTER_ASK },
635 { "autoask", SSHCTL_MASTER_AUTO_ASK },
636 { NULL, -1 }
637};
638static const struct multistate multistate_tunnel[] = {
639 { "ethernet", SSH_TUNMODE_ETHERNET },
640 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
641 { "true", SSH_TUNMODE_DEFAULT },
642 { "yes", SSH_TUNMODE_DEFAULT },
643 { "false", SSH_TUNMODE_NO },
644 { "no", SSH_TUNMODE_NO },
645 { NULL, -1 }
646};
647static const struct multistate multistate_requesttty[] = {
648 { "true", REQUEST_TTY_YES },
649 { "yes", REQUEST_TTY_YES },
650 { "false", REQUEST_TTY_NO },
651 { "no", REQUEST_TTY_NO },
652 { "force", REQUEST_TTY_FORCE },
653 { "auto", REQUEST_TTY_AUTO },
654 { NULL, -1 }
655};
Damien Miller38505592013-10-17 11:48:13 +1100656static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100657 { "true", SSH_CANONICALISE_YES },
658 { "false", SSH_CANONICALISE_NO },
659 { "yes", SSH_CANONICALISE_YES },
660 { "no", SSH_CANONICALISE_NO },
661 { "always", SSH_CANONICALISE_ALWAYS },
662 { NULL, -1 }
663};
Damien Millere9fc72e2013-10-15 12:14:12 +1100664
Damien Miller5428f641999-11-25 11:54:57 +1100665/*
666 * Processes a single option line as used in the configuration files. This
667 * only sets those values that have not already been set.
668 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100669#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100670int
Damien Miller194fd902013-10-15 12:13:05 +1100671process_config_line(Options *options, struct passwd *pw, const char *host,
672 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673{
Damien Miller295ee632011-05-29 21:42:31 +1000674 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
675 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000676 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100677 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100678 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000679 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100680 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100681 Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100682 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100683 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller194fd902013-10-15 12:13:05 +1100685 if (activep == NULL) { /* We are processing a command line directive */
686 cmdline = 1;
687 activep = &cmdline;
688 }
689
Damien Millerc652cac2003-05-14 13:40:54 +1000690 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100691 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000692 if (strchr(WHITESPACE, line[len]) == NULL)
693 break;
694 line[len] = '\0';
695 }
696
Damien Millerbe484b52000-07-15 14:14:16 +1000697 s = line;
698 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100699 if ((keyword = strdelim(&s)) == NULL)
700 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000701 /* Ignore leading whitespace. */
702 if (*keyword == '\0')
703 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000704 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100705 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000706 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100707 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Darren Tucker07636982013-05-16 20:30:03 +1000709 opcode = parse_token(keyword, filename, linenum,
710 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
Damien Miller95def091999-11-25 00:26:21 +1100712 switch (opcode) {
713 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100714 /* don't panic, but count bad options */
715 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100716 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000717 case oIgnoredUnknownOption:
718 debug("%s line %d: Ignored unknown option \"%s\"",
719 filename, linenum, keyword);
720 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000721 case oConnectTimeout:
722 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100723parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000724 arg = strdelim(&s);
725 if (!arg || *arg == '\0')
726 fatal("%s line %d: missing time value.",
727 filename, linenum);
728 if ((value = convtime(arg)) == -1)
729 fatal("%s line %d: invalid time value.",
730 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100731 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000732 *intptr = value;
733 break;
734
Damien Miller95def091999-11-25 00:26:21 +1100735 case oForwardAgent:
736 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100737 parse_flag:
738 multistate_ptr = multistate_flag;
739 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000740 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000741 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100742 fatal("%s line %d: missing argument.",
743 filename, linenum);
744 value = -1;
745 for (i = 0; multistate_ptr[i].key != NULL; i++) {
746 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
747 value = multistate_ptr[i].value;
748 break;
749 }
750 }
751 if (value == -1)
752 fatal("%s line %d: unsupported option \"%s\".",
753 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100754 if (*activep && *intptr == -1)
755 *intptr = value;
756 break;
757
758 case oForwardX11:
759 intptr = &options->forward_x11;
760 goto parse_flag;
761
Darren Tucker0a118da2003-10-15 15:54:32 +1000762 case oForwardX11Trusted:
763 intptr = &options->forward_x11_trusted;
764 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000765
766 case oForwardX11Timeout:
767 intptr = &options->forward_x11_timeout;
768 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000769
Damien Miller95def091999-11-25 00:26:21 +1100770 case oGatewayPorts:
771 intptr = &options->gateway_ports;
772 goto parse_flag;
773
Darren Tuckere7d4b192006-07-12 22:17:10 +1000774 case oExitOnForwardFailure:
775 intptr = &options->exit_on_forward_failure;
776 goto parse_flag;
777
Damien Miller95def091999-11-25 00:26:21 +1100778 case oUsePrivilegedPort:
779 intptr = &options->use_privileged_port;
780 goto parse_flag;
781
Damien Miller95def091999-11-25 00:26:21 +1100782 case oPasswordAuthentication:
783 intptr = &options->password_authentication;
784 goto parse_flag;
785
Damien Miller01ed2272008-11-05 16:20:46 +1100786 case oZeroKnowledgePasswordAuthentication:
787 intptr = &options->zero_knowledge_password_authentication;
788 goto parse_flag;
789
Damien Miller874d77b2000-10-14 16:23:11 +1100790 case oKbdInteractiveAuthentication:
791 intptr = &options->kbd_interactive_authentication;
792 goto parse_flag;
793
794 case oKbdInteractiveDevices:
795 charptr = &options->kbd_interactive_devices;
796 goto parse_string;
797
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 case oPubkeyAuthentication:
799 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000800 goto parse_flag;
801
Damien Miller95def091999-11-25 00:26:21 +1100802 case oRSAAuthentication:
803 intptr = &options->rsa_authentication;
804 goto parse_flag;
805
806 case oRhostsRSAAuthentication:
807 intptr = &options->rhosts_rsa_authentication;
808 goto parse_flag;
809
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000810 case oHostbasedAuthentication:
811 intptr = &options->hostbased_authentication;
812 goto parse_flag;
813
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000814 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000815 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100816 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000817
Darren Tucker0efd1552003-08-26 11:49:55 +1000818 case oGssAuthentication:
819 intptr = &options->gss_authentication;
820 goto parse_flag;
821
822 case oGssDelegateCreds:
823 intptr = &options->gss_deleg_creds;
824 goto parse_flag;
825
Damien Miller95def091999-11-25 00:26:21 +1100826 case oBatchMode:
827 intptr = &options->batch_mode;
828 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829
Damien Miller95def091999-11-25 00:26:21 +1100830 case oCheckHostIP:
831 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000832 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833
Damien Miller37876e92003-05-15 10:19:46 +1000834 case oVerifyHostKeyDNS:
835 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100836 multistate_ptr = multistate_yesnoask;
837 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000838
Damien Miller95def091999-11-25 00:26:21 +1100839 case oStrictHostKeyChecking:
840 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100841 multistate_ptr = multistate_yesnoask;
842 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843
Damien Miller95def091999-11-25 00:26:21 +1100844 case oCompression:
845 intptr = &options->compression;
846 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Miller12c150e2003-12-17 16:31:10 +1100848 case oTCPKeepAlive:
849 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100850 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000852 case oNoHostAuthenticationForLocalhost:
853 intptr = &options->no_host_authentication_for_localhost;
854 goto parse_flag;
855
Damien Miller95def091999-11-25 00:26:21 +1100856 case oNumberOfPasswordPrompts:
857 intptr = &options->number_of_password_prompts;
858 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859
Damien Miller95def091999-11-25 00:26:21 +1100860 case oCompressionLevel:
861 intptr = &options->compression_level;
862 goto parse_int;
863
Damien Millera5539d22003-04-09 20:50:06 +1000864 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000865 arg = strdelim(&s);
866 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000867 fatal("%.200s line %d: Missing argument.", filename,
868 linenum);
869 if (strcmp(arg, "default") == 0) {
870 val64 = 0;
871 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000872 if (scan_scaled(arg, &val64) == -1)
873 fatal("%.200s line %d: Bad number '%s': %s",
874 filename, linenum, arg, strerror(errno));
875 /* check for too-large or too-small limits */
876 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000877 fatal("%.200s line %d: RekeyLimit too large",
878 filename, linenum);
879 if (val64 != 0 && val64 < 16)
880 fatal("%.200s line %d: RekeyLimit too small",
881 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000882 }
Damien Miller3dff1762008-02-10 22:25:52 +1100883 if (*activep && options->rekey_limit == -1)
884 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000885 if (s != NULL) { /* optional rekey interval present */
886 if (strcmp(s, "none") == 0) {
887 (void)strdelim(&s); /* discard */
888 break;
889 }
890 intptr = &options->rekey_interval;
891 goto parse_time;
892 }
Damien Millera5539d22003-04-09 20:50:06 +1000893 break;
894
Damien Miller95def091999-11-25 00:26:21 +1100895 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000896 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000897 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100898 fatal("%.200s line %d: Missing argument.", filename, linenum);
899 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100900 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000901 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100902 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100903 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100904 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100905 }
906 break;
907
Damien Millerd3a18572000-06-07 19:55:44 +1000908 case oXAuthLocation:
909 charptr=&options->xauth_location;
910 goto parse_string;
911
Damien Miller95def091999-11-25 00:26:21 +1100912 case oUser:
913 charptr = &options->user;
914parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000915 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000916 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000917 fatal("%.200s line %d: Missing argument.",
918 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100919 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000920 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100921 break;
922
923 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000924 cpptr = (char **)&options->system_hostfiles;
925 uintptr = &options->num_system_hostfiles;
926 max_entries = SSH_MAX_HOSTS_FILES;
927parse_char_array:
928 if (*activep && *uintptr == 0) {
929 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
930 if ((*uintptr) >= max_entries)
931 fatal("%s line %d: "
932 "too many authorized keys files.",
933 filename, linenum);
934 cpptr[(*uintptr)++] = xstrdup(arg);
935 }
936 }
937 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100938
939 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000940 cpptr = (char **)&options->user_hostfiles;
941 uintptr = &options->num_user_hostfiles;
942 max_entries = SSH_MAX_HOSTS_FILES;
943 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000944
Damien Miller95def091999-11-25 00:26:21 +1100945 case oHostName:
946 charptr = &options->hostname;
947 goto parse_string;
948
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000949 case oHostKeyAlias:
950 charptr = &options->host_key_alias;
951 goto parse_string;
952
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000953 case oPreferredAuthentications:
954 charptr = &options->preferred_authentications;
955 goto parse_string;
956
Ben Lindstrome0f88042001-04-30 13:06:24 +0000957 case oBindAddress:
958 charptr = &options->bind_address;
959 goto parse_string;
960
Damien Miller7ea845e2010-02-12 09:21:02 +1100961 case oPKCS11Provider:
962 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000963 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000964
Damien Miller95def091999-11-25 00:26:21 +1100965 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100966 charptr = &options->proxy_command;
967parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000968 if (s == NULL)
969 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100970 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100971 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100972 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100973 return 0;
974
975 case oPort:
976 intptr = &options->port;
977parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000978 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000979 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100980 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000981 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100982 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100983
984 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000985 value = strtol(arg, &endofnumber, 0);
986 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100987 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100988 if (*activep && *intptr == -1)
989 *intptr = value;
990 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 case oConnectionAttempts:
993 intptr = &options->connection_attempts;
994 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100995
Damien Miller95def091999-11-25 00:26:21 +1100996 case oCipher:
997 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000998 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000999 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001000 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001001 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001002 if (value == -1)
1003 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001004 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001005 if (*activep && *intptr == -1)
1006 *intptr = value;
1007 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller78928792000-04-12 20:17:38 +10001009 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001010 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001011 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001012 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001013 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001014 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001015 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001016 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001017 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001018 break;
1019
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001020 case oMacs:
1021 arg = strdelim(&s);
1022 if (!arg || *arg == '\0')
1023 fatal("%.200s line %d: Missing argument.", filename, linenum);
1024 if (!mac_valid(arg))
1025 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001026 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001027 if (*activep && options->macs == NULL)
1028 options->macs = xstrdup(arg);
1029 break;
1030
Damien Millerd5f62bf2010-09-24 22:11:14 +10001031 case oKexAlgorithms:
1032 arg = strdelim(&s);
1033 if (!arg || *arg == '\0')
1034 fatal("%.200s line %d: Missing argument.",
1035 filename, linenum);
1036 if (!kex_names_valid(arg))
1037 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1038 filename, linenum, arg ? arg : "<NONE>");
1039 if (*activep && options->kex_algorithms == NULL)
1040 options->kex_algorithms = xstrdup(arg);
1041 break;
1042
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001043 case oHostKeyAlgorithms:
1044 arg = strdelim(&s);
1045 if (!arg || *arg == '\0')
1046 fatal("%.200s line %d: Missing argument.", filename, linenum);
1047 if (!key_names_valid2(arg))
1048 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001049 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001050 if (*activep && options->hostkeyalgorithms == NULL)
1051 options->hostkeyalgorithms = xstrdup(arg);
1052 break;
1053
Damien Miller78928792000-04-12 20:17:38 +10001054 case oProtocol:
1055 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001056 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001057 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001058 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001059 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001060 if (value == SSH_PROTO_UNKNOWN)
1061 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001062 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001063 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1064 *intptr = value;
1065 break;
1066
Damien Miller95def091999-11-25 00:26:21 +11001067 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001068 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001069 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001070 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001071 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001072 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001073 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001074 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1075 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001076 break;
1077
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001078 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001079 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001080 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001081 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001082 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001083 fatal("%.200s line %d: Missing port argument.",
1084 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001085
Damien Millera699d952008-11-03 19:27:34 +11001086 if (opcode == oLocalForward ||
1087 opcode == oRemoteForward) {
1088 arg2 = strdelim(&s);
1089 if (arg2 == NULL || *arg2 == '\0')
1090 fatal("%.200s line %d: Missing target argument.",
1091 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001092
Damien Millera699d952008-11-03 19:27:34 +11001093 /* construct a string for parse_forward */
1094 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1095 } else if (opcode == oDynamicForward) {
1096 strlcpy(fwdarg, arg, sizeof(fwdarg));
1097 }
1098
1099 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001100 opcode == oDynamicForward ? 1 : 0,
1101 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001102 fatal("%.200s line %d: Bad forwarding specification.",
1103 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001104
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001105 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001106 if (opcode == oLocalForward ||
1107 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001108 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001109 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001110 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001111 }
Damien Miller95def091999-11-25 00:26:21 +11001112 break;
1113
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001114 case oClearAllForwardings:
1115 intptr = &options->clear_forwardings;
1116 goto parse_flag;
1117
Damien Miller95def091999-11-25 00:26:21 +11001118 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001119 if (cmdline)
1120 fatal("Host directive not supported as a command-line "
1121 "option");
Damien Miller95def091999-11-25 00:26:21 +11001122 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001123 arg2 = NULL;
1124 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1125 negated = *arg == '!';
1126 if (negated)
1127 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001128 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001129 if (negated) {
1130 debug("%.200s line %d: Skipping Host "
1131 "block because of negated match "
1132 "for %.100s", filename, linenum,
1133 arg);
1134 *activep = 0;
1135 break;
1136 }
1137 if (!*activep)
1138 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001139 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001140 }
Damien Millerfe924212011-05-15 08:44:45 +10001141 }
1142 if (*activep)
1143 debug("%.200s line %d: Applying options for %.100s",
1144 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001145 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001146 return 0;
1147
Damien Miller194fd902013-10-15 12:13:05 +11001148 case oMatch:
1149 if (cmdline)
1150 fatal("Host directive not supported as a command-line "
1151 "option");
1152 value = match_cfg_line(options, &s, pw, host,
1153 filename, linenum);
1154 if (value < 0)
1155 fatal("%.200s line %d: Bad Match condition", filename,
1156 linenum);
1157 *activep = value;
1158 break;
1159
Damien Miller95def091999-11-25 00:26:21 +11001160 case oEscapeChar:
1161 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001162 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001163 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001164 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001165 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001166 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1167 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001168 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001169 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001170 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001171 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001172 else {
1173 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001174 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001175 /* NOTREACHED */
1176 value = 0; /* Avoid compiler warning. */
1177 }
1178 if (*activep && *intptr == -1)
1179 *intptr = value;
1180 break;
1181
Damien Miller20a8f972003-05-18 20:50:30 +10001182 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001183 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001184 multistate_ptr = multistate_addressfamily;
1185 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001186
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001187 case oEnableSSHKeysign:
1188 intptr = &options->enable_ssh_keysign;
1189 goto parse_flag;
1190
Damien Millerbd394c32004-03-08 23:12:36 +11001191 case oIdentitiesOnly:
1192 intptr = &options->identities_only;
1193 goto parse_flag;
1194
Damien Miller509b0102003-12-17 16:33:10 +11001195 case oServerAliveInterval:
1196 intptr = &options->server_alive_interval;
1197 goto parse_time;
1198
1199 case oServerAliveCountMax:
1200 intptr = &options->server_alive_count_max;
1201 goto parse_int;
1202
Darren Tucker46bc0752004-05-02 22:11:30 +10001203 case oSendEnv:
1204 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1205 if (strchr(arg, '=') != NULL)
1206 fatal("%s line %d: Invalid environment name.",
1207 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001208 if (!*activep)
1209 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001210 if (options->num_send_env >= MAX_SEND_ENV)
1211 fatal("%s line %d: too many send env.",
1212 filename, linenum);
1213 options->send_env[options->num_send_env++] =
1214 xstrdup(arg);
1215 }
1216 break;
1217
Damien Miller0e220db2004-06-15 10:34:08 +10001218 case oControlPath:
1219 charptr = &options->control_path;
1220 goto parse_string;
1221
1222 case oControlMaster:
1223 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001224 multistate_ptr = multistate_controlmaster;
1225 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001226
Damien Millere11e1ea2010-08-03 16:04:46 +10001227 case oControlPersist:
1228 /* no/false/yes/true, or a time spec */
1229 intptr = &options->control_persist;
1230 arg = strdelim(&s);
1231 if (!arg || *arg == '\0')
1232 fatal("%.200s line %d: Missing ControlPersist"
1233 " argument.", filename, linenum);
1234 value = 0;
1235 value2 = 0; /* timeout */
1236 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1237 value = 0;
1238 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1239 value = 1;
1240 else if ((value2 = convtime(arg)) >= 0)
1241 value = 1;
1242 else
1243 fatal("%.200s line %d: Bad ControlPersist argument.",
1244 filename, linenum);
1245 if (*activep && *intptr == -1) {
1246 *intptr = value;
1247 options->control_persist_timeout = value2;
1248 }
1249 break;
1250
Damien Millere1776152005-03-01 21:47:37 +11001251 case oHashKnownHosts:
1252 intptr = &options->hash_known_hosts;
1253 goto parse_flag;
1254
Damien Millerd27b9472005-12-13 19:29:02 +11001255 case oTunnel:
1256 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001257 multistate_ptr = multistate_tunnel;
1258 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001259
1260 case oTunnelDevice:
1261 arg = strdelim(&s);
1262 if (!arg || *arg == '\0')
1263 fatal("%.200s line %d: Missing argument.", filename, linenum);
1264 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001265 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001266 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1267 if (*activep) {
1268 options->tun_local = value;
1269 options->tun_remote = value2;
1270 }
1271 break;
1272
1273 case oLocalCommand:
1274 charptr = &options->local_command;
1275 goto parse_command;
1276
1277 case oPermitLocalCommand:
1278 intptr = &options->permit_local_command;
1279 goto parse_flag;
1280
Damien Miller10288242008-06-30 00:04:03 +10001281 case oVisualHostKey:
1282 intptr = &options->visual_host_key;
1283 goto parse_flag;
1284
Damien Miller0dac6fb2010-11-20 15:19:38 +11001285 case oIPQoS:
1286 arg = strdelim(&s);
1287 if ((value = parse_ipqos(arg)) == -1)
1288 fatal("%s line %d: Bad IPQoS value: %s",
1289 filename, linenum, arg);
1290 arg = strdelim(&s);
1291 if (arg == NULL)
1292 value2 = value;
1293 else if ((value2 = parse_ipqos(arg)) == -1)
1294 fatal("%s line %d: Bad IPQoS value: %s",
1295 filename, linenum, arg);
1296 if (*activep) {
1297 options->ip_qos_interactive = value;
1298 options->ip_qos_bulk = value2;
1299 }
1300 break;
1301
Darren Tucker71e4d542009-07-06 07:12:27 +10001302 case oUseRoaming:
1303 intptr = &options->use_roaming;
1304 goto parse_flag;
1305
Damien Miller21771e22011-05-15 08:45:50 +10001306 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001307 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001308 multistate_ptr = multistate_requesttty;
1309 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001310
Darren Tucker07636982013-05-16 20:30:03 +10001311 case oIgnoreUnknown:
1312 charptr = &options->ignored_unknown;
1313 goto parse_string;
1314
Damien Miller1262b662013-08-21 02:44:24 +10001315 case oProxyUseFdpass:
1316 intptr = &options->proxy_use_fdpass;
1317 goto parse_flag;
1318
Damien Miller0faf7472013-10-17 11:47:23 +11001319 case oCanonicalDomains:
1320 value = options->num_canonical_domains != 0;
1321 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1322 valid_domain(arg, filename, linenum);
1323 if (!*activep || value)
1324 continue;
1325 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1326 fatal("%s line %d: too many hostname suffixes.",
1327 filename, linenum);
1328 options->canonical_domains[
1329 options->num_canonical_domains++] = xstrdup(arg);
1330 }
1331 break;
1332
Damien Miller38505592013-10-17 11:48:13 +11001333 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001334 value = options->num_permitted_cnames != 0;
1335 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1336 /* Either '*' for everything or 'list:list' */
1337 if (strcmp(arg, "*") == 0)
1338 arg2 = arg;
1339 else {
1340 lowercase(arg);
1341 if ((arg2 = strchr(arg, ':')) == NULL ||
1342 arg2[1] == '\0') {
1343 fatal("%s line %d: "
1344 "Invalid permitted CNAME \"%s\"",
1345 filename, linenum, arg);
1346 }
1347 *arg2 = '\0';
1348 arg2++;
1349 }
1350 if (!*activep || value)
1351 continue;
1352 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1353 fatal("%s line %d: too many permitted CNAMEs.",
1354 filename, linenum);
1355 cname = options->permitted_cnames +
1356 options->num_permitted_cnames++;
1357 cname->source_list = xstrdup(arg);
1358 cname->target_list = xstrdup(arg2);
1359 }
1360 break;
1361
Damien Miller38505592013-10-17 11:48:13 +11001362 case oCanonicalizeHostname:
1363 intptr = &options->canonicalize_hostname;
1364 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001365 goto parse_multistate;
1366
Damien Miller38505592013-10-17 11:48:13 +11001367 case oCanonicalizeMaxDots:
1368 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001369 goto parse_int;
1370
Damien Miller38505592013-10-17 11:48:13 +11001371 case oCanonicalizeFallbackLocal:
1372 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001373 goto parse_flag;
1374
Ben Lindstrom4daea862002-06-09 20:04:02 +00001375 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001376 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001377 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001378 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001379
Damien Millerf9b3feb2003-05-16 11:38:32 +10001380 case oUnsupported:
1381 error("%s line %d: Unsupported option \"%s\"",
1382 filename, linenum, keyword);
1383 return 0;
1384
Damien Miller95def091999-11-25 00:26:21 +11001385 default:
1386 fatal("process_config_line: Unimplemented opcode %d", opcode);
1387 }
1388
1389 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001390 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001391 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001392 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001393 }
Damien Miller95def091999-11-25 00:26:21 +11001394 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395}
1396
1397
Damien Miller5428f641999-11-25 11:54:57 +11001398/*
1399 * Reads the config file and modifies the options accordingly. Options
1400 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001401 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001402 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001404int
Damien Miller194fd902013-10-15 12:13:05 +11001405read_config_file(const char *filename, struct passwd *pw, const char *host,
1406 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407{
Damien Miller95def091999-11-25 00:26:21 +11001408 FILE *f;
1409 char line[1024];
1410 int active, linenum;
1411 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412
Damien Miller57a44762004-04-20 20:11:57 +10001413 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001414 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415
Darren Tuckeraefa3682013-04-05 11:18:35 +11001416 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001417 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001418
Damien Miller33793852004-06-15 10:27:55 +10001419 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001420 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001421 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001422 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001423 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001424 }
1425
Damien Miller95def091999-11-25 00:26:21 +11001426 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427
Damien Miller5428f641999-11-25 11:54:57 +11001428 /*
1429 * Mark that we are now processing the options. This flag is turned
1430 * on/off by Host specifications.
1431 */
Damien Miller95def091999-11-25 00:26:21 +11001432 active = 1;
1433 linenum = 0;
1434 while (fgets(line, sizeof(line), f)) {
1435 /* Update line number counter. */
1436 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001437 if (process_config_line(options, pw, host, line, filename,
1438 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001439 bad_options++;
1440 }
1441 fclose(f);
1442 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001443 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001444 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001445 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
Damien Miller5428f641999-11-25 11:54:57 +11001448/*
1449 * Initializes options to special values that indicate that they have not yet
1450 * been set. Read_config_file will only set options with this value. Options
1451 * are processed in the following order: command line, user config file,
1452 * system config file. Last, fill_default_options is called.
1453 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Damien Miller4af51302000-04-16 11:18:38 +10001455void
Damien Miller95def091999-11-25 00:26:21 +11001456initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457{
Damien Miller95def091999-11-25 00:26:21 +11001458 memset(options, 'X', sizeof(*options));
1459 options->forward_agent = -1;
1460 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001461 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001462 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001463 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001464 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001465 options->gateway_ports = -1;
1466 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001467 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001468 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001469 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001470 options->gss_authentication = -1;
1471 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001472 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001473 options->kbd_interactive_authentication = -1;
1474 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001475 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001476 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001477 options->batch_mode = -1;
1478 options->check_host_ip = -1;
1479 options->strict_host_key_checking = -1;
1480 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001481 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001482 options->compression_level = -1;
1483 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001484 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001485 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001486 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001487 options->number_of_password_prompts = -1;
1488 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001489 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001490 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001491 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001492 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001493 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001494 options->num_identity_files = 0;
1495 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001496 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001497 options->proxy_command = NULL;
1498 options->user = NULL;
1499 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001500 options->num_system_hostfiles = 0;
1501 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001502 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001503 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001504 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001505 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001506 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001507 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001508 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001509 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001510 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001511 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001512 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001513 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001514 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001515 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001516 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001517 options->server_alive_interval = -1;
1518 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001519 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001520 options->control_path = NULL;
1521 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001522 options->control_persist = -1;
1523 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001524 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001525 options->tun_open = -1;
1526 options->tun_local = -1;
1527 options->tun_remote = -1;
1528 options->local_command = NULL;
1529 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001530 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001531 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001532 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001533 options->ip_qos_interactive = -1;
1534 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001535 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001536 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001537 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001538 options->num_canonical_domains = 0;
1539 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001540 options->canonicalize_max_dots = -1;
1541 options->canonicalize_fallback_local = -1;
1542 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001543}
1544
Damien Miller5428f641999-11-25 11:54:57 +11001545/*
1546 * Called after processing other sources of option data, this fills those
1547 * options for which no value has been specified with their default values.
1548 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller4af51302000-04-16 11:18:38 +10001550void
Damien Miller95def091999-11-25 00:26:21 +11001551fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552{
Damien Miller95def091999-11-25 00:26:21 +11001553 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001554 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001555 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001556 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001557 if (options->forward_x11_trusted == -1)
1558 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001559 if (options->forward_x11_timeout == -1)
1560 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001561 if (options->exit_on_forward_failure == -1)
1562 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001563 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001564 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001565 if (options->gateway_ports == -1)
1566 options->gateway_ports = 0;
1567 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001568 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001569 if (options->rsa_authentication == -1)
1570 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001571 if (options->pubkey_authentication == -1)
1572 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001573 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001574 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001575 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001576 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001577 if (options->gss_deleg_creds == -1)
1578 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001579 if (options->password_authentication == -1)
1580 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001581 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001582 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001583 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001584 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001585 if (options->hostbased_authentication == -1)
1586 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001587 if (options->batch_mode == -1)
1588 options->batch_mode = 0;
1589 if (options->check_host_ip == -1)
1590 options->check_host_ip = 1;
1591 if (options->strict_host_key_checking == -1)
1592 options->strict_host_key_checking = 2; /* 2 is default */
1593 if (options->compression == -1)
1594 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001595 if (options->tcp_keep_alive == -1)
1596 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001597 if (options->compression_level == -1)
1598 options->compression_level = 6;
1599 if (options->port == -1)
1600 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001601 if (options->address_family == -1)
1602 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001603 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001604 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001605 if (options->number_of_password_prompts == -1)
1606 options->number_of_password_prompts = 3;
1607 /* Selected in ssh_login(). */
1608 if (options->cipher == -1)
1609 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001610 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001611 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001612 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001613 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001614 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001615 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001616 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001617 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001618 add_identity_file(options, "~/",
1619 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001620 }
1621 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001622 add_identity_file(options, "~/",
1623 _PATH_SSH_CLIENT_ID_RSA, 0);
1624 add_identity_file(options, "~/",
1625 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001626#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001627 add_identity_file(options, "~/",
1628 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001629#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001630 }
Damien Millereba71ba2000-04-29 23:57:08 +10001631 }
Damien Miller95def091999-11-25 00:26:21 +11001632 if (options->escape_char == -1)
1633 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001634 if (options->num_system_hostfiles == 0) {
1635 options->system_hostfiles[options->num_system_hostfiles++] =
1636 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1637 options->system_hostfiles[options->num_system_hostfiles++] =
1638 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1639 }
1640 if (options->num_user_hostfiles == 0) {
1641 options->user_hostfiles[options->num_user_hostfiles++] =
1642 xstrdup(_PATH_SSH_USER_HOSTFILE);
1643 options->user_hostfiles[options->num_user_hostfiles++] =
1644 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1645 }
Damien Millerfcd93202002-02-05 12:26:34 +11001646 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001647 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001648 if (options->clear_forwardings == 1)
1649 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001650 if (options->no_host_authentication_for_localhost == - 1)
1651 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001652 if (options->identities_only == -1)
1653 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001654 if (options->enable_ssh_keysign == -1)
1655 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001656 if (options->rekey_limit == -1)
1657 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001658 if (options->rekey_interval == -1)
1659 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001660 if (options->verify_host_key_dns == -1)
1661 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001662 if (options->server_alive_interval == -1)
1663 options->server_alive_interval = 0;
1664 if (options->server_alive_count_max == -1)
1665 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001666 if (options->control_master == -1)
1667 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001668 if (options->control_persist == -1) {
1669 options->control_persist = 0;
1670 options->control_persist_timeout = 0;
1671 }
Damien Millere1776152005-03-01 21:47:37 +11001672 if (options->hash_known_hosts == -1)
1673 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001674 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001675 options->tun_open = SSH_TUNMODE_NO;
1676 if (options->tun_local == -1)
1677 options->tun_local = SSH_TUNID_ANY;
1678 if (options->tun_remote == -1)
1679 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001680 if (options->permit_local_command == -1)
1681 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001682 if (options->use_roaming == -1)
1683 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001684 if (options->visual_host_key == -1)
1685 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001686 if (options->zero_knowledge_password_authentication == -1)
1687 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001688 if (options->ip_qos_interactive == -1)
1689 options->ip_qos_interactive = IPTOS_LOWDELAY;
1690 if (options->ip_qos_bulk == -1)
1691 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001692 if (options->request_tty == -1)
1693 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001694 if (options->proxy_use_fdpass == -1)
1695 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001696 if (options->canonicalize_max_dots == -1)
1697 options->canonicalize_max_dots = 1;
1698 if (options->canonicalize_fallback_local == -1)
1699 options->canonicalize_fallback_local = 1;
1700 if (options->canonicalize_hostname == -1)
1701 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001702#define CLEAR_ON_NONE(v) \
1703 do { \
1704 if (v != NULL && strcasecmp(v, "none") == 0) { \
1705 free(v); \
1706 v = NULL; \
1707 } \
1708 } while(0)
1709 CLEAR_ON_NONE(options->local_command);
1710 CLEAR_ON_NONE(options->proxy_command);
1711 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001712 /* options->user will be set in the main program if appropriate */
1713 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001714 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001715 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001716}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001717
1718/*
1719 * parse_forward
1720 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001721 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001722 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001723 * dynamicfwd == 1
1724 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001725 * returns number of arguments parsed or zero on error
1726 */
1727int
Damien Miller4bf648f2009-02-14 16:28:21 +11001728parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001729{
1730 int i;
1731 char *p, *cp, *fwdarg[4];
1732
1733 memset(fwd, '\0', sizeof(*fwd));
1734
1735 cp = p = xstrdup(fwdspec);
1736
1737 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001738 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001739 cp++;
1740
1741 for (i = 0; i < 4; ++i)
1742 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1743 break;
1744
Damien Millerf4b39532008-11-03 19:28:21 +11001745 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001746 if (cp != NULL)
1747 i = 0; /* failure */
1748
1749 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001750 case 1:
1751 fwd->listen_host = NULL;
1752 fwd->listen_port = a2port(fwdarg[0]);
1753 fwd->connect_host = xstrdup("socks");
1754 break;
1755
1756 case 2:
1757 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1758 fwd->listen_port = a2port(fwdarg[1]);
1759 fwd->connect_host = xstrdup("socks");
1760 break;
1761
Damien Millerf91ee4c2005-03-01 21:24:33 +11001762 case 3:
1763 fwd->listen_host = NULL;
1764 fwd->listen_port = a2port(fwdarg[0]);
1765 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1766 fwd->connect_port = a2port(fwdarg[2]);
1767 break;
1768
1769 case 4:
1770 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1771 fwd->listen_port = a2port(fwdarg[1]);
1772 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1773 fwd->connect_port = a2port(fwdarg[3]);
1774 break;
1775 default:
1776 i = 0; /* failure */
1777 }
1778
Darren Tuckera627d422013-06-02 07:31:17 +10001779 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001780
Damien Millera699d952008-11-03 19:27:34 +11001781 if (dynamicfwd) {
1782 if (!(i == 1 || i == 2))
1783 goto fail_free;
1784 } else {
1785 if (!(i == 3 || i == 4))
1786 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001787 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001788 goto fail_free;
1789 }
1790
Damien Miller4bf648f2009-02-14 16:28:21 +11001791 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001792 goto fail_free;
1793
1794 if (fwd->connect_host != NULL &&
1795 strlen(fwd->connect_host) >= NI_MAXHOST)
1796 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001797 if (fwd->listen_host != NULL &&
1798 strlen(fwd->listen_host) >= NI_MAXHOST)
1799 goto fail_free;
1800
Damien Millerf91ee4c2005-03-01 21:24:33 +11001801
1802 return (i);
1803
1804 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001805 free(fwd->connect_host);
1806 fwd->connect_host = NULL;
1807 free(fwd->listen_host);
1808 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001809 return (0);
1810}