blob: 30c1e83bdb59478fdb16ac018dad3b53fd1266d1 [file] [log] [blame]
Darren Tucker9113d0c2013-05-16 20:48:14 +10001/* $OpenBSD: readconf.c,v 1.201 2013/05/16 10:43:34 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 Tuckere194ba42013-05-16 20:47:31 +100033#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100034#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100035#endif
Damien Millerc7b06362006-03-15 11:53:45 +110036
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100039#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "cipher.h"
41#include "pathnames.h"
42#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "readconf.h"
45#include "match.h"
46#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000048#include "kex.h"
49#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51/* Format of the configuration file:
52
53 # Configuration data is parsed as follows:
54 # 1. command line options
55 # 2. user-specific file
56 # 3. system-wide file
57 # Any configuration value is only changed the first time it is set.
58 # Thus, host-specific definitions should be at the beginning of the
59 # configuration file, and defaults at the end.
60
61 # Host-specific declarations. These may override anything above. A single
62 # host may match multiple declarations; these are processed in the order
63 # that they are given in.
64
65 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000066 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
68 Host fake.com
69 HostName another.host.name.real.org
70 User blaah
71 Port 34289
72 ForwardX11 no
73 ForwardAgent no
74
75 Host books.com
76 RemoteForward 9999 shadows.cs.hut.fi:9999
77 Cipher 3des
78
79 Host fascist.blob.com
80 Port 23123
81 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 PasswordAuthentication no
83
84 Host puukko.hut.fi
85 User t35124p
86 ProxyCommand ssh-proxy %h %p
87
88 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000089 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
91 Host *.su
92 Cipher none
93 PasswordAuthentication no
94
Damien Millerd27b9472005-12-13 19:29:02 +110095 Host vpn.fake.com
96 Tunnel yes
97 TunnelDevice 3
98
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099 # Defaults for various options
100 Host *
101 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100102 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 PasswordAuthentication yes
104 RSAAuthentication yes
105 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100107 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108 IdentityFile ~/.ssh/identity
109 Port 22
110 EscapeChar ~
111
112*/
113
114/* Keyword tokens. */
115
Damien Miller95def091999-11-25 00:26:21 +1100116typedef enum {
117 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000118 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
119 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000120 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000121 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100122 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
123 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
124 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
125 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100126 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000127 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100128 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000129 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000130 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100131 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000132 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000133 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000134 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100135 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000136 oSendEnv, oControlPath, oControlMaster, oControlPersist,
137 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100138 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100139 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Darren Tucker07636982013-05-16 20:30:03 +1000140 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown,
141 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142} OpCodes;
143
144/* Textual representations of the tokens. */
145
Damien Miller95def091999-11-25 00:26:21 +1100146static struct {
147 const char *name;
148 OpCodes opcode;
149} keywords[] = {
150 { "forwardagent", oForwardAgent },
151 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000152 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000153 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000154 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000155 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "gatewayports", oGatewayPorts },
157 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000158 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100160 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
161 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100162 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000164 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000165 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000166 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000167 { "challengeresponseauthentication", oChallengeResponseAuthentication },
168 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
169 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000170 { "kerberosauthentication", oUnsupported },
171 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000172 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000173#if defined(GSSAPI)
174 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000175 { "gssapidelegatecredentials", oGssDelegateCreds },
176#else
177 { "gssapiauthentication", oUnsupported },
178 { "gssapidelegatecredentials", oUnsupported },
179#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000180 { "fallbacktorsh", oDeprecated },
181 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100183 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100184 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000186 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100187 { "proxycommand", oProxyCommand },
188 { "port", oPort },
189 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000190 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000191 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000192 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "remoteforward", oRemoteForward },
194 { "localforward", oLocalForward },
195 { "user", oUser },
196 { "host", oHost },
197 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100198 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000199 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100200 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000201 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100202 { "connectionattempts", oConnectionAttempts },
203 { "batchmode", oBatchMode },
204 { "checkhostip", oCheckHostIP },
205 { "stricthostkeychecking", oStrictHostKeyChecking },
206 { "compression", oCompression },
207 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100208 { "tcpkeepalive", oTCPKeepAlive },
209 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100210 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100211 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000212 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000213 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000214 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000215 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100216#ifdef ENABLE_PKCS11
217 { "smartcarddevice", oPKCS11Provider },
218 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000219#else
220 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100221 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000222#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000224 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000225 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100226 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000227 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000228 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000229 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100230 { "serveraliveinterval", oServerAliveInterval },
231 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000232 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000233 { "controlpath", oControlPath },
234 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000235 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100236 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100237 { "tunnel", oTunnel },
238 { "tunneldevice", oTunnelDevice },
239 { "localcommand", oLocalCommand },
240 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000241 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000242 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100243#ifdef JPAKE
244 { "zeroknowledgepasswordauthentication",
245 oZeroKnowledgePasswordAuthentication },
246#else
247 { "zeroknowledgepasswordauthentication", oUnsupported },
248#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000249 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100250 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000251 { "requesttty", oRequestTTY },
Darren Tucker07636982013-05-16 20:30:03 +1000252 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100253
Ben Lindstrom65366a82001-12-06 16:32:47 +0000254 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100255};
256
Damien Miller5428f641999-11-25 11:54:57 +1100257/*
258 * Adds a local TCP/IP port forward to options. Never returns if there is an
259 * error.
260 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller4af51302000-04-16 11:18:38 +1000262void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264{
Damien Miller95def091999-11-25 00:26:21 +1100265 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000266#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100267 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100268 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000269 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100270#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000271 options->local_forwards = xrealloc(options->local_forwards,
272 options->num_local_forwards + 1,
273 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100274 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275
Damien Miller1a0442f2008-11-05 16:30:06 +1100276 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100277 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100278 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100279 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280}
281
Damien Miller5428f641999-11-25 11:54:57 +1100282/*
283 * Adds a remote TCP/IP port forward to options. Never returns if there is
284 * an error.
285 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller4af51302000-04-16 11:18:38 +1000287void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289{
Damien Miller95def091999-11-25 00:26:21 +1100290 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000291
292 options->remote_forwards = xrealloc(options->remote_forwards,
293 options->num_remote_forwards + 1,
294 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100295 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296
Damien Miller1a0442f2008-11-05 16:30:06 +1100297 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100298 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100299 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100301 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000302 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000305static void
306clear_forwardings(Options *options)
307{
308 int i;
309
Damien Millerf91ee4c2005-03-01 21:24:33 +1100310 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100311 if (options->local_forwards[i].listen_host != NULL)
312 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100313 xfree(options->local_forwards[i].connect_host);
314 }
Damien Miller232cfb12010-06-26 09:50:30 +1000315 if (options->num_local_forwards > 0) {
316 xfree(options->local_forwards);
317 options->local_forwards = NULL;
318 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000319 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100320 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100321 if (options->remote_forwards[i].listen_host != NULL)
322 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100323 xfree(options->remote_forwards[i].connect_host);
324 }
Damien Miller232cfb12010-06-26 09:50:30 +1000325 if (options->num_remote_forwards > 0) {
326 xfree(options->remote_forwards);
327 options->remote_forwards = NULL;
328 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000329 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100330 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000331}
332
Darren Tucker19104782013-04-05 11:13:08 +1100333void
334add_identity_file(Options *options, const char *dir, const char *filename,
335 int userprovided)
336{
337 char *path;
338
339 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
340 fatal("Too many identity files specified (max %d)",
341 SSH_MAX_IDENTITY_FILES);
342
343 if (dir == NULL) /* no dir, filename is absolute */
344 path = xstrdup(filename);
345 else
346 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
347
348 options->identity_file_userprovided[options->num_identity_files] =
349 userprovided;
350 options->identity_files[options->num_identity_files++] = path;
351}
352
Damien Miller5428f641999-11-25 11:54:57 +1100353/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000354 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100355 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller4af51302000-04-16 11:18:38 +1000357static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000358parse_token(const char *cp, const char *filename, int linenum,
359 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Darren Tucker07636982013-05-16 20:30:03 +1000361 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller95def091999-11-25 00:26:21 +1100363 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000364 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100365 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000366 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
367 strlen(ignored_unknown), 1) == 1)
368 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000369 error("%s: line %d: Bad configuration option: %s",
370 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100371 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
Damien Miller5428f641999-11-25 11:54:57 +1100374/*
375 * Processes a single option line as used in the configuration files. This
376 * only sets those values that have not already been set.
377 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100378#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Damien Miller2ccf6611999-11-15 15:25:10 +1100380int
381process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100382 char *line, const char *filename, int linenum,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100383 int *activep, int userconfig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384{
Damien Miller295ee632011-05-29 21:42:31 +1000385 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
386 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000387 u_int i, *uintptr, max_entries = 0;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000388 int negated, opcode, *intptr, value, value2;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100389 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000390 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100391 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
Damien Millerc652cac2003-05-14 13:40:54 +1000394 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100395 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000396 if (strchr(WHITESPACE, line[len]) == NULL)
397 break;
398 line[len] = '\0';
399 }
400
Damien Millerbe484b52000-07-15 14:14:16 +1000401 s = line;
402 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100403 if ((keyword = strdelim(&s)) == NULL)
404 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000405 /* Ignore leading whitespace. */
406 if (*keyword == '\0')
407 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000408 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100409 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000410 /* Match lowercase keyword */
411 for (i = 0; i < strlen(keyword); i++)
412 keyword[i] = tolower(keyword[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Darren Tucker07636982013-05-16 20:30:03 +1000414 opcode = parse_token(keyword, filename, linenum,
415 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller95def091999-11-25 00:26:21 +1100417 switch (opcode) {
418 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100419 /* don't panic, but count bad options */
420 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100421 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000422 case oIgnoredUnknownOption:
423 debug("%s line %d: Ignored unknown option \"%s\"",
424 filename, linenum, keyword);
425 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000426 case oConnectTimeout:
427 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100428parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000429 arg = strdelim(&s);
430 if (!arg || *arg == '\0')
431 fatal("%s line %d: missing time value.",
432 filename, linenum);
433 if ((value = convtime(arg)) == -1)
434 fatal("%s line %d: invalid time value.",
435 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100436 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000437 *intptr = value;
438 break;
439
Damien Miller95def091999-11-25 00:26:21 +1100440 case oForwardAgent:
441 intptr = &options->forward_agent;
442parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000443 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000444 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100445 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
446 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000447 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100448 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000449 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100450 value = 0;
451 else
452 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
453 if (*activep && *intptr == -1)
454 *intptr = value;
455 break;
456
457 case oForwardX11:
458 intptr = &options->forward_x11;
459 goto parse_flag;
460
Darren Tucker0a118da2003-10-15 15:54:32 +1000461 case oForwardX11Trusted:
462 intptr = &options->forward_x11_trusted;
463 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000464
465 case oForwardX11Timeout:
466 intptr = &options->forward_x11_timeout;
467 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 case oGatewayPorts:
470 intptr = &options->gateway_ports;
471 goto parse_flag;
472
Darren Tuckere7d4b192006-07-12 22:17:10 +1000473 case oExitOnForwardFailure:
474 intptr = &options->exit_on_forward_failure;
475 goto parse_flag;
476
Damien Miller95def091999-11-25 00:26:21 +1100477 case oUsePrivilegedPort:
478 intptr = &options->use_privileged_port;
479 goto parse_flag;
480
Damien Miller95def091999-11-25 00:26:21 +1100481 case oPasswordAuthentication:
482 intptr = &options->password_authentication;
483 goto parse_flag;
484
Damien Miller01ed2272008-11-05 16:20:46 +1100485 case oZeroKnowledgePasswordAuthentication:
486 intptr = &options->zero_knowledge_password_authentication;
487 goto parse_flag;
488
Damien Miller874d77b2000-10-14 16:23:11 +1100489 case oKbdInteractiveAuthentication:
490 intptr = &options->kbd_interactive_authentication;
491 goto parse_flag;
492
493 case oKbdInteractiveDevices:
494 charptr = &options->kbd_interactive_devices;
495 goto parse_string;
496
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497 case oPubkeyAuthentication:
498 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000499 goto parse_flag;
500
Damien Miller95def091999-11-25 00:26:21 +1100501 case oRSAAuthentication:
502 intptr = &options->rsa_authentication;
503 goto parse_flag;
504
505 case oRhostsRSAAuthentication:
506 intptr = &options->rhosts_rsa_authentication;
507 goto parse_flag;
508
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000509 case oHostbasedAuthentication:
510 intptr = &options->hostbased_authentication;
511 goto parse_flag;
512
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000513 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000514 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100515 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000516
Darren Tucker0efd1552003-08-26 11:49:55 +1000517 case oGssAuthentication:
518 intptr = &options->gss_authentication;
519 goto parse_flag;
520
521 case oGssDelegateCreds:
522 intptr = &options->gss_deleg_creds;
523 goto parse_flag;
524
Damien Miller95def091999-11-25 00:26:21 +1100525 case oBatchMode:
526 intptr = &options->batch_mode;
527 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 case oCheckHostIP:
530 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000531 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Damien Miller37876e92003-05-15 10:19:46 +1000533 case oVerifyHostKeyDNS:
534 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100535 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000536
Damien Miller95def091999-11-25 00:26:21 +1100537 case oStrictHostKeyChecking:
538 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100539parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000540 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000541 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000542 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100543 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100544 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000545 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100546 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000547 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100548 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000549 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100550 value = 2;
551 else
552 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
553 if (*activep && *intptr == -1)
554 *intptr = value;
555 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
Damien Miller95def091999-11-25 00:26:21 +1100557 case oCompression:
558 intptr = &options->compression;
559 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Damien Miller12c150e2003-12-17 16:31:10 +1100561 case oTCPKeepAlive:
562 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100563 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000565 case oNoHostAuthenticationForLocalhost:
566 intptr = &options->no_host_authentication_for_localhost;
567 goto parse_flag;
568
Damien Miller95def091999-11-25 00:26:21 +1100569 case oNumberOfPasswordPrompts:
570 intptr = &options->number_of_password_prompts;
571 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 case oCompressionLevel:
574 intptr = &options->compression_level;
575 goto parse_int;
576
Damien Millera5539d22003-04-09 20:50:06 +1000577 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000578 arg = strdelim(&s);
579 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000580 fatal("%.200s line %d: Missing argument.", filename,
581 linenum);
582 if (strcmp(arg, "default") == 0) {
583 val64 = 0;
584 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000585 if (scan_scaled(arg, &val64) == -1)
586 fatal("%.200s line %d: Bad number '%s': %s",
587 filename, linenum, arg, strerror(errno));
588 /* check for too-large or too-small limits */
589 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000590 fatal("%.200s line %d: RekeyLimit too large",
591 filename, linenum);
592 if (val64 != 0 && val64 < 16)
593 fatal("%.200s line %d: RekeyLimit too small",
594 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000595 }
Damien Miller3dff1762008-02-10 22:25:52 +1100596 if (*activep && options->rekey_limit == -1)
597 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000598 if (s != NULL) { /* optional rekey interval present */
599 if (strcmp(s, "none") == 0) {
600 (void)strdelim(&s); /* discard */
601 break;
602 }
603 intptr = &options->rekey_interval;
604 goto parse_time;
605 }
Damien Millera5539d22003-04-09 20:50:06 +1000606 break;
607
Damien Miller95def091999-11-25 00:26:21 +1100608 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000609 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000610 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100611 fatal("%.200s line %d: Missing argument.", filename, linenum);
612 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100613 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000614 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100615 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100616 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckeraefa3682013-04-05 11:18:35 +1100617 add_identity_file(options, NULL, arg, userconfig);
Damien Miller95def091999-11-25 00:26:21 +1100618 }
619 break;
620
Damien Millerd3a18572000-06-07 19:55:44 +1000621 case oXAuthLocation:
622 charptr=&options->xauth_location;
623 goto parse_string;
624
Damien Miller95def091999-11-25 00:26:21 +1100625 case oUser:
626 charptr = &options->user;
627parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000628 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000629 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000630 fatal("%.200s line %d: Missing argument.",
631 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100632 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000633 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100634 break;
635
636 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000637 cpptr = (char **)&options->system_hostfiles;
638 uintptr = &options->num_system_hostfiles;
639 max_entries = SSH_MAX_HOSTS_FILES;
640parse_char_array:
641 if (*activep && *uintptr == 0) {
642 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
643 if ((*uintptr) >= max_entries)
644 fatal("%s line %d: "
645 "too many authorized keys files.",
646 filename, linenum);
647 cpptr[(*uintptr)++] = xstrdup(arg);
648 }
649 }
650 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100651
652 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000653 cpptr = (char **)&options->user_hostfiles;
654 uintptr = &options->num_user_hostfiles;
655 max_entries = SSH_MAX_HOSTS_FILES;
656 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000657
Damien Miller95def091999-11-25 00:26:21 +1100658 case oHostName:
659 charptr = &options->hostname;
660 goto parse_string;
661
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000662 case oHostKeyAlias:
663 charptr = &options->host_key_alias;
664 goto parse_string;
665
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000666 case oPreferredAuthentications:
667 charptr = &options->preferred_authentications;
668 goto parse_string;
669
Ben Lindstrome0f88042001-04-30 13:06:24 +0000670 case oBindAddress:
671 charptr = &options->bind_address;
672 goto parse_string;
673
Damien Miller7ea845e2010-02-12 09:21:02 +1100674 case oPKCS11Provider:
675 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000676 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000677
Damien Miller95def091999-11-25 00:26:21 +1100678 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100679 charptr = &options->proxy_command;
680parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000681 if (s == NULL)
682 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100683 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100684 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100685 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100686 return 0;
687
688 case oPort:
689 intptr = &options->port;
690parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000691 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000692 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100693 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000694 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100695 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100696
697 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000698 value = strtol(arg, &endofnumber, 0);
699 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100700 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100701 if (*activep && *intptr == -1)
702 *intptr = value;
703 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller95def091999-11-25 00:26:21 +1100705 case oConnectionAttempts:
706 intptr = &options->connection_attempts;
707 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100708
Damien Miller95def091999-11-25 00:26:21 +1100709 case oCipher:
710 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000711 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000712 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000713 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000714 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100715 if (value == -1)
716 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100718 if (*activep && *intptr == -1)
719 *intptr = value;
720 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller78928792000-04-12 20:17:38 +1000722 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000723 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000724 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000725 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000726 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000727 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100728 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000729 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000730 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000731 break;
732
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000733 case oMacs:
734 arg = strdelim(&s);
735 if (!arg || *arg == '\0')
736 fatal("%.200s line %d: Missing argument.", filename, linenum);
737 if (!mac_valid(arg))
738 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100739 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000740 if (*activep && options->macs == NULL)
741 options->macs = xstrdup(arg);
742 break;
743
Damien Millerd5f62bf2010-09-24 22:11:14 +1000744 case oKexAlgorithms:
745 arg = strdelim(&s);
746 if (!arg || *arg == '\0')
747 fatal("%.200s line %d: Missing argument.",
748 filename, linenum);
749 if (!kex_names_valid(arg))
750 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
751 filename, linenum, arg ? arg : "<NONE>");
752 if (*activep && options->kex_algorithms == NULL)
753 options->kex_algorithms = xstrdup(arg);
754 break;
755
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000756 case oHostKeyAlgorithms:
757 arg = strdelim(&s);
758 if (!arg || *arg == '\0')
759 fatal("%.200s line %d: Missing argument.", filename, linenum);
760 if (!key_names_valid2(arg))
761 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100762 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000763 if (*activep && options->hostkeyalgorithms == NULL)
764 options->hostkeyalgorithms = xstrdup(arg);
765 break;
766
Damien Miller78928792000-04-12 20:17:38 +1000767 case oProtocol:
768 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000769 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000770 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000771 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000772 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000773 if (value == SSH_PROTO_UNKNOWN)
774 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100775 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000776 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
777 *intptr = value;
778 break;
779
Damien Miller95def091999-11-25 00:26:21 +1100780 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100781 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000782 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000783 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100784 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000785 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100786 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100787 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
788 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100789 break;
790
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000791 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100792 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100793 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000794 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100795 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000796 fatal("%.200s line %d: Missing port argument.",
797 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100798
Damien Millera699d952008-11-03 19:27:34 +1100799 if (opcode == oLocalForward ||
800 opcode == oRemoteForward) {
801 arg2 = strdelim(&s);
802 if (arg2 == NULL || *arg2 == '\0')
803 fatal("%.200s line %d: Missing target argument.",
804 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100805
Damien Millera699d952008-11-03 19:27:34 +1100806 /* construct a string for parse_forward */
807 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
808 } else if (opcode == oDynamicForward) {
809 strlcpy(fwdarg, arg, sizeof(fwdarg));
810 }
811
812 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100813 opcode == oDynamicForward ? 1 : 0,
814 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000815 fatal("%.200s line %d: Bad forwarding specification.",
816 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100817
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000818 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100819 if (opcode == oLocalForward ||
820 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000822 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100823 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000824 }
Damien Miller95def091999-11-25 00:26:21 +1100825 break;
826
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000827 case oClearAllForwardings:
828 intptr = &options->clear_forwardings;
829 goto parse_flag;
830
Damien Miller95def091999-11-25 00:26:21 +1100831 case oHost:
832 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000833 arg2 = NULL;
834 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
835 negated = *arg == '!';
836 if (negated)
837 arg++;
Damien Miller37023962000-07-11 17:31:38 +1000838 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +1000839 if (negated) {
840 debug("%.200s line %d: Skipping Host "
841 "block because of negated match "
842 "for %.100s", filename, linenum,
843 arg);
844 *activep = 0;
845 break;
846 }
847 if (!*activep)
848 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +1100849 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +1100850 }
Damien Millerfe924212011-05-15 08:44:45 +1000851 }
852 if (*activep)
853 debug("%.200s line %d: Applying options for %.100s",
854 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +1000855 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100856 return 0;
857
858 case oEscapeChar:
859 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000860 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000861 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100862 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000863 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000864 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
865 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000866 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000867 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000868 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000869 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100870 else {
871 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100872 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100873 /* NOTREACHED */
874 value = 0; /* Avoid compiler warning. */
875 }
876 if (*activep && *intptr == -1)
877 *intptr = value;
878 break;
879
Damien Miller20a8f972003-05-18 20:50:30 +1000880 case oAddressFamily:
881 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000882 if (!arg || *arg == '\0')
883 fatal("%s line %d: missing address family.",
884 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000885 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000886 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000887 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000888 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000889 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000890 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000891 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000892 else
893 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000894 if (*activep && *intptr == -1)
895 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000896 break;
897
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000898 case oEnableSSHKeysign:
899 intptr = &options->enable_ssh_keysign;
900 goto parse_flag;
901
Damien Millerbd394c32004-03-08 23:12:36 +1100902 case oIdentitiesOnly:
903 intptr = &options->identities_only;
904 goto parse_flag;
905
Damien Miller509b0102003-12-17 16:33:10 +1100906 case oServerAliveInterval:
907 intptr = &options->server_alive_interval;
908 goto parse_time;
909
910 case oServerAliveCountMax:
911 intptr = &options->server_alive_count_max;
912 goto parse_int;
913
Darren Tucker46bc0752004-05-02 22:11:30 +1000914 case oSendEnv:
915 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
916 if (strchr(arg, '=') != NULL)
917 fatal("%s line %d: Invalid environment name.",
918 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100919 if (!*activep)
920 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000921 if (options->num_send_env >= MAX_SEND_ENV)
922 fatal("%s line %d: too many send env.",
923 filename, linenum);
924 options->send_env[options->num_send_env++] =
925 xstrdup(arg);
926 }
927 break;
928
Damien Miller0e220db2004-06-15 10:34:08 +1000929 case oControlPath:
930 charptr = &options->control_path;
931 goto parse_string;
932
933 case oControlMaster:
934 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000935 arg = strdelim(&s);
936 if (!arg || *arg == '\0')
937 fatal("%.200s line %d: Missing ControlMaster argument.",
938 filename, linenum);
939 value = 0; /* To avoid compiler warning... */
940 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
941 value = SSHCTL_MASTER_YES;
942 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
943 value = SSHCTL_MASTER_NO;
944 else if (strcmp(arg, "auto") == 0)
945 value = SSHCTL_MASTER_AUTO;
946 else if (strcmp(arg, "ask") == 0)
947 value = SSHCTL_MASTER_ASK;
948 else if (strcmp(arg, "autoask") == 0)
949 value = SSHCTL_MASTER_AUTO_ASK;
950 else
951 fatal("%.200s line %d: Bad ControlMaster argument.",
952 filename, linenum);
953 if (*activep && *intptr == -1)
954 *intptr = value;
955 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000956
Damien Millere11e1ea2010-08-03 16:04:46 +1000957 case oControlPersist:
958 /* no/false/yes/true, or a time spec */
959 intptr = &options->control_persist;
960 arg = strdelim(&s);
961 if (!arg || *arg == '\0')
962 fatal("%.200s line %d: Missing ControlPersist"
963 " argument.", filename, linenum);
964 value = 0;
965 value2 = 0; /* timeout */
966 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
967 value = 0;
968 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
969 value = 1;
970 else if ((value2 = convtime(arg)) >= 0)
971 value = 1;
972 else
973 fatal("%.200s line %d: Bad ControlPersist argument.",
974 filename, linenum);
975 if (*activep && *intptr == -1) {
976 *intptr = value;
977 options->control_persist_timeout = value2;
978 }
979 break;
980
Damien Millere1776152005-03-01 21:47:37 +1100981 case oHashKnownHosts:
982 intptr = &options->hash_known_hosts;
983 goto parse_flag;
984
Damien Millerd27b9472005-12-13 19:29:02 +1100985 case oTunnel:
986 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100987 arg = strdelim(&s);
988 if (!arg || *arg == '\0')
989 fatal("%s line %d: Missing yes/point-to-point/"
990 "ethernet/no argument.", filename, linenum);
991 value = 0; /* silence compiler */
992 if (strcasecmp(arg, "ethernet") == 0)
993 value = SSH_TUNMODE_ETHERNET;
994 else if (strcasecmp(arg, "point-to-point") == 0)
995 value = SSH_TUNMODE_POINTOPOINT;
996 else if (strcasecmp(arg, "yes") == 0)
997 value = SSH_TUNMODE_DEFAULT;
998 else if (strcasecmp(arg, "no") == 0)
999 value = SSH_TUNMODE_NO;
1000 else
1001 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1002 "no argument: %s", filename, linenum, arg);
1003 if (*activep)
1004 *intptr = value;
1005 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001006
1007 case oTunnelDevice:
1008 arg = strdelim(&s);
1009 if (!arg || *arg == '\0')
1010 fatal("%.200s line %d: Missing argument.", filename, linenum);
1011 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001012 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001013 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1014 if (*activep) {
1015 options->tun_local = value;
1016 options->tun_remote = value2;
1017 }
1018 break;
1019
1020 case oLocalCommand:
1021 charptr = &options->local_command;
1022 goto parse_command;
1023
1024 case oPermitLocalCommand:
1025 intptr = &options->permit_local_command;
1026 goto parse_flag;
1027
Damien Miller10288242008-06-30 00:04:03 +10001028 case oVisualHostKey:
1029 intptr = &options->visual_host_key;
1030 goto parse_flag;
1031
Damien Miller0dac6fb2010-11-20 15:19:38 +11001032 case oIPQoS:
1033 arg = strdelim(&s);
1034 if ((value = parse_ipqos(arg)) == -1)
1035 fatal("%s line %d: Bad IPQoS value: %s",
1036 filename, linenum, arg);
1037 arg = strdelim(&s);
1038 if (arg == NULL)
1039 value2 = value;
1040 else if ((value2 = parse_ipqos(arg)) == -1)
1041 fatal("%s line %d: Bad IPQoS value: %s",
1042 filename, linenum, arg);
1043 if (*activep) {
1044 options->ip_qos_interactive = value;
1045 options->ip_qos_bulk = value2;
1046 }
1047 break;
1048
Darren Tucker71e4d542009-07-06 07:12:27 +10001049 case oUseRoaming:
1050 intptr = &options->use_roaming;
1051 goto parse_flag;
1052
Damien Miller21771e22011-05-15 08:45:50 +10001053 case oRequestTTY:
1054 arg = strdelim(&s);
1055 if (!arg || *arg == '\0')
1056 fatal("%s line %d: missing argument.",
1057 filename, linenum);
1058 intptr = &options->request_tty;
1059 if (strcasecmp(arg, "yes") == 0)
1060 value = REQUEST_TTY_YES;
1061 else if (strcasecmp(arg, "no") == 0)
1062 value = REQUEST_TTY_NO;
1063 else if (strcasecmp(arg, "force") == 0)
1064 value = REQUEST_TTY_FORCE;
1065 else if (strcasecmp(arg, "auto") == 0)
1066 value = REQUEST_TTY_AUTO;
1067 else
1068 fatal("Unsupported RequestTTY \"%s\"", arg);
1069 if (*activep && *intptr == -1)
1070 *intptr = value;
1071 break;
1072
Darren Tucker07636982013-05-16 20:30:03 +10001073 case oIgnoreUnknown:
1074 charptr = &options->ignored_unknown;
1075 goto parse_string;
1076
Ben Lindstrom4daea862002-06-09 20:04:02 +00001077 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001078 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001079 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001080 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001081
Damien Millerf9b3feb2003-05-16 11:38:32 +10001082 case oUnsupported:
1083 error("%s line %d: Unsupported option \"%s\"",
1084 filename, linenum, keyword);
1085 return 0;
1086
Damien Miller95def091999-11-25 00:26:21 +11001087 default:
1088 fatal("process_config_line: Unimplemented opcode %d", opcode);
1089 }
1090
1091 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001092 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001093 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001094 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001095 }
Damien Miller95def091999-11-25 00:26:21 +11001096 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097}
1098
1099
Damien Miller5428f641999-11-25 11:54:57 +11001100/*
1101 * Reads the config file and modifies the options accordingly. Options
1102 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001103 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001106int
Darren Tuckerfc959702004-07-17 16:12:08 +10001107read_config_file(const char *filename, const char *host, Options *options,
Darren Tuckeraefa3682013-04-05 11:18:35 +11001108 int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109{
Damien Miller95def091999-11-25 00:26:21 +11001110 FILE *f;
1111 char line[1024];
1112 int active, linenum;
1113 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller57a44762004-04-20 20:11:57 +10001115 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001116 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117
Darren Tuckeraefa3682013-04-05 11:18:35 +11001118 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001119 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001120
Damien Miller33793852004-06-15 10:27:55 +10001121 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001122 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001123 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001124 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001125 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001126 }
1127
Damien Miller95def091999-11-25 00:26:21 +11001128 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
Damien Miller5428f641999-11-25 11:54:57 +11001130 /*
1131 * Mark that we are now processing the options. This flag is turned
1132 * on/off by Host specifications.
1133 */
Damien Miller95def091999-11-25 00:26:21 +11001134 active = 1;
1135 linenum = 0;
1136 while (fgets(line, sizeof(line), f)) {
1137 /* Update line number counter. */
1138 linenum++;
Darren Tuckeraefa3682013-04-05 11:18:35 +11001139 if (process_config_line(options, host, line, filename, linenum,
1140 &active, flags & SSHCONF_USERCONF) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001141 bad_options++;
1142 }
1143 fclose(f);
1144 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001145 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001146 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001147 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148}
1149
Damien Miller5428f641999-11-25 11:54:57 +11001150/*
1151 * Initializes options to special values that indicate that they have not yet
1152 * been set. Read_config_file will only set options with this value. Options
1153 * are processed in the following order: command line, user config file,
1154 * system config file. Last, fill_default_options is called.
1155 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156
Damien Miller4af51302000-04-16 11:18:38 +10001157void
Damien Miller95def091999-11-25 00:26:21 +11001158initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159{
Damien Miller95def091999-11-25 00:26:21 +11001160 memset(options, 'X', sizeof(*options));
1161 options->forward_agent = -1;
1162 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001163 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001164 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001165 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001166 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001167 options->gateway_ports = -1;
1168 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001169 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001170 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001171 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001172 options->gss_authentication = -1;
1173 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001174 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001175 options->kbd_interactive_authentication = -1;
1176 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001177 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001178 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001179 options->batch_mode = -1;
1180 options->check_host_ip = -1;
1181 options->strict_host_key_checking = -1;
1182 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001183 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001184 options->compression_level = -1;
1185 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001186 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001187 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001188 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001189 options->number_of_password_prompts = -1;
1190 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001191 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001192 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001193 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001194 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001195 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001196 options->num_identity_files = 0;
1197 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001198 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001199 options->proxy_command = NULL;
1200 options->user = NULL;
1201 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001202 options->num_system_hostfiles = 0;
1203 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001204 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001205 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001206 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001207 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001208 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001209 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001210 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001211 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001212 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001213 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001214 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001215 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001216 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001217 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001218 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001219 options->server_alive_interval = -1;
1220 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001221 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001222 options->control_path = NULL;
1223 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001224 options->control_persist = -1;
1225 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001226 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001227 options->tun_open = -1;
1228 options->tun_local = -1;
1229 options->tun_remote = -1;
1230 options->local_command = NULL;
1231 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001232 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001233 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001234 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001235 options->ip_qos_interactive = -1;
1236 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001237 options->request_tty = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001238 options->ignored_unknown = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239}
1240
Damien Miller5428f641999-11-25 11:54:57 +11001241/*
1242 * Called after processing other sources of option data, this fills those
1243 * options for which no value has been specified with their default values.
1244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245
Damien Miller4af51302000-04-16 11:18:38 +10001246void
Damien Miller95def091999-11-25 00:26:21 +11001247fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248{
Damien Miller95def091999-11-25 00:26:21 +11001249 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001250 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001251 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001252 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001253 if (options->forward_x11_trusted == -1)
1254 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001255 if (options->forward_x11_timeout == -1)
1256 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001257 if (options->exit_on_forward_failure == -1)
1258 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001259 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001260 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001261 if (options->gateway_ports == -1)
1262 options->gateway_ports = 0;
1263 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001264 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001265 if (options->rsa_authentication == -1)
1266 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001267 if (options->pubkey_authentication == -1)
1268 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001269 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001270 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001271 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001272 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001273 if (options->gss_deleg_creds == -1)
1274 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001275 if (options->password_authentication == -1)
1276 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001277 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001278 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001279 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001280 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001281 if (options->hostbased_authentication == -1)
1282 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001283 if (options->batch_mode == -1)
1284 options->batch_mode = 0;
1285 if (options->check_host_ip == -1)
1286 options->check_host_ip = 1;
1287 if (options->strict_host_key_checking == -1)
1288 options->strict_host_key_checking = 2; /* 2 is default */
1289 if (options->compression == -1)
1290 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001291 if (options->tcp_keep_alive == -1)
1292 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001293 if (options->compression_level == -1)
1294 options->compression_level = 6;
1295 if (options->port == -1)
1296 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001297 if (options->address_family == -1)
1298 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001299 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001300 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001301 if (options->number_of_password_prompts == -1)
1302 options->number_of_password_prompts = 3;
1303 /* Selected in ssh_login(). */
1304 if (options->cipher == -1)
1305 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001306 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001307 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001308 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001309 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001310 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001311 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001312 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001313 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001314 add_identity_file(options, "~/",
1315 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001316 }
1317 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001318 add_identity_file(options, "~/",
1319 _PATH_SSH_CLIENT_ID_RSA, 0);
1320 add_identity_file(options, "~/",
1321 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001322#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001323 add_identity_file(options, "~/",
1324 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001325#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001326 }
Damien Millereba71ba2000-04-29 23:57:08 +10001327 }
Damien Miller95def091999-11-25 00:26:21 +11001328 if (options->escape_char == -1)
1329 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001330 if (options->num_system_hostfiles == 0) {
1331 options->system_hostfiles[options->num_system_hostfiles++] =
1332 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1333 options->system_hostfiles[options->num_system_hostfiles++] =
1334 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1335 }
1336 if (options->num_user_hostfiles == 0) {
1337 options->user_hostfiles[options->num_user_hostfiles++] =
1338 xstrdup(_PATH_SSH_USER_HOSTFILE);
1339 options->user_hostfiles[options->num_user_hostfiles++] =
1340 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1341 }
Damien Millerfcd93202002-02-05 12:26:34 +11001342 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001343 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001344 if (options->clear_forwardings == 1)
1345 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001346 if (options->no_host_authentication_for_localhost == - 1)
1347 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001348 if (options->identities_only == -1)
1349 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001350 if (options->enable_ssh_keysign == -1)
1351 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001352 if (options->rekey_limit == -1)
1353 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001354 if (options->rekey_interval == -1)
1355 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001356 if (options->verify_host_key_dns == -1)
1357 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001358 if (options->server_alive_interval == -1)
1359 options->server_alive_interval = 0;
1360 if (options->server_alive_count_max == -1)
1361 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001362 if (options->control_master == -1)
1363 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001364 if (options->control_persist == -1) {
1365 options->control_persist = 0;
1366 options->control_persist_timeout = 0;
1367 }
Damien Millere1776152005-03-01 21:47:37 +11001368 if (options->hash_known_hosts == -1)
1369 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001370 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001371 options->tun_open = SSH_TUNMODE_NO;
1372 if (options->tun_local == -1)
1373 options->tun_local = SSH_TUNID_ANY;
1374 if (options->tun_remote == -1)
1375 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001376 if (options->permit_local_command == -1)
1377 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001378 if (options->use_roaming == -1)
1379 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001380 if (options->visual_host_key == -1)
1381 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001382 if (options->zero_knowledge_password_authentication == -1)
1383 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001384 if (options->ip_qos_interactive == -1)
1385 options->ip_qos_interactive = IPTOS_LOWDELAY;
1386 if (options->ip_qos_bulk == -1)
1387 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001388 if (options->request_tty == -1)
1389 options->request_tty = REQUEST_TTY_AUTO;
Damien Millerd27b9472005-12-13 19:29:02 +11001390 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001391 /* options->proxy_command should not be set by default */
1392 /* options->user will be set in the main program if appropriate */
1393 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001394 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001395 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001397
1398/*
1399 * parse_forward
1400 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001401 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001402 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001403 * dynamicfwd == 1
1404 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001405 * returns number of arguments parsed or zero on error
1406 */
1407int
Damien Miller4bf648f2009-02-14 16:28:21 +11001408parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001409{
1410 int i;
1411 char *p, *cp, *fwdarg[4];
1412
1413 memset(fwd, '\0', sizeof(*fwd));
1414
1415 cp = p = xstrdup(fwdspec);
1416
1417 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001418 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001419 cp++;
1420
1421 for (i = 0; i < 4; ++i)
1422 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1423 break;
1424
Damien Millerf4b39532008-11-03 19:28:21 +11001425 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001426 if (cp != NULL)
1427 i = 0; /* failure */
1428
1429 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001430 case 1:
1431 fwd->listen_host = NULL;
1432 fwd->listen_port = a2port(fwdarg[0]);
1433 fwd->connect_host = xstrdup("socks");
1434 break;
1435
1436 case 2:
1437 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1438 fwd->listen_port = a2port(fwdarg[1]);
1439 fwd->connect_host = xstrdup("socks");
1440 break;
1441
Damien Millerf91ee4c2005-03-01 21:24:33 +11001442 case 3:
1443 fwd->listen_host = NULL;
1444 fwd->listen_port = a2port(fwdarg[0]);
1445 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1446 fwd->connect_port = a2port(fwdarg[2]);
1447 break;
1448
1449 case 4:
1450 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1451 fwd->listen_port = a2port(fwdarg[1]);
1452 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1453 fwd->connect_port = a2port(fwdarg[3]);
1454 break;
1455 default:
1456 i = 0; /* failure */
1457 }
1458
1459 xfree(p);
1460
Damien Millera699d952008-11-03 19:27:34 +11001461 if (dynamicfwd) {
1462 if (!(i == 1 || i == 2))
1463 goto fail_free;
1464 } else {
1465 if (!(i == 3 || i == 4))
1466 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001467 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001468 goto fail_free;
1469 }
1470
Damien Miller4bf648f2009-02-14 16:28:21 +11001471 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001472 goto fail_free;
1473
1474 if (fwd->connect_host != NULL &&
1475 strlen(fwd->connect_host) >= NI_MAXHOST)
1476 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001477 if (fwd->listen_host != NULL &&
1478 strlen(fwd->listen_host) >= NI_MAXHOST)
1479 goto fail_free;
1480
Damien Millerf91ee4c2005-03-01 21:24:33 +11001481
1482 return (i);
1483
1484 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001485 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001486 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001487 fwd->connect_host = NULL;
1488 }
1489 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001490 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001491 fwd->listen_host = NULL;
1492 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001493 return (0);
1494}