blob: dc884c9b1e34be866fc547e8700c5d57d36c9fec [file] [log] [blame]
Damien Miller13f97b22014-02-24 15:57:55 +11001/* $OpenBSD: readconf.c,v 1.218 2014/02/23 20:11:36 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100021
22#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110023#include <netinet/in_systm.h>
24#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110025#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Millerc7b06362006-03-15 11:53:45 +110027#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100028#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110029#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110031#ifdef HAVE_PATHS_H
32# include <paths.h>
33#endif
Damien Miller194fd902013-10-15 12:13:05 +110034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100039#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
Damien Millerc7b06362006-03-15 11:53:45 +110043
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100046#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "cipher.h"
48#include "pathnames.h"
49#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "readconf.h"
52#include "match.h"
53#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000055#include "kex.h"
56#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110057#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59/* Format of the configuration file:
60
61 # Configuration data is parsed as follows:
62 # 1. command line options
63 # 2. user-specific file
64 # 3. system-wide file
65 # Any configuration value is only changed the first time it is set.
66 # Thus, host-specific definitions should be at the beginning of the
67 # configuration file, and defaults at the end.
68
69 # Host-specific declarations. These may override anything above. A single
70 # host may match multiple declarations; these are processed in the order
71 # that they are given in.
72
73 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000074 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
76 Host fake.com
77 HostName another.host.name.real.org
78 User blaah
79 Port 34289
80 ForwardX11 no
81 ForwardAgent no
82
83 Host books.com
84 RemoteForward 9999 shadows.cs.hut.fi:9999
85 Cipher 3des
86
87 Host fascist.blob.com
88 Port 23123
89 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 PasswordAuthentication no
91
92 Host puukko.hut.fi
93 User t35124p
94 ProxyCommand ssh-proxy %h %p
95
96 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000097 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99 Host *.su
100 Cipher none
101 PasswordAuthentication no
102
Damien Millerd27b9472005-12-13 19:29:02 +1100103 Host vpn.fake.com
104 Tunnel yes
105 TunnelDevice 3
106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107 # Defaults for various options
108 Host *
109 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111 PasswordAuthentication yes
112 RSAAuthentication yes
113 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100115 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 IdentityFile ~/.ssh/identity
117 Port 22
118 EscapeChar ~
119
120*/
121
122/* Keyword tokens. */
123
Damien Miller95def091999-11-25 00:26:21 +1100124typedef enum {
125 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100126 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000127 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
128 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000129 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000130 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100131 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100133 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
134 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100135 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000136 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000138 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000139 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100140 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000141 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000142 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000143 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100144 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000145 oSendEnv, oControlPath, oControlMaster, oControlPersist,
146 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100147 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100148 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000149 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100150 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
151 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
Darren Tucker07636982013-05-16 20:30:03 +1000152 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153} OpCodes;
154
155/* Textual representations of the tokens. */
156
Damien Miller95def091999-11-25 00:26:21 +1100157static struct {
158 const char *name;
159 OpCodes opcode;
160} keywords[] = {
161 { "forwardagent", oForwardAgent },
162 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000163 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000164 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000165 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000166 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100167 { "gatewayports", oGatewayPorts },
168 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000169 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100171 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
172 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000175 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000176 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000177 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000178 { "challengeresponseauthentication", oChallengeResponseAuthentication },
179 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
180 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000181 { "kerberosauthentication", oUnsupported },
182 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000183 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000184#if defined(GSSAPI)
185 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000186 { "gssapidelegatecredentials", oGssDelegateCreds },
187#else
188 { "gssapiauthentication", oUnsupported },
189 { "gssapidelegatecredentials", oUnsupported },
190#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000191 { "fallbacktorsh", oDeprecated },
192 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100194 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100195 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100196 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000197 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100198 { "proxycommand", oProxyCommand },
199 { "port", oPort },
200 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000201 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000202 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000203 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "remoteforward", oRemoteForward },
205 { "localforward", oLocalForward },
206 { "user", oUser },
207 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100208 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100210 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000211 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100212 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000213 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100214 { "connectionattempts", oConnectionAttempts },
215 { "batchmode", oBatchMode },
216 { "checkhostip", oCheckHostIP },
217 { "stricthostkeychecking", oStrictHostKeyChecking },
218 { "compression", oCompression },
219 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100220 { "tcpkeepalive", oTCPKeepAlive },
221 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100222 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100223 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000224 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000225 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000226 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000227 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100228#ifdef ENABLE_PKCS11
229 { "smartcarddevice", oPKCS11Provider },
230 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000231#else
232 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100233 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000234#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100235 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000236 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000237 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000239 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000240 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000241 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100242 { "serveraliveinterval", oServerAliveInterval },
243 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000244 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000245 { "controlpath", oControlPath },
246 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000247 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100248 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100249 { "tunnel", oTunnel },
250 { "tunneldevice", oTunnelDevice },
251 { "localcommand", oLocalCommand },
252 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000253 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000254 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000255 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100256 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000257 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000258 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100259 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100260 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
261 { "canonicalizehostname", oCanonicalizeHostname },
262 { "canonicalizemaxdots", oCanonicalizeMaxDots },
263 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Darren Tucker07636982013-05-16 20:30:03 +1000264 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100265
Ben Lindstrom65366a82001-12-06 16:32:47 +0000266 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100267};
268
Damien Miller5428f641999-11-25 11:54:57 +1100269/*
270 * Adds a local TCP/IP port forward to options. Never returns if there is an
271 * error.
272 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller4af51302000-04-16 11:18:38 +1000274void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Damien Miller95def091999-11-25 00:26:21 +1100277 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000278#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100279 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000281 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100282#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000283 options->local_forwards = xrealloc(options->local_forwards,
284 options->num_local_forwards + 1,
285 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100286 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287
Damien Miller1a0442f2008-11-05 16:30:06 +1100288 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100289 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100290 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Damien Miller5428f641999-11-25 11:54:57 +1100294/*
295 * Adds a remote TCP/IP port forward to options. Never returns if there is
296 * an error.
297 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller4af51302000-04-16 11:18:38 +1000299void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Damien Miller95def091999-11-25 00:26:21 +1100302 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000303
304 options->remote_forwards = xrealloc(options->remote_forwards,
305 options->num_remote_forwards + 1,
306 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100307 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308
Damien Miller1a0442f2008-11-05 16:30:06 +1100309 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100310 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100311 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100312 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100313 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000314 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315}
316
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000317static void
318clear_forwardings(Options *options)
319{
320 int i;
321
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(options->local_forwards[i].listen_host);
324 free(options->local_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325 }
Damien Miller232cfb12010-06-26 09:50:30 +1000326 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000327 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000328 options->local_forwards = NULL;
329 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000330 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100331 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(options->remote_forwards[i].listen_host);
333 free(options->remote_forwards[i].connect_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 }
Damien Miller232cfb12010-06-26 09:50:30 +1000335 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000336 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000337 options->remote_forwards = NULL;
338 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000339 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100340 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000341}
342
Darren Tucker19104782013-04-05 11:13:08 +1100343void
344add_identity_file(Options *options, const char *dir, const char *filename,
345 int userprovided)
346{
347 char *path;
348
349 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
350 fatal("Too many identity files specified (max %d)",
351 SSH_MAX_IDENTITY_FILES);
352
353 if (dir == NULL) /* no dir, filename is absolute */
354 path = xstrdup(filename);
355 else
356 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
357
358 options->identity_file_userprovided[options->num_identity_files] =
359 userprovided;
360 options->identity_files[options->num_identity_files++] = path;
361}
362
Damien Miller194fd902013-10-15 12:13:05 +1100363int
364default_ssh_port(void)
365{
366 static int port;
367 struct servent *sp;
368
369 if (port == 0) {
370 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
371 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
372 }
373 return port;
374}
375
376/*
377 * Execute a command in a shell.
378 * Return its exit status or -1 on abnormal exit.
379 */
380static int
381execute_in_shell(const char *cmd)
382{
383 char *shell, *command_string;
384 pid_t pid;
385 int devnull, status;
386 extern uid_t original_real_uid;
387
388 if ((shell = getenv("SHELL")) == NULL)
389 shell = _PATH_BSHELL;
390
391 /*
392 * Use "exec" to avoid "sh -c" processes on some platforms
393 * (e.g. Solaris)
394 */
395 xasprintf(&command_string, "exec %s", cmd);
396
397 /* Need this to redirect subprocess stdin/out */
398 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
399 fatal("open(/dev/null): %s", strerror(errno));
400
401 debug("Executing command: '%.500s'", cmd);
402
403 /* Fork and execute the command. */
404 if ((pid = fork()) == 0) {
405 char *argv[4];
406
407 /* Child. Permanently give up superuser privileges. */
408 permanently_drop_suid(original_real_uid);
409
410 /* Redirect child stdin and stdout. Leave stderr */
411 if (dup2(devnull, STDIN_FILENO) == -1)
412 fatal("dup2: %s", strerror(errno));
413 if (dup2(devnull, STDOUT_FILENO) == -1)
414 fatal("dup2: %s", strerror(errno));
415 if (devnull > STDERR_FILENO)
416 close(devnull);
417 closefrom(STDERR_FILENO + 1);
418
419 argv[0] = shell;
420 argv[1] = "-c";
421 argv[2] = command_string;
422 argv[3] = NULL;
423
424 execv(argv[0], argv);
425 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
426 /* Die with signal to make this error apparent to parent. */
427 signal(SIGTERM, SIG_DFL);
428 kill(getpid(), SIGTERM);
429 _exit(1);
430 }
431 /* Parent. */
432 if (pid < 0)
433 fatal("%s: fork: %.100s", __func__, strerror(errno));
434
435 close(devnull);
436 free(command_string);
437
438 while (waitpid(pid, &status, 0) == -1) {
439 if (errno != EINTR && errno != EAGAIN)
440 fatal("%s: waitpid: %s", __func__, strerror(errno));
441 }
442 if (!WIFEXITED(status)) {
443 error("command '%.100s' exited abnormally", cmd);
444 return -1;
445 }
446 debug3("command returned status %d", WEXITSTATUS(status));
447 return WEXITSTATUS(status);
448}
449
450/*
451 * Parse and execute a Match directive.
452 */
453static int
454match_cfg_line(Options *options, char **condition, struct passwd *pw,
455 const char *host_arg, const char *filename, int linenum)
456{
Damien Miller084bcd22013-10-23 16:30:51 +1100457 char *arg, *attrib, *cmd, *cp = *condition, *host;
458 const char *ruser;
Damien Millercf31f382013-10-24 21:02:56 +1100459 int r, port, result = 1, attributes = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100460 size_t len;
461 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
462
463 /*
464 * Configuration is likely to be incomplete at this point so we
465 * must be prepared to use default values.
466 */
467 port = options->port <= 0 ? default_ssh_port() : options->port;
468 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100469 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100470 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100471 host = percent_expand(options->hostname,
472 "h", host_arg, (char *)NULL);
473 } else
474 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100475
476 debug3("checking match for '%s' host %s", cp, host);
477 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100478 attributes++;
479 if (strcasecmp(attrib, "all") == 0) {
480 if (attributes != 1 ||
481 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
482 error("'all' cannot be combined with other "
483 "Match attributes");
484 result = -1;
485 goto out;
486 }
487 *condition = cp;
488 result = 1;
489 goto out;
490 }
Damien Miller194fd902013-10-15 12:13:05 +1100491 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
492 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100493 result = -1;
494 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100495 }
496 len = strlen(arg);
497 if (strcasecmp(attrib, "host") == 0) {
498 if (match_hostname(host, arg, len) != 1)
499 result = 0;
500 else
501 debug("%.200s line %d: matched 'Host %.100s' ",
502 filename, linenum, host);
503 } else if (strcasecmp(attrib, "originalhost") == 0) {
504 if (match_hostname(host_arg, arg, len) != 1)
505 result = 0;
506 else
507 debug("%.200s line %d: matched "
508 "'OriginalHost %.100s' ",
509 filename, linenum, host_arg);
510 } else if (strcasecmp(attrib, "user") == 0) {
511 if (match_pattern_list(ruser, arg, len, 0) != 1)
512 result = 0;
513 else
514 debug("%.200s line %d: matched 'User %.100s' ",
515 filename, linenum, ruser);
516 } else if (strcasecmp(attrib, "localuser") == 0) {
517 if (match_pattern_list(pw->pw_name, arg, len, 0) != 1)
518 result = 0;
519 else
520 debug("%.200s line %d: matched "
521 "'LocalUser %.100s' ",
522 filename, linenum, pw->pw_name);
Damien Miller8a04be72013-10-23 16:29:40 +1100523 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100524 if (gethostname(thishost, sizeof(thishost)) == -1)
525 fatal("gethostname: %s", strerror(errno));
526 strlcpy(shorthost, thishost, sizeof(shorthost));
527 shorthost[strcspn(thishost, ".")] = '\0';
528 snprintf(portstr, sizeof(portstr), "%d", port);
529
530 cmd = percent_expand(arg,
531 "L", shorthost,
532 "d", pw->pw_dir,
533 "h", host,
534 "l", thishost,
535 "n", host_arg,
536 "p", portstr,
537 "r", ruser,
538 "u", pw->pw_name,
539 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100540 if (result != 1) {
541 /* skip execution if prior predicate failed */
542 debug("%.200s line %d: skipped exec \"%.100s\"",
Damien Miller194fd902013-10-15 12:13:05 +1100543 filename, linenum, cmd);
Damien Miller06287802014-02-24 15:56:45 +1100544 } else {
545 r = execute_in_shell(cmd);
546 if (r == -1) {
547 fatal("%.200s line %d: match exec "
548 "'%.100s' error", filename,
549 linenum, cmd);
550 } else if (r == 0) {
551 debug("%.200s line %d: matched "
552 "'exec \"%.100s\"'", filename,
553 linenum, cmd);
554 } else {
555 debug("%.200s line %d: no match "
556 "'exec \"%.100s\"'", filename,
557 linenum, cmd);
558 result = 0;
559 }
560 }
Damien Miller194fd902013-10-15 12:13:05 +1100561 free(cmd);
562 } else {
563 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100564 result = -1;
565 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100566 }
567 }
Damien Millercf31f382013-10-24 21:02:56 +1100568 if (attributes == 0) {
569 error("One or more attributes required for Match");
570 result = -1;
571 goto out;
572 }
Damien Miller194fd902013-10-15 12:13:05 +1100573 debug3("match %sfound", result ? "" : "not ");
574 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100575 out:
576 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100577 return result;
578}
579
Damien Miller0faf7472013-10-17 11:47:23 +1100580/* Check and prepare a domain name: removes trailing '.' and lowercases */
581static void
582valid_domain(char *name, const char *filename, int linenum)
583{
584 size_t i, l = strlen(name);
585 u_char c, last = '\0';
586
587 if (l == 0)
588 fatal("%s line %d: empty hostname suffix", filename, linenum);
589 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
590 fatal("%s line %d: hostname suffix \"%.100s\" "
591 "starts with invalid character", filename, linenum, name);
592 for (i = 0; i < l; i++) {
593 c = tolower((u_char)name[i]);
594 name[i] = (char)c;
595 if (last == '.' && c == '.')
596 fatal("%s line %d: hostname suffix \"%.100s\" contains "
597 "consecutive separators", filename, linenum, name);
598 if (c != '.' && c != '-' && !isalnum(c) &&
599 c != '_') /* technically invalid, but common */
600 fatal("%s line %d: hostname suffix \"%.100s\" contains "
601 "invalid characters", filename, linenum, name);
602 last = c;
603 }
604 if (name[l - 1] == '.')
605 name[l - 1] = '\0';
606}
607
Damien Miller5428f641999-11-25 11:54:57 +1100608/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000609 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100610 */
Damien Miller4af51302000-04-16 11:18:38 +1000611static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000612parse_token(const char *cp, const char *filename, int linenum,
613 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Darren Tucker07636982013-05-16 20:30:03 +1000615 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Damien Miller95def091999-11-25 00:26:21 +1100617 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000618 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100619 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000620 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
621 strlen(ignored_unknown), 1) == 1)
622 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000623 error("%s: line %d: Bad configuration option: %s",
624 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100625 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626}
627
Damien Millere9fc72e2013-10-15 12:14:12 +1100628/* Multistate option parsing */
629struct multistate {
630 char *key;
631 int value;
632};
633static const struct multistate multistate_flag[] = {
634 { "true", 1 },
635 { "false", 0 },
636 { "yes", 1 },
637 { "no", 0 },
638 { NULL, -1 }
639};
640static const struct multistate multistate_yesnoask[] = {
641 { "true", 1 },
642 { "false", 0 },
643 { "yes", 1 },
644 { "no", 0 },
645 { "ask", 2 },
646 { NULL, -1 }
647};
648static const struct multistate multistate_addressfamily[] = {
649 { "inet", AF_INET },
650 { "inet6", AF_INET6 },
651 { "any", AF_UNSPEC },
652 { NULL, -1 }
653};
654static const struct multistate multistate_controlmaster[] = {
655 { "true", SSHCTL_MASTER_YES },
656 { "yes", SSHCTL_MASTER_YES },
657 { "false", SSHCTL_MASTER_NO },
658 { "no", SSHCTL_MASTER_NO },
659 { "auto", SSHCTL_MASTER_AUTO },
660 { "ask", SSHCTL_MASTER_ASK },
661 { "autoask", SSHCTL_MASTER_AUTO_ASK },
662 { NULL, -1 }
663};
664static const struct multistate multistate_tunnel[] = {
665 { "ethernet", SSH_TUNMODE_ETHERNET },
666 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
667 { "true", SSH_TUNMODE_DEFAULT },
668 { "yes", SSH_TUNMODE_DEFAULT },
669 { "false", SSH_TUNMODE_NO },
670 { "no", SSH_TUNMODE_NO },
671 { NULL, -1 }
672};
673static const struct multistate multistate_requesttty[] = {
674 { "true", REQUEST_TTY_YES },
675 { "yes", REQUEST_TTY_YES },
676 { "false", REQUEST_TTY_NO },
677 { "no", REQUEST_TTY_NO },
678 { "force", REQUEST_TTY_FORCE },
679 { "auto", REQUEST_TTY_AUTO },
680 { NULL, -1 }
681};
Damien Miller38505592013-10-17 11:48:13 +1100682static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100683 { "true", SSH_CANONICALISE_YES },
684 { "false", SSH_CANONICALISE_NO },
685 { "yes", SSH_CANONICALISE_YES },
686 { "no", SSH_CANONICALISE_NO },
687 { "always", SSH_CANONICALISE_ALWAYS },
688 { NULL, -1 }
689};
Damien Millere9fc72e2013-10-15 12:14:12 +1100690
Damien Miller5428f641999-11-25 11:54:57 +1100691/*
692 * Processes a single option line as used in the configuration files. This
693 * only sets those values that have not already been set.
694 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100695#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100696int
Damien Miller194fd902013-10-15 12:13:05 +1100697process_config_line(Options *options, struct passwd *pw, const char *host,
698 char *line, const char *filename, int linenum, int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699{
Damien Miller295ee632011-05-29 21:42:31 +1000700 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
701 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000702 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100703 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100704 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000705 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100706 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100708 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100709 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller194fd902013-10-15 12:13:05 +1100711 if (activep == NULL) { /* We are processing a command line directive */
712 cmdline = 1;
713 activep = &cmdline;
714 }
715
Damien Millerc652cac2003-05-14 13:40:54 +1000716 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100717 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000718 if (strchr(WHITESPACE, line[len]) == NULL)
719 break;
720 line[len] = '\0';
721 }
722
Damien Millerbe484b52000-07-15 14:14:16 +1000723 s = line;
724 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100725 if ((keyword = strdelim(&s)) == NULL)
726 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000727 /* Ignore leading whitespace. */
728 if (*keyword == '\0')
729 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000730 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100731 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000732 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100733 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Darren Tucker07636982013-05-16 20:30:03 +1000735 opcode = parse_token(keyword, filename, linenum,
736 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller95def091999-11-25 00:26:21 +1100738 switch (opcode) {
739 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100740 /* don't panic, but count bad options */
741 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100742 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000743 case oIgnoredUnknownOption:
744 debug("%s line %d: Ignored unknown option \"%s\"",
745 filename, linenum, keyword);
746 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000747 case oConnectTimeout:
748 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100749parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000750 arg = strdelim(&s);
751 if (!arg || *arg == '\0')
752 fatal("%s line %d: missing time value.",
753 filename, linenum);
754 if ((value = convtime(arg)) == -1)
755 fatal("%s line %d: invalid time value.",
756 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100757 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000758 *intptr = value;
759 break;
760
Damien Miller95def091999-11-25 00:26:21 +1100761 case oForwardAgent:
762 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100763 parse_flag:
764 multistate_ptr = multistate_flag;
765 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000766 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000767 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100768 fatal("%s line %d: missing argument.",
769 filename, linenum);
770 value = -1;
771 for (i = 0; multistate_ptr[i].key != NULL; i++) {
772 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
773 value = multistate_ptr[i].value;
774 break;
775 }
776 }
777 if (value == -1)
778 fatal("%s line %d: unsupported option \"%s\".",
779 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100780 if (*activep && *intptr == -1)
781 *intptr = value;
782 break;
783
784 case oForwardX11:
785 intptr = &options->forward_x11;
786 goto parse_flag;
787
Darren Tucker0a118da2003-10-15 15:54:32 +1000788 case oForwardX11Trusted:
789 intptr = &options->forward_x11_trusted;
790 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000791
792 case oForwardX11Timeout:
793 intptr = &options->forward_x11_timeout;
794 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000795
Damien Miller95def091999-11-25 00:26:21 +1100796 case oGatewayPorts:
797 intptr = &options->gateway_ports;
798 goto parse_flag;
799
Darren Tuckere7d4b192006-07-12 22:17:10 +1000800 case oExitOnForwardFailure:
801 intptr = &options->exit_on_forward_failure;
802 goto parse_flag;
803
Damien Miller95def091999-11-25 00:26:21 +1100804 case oUsePrivilegedPort:
805 intptr = &options->use_privileged_port;
806 goto parse_flag;
807
Damien Miller95def091999-11-25 00:26:21 +1100808 case oPasswordAuthentication:
809 intptr = &options->password_authentication;
810 goto parse_flag;
811
Damien Miller874d77b2000-10-14 16:23:11 +1100812 case oKbdInteractiveAuthentication:
813 intptr = &options->kbd_interactive_authentication;
814 goto parse_flag;
815
816 case oKbdInteractiveDevices:
817 charptr = &options->kbd_interactive_devices;
818 goto parse_string;
819
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820 case oPubkeyAuthentication:
821 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000822 goto parse_flag;
823
Damien Miller95def091999-11-25 00:26:21 +1100824 case oRSAAuthentication:
825 intptr = &options->rsa_authentication;
826 goto parse_flag;
827
828 case oRhostsRSAAuthentication:
829 intptr = &options->rhosts_rsa_authentication;
830 goto parse_flag;
831
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000832 case oHostbasedAuthentication:
833 intptr = &options->hostbased_authentication;
834 goto parse_flag;
835
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000836 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000837 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100838 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000839
Darren Tucker0efd1552003-08-26 11:49:55 +1000840 case oGssAuthentication:
841 intptr = &options->gss_authentication;
842 goto parse_flag;
843
844 case oGssDelegateCreds:
845 intptr = &options->gss_deleg_creds;
846 goto parse_flag;
847
Damien Miller95def091999-11-25 00:26:21 +1100848 case oBatchMode:
849 intptr = &options->batch_mode;
850 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 case oCheckHostIP:
853 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000854 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855
Damien Miller37876e92003-05-15 10:19:46 +1000856 case oVerifyHostKeyDNS:
857 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100858 multistate_ptr = multistate_yesnoask;
859 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000860
Damien Miller95def091999-11-25 00:26:21 +1100861 case oStrictHostKeyChecking:
862 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100863 multistate_ptr = multistate_yesnoask;
864 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Damien Miller95def091999-11-25 00:26:21 +1100866 case oCompression:
867 intptr = &options->compression;
868 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869
Damien Miller12c150e2003-12-17 16:31:10 +1100870 case oTCPKeepAlive:
871 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100872 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000874 case oNoHostAuthenticationForLocalhost:
875 intptr = &options->no_host_authentication_for_localhost;
876 goto parse_flag;
877
Damien Miller95def091999-11-25 00:26:21 +1100878 case oNumberOfPasswordPrompts:
879 intptr = &options->number_of_password_prompts;
880 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
Damien Miller95def091999-11-25 00:26:21 +1100882 case oCompressionLevel:
883 intptr = &options->compression_level;
884 goto parse_int;
885
Damien Millera5539d22003-04-09 20:50:06 +1000886 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000887 arg = strdelim(&s);
888 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000889 fatal("%.200s line %d: Missing argument.", filename,
890 linenum);
891 if (strcmp(arg, "default") == 0) {
892 val64 = 0;
893 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000894 if (scan_scaled(arg, &val64) == -1)
895 fatal("%.200s line %d: Bad number '%s': %s",
896 filename, linenum, arg, strerror(errno));
897 /* check for too-large or too-small limits */
898 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000899 fatal("%.200s line %d: RekeyLimit too large",
900 filename, linenum);
901 if (val64 != 0 && val64 < 16)
902 fatal("%.200s line %d: RekeyLimit too small",
903 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000904 }
Damien Miller3dff1762008-02-10 22:25:52 +1100905 if (*activep && options->rekey_limit == -1)
906 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000907 if (s != NULL) { /* optional rekey interval present */
908 if (strcmp(s, "none") == 0) {
909 (void)strdelim(&s); /* discard */
910 break;
911 }
912 intptr = &options->rekey_interval;
913 goto parse_time;
914 }
Damien Millera5539d22003-04-09 20:50:06 +1000915 break;
916
Damien Miller95def091999-11-25 00:26:21 +1100917 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000918 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000919 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100920 fatal("%.200s line %d: Missing argument.", filename, linenum);
921 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100922 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000923 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100924 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100925 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100926 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100927 }
928 break;
929
Damien Millerd3a18572000-06-07 19:55:44 +1000930 case oXAuthLocation:
931 charptr=&options->xauth_location;
932 goto parse_string;
933
Damien Miller95def091999-11-25 00:26:21 +1100934 case oUser:
935 charptr = &options->user;
936parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000937 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000938 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000939 fatal("%.200s line %d: Missing argument.",
940 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100941 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000942 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100943 break;
944
945 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000946 cpptr = (char **)&options->system_hostfiles;
947 uintptr = &options->num_system_hostfiles;
948 max_entries = SSH_MAX_HOSTS_FILES;
949parse_char_array:
950 if (*activep && *uintptr == 0) {
951 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
952 if ((*uintptr) >= max_entries)
953 fatal("%s line %d: "
954 "too many authorized keys files.",
955 filename, linenum);
956 cpptr[(*uintptr)++] = xstrdup(arg);
957 }
958 }
959 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100960
961 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000962 cpptr = (char **)&options->user_hostfiles;
963 uintptr = &options->num_user_hostfiles;
964 max_entries = SSH_MAX_HOSTS_FILES;
965 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000966
Damien Miller95def091999-11-25 00:26:21 +1100967 case oHostName:
968 charptr = &options->hostname;
969 goto parse_string;
970
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000971 case oHostKeyAlias:
972 charptr = &options->host_key_alias;
973 goto parse_string;
974
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000975 case oPreferredAuthentications:
976 charptr = &options->preferred_authentications;
977 goto parse_string;
978
Ben Lindstrome0f88042001-04-30 13:06:24 +0000979 case oBindAddress:
980 charptr = &options->bind_address;
981 goto parse_string;
982
Damien Miller7ea845e2010-02-12 09:21:02 +1100983 case oPKCS11Provider:
984 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000985 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000986
Damien Miller95def091999-11-25 00:26:21 +1100987 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100988 charptr = &options->proxy_command;
989parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000990 if (s == NULL)
991 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100992 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100993 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100994 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100995 return 0;
996
997 case oPort:
998 intptr = &options->port;
999parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001000 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001001 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001002 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001003 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001004 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001005
1006 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001007 value = strtol(arg, &endofnumber, 0);
1008 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001009 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001010 if (*activep && *intptr == -1)
1011 *intptr = value;
1012 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller95def091999-11-25 00:26:21 +11001014 case oConnectionAttempts:
1015 intptr = &options->connection_attempts;
1016 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001017
Damien Miller95def091999-11-25 00:26:21 +11001018 case oCipher:
1019 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001020 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001021 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001022 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001023 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001024 if (value == -1)
1025 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001026 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001027 if (*activep && *intptr == -1)
1028 *intptr = value;
1029 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Miller78928792000-04-12 20:17:38 +10001031 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001032 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001033 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001034 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001035 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001036 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001037 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001038 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001039 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001040 break;
1041
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001042 case oMacs:
1043 arg = strdelim(&s);
1044 if (!arg || *arg == '\0')
1045 fatal("%.200s line %d: Missing argument.", filename, linenum);
1046 if (!mac_valid(arg))
1047 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001048 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001049 if (*activep && options->macs == NULL)
1050 options->macs = xstrdup(arg);
1051 break;
1052
Damien Millerd5f62bf2010-09-24 22:11:14 +10001053 case oKexAlgorithms:
1054 arg = strdelim(&s);
1055 if (!arg || *arg == '\0')
1056 fatal("%.200s line %d: Missing argument.",
1057 filename, linenum);
1058 if (!kex_names_valid(arg))
1059 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1060 filename, linenum, arg ? arg : "<NONE>");
1061 if (*activep && options->kex_algorithms == NULL)
1062 options->kex_algorithms = xstrdup(arg);
1063 break;
1064
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001065 case oHostKeyAlgorithms:
1066 arg = strdelim(&s);
1067 if (!arg || *arg == '\0')
1068 fatal("%.200s line %d: Missing argument.", filename, linenum);
1069 if (!key_names_valid2(arg))
1070 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001071 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001072 if (*activep && options->hostkeyalgorithms == NULL)
1073 options->hostkeyalgorithms = xstrdup(arg);
1074 break;
1075
Damien Miller78928792000-04-12 20:17:38 +10001076 case oProtocol:
1077 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001078 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001079 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001080 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001081 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001082 if (value == SSH_PROTO_UNKNOWN)
1083 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001084 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001085 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1086 *intptr = value;
1087 break;
1088
Damien Miller95def091999-11-25 00:26:21 +11001089 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001090 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001091 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001092 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001093 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001094 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001095 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001096 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1097 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001098 break;
1099
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001100 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001101 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001102 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001103 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001104 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001105 fatal("%.200s line %d: Missing port argument.",
1106 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001107
Damien Millera699d952008-11-03 19:27:34 +11001108 if (opcode == oLocalForward ||
1109 opcode == oRemoteForward) {
1110 arg2 = strdelim(&s);
1111 if (arg2 == NULL || *arg2 == '\0')
1112 fatal("%.200s line %d: Missing target argument.",
1113 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001114
Damien Millera699d952008-11-03 19:27:34 +11001115 /* construct a string for parse_forward */
1116 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1117 } else if (opcode == oDynamicForward) {
1118 strlcpy(fwdarg, arg, sizeof(fwdarg));
1119 }
1120
1121 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001122 opcode == oDynamicForward ? 1 : 0,
1123 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001124 fatal("%.200s line %d: Bad forwarding specification.",
1125 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001126
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001127 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001128 if (opcode == oLocalForward ||
1129 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001130 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001131 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001132 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001133 }
Damien Miller95def091999-11-25 00:26:21 +11001134 break;
1135
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001136 case oClearAllForwardings:
1137 intptr = &options->clear_forwardings;
1138 goto parse_flag;
1139
Damien Miller95def091999-11-25 00:26:21 +11001140 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001141 if (cmdline)
1142 fatal("Host directive not supported as a command-line "
1143 "option");
Damien Miller95def091999-11-25 00:26:21 +11001144 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001145 arg2 = NULL;
1146 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1147 negated = *arg == '!';
1148 if (negated)
1149 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001150 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001151 if (negated) {
1152 debug("%.200s line %d: Skipping Host "
1153 "block because of negated match "
1154 "for %.100s", filename, linenum,
1155 arg);
1156 *activep = 0;
1157 break;
1158 }
1159 if (!*activep)
1160 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001161 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001162 }
Damien Millerfe924212011-05-15 08:44:45 +10001163 }
1164 if (*activep)
1165 debug("%.200s line %d: Applying options for %.100s",
1166 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001167 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001168 return 0;
1169
Damien Miller194fd902013-10-15 12:13:05 +11001170 case oMatch:
1171 if (cmdline)
1172 fatal("Host directive not supported as a command-line "
1173 "option");
1174 value = match_cfg_line(options, &s, pw, host,
1175 filename, linenum);
1176 if (value < 0)
1177 fatal("%.200s line %d: Bad Match condition", filename,
1178 linenum);
1179 *activep = value;
1180 break;
1181
Damien Miller95def091999-11-25 00:26:21 +11001182 case oEscapeChar:
1183 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001184 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001185 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001186 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001187 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001188 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1189 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001190 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001191 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001192 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001193 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001194 else {
1195 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001196 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001197 /* NOTREACHED */
1198 value = 0; /* Avoid compiler warning. */
1199 }
1200 if (*activep && *intptr == -1)
1201 *intptr = value;
1202 break;
1203
Damien Miller20a8f972003-05-18 20:50:30 +10001204 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001205 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001206 multistate_ptr = multistate_addressfamily;
1207 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001208
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001209 case oEnableSSHKeysign:
1210 intptr = &options->enable_ssh_keysign;
1211 goto parse_flag;
1212
Damien Millerbd394c32004-03-08 23:12:36 +11001213 case oIdentitiesOnly:
1214 intptr = &options->identities_only;
1215 goto parse_flag;
1216
Damien Miller509b0102003-12-17 16:33:10 +11001217 case oServerAliveInterval:
1218 intptr = &options->server_alive_interval;
1219 goto parse_time;
1220
1221 case oServerAliveCountMax:
1222 intptr = &options->server_alive_count_max;
1223 goto parse_int;
1224
Darren Tucker46bc0752004-05-02 22:11:30 +10001225 case oSendEnv:
1226 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1227 if (strchr(arg, '=') != NULL)
1228 fatal("%s line %d: Invalid environment name.",
1229 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001230 if (!*activep)
1231 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001232 if (options->num_send_env >= MAX_SEND_ENV)
1233 fatal("%s line %d: too many send env.",
1234 filename, linenum);
1235 options->send_env[options->num_send_env++] =
1236 xstrdup(arg);
1237 }
1238 break;
1239
Damien Miller0e220db2004-06-15 10:34:08 +10001240 case oControlPath:
1241 charptr = &options->control_path;
1242 goto parse_string;
1243
1244 case oControlMaster:
1245 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001246 multistate_ptr = multistate_controlmaster;
1247 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001248
Damien Millere11e1ea2010-08-03 16:04:46 +10001249 case oControlPersist:
1250 /* no/false/yes/true, or a time spec */
1251 intptr = &options->control_persist;
1252 arg = strdelim(&s);
1253 if (!arg || *arg == '\0')
1254 fatal("%.200s line %d: Missing ControlPersist"
1255 " argument.", filename, linenum);
1256 value = 0;
1257 value2 = 0; /* timeout */
1258 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1259 value = 0;
1260 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1261 value = 1;
1262 else if ((value2 = convtime(arg)) >= 0)
1263 value = 1;
1264 else
1265 fatal("%.200s line %d: Bad ControlPersist argument.",
1266 filename, linenum);
1267 if (*activep && *intptr == -1) {
1268 *intptr = value;
1269 options->control_persist_timeout = value2;
1270 }
1271 break;
1272
Damien Millere1776152005-03-01 21:47:37 +11001273 case oHashKnownHosts:
1274 intptr = &options->hash_known_hosts;
1275 goto parse_flag;
1276
Damien Millerd27b9472005-12-13 19:29:02 +11001277 case oTunnel:
1278 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001279 multistate_ptr = multistate_tunnel;
1280 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001281
1282 case oTunnelDevice:
1283 arg = strdelim(&s);
1284 if (!arg || *arg == '\0')
1285 fatal("%.200s line %d: Missing argument.", filename, linenum);
1286 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001287 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001288 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1289 if (*activep) {
1290 options->tun_local = value;
1291 options->tun_remote = value2;
1292 }
1293 break;
1294
1295 case oLocalCommand:
1296 charptr = &options->local_command;
1297 goto parse_command;
1298
1299 case oPermitLocalCommand:
1300 intptr = &options->permit_local_command;
1301 goto parse_flag;
1302
Damien Miller10288242008-06-30 00:04:03 +10001303 case oVisualHostKey:
1304 intptr = &options->visual_host_key;
1305 goto parse_flag;
1306
Damien Miller0dac6fb2010-11-20 15:19:38 +11001307 case oIPQoS:
1308 arg = strdelim(&s);
1309 if ((value = parse_ipqos(arg)) == -1)
1310 fatal("%s line %d: Bad IPQoS value: %s",
1311 filename, linenum, arg);
1312 arg = strdelim(&s);
1313 if (arg == NULL)
1314 value2 = value;
1315 else if ((value2 = parse_ipqos(arg)) == -1)
1316 fatal("%s line %d: Bad IPQoS value: %s",
1317 filename, linenum, arg);
1318 if (*activep) {
1319 options->ip_qos_interactive = value;
1320 options->ip_qos_bulk = value2;
1321 }
1322 break;
1323
Darren Tucker71e4d542009-07-06 07:12:27 +10001324 case oUseRoaming:
1325 intptr = &options->use_roaming;
1326 goto parse_flag;
1327
Damien Miller21771e22011-05-15 08:45:50 +10001328 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001329 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001330 multistate_ptr = multistate_requesttty;
1331 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001332
Darren Tucker07636982013-05-16 20:30:03 +10001333 case oIgnoreUnknown:
1334 charptr = &options->ignored_unknown;
1335 goto parse_string;
1336
Damien Miller1262b662013-08-21 02:44:24 +10001337 case oProxyUseFdpass:
1338 intptr = &options->proxy_use_fdpass;
1339 goto parse_flag;
1340
Damien Miller0faf7472013-10-17 11:47:23 +11001341 case oCanonicalDomains:
1342 value = options->num_canonical_domains != 0;
1343 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1344 valid_domain(arg, filename, linenum);
1345 if (!*activep || value)
1346 continue;
1347 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1348 fatal("%s line %d: too many hostname suffixes.",
1349 filename, linenum);
1350 options->canonical_domains[
1351 options->num_canonical_domains++] = xstrdup(arg);
1352 }
1353 break;
1354
Damien Miller38505592013-10-17 11:48:13 +11001355 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001356 value = options->num_permitted_cnames != 0;
1357 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1358 /* Either '*' for everything or 'list:list' */
1359 if (strcmp(arg, "*") == 0)
1360 arg2 = arg;
1361 else {
1362 lowercase(arg);
1363 if ((arg2 = strchr(arg, ':')) == NULL ||
1364 arg2[1] == '\0') {
1365 fatal("%s line %d: "
1366 "Invalid permitted CNAME \"%s\"",
1367 filename, linenum, arg);
1368 }
1369 *arg2 = '\0';
1370 arg2++;
1371 }
1372 if (!*activep || value)
1373 continue;
1374 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1375 fatal("%s line %d: too many permitted CNAMEs.",
1376 filename, linenum);
1377 cname = options->permitted_cnames +
1378 options->num_permitted_cnames++;
1379 cname->source_list = xstrdup(arg);
1380 cname->target_list = xstrdup(arg2);
1381 }
1382 break;
1383
Damien Miller38505592013-10-17 11:48:13 +11001384 case oCanonicalizeHostname:
1385 intptr = &options->canonicalize_hostname;
1386 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001387 goto parse_multistate;
1388
Damien Miller38505592013-10-17 11:48:13 +11001389 case oCanonicalizeMaxDots:
1390 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001391 goto parse_int;
1392
Damien Miller38505592013-10-17 11:48:13 +11001393 case oCanonicalizeFallbackLocal:
1394 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001395 goto parse_flag;
1396
Ben Lindstrom4daea862002-06-09 20:04:02 +00001397 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001398 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001399 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001400 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001401
Damien Millerf9b3feb2003-05-16 11:38:32 +10001402 case oUnsupported:
1403 error("%s line %d: Unsupported option \"%s\"",
1404 filename, linenum, keyword);
1405 return 0;
1406
Damien Miller95def091999-11-25 00:26:21 +11001407 default:
1408 fatal("process_config_line: Unimplemented opcode %d", opcode);
1409 }
1410
1411 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001412 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001413 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001414 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001415 }
Damien Miller95def091999-11-25 00:26:21 +11001416 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417}
1418
1419
Damien Miller5428f641999-11-25 11:54:57 +11001420/*
1421 * Reads the config file and modifies the options accordingly. Options
1422 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001423 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001424 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001426int
Damien Miller194fd902013-10-15 12:13:05 +11001427read_config_file(const char *filename, struct passwd *pw, const char *host,
1428 Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429{
Damien Miller95def091999-11-25 00:26:21 +11001430 FILE *f;
1431 char line[1024];
1432 int active, linenum;
1433 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Damien Miller57a44762004-04-20 20:11:57 +10001435 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001436 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Darren Tuckeraefa3682013-04-05 11:18:35 +11001438 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001439 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001440
Damien Miller33793852004-06-15 10:27:55 +10001441 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001442 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001443 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001444 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001445 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001446 }
1447
Damien Miller95def091999-11-25 00:26:21 +11001448 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller5428f641999-11-25 11:54:57 +11001450 /*
1451 * Mark that we are now processing the options. This flag is turned
1452 * on/off by Host specifications.
1453 */
Damien Miller95def091999-11-25 00:26:21 +11001454 active = 1;
1455 linenum = 0;
1456 while (fgets(line, sizeof(line), f)) {
1457 /* Update line number counter. */
1458 linenum++;
Damien Miller194fd902013-10-15 12:13:05 +11001459 if (process_config_line(options, pw, host, line, filename,
1460 linenum, &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001461 bad_options++;
1462 }
1463 fclose(f);
1464 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001465 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001466 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001467 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468}
1469
Damien Miller13f97b22014-02-24 15:57:55 +11001470/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1471int
1472option_clear_or_none(const char *o)
1473{
1474 return o == NULL || strcasecmp(o, "none") == 0;
1475}
1476
Damien Miller5428f641999-11-25 11:54:57 +11001477/*
1478 * Initializes options to special values that indicate that they have not yet
1479 * been set. Read_config_file will only set options with this value. Options
1480 * are processed in the following order: command line, user config file,
1481 * system config file. Last, fill_default_options is called.
1482 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Damien Miller4af51302000-04-16 11:18:38 +10001484void
Damien Miller95def091999-11-25 00:26:21 +11001485initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486{
Damien Miller95def091999-11-25 00:26:21 +11001487 memset(options, 'X', sizeof(*options));
1488 options->forward_agent = -1;
1489 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001490 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001491 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001492 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001493 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001494 options->gateway_ports = -1;
1495 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001496 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001497 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001498 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001499 options->gss_authentication = -1;
1500 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001501 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001502 options->kbd_interactive_authentication = -1;
1503 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001504 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001505 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001506 options->batch_mode = -1;
1507 options->check_host_ip = -1;
1508 options->strict_host_key_checking = -1;
1509 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001510 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001511 options->compression_level = -1;
1512 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001513 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001514 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001515 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001516 options->number_of_password_prompts = -1;
1517 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001518 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001519 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001520 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001521 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001522 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001523 options->num_identity_files = 0;
1524 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001525 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001526 options->proxy_command = NULL;
1527 options->user = NULL;
1528 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001529 options->num_system_hostfiles = 0;
1530 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001531 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001532 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001533 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001534 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001535 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001536 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001537 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001538 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001539 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001540 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001541 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001542 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001543 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001544 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001545 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001546 options->server_alive_interval = -1;
1547 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001548 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001549 options->control_path = NULL;
1550 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001551 options->control_persist = -1;
1552 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001553 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001554 options->tun_open = -1;
1555 options->tun_local = -1;
1556 options->tun_remote = -1;
1557 options->local_command = NULL;
1558 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001559 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001560 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001561 options->ip_qos_interactive = -1;
1562 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001563 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001564 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001565 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001566 options->num_canonical_domains = 0;
1567 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001568 options->canonicalize_max_dots = -1;
1569 options->canonicalize_fallback_local = -1;
1570 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571}
1572
Damien Miller5428f641999-11-25 11:54:57 +11001573/*
Damien Miller13f97b22014-02-24 15:57:55 +11001574 * A petite version of fill_default_options() that just fills the options
1575 * needed for hostname canonicalization to proceed.
1576 */
1577void
1578fill_default_options_for_canonicalization(Options *options)
1579{
1580 if (options->canonicalize_max_dots == -1)
1581 options->canonicalize_max_dots = 1;
1582 if (options->canonicalize_fallback_local == -1)
1583 options->canonicalize_fallback_local = 1;
1584 if (options->canonicalize_hostname == -1)
1585 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1586}
1587
1588/*
Damien Miller5428f641999-11-25 11:54:57 +11001589 * Called after processing other sources of option data, this fills those
1590 * options for which no value has been specified with their default values.
1591 */
Damien Miller4af51302000-04-16 11:18:38 +10001592void
Damien Miller95def091999-11-25 00:26:21 +11001593fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594{
Damien Miller95def091999-11-25 00:26:21 +11001595 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001596 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001597 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001598 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001599 if (options->forward_x11_trusted == -1)
1600 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001601 if (options->forward_x11_timeout == -1)
1602 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001603 if (options->exit_on_forward_failure == -1)
1604 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001605 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001606 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001607 if (options->gateway_ports == -1)
1608 options->gateway_ports = 0;
1609 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001610 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001611 if (options->rsa_authentication == -1)
1612 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001613 if (options->pubkey_authentication == -1)
1614 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001615 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001616 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001617 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001618 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001619 if (options->gss_deleg_creds == -1)
1620 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001621 if (options->password_authentication == -1)
1622 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001623 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001624 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001625 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001626 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001627 if (options->hostbased_authentication == -1)
1628 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001629 if (options->batch_mode == -1)
1630 options->batch_mode = 0;
1631 if (options->check_host_ip == -1)
1632 options->check_host_ip = 1;
1633 if (options->strict_host_key_checking == -1)
1634 options->strict_host_key_checking = 2; /* 2 is default */
1635 if (options->compression == -1)
1636 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001637 if (options->tcp_keep_alive == -1)
1638 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001639 if (options->compression_level == -1)
1640 options->compression_level = 6;
1641 if (options->port == -1)
1642 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001643 if (options->address_family == -1)
1644 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001645 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001646 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001647 if (options->number_of_password_prompts == -1)
1648 options->number_of_password_prompts = 3;
1649 /* Selected in ssh_login(). */
1650 if (options->cipher == -1)
1651 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001652 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001653 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001654 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001655 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001656 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001657 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001658 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001659 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001660 add_identity_file(options, "~/",
1661 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662 }
1663 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001664 add_identity_file(options, "~/",
1665 _PATH_SSH_CLIENT_ID_RSA, 0);
1666 add_identity_file(options, "~/",
1667 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001668#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001669 add_identity_file(options, "~/",
1670 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001671#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001672 add_identity_file(options, "~/",
1673 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001674 }
Damien Millereba71ba2000-04-29 23:57:08 +10001675 }
Damien Miller95def091999-11-25 00:26:21 +11001676 if (options->escape_char == -1)
1677 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001678 if (options->num_system_hostfiles == 0) {
1679 options->system_hostfiles[options->num_system_hostfiles++] =
1680 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1681 options->system_hostfiles[options->num_system_hostfiles++] =
1682 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1683 }
1684 if (options->num_user_hostfiles == 0) {
1685 options->user_hostfiles[options->num_user_hostfiles++] =
1686 xstrdup(_PATH_SSH_USER_HOSTFILE);
1687 options->user_hostfiles[options->num_user_hostfiles++] =
1688 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1689 }
Damien Millerfcd93202002-02-05 12:26:34 +11001690 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001691 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001692 if (options->clear_forwardings == 1)
1693 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001694 if (options->no_host_authentication_for_localhost == - 1)
1695 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001696 if (options->identities_only == -1)
1697 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001698 if (options->enable_ssh_keysign == -1)
1699 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001700 if (options->rekey_limit == -1)
1701 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001702 if (options->rekey_interval == -1)
1703 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001704 if (options->verify_host_key_dns == -1)
1705 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001706 if (options->server_alive_interval == -1)
1707 options->server_alive_interval = 0;
1708 if (options->server_alive_count_max == -1)
1709 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001710 if (options->control_master == -1)
1711 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001712 if (options->control_persist == -1) {
1713 options->control_persist = 0;
1714 options->control_persist_timeout = 0;
1715 }
Damien Millere1776152005-03-01 21:47:37 +11001716 if (options->hash_known_hosts == -1)
1717 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001718 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001719 options->tun_open = SSH_TUNMODE_NO;
1720 if (options->tun_local == -1)
1721 options->tun_local = SSH_TUNID_ANY;
1722 if (options->tun_remote == -1)
1723 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001724 if (options->permit_local_command == -1)
1725 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001726 if (options->use_roaming == -1)
1727 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001728 if (options->visual_host_key == -1)
1729 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001730 if (options->ip_qos_interactive == -1)
1731 options->ip_qos_interactive = IPTOS_LOWDELAY;
1732 if (options->ip_qos_bulk == -1)
1733 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001734 if (options->request_tty == -1)
1735 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001736 if (options->proxy_use_fdpass == -1)
1737 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001738 if (options->canonicalize_max_dots == -1)
1739 options->canonicalize_max_dots = 1;
1740 if (options->canonicalize_fallback_local == -1)
1741 options->canonicalize_fallback_local = 1;
1742 if (options->canonicalize_hostname == -1)
1743 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001744#define CLEAR_ON_NONE(v) \
1745 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001746 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001747 free(v); \
1748 v = NULL; \
1749 } \
1750 } while(0)
1751 CLEAR_ON_NONE(options->local_command);
1752 CLEAR_ON_NONE(options->proxy_command);
1753 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001754 /* options->user will be set in the main program if appropriate */
1755 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001756 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001757 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001759
1760/*
1761 * parse_forward
1762 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001763 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001764 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001765 * dynamicfwd == 1
1766 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001767 * returns number of arguments parsed or zero on error
1768 */
1769int
Damien Miller4bf648f2009-02-14 16:28:21 +11001770parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001771{
1772 int i;
1773 char *p, *cp, *fwdarg[4];
1774
1775 memset(fwd, '\0', sizeof(*fwd));
1776
1777 cp = p = xstrdup(fwdspec);
1778
1779 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001780 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001781 cp++;
1782
1783 for (i = 0; i < 4; ++i)
1784 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1785 break;
1786
Damien Millerf4b39532008-11-03 19:28:21 +11001787 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001788 if (cp != NULL)
1789 i = 0; /* failure */
1790
1791 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001792 case 1:
1793 fwd->listen_host = NULL;
1794 fwd->listen_port = a2port(fwdarg[0]);
1795 fwd->connect_host = xstrdup("socks");
1796 break;
1797
1798 case 2:
1799 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1800 fwd->listen_port = a2port(fwdarg[1]);
1801 fwd->connect_host = xstrdup("socks");
1802 break;
1803
Damien Millerf91ee4c2005-03-01 21:24:33 +11001804 case 3:
1805 fwd->listen_host = NULL;
1806 fwd->listen_port = a2port(fwdarg[0]);
1807 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1808 fwd->connect_port = a2port(fwdarg[2]);
1809 break;
1810
1811 case 4:
1812 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1813 fwd->listen_port = a2port(fwdarg[1]);
1814 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1815 fwd->connect_port = a2port(fwdarg[3]);
1816 break;
1817 default:
1818 i = 0; /* failure */
1819 }
1820
Darren Tuckera627d422013-06-02 07:31:17 +10001821 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001822
Damien Millera699d952008-11-03 19:27:34 +11001823 if (dynamicfwd) {
1824 if (!(i == 1 || i == 2))
1825 goto fail_free;
1826 } else {
1827 if (!(i == 3 || i == 4))
1828 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001829 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001830 goto fail_free;
1831 }
1832
Damien Miller4bf648f2009-02-14 16:28:21 +11001833 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001834 goto fail_free;
1835
1836 if (fwd->connect_host != NULL &&
1837 strlen(fwd->connect_host) >= NI_MAXHOST)
1838 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001839 if (fwd->listen_host != NULL &&
1840 strlen(fwd->listen_host) >= NI_MAXHOST)
1841 goto fail_free;
1842
Damien Millerf91ee4c2005-03-01 21:24:33 +11001843
1844 return (i);
1845
1846 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10001847 free(fwd->connect_host);
1848 fwd->connect_host = NULL;
1849 free(fwd->listen_host);
1850 fwd->listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001851 return (0);
1852}