blob: 097bb0515c546391ae5ec6a34270d6c98e00e467 [file] [log] [blame]
Darren Tucker68afb8c2011-10-02 18:59:03 +11001/* $OpenBSD: readconf.c,v 1.194 2011/09/23 07:45:05 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
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>
Damien Millerc7b06362006-03-15 11:53:45 +110033
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100036#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "cipher.h"
38#include "pathnames.h"
39#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "readconf.h"
42#include "match.h"
43#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100044#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000045#include "kex.h"
46#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48/* Format of the configuration file:
49
50 # Configuration data is parsed as follows:
51 # 1. command line options
52 # 2. user-specific file
53 # 3. system-wide file
54 # Any configuration value is only changed the first time it is set.
55 # Thus, host-specific definitions should be at the beginning of the
56 # configuration file, and defaults at the end.
57
58 # Host-specific declarations. These may override anything above. A single
59 # host may match multiple declarations; these are processed in the order
60 # that they are given in.
61
62 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000063 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65 Host fake.com
66 HostName another.host.name.real.org
67 User blaah
68 Port 34289
69 ForwardX11 no
70 ForwardAgent no
71
72 Host books.com
73 RemoteForward 9999 shadows.cs.hut.fi:9999
74 Cipher 3des
75
76 Host fascist.blob.com
77 Port 23123
78 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079 PasswordAuthentication no
80
81 Host puukko.hut.fi
82 User t35124p
83 ProxyCommand ssh-proxy %h %p
84
85 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host *.su
89 Cipher none
90 PasswordAuthentication no
91
Damien Millerd27b9472005-12-13 19:29:02 +110092 Host vpn.fake.com
93 Tunnel yes
94 TunnelDevice 3
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 # Defaults for various options
97 Host *
98 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110099 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 PasswordAuthentication yes
101 RSAAuthentication yes
102 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100104 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105 IdentityFile ~/.ssh/identity
106 Port 22
107 EscapeChar ~
108
109*/
110
111/* Keyword tokens. */
112
Damien Miller95def091999-11-25 00:26:21 +1100113typedef enum {
114 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000115 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
116 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000117 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000118 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100119 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
120 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
121 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
122 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100123 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000124 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000126 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000127 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100128 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000129 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000130 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000131 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100132 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000133 oSendEnv, oControlPath, oControlMaster, oControlPersist,
134 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100135 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100136 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Miller21771e22011-05-15 08:45:50 +1000137 oKexAlgorithms, oIPQoS, oRequestTTY,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100138 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139} OpCodes;
140
141/* Textual representations of the tokens. */
142
Damien Miller95def091999-11-25 00:26:21 +1100143static struct {
144 const char *name;
145 OpCodes opcode;
146} keywords[] = {
147 { "forwardagent", oForwardAgent },
148 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000149 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000150 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000151 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000152 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100153 { "gatewayports", oGatewayPorts },
154 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000155 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100157 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
158 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000161 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000162 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000163 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000164 { "challengeresponseauthentication", oChallengeResponseAuthentication },
165 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
166 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000167 { "kerberosauthentication", oUnsupported },
168 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000169 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000170#if defined(GSSAPI)
171 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000172 { "gssapidelegatecredentials", oGssDelegateCreds },
173#else
174 { "gssapiauthentication", oUnsupported },
175 { "gssapidelegatecredentials", oUnsupported },
176#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000177 { "fallbacktorsh", oDeprecated },
178 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100180 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100181 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000183 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100184 { "proxycommand", oProxyCommand },
185 { "port", oPort },
186 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000187 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000188 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000189 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "remoteforward", oRemoteForward },
191 { "localforward", oLocalForward },
192 { "user", oUser },
193 { "host", oHost },
194 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000196 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100197 { "userknownhostsfile", oUserKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000198 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100199 { "connectionattempts", oConnectionAttempts },
200 { "batchmode", oBatchMode },
201 { "checkhostip", oCheckHostIP },
202 { "stricthostkeychecking", oStrictHostKeyChecking },
203 { "compression", oCompression },
204 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100205 { "tcpkeepalive", oTCPKeepAlive },
206 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100207 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000209 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000210 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000211 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000212 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100213#ifdef ENABLE_PKCS11
214 { "smartcarddevice", oPKCS11Provider },
215 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000216#else
217 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100218 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000219#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100220 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000221 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000222 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000224 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000225 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000226 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100227 { "serveraliveinterval", oServerAliveInterval },
228 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000229 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000230 { "controlpath", oControlPath },
231 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000232 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100233 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100234 { "tunnel", oTunnel },
235 { "tunneldevice", oTunnelDevice },
236 { "localcommand", oLocalCommand },
237 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000238 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000239 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100240#ifdef JPAKE
241 { "zeroknowledgepasswordauthentication",
242 oZeroKnowledgePasswordAuthentication },
243#else
244 { "zeroknowledgepasswordauthentication", oUnsupported },
245#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000246 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100247 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000248 { "requesttty", oRequestTTY },
Damien Miller01ed2272008-11-05 16:20:46 +1100249
Ben Lindstrom65366a82001-12-06 16:32:47 +0000250 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100251};
252
Damien Miller5428f641999-11-25 11:54:57 +1100253/*
254 * Adds a local TCP/IP port forward to options. Never returns if there is an
255 * error.
256 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller4af51302000-04-16 11:18:38 +1000258void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100259add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Miller95def091999-11-25 00:26:21 +1100261 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000262#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100263 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000265 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100266#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000267 options->local_forwards = xrealloc(options->local_forwards,
268 options->num_local_forwards + 1,
269 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100270 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271
Damien Miller1a0442f2008-11-05 16:30:06 +1100272 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100273 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100274 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
Damien Miller5428f641999-11-25 11:54:57 +1100278/*
279 * Adds a remote TCP/IP port forward to options. Never returns if there is
280 * an error.
281 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Damien Miller4af51302000-04-16 11:18:38 +1000283void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Damien Miller95def091999-11-25 00:26:21 +1100286 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000287
288 options->remote_forwards = xrealloc(options->remote_forwards,
289 options->num_remote_forwards + 1,
290 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100291 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100292
Damien Miller1a0442f2008-11-05 16:30:06 +1100293 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100295 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 fwd->connect_port = newfwd->connect_port;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100297 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000298 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299}
300
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000301static void
302clear_forwardings(Options *options)
303{
304 int i;
305
Damien Millerf91ee4c2005-03-01 21:24:33 +1100306 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100307 if (options->local_forwards[i].listen_host != NULL)
308 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100309 xfree(options->local_forwards[i].connect_host);
310 }
Damien Miller232cfb12010-06-26 09:50:30 +1000311 if (options->num_local_forwards > 0) {
312 xfree(options->local_forwards);
313 options->local_forwards = NULL;
314 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000315 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100316 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100317 if (options->remote_forwards[i].listen_host != NULL)
318 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100319 xfree(options->remote_forwards[i].connect_host);
320 }
Damien Miller232cfb12010-06-26 09:50:30 +1000321 if (options->num_remote_forwards > 0) {
322 xfree(options->remote_forwards);
323 options->remote_forwards = NULL;
324 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000325 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100326 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000327}
328
Damien Miller5428f641999-11-25 11:54:57 +1100329/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000330 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100331 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller4af51302000-04-16 11:18:38 +1000333static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100334parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000336 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Damien Miller95def091999-11-25 00:26:21 +1100338 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100339 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100340 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000342 error("%s: line %d: Bad configuration option: %s",
343 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100344 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345}
346
Damien Miller5428f641999-11-25 11:54:57 +1100347/*
348 * Processes a single option line as used in the configuration files. This
349 * only sets those values that have not already been set.
350 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100351#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller2ccf6611999-11-15 15:25:10 +1100353int
354process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100355 char *line, const char *filename, int linenum,
356 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357{
Damien Miller295ee632011-05-29 21:42:31 +1000358 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
359 char **cpptr, fwdarg[256];
360 u_int *uintptr, max_entries = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000361 int negated, opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100362 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100363 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100364 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Millerc652cac2003-05-14 13:40:54 +1000367 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100368 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000369 if (strchr(WHITESPACE, line[len]) == NULL)
370 break;
371 line[len] = '\0';
372 }
373
Damien Millerbe484b52000-07-15 14:14:16 +1000374 s = line;
375 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100376 if ((keyword = strdelim(&s)) == NULL)
377 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000378 /* Ignore leading whitespace. */
379 if (*keyword == '\0')
380 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000381 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100382 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
Damien Miller37023962000-07-11 17:31:38 +1000384 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 switch (opcode) {
387 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100388 /* don't panic, but count bad options */
389 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100390 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000391 case oConnectTimeout:
392 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100393parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000394 arg = strdelim(&s);
395 if (!arg || *arg == '\0')
396 fatal("%s line %d: missing time value.",
397 filename, linenum);
398 if ((value = convtime(arg)) == -1)
399 fatal("%s line %d: invalid time value.",
400 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100401 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000402 *intptr = value;
403 break;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oForwardAgent:
406 intptr = &options->forward_agent;
407parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000408 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000409 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100410 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
411 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000412 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100413 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000414 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100415 value = 0;
416 else
417 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
418 if (*activep && *intptr == -1)
419 *intptr = value;
420 break;
421
422 case oForwardX11:
423 intptr = &options->forward_x11;
424 goto parse_flag;
425
Darren Tucker0a118da2003-10-15 15:54:32 +1000426 case oForwardX11Trusted:
427 intptr = &options->forward_x11_trusted;
428 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000429
430 case oForwardX11Timeout:
431 intptr = &options->forward_x11_timeout;
432 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 case oGatewayPorts:
435 intptr = &options->gateway_ports;
436 goto parse_flag;
437
Darren Tuckere7d4b192006-07-12 22:17:10 +1000438 case oExitOnForwardFailure:
439 intptr = &options->exit_on_forward_failure;
440 goto parse_flag;
441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oUsePrivilegedPort:
443 intptr = &options->use_privileged_port;
444 goto parse_flag;
445
Damien Miller95def091999-11-25 00:26:21 +1100446 case oPasswordAuthentication:
447 intptr = &options->password_authentication;
448 goto parse_flag;
449
Damien Miller01ed2272008-11-05 16:20:46 +1100450 case oZeroKnowledgePasswordAuthentication:
451 intptr = &options->zero_knowledge_password_authentication;
452 goto parse_flag;
453
Damien Miller874d77b2000-10-14 16:23:11 +1100454 case oKbdInteractiveAuthentication:
455 intptr = &options->kbd_interactive_authentication;
456 goto parse_flag;
457
458 case oKbdInteractiveDevices:
459 charptr = &options->kbd_interactive_devices;
460 goto parse_string;
461
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 case oPubkeyAuthentication:
463 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000464 goto parse_flag;
465
Damien Miller95def091999-11-25 00:26:21 +1100466 case oRSAAuthentication:
467 intptr = &options->rsa_authentication;
468 goto parse_flag;
469
470 case oRhostsRSAAuthentication:
471 intptr = &options->rhosts_rsa_authentication;
472 goto parse_flag;
473
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000474 case oHostbasedAuthentication:
475 intptr = &options->hostbased_authentication;
476 goto parse_flag;
477
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000478 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000479 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100480 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000481
Darren Tucker0efd1552003-08-26 11:49:55 +1000482 case oGssAuthentication:
483 intptr = &options->gss_authentication;
484 goto parse_flag;
485
486 case oGssDelegateCreds:
487 intptr = &options->gss_deleg_creds;
488 goto parse_flag;
489
Damien Miller95def091999-11-25 00:26:21 +1100490 case oBatchMode:
491 intptr = &options->batch_mode;
492 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller95def091999-11-25 00:26:21 +1100494 case oCheckHostIP:
495 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000496 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller37876e92003-05-15 10:19:46 +1000498 case oVerifyHostKeyDNS:
499 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100500 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000501
Damien Miller95def091999-11-25 00:26:21 +1100502 case oStrictHostKeyChecking:
503 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100504parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000505 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000506 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000507 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100508 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100509 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000510 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100511 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000512 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100513 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000514 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100515 value = 2;
516 else
517 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
518 if (*activep && *intptr == -1)
519 *intptr = value;
520 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller95def091999-11-25 00:26:21 +1100522 case oCompression:
523 intptr = &options->compression;
524 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Miller12c150e2003-12-17 16:31:10 +1100526 case oTCPKeepAlive:
527 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100528 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000530 case oNoHostAuthenticationForLocalhost:
531 intptr = &options->no_host_authentication_for_localhost;
532 goto parse_flag;
533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oNumberOfPasswordPrompts:
535 intptr = &options->number_of_password_prompts;
536 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
Damien Miller95def091999-11-25 00:26:21 +1100538 case oCompressionLevel:
539 intptr = &options->compression_level;
540 goto parse_int;
541
Damien Millera5539d22003-04-09 20:50:06 +1000542 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000543 arg = strdelim(&s);
544 if (!arg || *arg == '\0')
545 fatal("%.200s line %d: Missing argument.", filename, linenum);
546 if (arg[0] < '0' || arg[0] > '9')
547 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100548 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000549 if (arg == endofnumber)
550 fatal("%.200s line %d: Bad number.", filename, linenum);
551 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100552 case '\0':
553 scale = 1;
554 break;
Damien Millera5539d22003-04-09 20:50:06 +1000555 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100556 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000557 break;
558 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100559 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000560 break;
561 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100562 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000563 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100564 default:
565 fatal("%.200s line %d: Invalid RekeyLimit suffix",
566 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000567 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100568 val64 *= scale;
569 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100570 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100571 fatal("%.200s line %d: RekeyLimit too large",
572 filename, linenum);
573 if (val64 < 16)
574 fatal("%.200s line %d: RekeyLimit too small",
575 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100576 if (*activep && options->rekey_limit == -1)
577 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000578 break;
579
Damien Miller95def091999-11-25 00:26:21 +1100580 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000581 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000582 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100583 fatal("%.200s line %d: Missing argument.", filename, linenum);
584 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100585 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000586 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100587 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100589 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000590 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000591 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100592 }
593 break;
594
Damien Millerd3a18572000-06-07 19:55:44 +1000595 case oXAuthLocation:
596 charptr=&options->xauth_location;
597 goto parse_string;
598
Damien Miller95def091999-11-25 00:26:21 +1100599 case oUser:
600 charptr = &options->user;
601parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000602 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000603 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000604 fatal("%.200s line %d: Missing argument.",
605 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100606 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000607 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100608 break;
609
610 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000611 cpptr = (char **)&options->system_hostfiles;
612 uintptr = &options->num_system_hostfiles;
613 max_entries = SSH_MAX_HOSTS_FILES;
614parse_char_array:
615 if (*activep && *uintptr == 0) {
616 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
617 if ((*uintptr) >= max_entries)
618 fatal("%s line %d: "
619 "too many authorized keys files.",
620 filename, linenum);
621 cpptr[(*uintptr)++] = xstrdup(arg);
622 }
623 }
624 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100625
626 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000627 cpptr = (char **)&options->user_hostfiles;
628 uintptr = &options->num_user_hostfiles;
629 max_entries = SSH_MAX_HOSTS_FILES;
630 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +1000631
Damien Miller95def091999-11-25 00:26:21 +1100632 case oHostName:
633 charptr = &options->hostname;
634 goto parse_string;
635
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000636 case oHostKeyAlias:
637 charptr = &options->host_key_alias;
638 goto parse_string;
639
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000640 case oPreferredAuthentications:
641 charptr = &options->preferred_authentications;
642 goto parse_string;
643
Ben Lindstrome0f88042001-04-30 13:06:24 +0000644 case oBindAddress:
645 charptr = &options->bind_address;
646 goto parse_string;
647
Damien Miller7ea845e2010-02-12 09:21:02 +1100648 case oPKCS11Provider:
649 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000650 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000651
Damien Miller95def091999-11-25 00:26:21 +1100652 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100653 charptr = &options->proxy_command;
654parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000655 if (s == NULL)
656 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100657 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100658 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100659 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100660 return 0;
661
662 case oPort:
663 intptr = &options->port;
664parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000665 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000666 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100667 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000668 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100669 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100670
671 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000672 value = strtol(arg, &endofnumber, 0);
673 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100674 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100675 if (*activep && *intptr == -1)
676 *intptr = value;
677 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
Damien Miller95def091999-11-25 00:26:21 +1100679 case oConnectionAttempts:
680 intptr = &options->connection_attempts;
681 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100682
Damien Miller95def091999-11-25 00:26:21 +1100683 case oCipher:
684 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000685 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000686 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000687 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000688 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100689 if (value == -1)
690 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100691 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100692 if (*activep && *intptr == -1)
693 *intptr = value;
694 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695
Damien Miller78928792000-04-12 20:17:38 +1000696 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000697 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000698 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000699 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000700 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000701 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000703 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000704 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000705 break;
706
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000707 case oMacs:
708 arg = strdelim(&s);
709 if (!arg || *arg == '\0')
710 fatal("%.200s line %d: Missing argument.", filename, linenum);
711 if (!mac_valid(arg))
712 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100713 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000714 if (*activep && options->macs == NULL)
715 options->macs = xstrdup(arg);
716 break;
717
Damien Millerd5f62bf2010-09-24 22:11:14 +1000718 case oKexAlgorithms:
719 arg = strdelim(&s);
720 if (!arg || *arg == '\0')
721 fatal("%.200s line %d: Missing argument.",
722 filename, linenum);
723 if (!kex_names_valid(arg))
724 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
725 filename, linenum, arg ? arg : "<NONE>");
726 if (*activep && options->kex_algorithms == NULL)
727 options->kex_algorithms = xstrdup(arg);
728 break;
729
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000730 case oHostKeyAlgorithms:
731 arg = strdelim(&s);
732 if (!arg || *arg == '\0')
733 fatal("%.200s line %d: Missing argument.", filename, linenum);
734 if (!key_names_valid2(arg))
735 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100736 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000737 if (*activep && options->hostkeyalgorithms == NULL)
738 options->hostkeyalgorithms = xstrdup(arg);
739 break;
740
Damien Miller78928792000-04-12 20:17:38 +1000741 case oProtocol:
742 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000743 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000744 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000745 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000746 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000747 if (value == SSH_PROTO_UNKNOWN)
748 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100749 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000750 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
751 *intptr = value;
752 break;
753
Damien Miller95def091999-11-25 00:26:21 +1100754 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100755 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000756 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000757 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100758 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000759 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100760 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100761 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
762 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100763 break;
764
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000765 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100766 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100767 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000768 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100769 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000770 fatal("%.200s line %d: Missing port argument.",
771 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100772
Damien Millera699d952008-11-03 19:27:34 +1100773 if (opcode == oLocalForward ||
774 opcode == oRemoteForward) {
775 arg2 = strdelim(&s);
776 if (arg2 == NULL || *arg2 == '\0')
777 fatal("%.200s line %d: Missing target argument.",
778 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100779
Damien Millera699d952008-11-03 19:27:34 +1100780 /* construct a string for parse_forward */
781 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
782 } else if (opcode == oDynamicForward) {
783 strlcpy(fwdarg, arg, sizeof(fwdarg));
784 }
785
786 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100787 opcode == oDynamicForward ? 1 : 0,
788 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000789 fatal("%.200s line %d: Bad forwarding specification.",
790 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100791
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000792 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100793 if (opcode == oLocalForward ||
794 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100795 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000796 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100797 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000798 }
Damien Miller95def091999-11-25 00:26:21 +1100799 break;
800
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000801 case oClearAllForwardings:
802 intptr = &options->clear_forwardings;
803 goto parse_flag;
804
Damien Miller95def091999-11-25 00:26:21 +1100805 case oHost:
806 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000807 arg2 = NULL;
808 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
809 negated = *arg == '!';
810 if (negated)
811 arg++;
Damien Miller37023962000-07-11 17:31:38 +1000812 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +1000813 if (negated) {
814 debug("%.200s line %d: Skipping Host "
815 "block because of negated match "
816 "for %.100s", filename, linenum,
817 arg);
818 *activep = 0;
819 break;
820 }
821 if (!*activep)
822 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +1100823 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +1100824 }
Damien Millerfe924212011-05-15 08:44:45 +1000825 }
826 if (*activep)
827 debug("%.200s line %d: Applying options for %.100s",
828 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +1000829 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100830 return 0;
831
832 case oEscapeChar:
833 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000834 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000835 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100836 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000837 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000838 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
839 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000840 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000841 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000842 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000843 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100844 else {
845 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100846 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100847 /* NOTREACHED */
848 value = 0; /* Avoid compiler warning. */
849 }
850 if (*activep && *intptr == -1)
851 *intptr = value;
852 break;
853
Damien Miller20a8f972003-05-18 20:50:30 +1000854 case oAddressFamily:
855 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000856 if (!arg || *arg == '\0')
857 fatal("%s line %d: missing address family.",
858 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000859 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000860 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000861 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000862 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000863 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000864 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000865 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000866 else
867 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000868 if (*activep && *intptr == -1)
869 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000870 break;
871
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000872 case oEnableSSHKeysign:
873 intptr = &options->enable_ssh_keysign;
874 goto parse_flag;
875
Damien Millerbd394c32004-03-08 23:12:36 +1100876 case oIdentitiesOnly:
877 intptr = &options->identities_only;
878 goto parse_flag;
879
Damien Miller509b0102003-12-17 16:33:10 +1100880 case oServerAliveInterval:
881 intptr = &options->server_alive_interval;
882 goto parse_time;
883
884 case oServerAliveCountMax:
885 intptr = &options->server_alive_count_max;
886 goto parse_int;
887
Darren Tucker46bc0752004-05-02 22:11:30 +1000888 case oSendEnv:
889 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
890 if (strchr(arg, '=') != NULL)
891 fatal("%s line %d: Invalid environment name.",
892 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100893 if (!*activep)
894 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000895 if (options->num_send_env >= MAX_SEND_ENV)
896 fatal("%s line %d: too many send env.",
897 filename, linenum);
898 options->send_env[options->num_send_env++] =
899 xstrdup(arg);
900 }
901 break;
902
Damien Miller0e220db2004-06-15 10:34:08 +1000903 case oControlPath:
904 charptr = &options->control_path;
905 goto parse_string;
906
907 case oControlMaster:
908 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000909 arg = strdelim(&s);
910 if (!arg || *arg == '\0')
911 fatal("%.200s line %d: Missing ControlMaster argument.",
912 filename, linenum);
913 value = 0; /* To avoid compiler warning... */
914 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
915 value = SSHCTL_MASTER_YES;
916 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
917 value = SSHCTL_MASTER_NO;
918 else if (strcmp(arg, "auto") == 0)
919 value = SSHCTL_MASTER_AUTO;
920 else if (strcmp(arg, "ask") == 0)
921 value = SSHCTL_MASTER_ASK;
922 else if (strcmp(arg, "autoask") == 0)
923 value = SSHCTL_MASTER_AUTO_ASK;
924 else
925 fatal("%.200s line %d: Bad ControlMaster argument.",
926 filename, linenum);
927 if (*activep && *intptr == -1)
928 *intptr = value;
929 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000930
Damien Millere11e1ea2010-08-03 16:04:46 +1000931 case oControlPersist:
932 /* no/false/yes/true, or a time spec */
933 intptr = &options->control_persist;
934 arg = strdelim(&s);
935 if (!arg || *arg == '\0')
936 fatal("%.200s line %d: Missing ControlPersist"
937 " argument.", filename, linenum);
938 value = 0;
939 value2 = 0; /* timeout */
940 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
941 value = 0;
942 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
943 value = 1;
944 else if ((value2 = convtime(arg)) >= 0)
945 value = 1;
946 else
947 fatal("%.200s line %d: Bad ControlPersist argument.",
948 filename, linenum);
949 if (*activep && *intptr == -1) {
950 *intptr = value;
951 options->control_persist_timeout = value2;
952 }
953 break;
954
Damien Millere1776152005-03-01 21:47:37 +1100955 case oHashKnownHosts:
956 intptr = &options->hash_known_hosts;
957 goto parse_flag;
958
Damien Millerd27b9472005-12-13 19:29:02 +1100959 case oTunnel:
960 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100961 arg = strdelim(&s);
962 if (!arg || *arg == '\0')
963 fatal("%s line %d: Missing yes/point-to-point/"
964 "ethernet/no argument.", filename, linenum);
965 value = 0; /* silence compiler */
966 if (strcasecmp(arg, "ethernet") == 0)
967 value = SSH_TUNMODE_ETHERNET;
968 else if (strcasecmp(arg, "point-to-point") == 0)
969 value = SSH_TUNMODE_POINTOPOINT;
970 else if (strcasecmp(arg, "yes") == 0)
971 value = SSH_TUNMODE_DEFAULT;
972 else if (strcasecmp(arg, "no") == 0)
973 value = SSH_TUNMODE_NO;
974 else
975 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
976 "no argument: %s", filename, linenum, arg);
977 if (*activep)
978 *intptr = value;
979 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100980
981 case oTunnelDevice:
982 arg = strdelim(&s);
983 if (!arg || *arg == '\0')
984 fatal("%.200s line %d: Missing argument.", filename, linenum);
985 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100986 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100987 fatal("%.200s line %d: Bad tun device.", filename, linenum);
988 if (*activep) {
989 options->tun_local = value;
990 options->tun_remote = value2;
991 }
992 break;
993
994 case oLocalCommand:
995 charptr = &options->local_command;
996 goto parse_command;
997
998 case oPermitLocalCommand:
999 intptr = &options->permit_local_command;
1000 goto parse_flag;
1001
Damien Miller10288242008-06-30 00:04:03 +10001002 case oVisualHostKey:
1003 intptr = &options->visual_host_key;
1004 goto parse_flag;
1005
Damien Miller0dac6fb2010-11-20 15:19:38 +11001006 case oIPQoS:
1007 arg = strdelim(&s);
1008 if ((value = parse_ipqos(arg)) == -1)
1009 fatal("%s line %d: Bad IPQoS value: %s",
1010 filename, linenum, arg);
1011 arg = strdelim(&s);
1012 if (arg == NULL)
1013 value2 = value;
1014 else if ((value2 = parse_ipqos(arg)) == -1)
1015 fatal("%s line %d: Bad IPQoS value: %s",
1016 filename, linenum, arg);
1017 if (*activep) {
1018 options->ip_qos_interactive = value;
1019 options->ip_qos_bulk = value2;
1020 }
1021 break;
1022
Darren Tucker71e4d542009-07-06 07:12:27 +10001023 case oUseRoaming:
1024 intptr = &options->use_roaming;
1025 goto parse_flag;
1026
Damien Miller21771e22011-05-15 08:45:50 +10001027 case oRequestTTY:
1028 arg = strdelim(&s);
1029 if (!arg || *arg == '\0')
1030 fatal("%s line %d: missing argument.",
1031 filename, linenum);
1032 intptr = &options->request_tty;
1033 if (strcasecmp(arg, "yes") == 0)
1034 value = REQUEST_TTY_YES;
1035 else if (strcasecmp(arg, "no") == 0)
1036 value = REQUEST_TTY_NO;
1037 else if (strcasecmp(arg, "force") == 0)
1038 value = REQUEST_TTY_FORCE;
1039 else if (strcasecmp(arg, "auto") == 0)
1040 value = REQUEST_TTY_AUTO;
1041 else
1042 fatal("Unsupported RequestTTY \"%s\"", arg);
1043 if (*activep && *intptr == -1)
1044 *intptr = value;
1045 break;
1046
Ben Lindstrom4daea862002-06-09 20:04:02 +00001047 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001048 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001049 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001050 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001051
Damien Millerf9b3feb2003-05-16 11:38:32 +10001052 case oUnsupported:
1053 error("%s line %d: Unsupported option \"%s\"",
1054 filename, linenum, keyword);
1055 return 0;
1056
Damien Miller95def091999-11-25 00:26:21 +11001057 default:
1058 fatal("process_config_line: Unimplemented opcode %d", opcode);
1059 }
1060
1061 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001062 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001063 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001064 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001065 }
Damien Miller95def091999-11-25 00:26:21 +11001066 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067}
1068
1069
Damien Miller5428f641999-11-25 11:54:57 +11001070/*
1071 * Reads the config file and modifies the options accordingly. Options
1072 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001073 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001074 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001076int
Darren Tuckerfc959702004-07-17 16:12:08 +10001077read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +10001078 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079{
Damien Miller95def091999-11-25 00:26:21 +11001080 FILE *f;
1081 char line[1024];
1082 int active, linenum;
1083 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084
Damien Miller57a44762004-04-20 20:11:57 +10001085 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001086 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087
Damien Miller57a44762004-04-20 20:11:57 +10001088 if (checkperm) {
1089 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001090
Damien Miller33793852004-06-15 10:27:55 +10001091 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001092 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001093 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001094 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001095 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001096 }
1097
Damien Miller95def091999-11-25 00:26:21 +11001098 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099
Damien Miller5428f641999-11-25 11:54:57 +11001100 /*
1101 * Mark that we are now processing the options. This flag is turned
1102 * on/off by Host specifications.
1103 */
Damien Miller95def091999-11-25 00:26:21 +11001104 active = 1;
1105 linenum = 0;
1106 while (fgets(line, sizeof(line), f)) {
1107 /* Update line number counter. */
1108 linenum++;
1109 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1110 bad_options++;
1111 }
1112 fclose(f);
1113 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001114 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001115 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001116 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117}
1118
Damien Miller5428f641999-11-25 11:54:57 +11001119/*
1120 * Initializes options to special values that indicate that they have not yet
1121 * been set. Read_config_file will only set options with this value. Options
1122 * are processed in the following order: command line, user config file,
1123 * system config file. Last, fill_default_options is called.
1124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125
Damien Miller4af51302000-04-16 11:18:38 +10001126void
Damien Miller95def091999-11-25 00:26:21 +11001127initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128{
Damien Miller95def091999-11-25 00:26:21 +11001129 memset(options, 'X', sizeof(*options));
1130 options->forward_agent = -1;
1131 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001132 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001133 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001134 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001135 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001136 options->gateway_ports = -1;
1137 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001138 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001140 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001141 options->gss_authentication = -1;
1142 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001143 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001144 options->kbd_interactive_authentication = -1;
1145 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001146 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001147 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001148 options->batch_mode = -1;
1149 options->check_host_ip = -1;
1150 options->strict_host_key_checking = -1;
1151 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001152 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001153 options->compression_level = -1;
1154 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001155 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001156 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001157 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001158 options->number_of_password_prompts = -1;
1159 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001160 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001161 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001162 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001163 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001164 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001165 options->num_identity_files = 0;
1166 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001167 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001168 options->proxy_command = NULL;
1169 options->user = NULL;
1170 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001171 options->num_system_hostfiles = 0;
1172 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001173 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001174 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001175 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001176 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001177 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001178 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001179 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001180 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001181 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001182 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001183 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001184 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001185 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001186 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001187 options->server_alive_interval = -1;
1188 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001189 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001190 options->control_path = NULL;
1191 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001192 options->control_persist = -1;
1193 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001194 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001195 options->tun_open = -1;
1196 options->tun_local = -1;
1197 options->tun_remote = -1;
1198 options->local_command = NULL;
1199 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001200 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001201 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001202 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001203 options->ip_qos_interactive = -1;
1204 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001205 options->request_tty = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206}
1207
Damien Miller5428f641999-11-25 11:54:57 +11001208/*
1209 * Called after processing other sources of option data, this fills those
1210 * options for which no value has been specified with their default values.
1211 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212
Damien Miller4af51302000-04-16 11:18:38 +10001213void
Damien Miller95def091999-11-25 00:26:21 +11001214fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001216 int len;
1217
Damien Miller95def091999-11-25 00:26:21 +11001218 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001219 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001220 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001221 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001222 if (options->forward_x11_trusted == -1)
1223 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001224 if (options->forward_x11_timeout == -1)
1225 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001226 if (options->exit_on_forward_failure == -1)
1227 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001228 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001229 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001230 if (options->gateway_ports == -1)
1231 options->gateway_ports = 0;
1232 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001233 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001234 if (options->rsa_authentication == -1)
1235 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001236 if (options->pubkey_authentication == -1)
1237 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001238 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001239 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001240 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001241 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001242 if (options->gss_deleg_creds == -1)
1243 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001244 if (options->password_authentication == -1)
1245 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001246 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001247 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001248 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001249 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001250 if (options->hostbased_authentication == -1)
1251 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001252 if (options->batch_mode == -1)
1253 options->batch_mode = 0;
1254 if (options->check_host_ip == -1)
1255 options->check_host_ip = 1;
1256 if (options->strict_host_key_checking == -1)
1257 options->strict_host_key_checking = 2; /* 2 is default */
1258 if (options->compression == -1)
1259 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001260 if (options->tcp_keep_alive == -1)
1261 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001262 if (options->compression_level == -1)
1263 options->compression_level = 6;
1264 if (options->port == -1)
1265 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001266 if (options->address_family == -1)
1267 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001268 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001269 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001270 if (options->number_of_password_prompts == -1)
1271 options->number_of_password_prompts = 3;
1272 /* Selected in ssh_login(). */
1273 if (options->cipher == -1)
1274 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001275 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001276 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001277 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001278 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001279 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001280 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001281 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001282 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001283 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001284 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001285 xmalloc(len);
1286 snprintf(options->identity_files[options->num_identity_files++],
1287 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001288 }
1289 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001290 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1291 options->identity_files[options->num_identity_files] =
1292 xmalloc(len);
1293 snprintf(options->identity_files[options->num_identity_files++],
1294 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1295
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001296 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001297 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001298 xmalloc(len);
1299 snprintf(options->identity_files[options->num_identity_files++],
1300 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001301#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001302 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1303 options->identity_files[options->num_identity_files] =
1304 xmalloc(len);
1305 snprintf(options->identity_files[options->num_identity_files++],
1306 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001307#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001308 }
Damien Millereba71ba2000-04-29 23:57:08 +10001309 }
Damien Miller95def091999-11-25 00:26:21 +11001310 if (options->escape_char == -1)
1311 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001312 if (options->num_system_hostfiles == 0) {
1313 options->system_hostfiles[options->num_system_hostfiles++] =
1314 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1315 options->system_hostfiles[options->num_system_hostfiles++] =
1316 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1317 }
1318 if (options->num_user_hostfiles == 0) {
1319 options->user_hostfiles[options->num_user_hostfiles++] =
1320 xstrdup(_PATH_SSH_USER_HOSTFILE);
1321 options->user_hostfiles[options->num_user_hostfiles++] =
1322 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1323 }
Damien Millerfcd93202002-02-05 12:26:34 +11001324 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001325 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001326 if (options->clear_forwardings == 1)
1327 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001328 if (options->no_host_authentication_for_localhost == - 1)
1329 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001330 if (options->identities_only == -1)
1331 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001332 if (options->enable_ssh_keysign == -1)
1333 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001334 if (options->rekey_limit == -1)
1335 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001336 if (options->verify_host_key_dns == -1)
1337 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001338 if (options->server_alive_interval == -1)
1339 options->server_alive_interval = 0;
1340 if (options->server_alive_count_max == -1)
1341 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001342 if (options->control_master == -1)
1343 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001344 if (options->control_persist == -1) {
1345 options->control_persist = 0;
1346 options->control_persist_timeout = 0;
1347 }
Damien Millere1776152005-03-01 21:47:37 +11001348 if (options->hash_known_hosts == -1)
1349 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001350 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001351 options->tun_open = SSH_TUNMODE_NO;
1352 if (options->tun_local == -1)
1353 options->tun_local = SSH_TUNID_ANY;
1354 if (options->tun_remote == -1)
1355 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001356 if (options->permit_local_command == -1)
1357 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001358 if (options->use_roaming == -1)
1359 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001360 if (options->visual_host_key == -1)
1361 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001362 if (options->zero_knowledge_password_authentication == -1)
1363 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001364 if (options->ip_qos_interactive == -1)
1365 options->ip_qos_interactive = IPTOS_LOWDELAY;
1366 if (options->ip_qos_bulk == -1)
1367 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001368 if (options->request_tty == -1)
1369 options->request_tty = REQUEST_TTY_AUTO;
Damien Millerd27b9472005-12-13 19:29:02 +11001370 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001371 /* options->proxy_command should not be set by default */
1372 /* options->user will be set in the main program if appropriate */
1373 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001374 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001375 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001377
1378/*
1379 * parse_forward
1380 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001381 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001382 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001383 * dynamicfwd == 1
1384 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001385 * returns number of arguments parsed or zero on error
1386 */
1387int
Damien Miller4bf648f2009-02-14 16:28:21 +11001388parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001389{
1390 int i;
1391 char *p, *cp, *fwdarg[4];
1392
1393 memset(fwd, '\0', sizeof(*fwd));
1394
1395 cp = p = xstrdup(fwdspec);
1396
1397 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001398 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001399 cp++;
1400
1401 for (i = 0; i < 4; ++i)
1402 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1403 break;
1404
Damien Millerf4b39532008-11-03 19:28:21 +11001405 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001406 if (cp != NULL)
1407 i = 0; /* failure */
1408
1409 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001410 case 1:
1411 fwd->listen_host = NULL;
1412 fwd->listen_port = a2port(fwdarg[0]);
1413 fwd->connect_host = xstrdup("socks");
1414 break;
1415
1416 case 2:
1417 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1418 fwd->listen_port = a2port(fwdarg[1]);
1419 fwd->connect_host = xstrdup("socks");
1420 break;
1421
Damien Millerf91ee4c2005-03-01 21:24:33 +11001422 case 3:
1423 fwd->listen_host = NULL;
1424 fwd->listen_port = a2port(fwdarg[0]);
1425 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1426 fwd->connect_port = a2port(fwdarg[2]);
1427 break;
1428
1429 case 4:
1430 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1431 fwd->listen_port = a2port(fwdarg[1]);
1432 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1433 fwd->connect_port = a2port(fwdarg[3]);
1434 break;
1435 default:
1436 i = 0; /* failure */
1437 }
1438
1439 xfree(p);
1440
Damien Millera699d952008-11-03 19:27:34 +11001441 if (dynamicfwd) {
1442 if (!(i == 1 || i == 2))
1443 goto fail_free;
1444 } else {
1445 if (!(i == 3 || i == 4))
1446 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001447 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001448 goto fail_free;
1449 }
1450
Damien Miller4bf648f2009-02-14 16:28:21 +11001451 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001452 goto fail_free;
1453
1454 if (fwd->connect_host != NULL &&
1455 strlen(fwd->connect_host) >= NI_MAXHOST)
1456 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001457 if (fwd->listen_host != NULL &&
1458 strlen(fwd->listen_host) >= NI_MAXHOST)
1459 goto fail_free;
1460
Damien Millerf91ee4c2005-03-01 21:24:33 +11001461
1462 return (i);
1463
1464 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001465 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001466 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001467 fwd->connect_host = NULL;
1468 }
1469 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001470 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001471 fwd->listen_host = NULL;
1472 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001473 return (0);
1474}