blob: dccf3ba1613e73f94898a5a1f241234ef8c60bd7 [file] [log] [blame]
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001/* $OpenBSD: readconf.c,v 1.200 2013/05/16 09:12:31 dtucker 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>
20
21#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in_systm.h>
23#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Millerc7b06362006-03-15 11:53:45 +110025#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100027#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100029#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100030#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100032#include <unistd.h>
Darren Tuckerb7ee8522013-05-16 20:33:10 +100033#include <util.h>
Damien Millerc7b06362006-03-15 11:53:45 +110034
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100037#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "cipher.h"
39#include "pathnames.h"
40#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "readconf.h"
43#include "match.h"
44#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000046#include "kex.h"
47#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
49/* Format of the configuration file:
50
51 # Configuration data is parsed as follows:
52 # 1. command line options
53 # 2. user-specific file
54 # 3. system-wide file
55 # Any configuration value is only changed the first time it is set.
56 # Thus, host-specific definitions should be at the beginning of the
57 # configuration file, and defaults at the end.
58
59 # Host-specific declarations. These may override anything above. A single
60 # host may match multiple declarations; these are processed in the order
61 # that they are given in.
62
63 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000064 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
66 Host fake.com
67 HostName another.host.name.real.org
68 User blaah
69 Port 34289
70 ForwardX11 no
71 ForwardAgent no
72
73 Host books.com
74 RemoteForward 9999 shadows.cs.hut.fi:9999
75 Cipher 3des
76
77 Host fascist.blob.com
78 Port 23123
79 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 PasswordAuthentication no
81
82 Host puukko.hut.fi
83 User t35124p
84 ProxyCommand ssh-proxy %h %p
85
86 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000087 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89 Host *.su
90 Cipher none
91 PasswordAuthentication no
92
Damien Millerd27b9472005-12-13 19:29:02 +110093 Host vpn.fake.com
94 Tunnel yes
95 TunnelDevice 3
96
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097 # Defaults for various options
98 Host *
99 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100100 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 PasswordAuthentication yes
102 RSAAuthentication yes
103 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100105 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106 IdentityFile ~/.ssh/identity
107 Port 22
108 EscapeChar ~
109
110*/
111
112/* Keyword tokens. */
113
Damien Miller95def091999-11-25 00:26:21 +1100114typedef enum {
115 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000116 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
117 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000118 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000119 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100120 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
121 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
122 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
123 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100124 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000125 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100126 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000127 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000128 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100129 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000130 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000131 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000132 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100133 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000134 oSendEnv, oControlPath, oControlMaster, oControlPersist,
135 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100136 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100137 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Darren Tucker07636982013-05-16 20:30:03 +1000138 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown,
139 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140} OpCodes;
141
142/* Textual representations of the tokens. */
143
Damien Miller95def091999-11-25 00:26:21 +1100144static struct {
145 const char *name;
146 OpCodes opcode;
147} keywords[] = {
148 { "forwardagent", oForwardAgent },
149 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000150 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000151 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000152 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000153 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "gatewayports", oGatewayPorts },
155 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000156 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100157 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100158 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
159 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100160 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100161 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000162 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000163 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000164 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000165 { "challengeresponseauthentication", oChallengeResponseAuthentication },
166 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
167 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000168 { "kerberosauthentication", oUnsupported },
169 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000170 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000171#if defined(GSSAPI)
172 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000173 { "gssapidelegatecredentials", oGssDelegateCreds },
174#else
175 { "gssapiauthentication", oUnsupported },
176 { "gssapidelegatecredentials", oUnsupported },
177#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000178 { "fallbacktorsh", oDeprecated },
179 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100181 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100182 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100183 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000184 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "proxycommand", oProxyCommand },
186 { "port", oPort },
187 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000188 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000189 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000190 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100191 { "remoteforward", oRemoteForward },
192 { "localforward", oLocalForward },
193 { "user", oUser },
194 { "host", oHost },
195 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100196 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000197 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100198 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000199 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100200 { "connectionattempts", oConnectionAttempts },
201 { "batchmode", oBatchMode },
202 { "checkhostip", oCheckHostIP },
203 { "stricthostkeychecking", oStrictHostKeyChecking },
204 { "compression", oCompression },
205 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100206 { "tcpkeepalive", oTCPKeepAlive },
207 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100208 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000210 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000211 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000212 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000213 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100214#ifdef ENABLE_PKCS11
215 { "smartcarddevice", oPKCS11Provider },
216 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000217#else
218 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100219 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000220#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100221 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000222 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000223 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100224 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000225 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000226 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000227 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100228 { "serveraliveinterval", oServerAliveInterval },
229 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000230 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000231 { "controlpath", oControlPath },
232 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000233 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100234 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100235 { "tunnel", oTunnel },
236 { "tunneldevice", oTunnelDevice },
237 { "localcommand", oLocalCommand },
238 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000239 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000240 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100241#ifdef JPAKE
242 { "zeroknowledgepasswordauthentication",
243 oZeroKnowledgePasswordAuthentication },
244#else
245 { "zeroknowledgepasswordauthentication", oUnsupported },
246#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000247 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100248 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000249 { "requesttty", oRequestTTY },
Darren Tucker07636982013-05-16 20:30:03 +1000250 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100251
Ben Lindstrom65366a82001-12-06 16:32:47 +0000252 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100253};
254
Damien Miller5428f641999-11-25 11:54:57 +1100255/*
256 * Adds a local TCP/IP port forward to options. Never returns if there is an
257 * error.
258 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller4af51302000-04-16 11:18:38 +1000260void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262{
Damien Miller95def091999-11-25 00:26:21 +1100263 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000264#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100265 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100266 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000267 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100268#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000269 options->local_forwards = xrealloc(options->local_forwards,
270 options->num_local_forwards + 1,
271 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100272 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100273
Damien Miller1a0442f2008-11-05 16:30:06 +1100274 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100276 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100277 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278}
279
Damien Miller5428f641999-11-25 11:54:57 +1100280/*
281 * Adds a remote TCP/IP port forward to options. Never returns if there is
282 * an error.
283 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller4af51302000-04-16 11:18:38 +1000285void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287{
Damien Miller95def091999-11-25 00:26:21 +1100288 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000289
290 options->remote_forwards = xrealloc(options->remote_forwards,
291 options->num_remote_forwards + 1,
292 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100293 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294
Damien Miller1a0442f2008-11-05 16:30:06 +1100295 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100297 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100298 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100299 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000300 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000303static void
304clear_forwardings(Options *options)
305{
306 int i;
307
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100309 if (options->local_forwards[i].listen_host != NULL)
310 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100311 xfree(options->local_forwards[i].connect_host);
312 }
Damien Miller232cfb12010-06-26 09:50:30 +1000313 if (options->num_local_forwards > 0) {
314 xfree(options->local_forwards);
315 options->local_forwards = NULL;
316 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000317 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100318 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100319 if (options->remote_forwards[i].listen_host != NULL)
320 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100321 xfree(options->remote_forwards[i].connect_host);
322 }
Damien Miller232cfb12010-06-26 09:50:30 +1000323 if (options->num_remote_forwards > 0) {
324 xfree(options->remote_forwards);
325 options->remote_forwards = NULL;
326 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000327 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100328 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000329}
330
Darren Tucker19104782013-04-05 11:13:08 +1100331void
332add_identity_file(Options *options, const char *dir, const char *filename,
333 int userprovided)
334{
335 char *path;
336
337 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
338 fatal("Too many identity files specified (max %d)",
339 SSH_MAX_IDENTITY_FILES);
340
341 if (dir == NULL) /* no dir, filename is absolute */
342 path = xstrdup(filename);
343 else
344 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
345
346 options->identity_file_userprovided[options->num_identity_files] =
347 userprovided;
348 options->identity_files[options->num_identity_files++] = path;
349}
350
Damien Miller5428f641999-11-25 11:54:57 +1100351/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000352 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100353 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller4af51302000-04-16 11:18:38 +1000355static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000356parse_token(const char *cp, const char *filename, int linenum,
357 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Darren Tucker07636982013-05-16 20:30:03 +1000359 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000362 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100363 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000364 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
365 strlen(ignored_unknown), 1) == 1)
366 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000367 error("%s: line %d: Bad configuration option: %s",
368 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100369 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
371
Damien Miller5428f641999-11-25 11:54:57 +1100372/*
373 * Processes a single option line as used in the configuration files. This
374 * only sets those values that have not already been set.
375 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100376#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
Damien Miller2ccf6611999-11-15 15:25:10 +1100378int
379process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100380 char *line, const char *filename, int linenum,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100381 int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382{
Damien Miller295ee632011-05-29 21:42:31 +1000383 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
384 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000385 u_int i, *uintptr, max_entries = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000386 int negated, opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100387 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100388 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100389 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100390 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Millerc652cac2003-05-14 13:40:54 +1000392 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100393 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000394 if (strchr(WHITESPACE, line[len]) == NULL)
395 break;
396 line[len] = '\0';
397 }
398
Damien Millerbe484b52000-07-15 14:14:16 +1000399 s = line;
400 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100401 if ((keyword = strdelim(&s)) == NULL)
402 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000403 /* Ignore leading whitespace. */
404 if (*keyword == '\0')
405 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000406 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100407 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000408 /* Match lowercase keyword */
409 for (i = 0; i < strlen(keyword); i++)
410 keyword[i] = tolower(keyword[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Darren Tucker07636982013-05-16 20:30:03 +1000412 opcode = parse_token(keyword, filename, linenum,
413 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 switch (opcode) {
416 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100417 /* don't panic, but count bad options */
418 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100419 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000420 case oIgnoredUnknownOption:
421 debug("%s line %d: Ignored unknown option \"%s\"",
422 filename, linenum, keyword);
423 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000424 case oConnectTimeout:
425 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100426parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000427 arg = strdelim(&s);
428 if (!arg || *arg == '\0')
429 fatal("%s line %d: missing time value.",
430 filename, linenum);
431 if ((value = convtime(arg)) == -1)
432 fatal("%s line %d: invalid time value.",
433 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100434 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000435 *intptr = value;
436 break;
437
Damien Miller95def091999-11-25 00:26:21 +1100438 case oForwardAgent:
439 intptr = &options->forward_agent;
440parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000441 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000442 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100443 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
444 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000445 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100446 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000447 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100448 value = 0;
449 else
450 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
451 if (*activep && *intptr == -1)
452 *intptr = value;
453 break;
454
455 case oForwardX11:
456 intptr = &options->forward_x11;
457 goto parse_flag;
458
Darren Tucker0a118da2003-10-15 15:54:32 +1000459 case oForwardX11Trusted:
460 intptr = &options->forward_x11_trusted;
461 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000462
463 case oForwardX11Timeout:
464 intptr = &options->forward_x11_timeout;
465 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000466
Damien Miller95def091999-11-25 00:26:21 +1100467 case oGatewayPorts:
468 intptr = &options->gateway_ports;
469 goto parse_flag;
470
Darren Tuckere7d4b192006-07-12 22:17:10 +1000471 case oExitOnForwardFailure:
472 intptr = &options->exit_on_forward_failure;
473 goto parse_flag;
474
Damien Miller95def091999-11-25 00:26:21 +1100475 case oUsePrivilegedPort:
476 intptr = &options->use_privileged_port;
477 goto parse_flag;
478
Damien Miller95def091999-11-25 00:26:21 +1100479 case oPasswordAuthentication:
480 intptr = &options->password_authentication;
481 goto parse_flag;
482
Damien Miller01ed2272008-11-05 16:20:46 +1100483 case oZeroKnowledgePasswordAuthentication:
484 intptr = &options->zero_knowledge_password_authentication;
485 goto parse_flag;
486
Damien Miller874d77b2000-10-14 16:23:11 +1100487 case oKbdInteractiveAuthentication:
488 intptr = &options->kbd_interactive_authentication;
489 goto parse_flag;
490
491 case oKbdInteractiveDevices:
492 charptr = &options->kbd_interactive_devices;
493 goto parse_string;
494
Damien Miller0bc1bd82000-11-13 22:57:25 +1100495 case oPubkeyAuthentication:
496 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000497 goto parse_flag;
498
Damien Miller95def091999-11-25 00:26:21 +1100499 case oRSAAuthentication:
500 intptr = &options->rsa_authentication;
501 goto parse_flag;
502
503 case oRhostsRSAAuthentication:
504 intptr = &options->rhosts_rsa_authentication;
505 goto parse_flag;
506
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000507 case oHostbasedAuthentication:
508 intptr = &options->hostbased_authentication;
509 goto parse_flag;
510
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000511 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000512 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100513 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000514
Darren Tucker0efd1552003-08-26 11:49:55 +1000515 case oGssAuthentication:
516 intptr = &options->gss_authentication;
517 goto parse_flag;
518
519 case oGssDelegateCreds:
520 intptr = &options->gss_deleg_creds;
521 goto parse_flag;
522
Damien Miller95def091999-11-25 00:26:21 +1100523 case oBatchMode:
524 intptr = &options->batch_mode;
525 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller95def091999-11-25 00:26:21 +1100527 case oCheckHostIP:
528 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000529 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Miller37876e92003-05-15 10:19:46 +1000531 case oVerifyHostKeyDNS:
532 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100533 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000534
Damien Miller95def091999-11-25 00:26:21 +1100535 case oStrictHostKeyChecking:
536 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100537parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000538 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000539 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000540 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100541 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100542 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000543 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100544 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000545 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100546 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000547 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100548 value = 2;
549 else
550 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
551 if (*activep && *intptr == -1)
552 *intptr = value;
553 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554
Damien Miller95def091999-11-25 00:26:21 +1100555 case oCompression:
556 intptr = &options->compression;
557 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Miller12c150e2003-12-17 16:31:10 +1100559 case oTCPKeepAlive:
560 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100561 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000563 case oNoHostAuthenticationForLocalhost:
564 intptr = &options->no_host_authentication_for_localhost;
565 goto parse_flag;
566
Damien Miller95def091999-11-25 00:26:21 +1100567 case oNumberOfPasswordPrompts:
568 intptr = &options->number_of_password_prompts;
569 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller95def091999-11-25 00:26:21 +1100571 case oCompressionLevel:
572 intptr = &options->compression_level;
573 goto parse_int;
574
Damien Millera5539d22003-04-09 20:50:06 +1000575 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000576 arg = strdelim(&s);
577 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000578 fatal("%.200s line %d: Missing argument.", filename,
579 linenum);
580 if (strcmp(arg, "default") == 0) {
581 val64 = 0;
582 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000583 if (scan_scaled(arg, &val64) == -1)
584 fatal("%.200s line %d: Bad number '%s': %s",
585 filename, linenum, arg, strerror(errno));
586 /* check for too-large or too-small limits */
587 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000588 fatal("%.200s line %d: RekeyLimit too large",
589 filename, linenum);
590 if (val64 != 0 && val64 < 16)
591 fatal("%.200s line %d: RekeyLimit too small",
592 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000593 }
Damien Miller3dff1762008-02-10 22:25:52 +1100594 if (*activep && options->rekey_limit == -1)
595 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000596 if (s != NULL) { /* optional rekey interval present */
597 if (strcmp(s, "none") == 0) {
598 (void)strdelim(&s); /* discard */
599 break;
600 }
601 intptr = &options->rekey_interval;
602 goto parse_time;
603 }
Damien Millera5539d22003-04-09 20:50:06 +1000604 break;
605
Damien Miller95def091999-11-25 00:26:21 +1100606 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000607 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000608 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100609 fatal("%.200s line %d: Missing argument.", filename, linenum);
610 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100611 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000612 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100613 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100614 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100615 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100616 }
617 break;
618
Damien Millerd3a18572000-06-07 19:55:44 +1000619 case oXAuthLocation:
620 charptr=&options->xauth_location;
621 goto parse_string;
622
Damien Miller95def091999-11-25 00:26:21 +1100623 case oUser:
624 charptr = &options->user;
625parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000626 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000627 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000628 fatal("%.200s line %d: Missing argument.",
629 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100630 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000631 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100632 break;
633
634 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000635 cpptr = (char **)&options->system_hostfiles;
636 uintptr = &options->num_system_hostfiles;
637 max_entries = SSH_MAX_HOSTS_FILES;
638parse_char_array:
639 if (*activep && *uintptr == 0) {
640 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
641 if ((*uintptr) >= max_entries)
642 fatal("%s line %d: "
643 "too many authorized keys files.",
644 filename, linenum);
645 cpptr[(*uintptr)++] = xstrdup(arg);
646 }
647 }
648 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100649
650 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000651 cpptr = (char **)&options->user_hostfiles;
652 uintptr = &options->num_user_hostfiles;
653 max_entries = SSH_MAX_HOSTS_FILES;
654 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656 case oHostName:
657 charptr = &options->hostname;
658 goto parse_string;
659
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000660 case oHostKeyAlias:
661 charptr = &options->host_key_alias;
662 goto parse_string;
663
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000664 case oPreferredAuthentications:
665 charptr = &options->preferred_authentications;
666 goto parse_string;
667
Ben Lindstrome0f88042001-04-30 13:06:24 +0000668 case oBindAddress:
669 charptr = &options->bind_address;
670 goto parse_string;
671
Damien Miller7ea845e2010-02-12 09:21:02 +1100672 case oPKCS11Provider:
673 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000674 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000675
Damien Miller95def091999-11-25 00:26:21 +1100676 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100677 charptr = &options->proxy_command;
678parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000679 if (s == NULL)
680 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100681 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100682 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100683 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100684 return 0;
685
686 case oPort:
687 intptr = &options->port;
688parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000689 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000690 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100691 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000692 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100693 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100694
695 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000696 value = strtol(arg, &endofnumber, 0);
697 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100698 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100699 if (*activep && *intptr == -1)
700 *intptr = value;
701 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702
Damien Miller95def091999-11-25 00:26:21 +1100703 case oConnectionAttempts:
704 intptr = &options->connection_attempts;
705 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100706
Damien Miller95def091999-11-25 00:26:21 +1100707 case oCipher:
708 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000709 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000710 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000711 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000712 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100713 if (value == -1)
714 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100715 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100716 if (*activep && *intptr == -1)
717 *intptr = value;
718 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller78928792000-04-12 20:17:38 +1000720 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000721 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000722 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000723 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000724 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000725 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100726 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000727 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000728 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000729 break;
730
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000731 case oMacs:
732 arg = strdelim(&s);
733 if (!arg || *arg == '\0')
734 fatal("%.200s line %d: Missing argument.", filename, linenum);
735 if (!mac_valid(arg))
736 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100737 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000738 if (*activep && options->macs == NULL)
739 options->macs = xstrdup(arg);
740 break;
741
Damien Millerd5f62bf2010-09-24 22:11:14 +1000742 case oKexAlgorithms:
743 arg = strdelim(&s);
744 if (!arg || *arg == '\0')
745 fatal("%.200s line %d: Missing argument.",
746 filename, linenum);
747 if (!kex_names_valid(arg))
748 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
749 filename, linenum, arg ? arg : "<NONE>");
750 if (*activep && options->kex_algorithms == NULL)
751 options->kex_algorithms = xstrdup(arg);
752 break;
753
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000754 case oHostKeyAlgorithms:
755 arg = strdelim(&s);
756 if (!arg || *arg == '\0')
757 fatal("%.200s line %d: Missing argument.", filename, linenum);
758 if (!key_names_valid2(arg))
759 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100760 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000761 if (*activep && options->hostkeyalgorithms == NULL)
762 options->hostkeyalgorithms = xstrdup(arg);
763 break;
764
Damien Miller78928792000-04-12 20:17:38 +1000765 case oProtocol:
766 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000767 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000768 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000769 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000770 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000771 if (value == SSH_PROTO_UNKNOWN)
772 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100773 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000774 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
775 *intptr = value;
776 break;
777
Damien Miller95def091999-11-25 00:26:21 +1100778 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100779 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000780 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000781 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100782 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000783 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100784 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100785 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
786 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100787 break;
788
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000789 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100790 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100791 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000792 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100793 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000794 fatal("%.200s line %d: Missing port argument.",
795 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100796
Damien Millera699d952008-11-03 19:27:34 +1100797 if (opcode == oLocalForward ||
798 opcode == oRemoteForward) {
799 arg2 = strdelim(&s);
800 if (arg2 == NULL || *arg2 == '\0')
801 fatal("%.200s line %d: Missing target argument.",
802 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100803
Damien Millera699d952008-11-03 19:27:34 +1100804 /* construct a string for parse_forward */
805 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
806 } else if (opcode == oDynamicForward) {
807 strlcpy(fwdarg, arg, sizeof(fwdarg));
808 }
809
810 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100811 opcode == oDynamicForward ? 1 : 0,
812 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000813 fatal("%.200s line %d: Bad forwarding specification.",
814 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100815
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000816 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100817 if (opcode == oLocalForward ||
818 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100819 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000820 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000822 }
Damien Miller95def091999-11-25 00:26:21 +1100823 break;
824
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000825 case oClearAllForwardings:
826 intptr = &options->clear_forwardings;
827 goto parse_flag;
828
Damien Miller95def091999-11-25 00:26:21 +1100829 case oHost:
830 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000831 arg2 = NULL;
832 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
833 negated = *arg == '!';
834 if (negated)
835 arg++;
Damien Miller37023962000-07-11 17:31:38 +1000836 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +1000837 if (negated) {
838 debug("%.200s line %d: Skipping Host "
839 "block because of negated match "
840 "for %.100s", filename, linenum,
841 arg);
842 *activep = 0;
843 break;
844 }
845 if (!*activep)
846 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +1100847 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +1100848 }
Damien Millerfe924212011-05-15 08:44:45 +1000849 }
850 if (*activep)
851 debug("%.200s line %d: Applying options for %.100s",
852 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +1000853 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100854 return 0;
855
856 case oEscapeChar:
857 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000858 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000859 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100860 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000861 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000862 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
863 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000864 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000865 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000866 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000867 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100868 else {
869 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100870 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100871 /* NOTREACHED */
872 value = 0; /* Avoid compiler warning. */
873 }
874 if (*activep && *intptr == -1)
875 *intptr = value;
876 break;
877
Damien Miller20a8f972003-05-18 20:50:30 +1000878 case oAddressFamily:
879 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000880 if (!arg || *arg == '\0')
881 fatal("%s line %d: missing address family.",
882 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000883 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000884 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000885 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000886 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000887 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000888 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000889 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000890 else
891 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000892 if (*activep && *intptr == -1)
893 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000894 break;
895
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000896 case oEnableSSHKeysign:
897 intptr = &options->enable_ssh_keysign;
898 goto parse_flag;
899
Damien Millerbd394c32004-03-08 23:12:36 +1100900 case oIdentitiesOnly:
901 intptr = &options->identities_only;
902 goto parse_flag;
903
Damien Miller509b0102003-12-17 16:33:10 +1100904 case oServerAliveInterval:
905 intptr = &options->server_alive_interval;
906 goto parse_time;
907
908 case oServerAliveCountMax:
909 intptr = &options->server_alive_count_max;
910 goto parse_int;
911
Darren Tucker46bc0752004-05-02 22:11:30 +1000912 case oSendEnv:
913 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
914 if (strchr(arg, '=') != NULL)
915 fatal("%s line %d: Invalid environment name.",
916 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100917 if (!*activep)
918 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000919 if (options->num_send_env >= MAX_SEND_ENV)
920 fatal("%s line %d: too many send env.",
921 filename, linenum);
922 options->send_env[options->num_send_env++] =
923 xstrdup(arg);
924 }
925 break;
926
Damien Miller0e220db2004-06-15 10:34:08 +1000927 case oControlPath:
928 charptr = &options->control_path;
929 goto parse_string;
930
931 case oControlMaster:
932 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000933 arg = strdelim(&s);
934 if (!arg || *arg == '\0')
935 fatal("%.200s line %d: Missing ControlMaster argument.",
936 filename, linenum);
937 value = 0; /* To avoid compiler warning... */
938 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
939 value = SSHCTL_MASTER_YES;
940 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
941 value = SSHCTL_MASTER_NO;
942 else if (strcmp(arg, "auto") == 0)
943 value = SSHCTL_MASTER_AUTO;
944 else if (strcmp(arg, "ask") == 0)
945 value = SSHCTL_MASTER_ASK;
946 else if (strcmp(arg, "autoask") == 0)
947 value = SSHCTL_MASTER_AUTO_ASK;
948 else
949 fatal("%.200s line %d: Bad ControlMaster argument.",
950 filename, linenum);
951 if (*activep && *intptr == -1)
952 *intptr = value;
953 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000954
Damien Millere11e1ea2010-08-03 16:04:46 +1000955 case oControlPersist:
956 /* no/false/yes/true, or a time spec */
957 intptr = &options->control_persist;
958 arg = strdelim(&s);
959 if (!arg || *arg == '\0')
960 fatal("%.200s line %d: Missing ControlPersist"
961 " argument.", filename, linenum);
962 value = 0;
963 value2 = 0; /* timeout */
964 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
965 value = 0;
966 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
967 value = 1;
968 else if ((value2 = convtime(arg)) >= 0)
969 value = 1;
970 else
971 fatal("%.200s line %d: Bad ControlPersist argument.",
972 filename, linenum);
973 if (*activep && *intptr == -1) {
974 *intptr = value;
975 options->control_persist_timeout = value2;
976 }
977 break;
978
Damien Millere1776152005-03-01 21:47:37 +1100979 case oHashKnownHosts:
980 intptr = &options->hash_known_hosts;
981 goto parse_flag;
982
Damien Millerd27b9472005-12-13 19:29:02 +1100983 case oTunnel:
984 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100985 arg = strdelim(&s);
986 if (!arg || *arg == '\0')
987 fatal("%s line %d: Missing yes/point-to-point/"
988 "ethernet/no argument.", filename, linenum);
989 value = 0; /* silence compiler */
990 if (strcasecmp(arg, "ethernet") == 0)
991 value = SSH_TUNMODE_ETHERNET;
992 else if (strcasecmp(arg, "point-to-point") == 0)
993 value = SSH_TUNMODE_POINTOPOINT;
994 else if (strcasecmp(arg, "yes") == 0)
995 value = SSH_TUNMODE_DEFAULT;
996 else if (strcasecmp(arg, "no") == 0)
997 value = SSH_TUNMODE_NO;
998 else
999 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1000 "no argument: %s", filename, linenum, arg);
1001 if (*activep)
1002 *intptr = value;
1003 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001004
1005 case oTunnelDevice:
1006 arg = strdelim(&s);
1007 if (!arg || *arg == '\0')
1008 fatal("%.200s line %d: Missing argument.", filename, linenum);
1009 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001010 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001011 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1012 if (*activep) {
1013 options->tun_local = value;
1014 options->tun_remote = value2;
1015 }
1016 break;
1017
1018 case oLocalCommand:
1019 charptr = &options->local_command;
1020 goto parse_command;
1021
1022 case oPermitLocalCommand:
1023 intptr = &options->permit_local_command;
1024 goto parse_flag;
1025
Damien Miller10288242008-06-30 00:04:03 +10001026 case oVisualHostKey:
1027 intptr = &options->visual_host_key;
1028 goto parse_flag;
1029
Damien Miller0dac6fb2010-11-20 15:19:38 +11001030 case oIPQoS:
1031 arg = strdelim(&s);
1032 if ((value = parse_ipqos(arg)) == -1)
1033 fatal("%s line %d: Bad IPQoS value: %s",
1034 filename, linenum, arg);
1035 arg = strdelim(&s);
1036 if (arg == NULL)
1037 value2 = value;
1038 else if ((value2 = parse_ipqos(arg)) == -1)
1039 fatal("%s line %d: Bad IPQoS value: %s",
1040 filename, linenum, arg);
1041 if (*activep) {
1042 options->ip_qos_interactive = value;
1043 options->ip_qos_bulk = value2;
1044 }
1045 break;
1046
Darren Tucker71e4d542009-07-06 07:12:27 +10001047 case oUseRoaming:
1048 intptr = &options->use_roaming;
1049 goto parse_flag;
1050
Damien Miller21771e22011-05-15 08:45:50 +10001051 case oRequestTTY:
1052 arg = strdelim(&s);
1053 if (!arg || *arg == '\0')
1054 fatal("%s line %d: missing argument.",
1055 filename, linenum);
1056 intptr = &options->request_tty;
1057 if (strcasecmp(arg, "yes") == 0)
1058 value = REQUEST_TTY_YES;
1059 else if (strcasecmp(arg, "no") == 0)
1060 value = REQUEST_TTY_NO;
1061 else if (strcasecmp(arg, "force") == 0)
1062 value = REQUEST_TTY_FORCE;
1063 else if (strcasecmp(arg, "auto") == 0)
1064 value = REQUEST_TTY_AUTO;
1065 else
1066 fatal("Unsupported RequestTTY \"%s\"", arg);
1067 if (*activep && *intptr == -1)
1068 *intptr = value;
1069 break;
1070
Darren Tucker07636982013-05-16 20:30:03 +10001071 case oIgnoreUnknown:
1072 charptr = &options->ignored_unknown;
1073 goto parse_string;
1074
Ben Lindstrom4daea862002-06-09 20:04:02 +00001075 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001076 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001077 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001078 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001079
Damien Millerf9b3feb2003-05-16 11:38:32 +10001080 case oUnsupported:
1081 error("%s line %d: Unsupported option \"%s\"",
1082 filename, linenum, keyword);
1083 return 0;
1084
Damien Miller95def091999-11-25 00:26:21 +11001085 default:
1086 fatal("process_config_line: Unimplemented opcode %d", opcode);
1087 }
1088
1089 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001090 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001091 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001092 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001093 }
Damien Miller95def091999-11-25 00:26:21 +11001094 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095}
1096
1097
Damien Miller5428f641999-11-25 11:54:57 +11001098/*
1099 * Reads the config file and modifies the options accordingly. Options
1100 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001101 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001102 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001103
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001104int
Darren Tuckerfc959702004-07-17 16:12:08 +10001105read_config_file(const char *filename, const char *host, Options *options,
Darren Tuckeraefa3682013-04-05 11:18:35 +11001106 int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107{
Damien Miller95def091999-11-25 00:26:21 +11001108 FILE *f;
1109 char line[1024];
1110 int active, linenum;
1111 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller57a44762004-04-20 20:11:57 +10001113 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001114 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Darren Tuckeraefa3682013-04-05 11:18:35 +11001116 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001117 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001118
Damien Miller33793852004-06-15 10:27:55 +10001119 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001120 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001121 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001122 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001123 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001124 }
1125
Damien Miller95def091999-11-25 00:26:21 +11001126 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
Damien Miller5428f641999-11-25 11:54:57 +11001128 /*
1129 * Mark that we are now processing the options. This flag is turned
1130 * on/off by Host specifications.
1131 */
Damien Miller95def091999-11-25 00:26:21 +11001132 active = 1;
1133 linenum = 0;
1134 while (fgets(line, sizeof(line), f)) {
1135 /* Update line number counter. */
1136 linenum++;
Darren Tuckeraefa3682013-04-05 11:18:35 +11001137 if (process_config_line(options, host, line, filename, linenum,
1138 &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001139 bad_options++;
1140 }
1141 fclose(f);
1142 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001143 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001144 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001145 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146}
1147
Damien Miller5428f641999-11-25 11:54:57 +11001148/*
1149 * Initializes options to special values that indicate that they have not yet
1150 * been set. Read_config_file will only set options with this value. Options
1151 * are processed in the following order: command line, user config file,
1152 * system config file. Last, fill_default_options is called.
1153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154
Damien Miller4af51302000-04-16 11:18:38 +10001155void
Damien Miller95def091999-11-25 00:26:21 +11001156initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157{
Damien Miller95def091999-11-25 00:26:21 +11001158 memset(options, 'X', sizeof(*options));
1159 options->forward_agent = -1;
1160 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001161 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001162 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001163 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001164 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001165 options->gateway_ports = -1;
1166 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001167 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001169 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001170 options->gss_authentication = -1;
1171 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001172 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001173 options->kbd_interactive_authentication = -1;
1174 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001175 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001176 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001177 options->batch_mode = -1;
1178 options->check_host_ip = -1;
1179 options->strict_host_key_checking = -1;
1180 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001181 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001182 options->compression_level = -1;
1183 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001184 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001185 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001186 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001187 options->number_of_password_prompts = -1;
1188 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001189 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001190 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001191 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001192 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001193 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001194 options->num_identity_files = 0;
1195 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001196 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001197 options->proxy_command = NULL;
1198 options->user = NULL;
1199 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001200 options->num_system_hostfiles = 0;
1201 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001202 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001203 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001204 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001205 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001206 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001207 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001208 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001209 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001210 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001211 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001212 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001213 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001214 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001215 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001216 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001217 options->server_alive_interval = -1;
1218 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001219 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001220 options->control_path = NULL;
1221 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001222 options->control_persist = -1;
1223 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001224 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001225 options->tun_open = -1;
1226 options->tun_local = -1;
1227 options->tun_remote = -1;
1228 options->local_command = NULL;
1229 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001230 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001231 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001232 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001233 options->ip_qos_interactive = -1;
1234 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001235 options->request_tty = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001236 options->ignored_unknown = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237}
1238
Damien Miller5428f641999-11-25 11:54:57 +11001239/*
1240 * Called after processing other sources of option data, this fills those
1241 * options for which no value has been specified with their default values.
1242 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
Damien Miller4af51302000-04-16 11:18:38 +10001244void
Damien Miller95def091999-11-25 00:26:21 +11001245fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Damien Miller95def091999-11-25 00:26:21 +11001247 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001248 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001249 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001250 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001251 if (options->forward_x11_trusted == -1)
1252 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001253 if (options->forward_x11_timeout == -1)
1254 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001255 if (options->exit_on_forward_failure == -1)
1256 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001257 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001258 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001259 if (options->gateway_ports == -1)
1260 options->gateway_ports = 0;
1261 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001262 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001263 if (options->rsa_authentication == -1)
1264 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001265 if (options->pubkey_authentication == -1)
1266 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001267 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001268 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001269 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001270 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001271 if (options->gss_deleg_creds == -1)
1272 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001273 if (options->password_authentication == -1)
1274 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001275 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001276 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001277 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001278 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001279 if (options->hostbased_authentication == -1)
1280 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001281 if (options->batch_mode == -1)
1282 options->batch_mode = 0;
1283 if (options->check_host_ip == -1)
1284 options->check_host_ip = 1;
1285 if (options->strict_host_key_checking == -1)
1286 options->strict_host_key_checking = 2; /* 2 is default */
1287 if (options->compression == -1)
1288 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001289 if (options->tcp_keep_alive == -1)
1290 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001291 if (options->compression_level == -1)
1292 options->compression_level = 6;
1293 if (options->port == -1)
1294 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001295 if (options->address_family == -1)
1296 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001297 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001298 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001299 if (options->number_of_password_prompts == -1)
1300 options->number_of_password_prompts = 3;
1301 /* Selected in ssh_login(). */
1302 if (options->cipher == -1)
1303 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001304 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001305 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001306 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001307 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001308 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001309 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001310 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001311 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001312 add_identity_file(options, "~/",
1313 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001314 }
1315 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001316 add_identity_file(options, "~/",
1317 _PATH_SSH_CLIENT_ID_RSA, 0);
1318 add_identity_file(options, "~/",
1319 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001320#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001321 add_identity_file(options, "~/",
1322 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001323#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001324 }
Damien Millereba71ba2000-04-29 23:57:08 +10001325 }
Damien Miller95def091999-11-25 00:26:21 +11001326 if (options->escape_char == -1)
1327 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001328 if (options->num_system_hostfiles == 0) {
1329 options->system_hostfiles[options->num_system_hostfiles++] =
1330 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1331 options->system_hostfiles[options->num_system_hostfiles++] =
1332 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1333 }
1334 if (options->num_user_hostfiles == 0) {
1335 options->user_hostfiles[options->num_user_hostfiles++] =
1336 xstrdup(_PATH_SSH_USER_HOSTFILE);
1337 options->user_hostfiles[options->num_user_hostfiles++] =
1338 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1339 }
Damien Millerfcd93202002-02-05 12:26:34 +11001340 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001341 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001342 if (options->clear_forwardings == 1)
1343 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001344 if (options->no_host_authentication_for_localhost == - 1)
1345 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001346 if (options->identities_only == -1)
1347 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001348 if (options->enable_ssh_keysign == -1)
1349 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001350 if (options->rekey_limit == -1)
1351 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001352 if (options->rekey_interval == -1)
1353 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001354 if (options->verify_host_key_dns == -1)
1355 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001356 if (options->server_alive_interval == -1)
1357 options->server_alive_interval = 0;
1358 if (options->server_alive_count_max == -1)
1359 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001360 if (options->control_master == -1)
1361 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001362 if (options->control_persist == -1) {
1363 options->control_persist = 0;
1364 options->control_persist_timeout = 0;
1365 }
Damien Millere1776152005-03-01 21:47:37 +11001366 if (options->hash_known_hosts == -1)
1367 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001368 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001369 options->tun_open = SSH_TUNMODE_NO;
1370 if (options->tun_local == -1)
1371 options->tun_local = SSH_TUNID_ANY;
1372 if (options->tun_remote == -1)
1373 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001374 if (options->permit_local_command == -1)
1375 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001376 if (options->use_roaming == -1)
1377 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001378 if (options->visual_host_key == -1)
1379 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001380 if (options->zero_knowledge_password_authentication == -1)
1381 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001382 if (options->ip_qos_interactive == -1)
1383 options->ip_qos_interactive = IPTOS_LOWDELAY;
1384 if (options->ip_qos_bulk == -1)
1385 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001386 if (options->request_tty == -1)
1387 options->request_tty = REQUEST_TTY_AUTO;
Damien Millerd27b9472005-12-13 19:29:02 +11001388 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001389 /* options->proxy_command should not be set by default */
1390 /* options->user will be set in the main program if appropriate */
1391 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001392 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001393 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001395
1396/*
1397 * parse_forward
1398 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001399 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001400 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001401 * dynamicfwd == 1
1402 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001403 * returns number of arguments parsed or zero on error
1404 */
1405int
Damien Miller4bf648f2009-02-14 16:28:21 +11001406parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001407{
1408 int i;
1409 char *p, *cp, *fwdarg[4];
1410
1411 memset(fwd, '\0', sizeof(*fwd));
1412
1413 cp = p = xstrdup(fwdspec);
1414
1415 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001416 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001417 cp++;
1418
1419 for (i = 0; i < 4; ++i)
1420 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1421 break;
1422
Damien Millerf4b39532008-11-03 19:28:21 +11001423 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001424 if (cp != NULL)
1425 i = 0; /* failure */
1426
1427 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001428 case 1:
1429 fwd->listen_host = NULL;
1430 fwd->listen_port = a2port(fwdarg[0]);
1431 fwd->connect_host = xstrdup("socks");
1432 break;
1433
1434 case 2:
1435 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1436 fwd->listen_port = a2port(fwdarg[1]);
1437 fwd->connect_host = xstrdup("socks");
1438 break;
1439
Damien Millerf91ee4c2005-03-01 21:24:33 +11001440 case 3:
1441 fwd->listen_host = NULL;
1442 fwd->listen_port = a2port(fwdarg[0]);
1443 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1444 fwd->connect_port = a2port(fwdarg[2]);
1445 break;
1446
1447 case 4:
1448 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1449 fwd->listen_port = a2port(fwdarg[1]);
1450 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1451 fwd->connect_port = a2port(fwdarg[3]);
1452 break;
1453 default:
1454 i = 0; /* failure */
1455 }
1456
1457 xfree(p);
1458
Damien Millera699d952008-11-03 19:27:34 +11001459 if (dynamicfwd) {
1460 if (!(i == 1 || i == 2))
1461 goto fail_free;
1462 } else {
1463 if (!(i == 3 || i == 4))
1464 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001465 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001466 goto fail_free;
1467 }
1468
Damien Miller4bf648f2009-02-14 16:28:21 +11001469 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001470 goto fail_free;
1471
1472 if (fwd->connect_host != NULL &&
1473 strlen(fwd->connect_host) >= NI_MAXHOST)
1474 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001475 if (fwd->listen_host != NULL &&
1476 strlen(fwd->listen_host) >= NI_MAXHOST)
1477 goto fail_free;
1478
Damien Millerf91ee4c2005-03-01 21:24:33 +11001479
1480 return (i);
1481
1482 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001483 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001484 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001485 fwd->connect_host = NULL;
1486 }
1487 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001488 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001489 fwd->listen_host = NULL;
1490 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001491 return (0);
1492}