blob: 4780ae2890f20d6150b983a86e0ced745a53b474 [file] [log] [blame]
Damien Miller21771e22011-05-15 08:45:50 +10001/* $OpenBSD: readconf.c,v 1.192 2011/05/06 21:34:32 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
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 Miller5bc6aae2009-01-28 16:27:31 +1100196 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
197 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000198 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
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;
Damien Miller388f6fc2010-05-21 14:57:35 +1000297 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000300static void
301clear_forwardings(Options *options)
302{
303 int i;
304
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100306 if (options->local_forwards[i].listen_host != NULL)
307 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308 xfree(options->local_forwards[i].connect_host);
309 }
Damien Miller232cfb12010-06-26 09:50:30 +1000310 if (options->num_local_forwards > 0) {
311 xfree(options->local_forwards);
312 options->local_forwards = NULL;
313 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000314 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100315 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100316 if (options->remote_forwards[i].listen_host != NULL)
317 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100318 xfree(options->remote_forwards[i].connect_host);
319 }
Damien Miller232cfb12010-06-26 09:50:30 +1000320 if (options->num_remote_forwards > 0) {
321 xfree(options->remote_forwards);
322 options->remote_forwards = NULL;
323 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000324 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100325 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000326}
327
Damien Miller5428f641999-11-25 11:54:57 +1100328/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000329 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100330 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller4af51302000-04-16 11:18:38 +1000332static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100333parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000335 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller95def091999-11-25 00:26:21 +1100337 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100338 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100339 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000341 error("%s: line %d: Bad configuration option: %s",
342 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100343 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344}
345
Damien Miller5428f641999-11-25 11:54:57 +1100346/*
347 * Processes a single option line as used in the configuration files. This
348 * only sets those values that have not already been set.
349 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100350#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller2ccf6611999-11-15 15:25:10 +1100352int
353process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100354 char *line, const char *filename, int linenum,
355 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100357 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerfe924212011-05-15 08:44:45 +1000358 int negated, opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100359 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100360 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100361 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100362 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Millerc652cac2003-05-14 13:40:54 +1000364 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100365 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000366 if (strchr(WHITESPACE, line[len]) == NULL)
367 break;
368 line[len] = '\0';
369 }
370
Damien Millerbe484b52000-07-15 14:14:16 +1000371 s = line;
372 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100373 if ((keyword = strdelim(&s)) == NULL)
374 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000375 /* Ignore leading whitespace. */
376 if (*keyword == '\0')
377 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000378 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100379 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
Damien Miller37023962000-07-11 17:31:38 +1000381 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382
Damien Miller95def091999-11-25 00:26:21 +1100383 switch (opcode) {
384 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100385 /* don't panic, but count bad options */
386 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100387 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000388 case oConnectTimeout:
389 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100390parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000391 arg = strdelim(&s);
392 if (!arg || *arg == '\0')
393 fatal("%s line %d: missing time value.",
394 filename, linenum);
395 if ((value = convtime(arg)) == -1)
396 fatal("%s line %d: invalid time value.",
397 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100398 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000399 *intptr = value;
400 break;
401
Damien Miller95def091999-11-25 00:26:21 +1100402 case oForwardAgent:
403 intptr = &options->forward_agent;
404parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000405 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000406 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100407 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
408 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000409 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100410 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000411 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100412 value = 0;
413 else
414 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
415 if (*activep && *intptr == -1)
416 *intptr = value;
417 break;
418
419 case oForwardX11:
420 intptr = &options->forward_x11;
421 goto parse_flag;
422
Darren Tucker0a118da2003-10-15 15:54:32 +1000423 case oForwardX11Trusted:
424 intptr = &options->forward_x11_trusted;
425 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000426
427 case oForwardX11Timeout:
428 intptr = &options->forward_x11_timeout;
429 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000430
Damien Miller95def091999-11-25 00:26:21 +1100431 case oGatewayPorts:
432 intptr = &options->gateway_ports;
433 goto parse_flag;
434
Darren Tuckere7d4b192006-07-12 22:17:10 +1000435 case oExitOnForwardFailure:
436 intptr = &options->exit_on_forward_failure;
437 goto parse_flag;
438
Damien Miller95def091999-11-25 00:26:21 +1100439 case oUsePrivilegedPort:
440 intptr = &options->use_privileged_port;
441 goto parse_flag;
442
Damien Miller95def091999-11-25 00:26:21 +1100443 case oPasswordAuthentication:
444 intptr = &options->password_authentication;
445 goto parse_flag;
446
Damien Miller01ed2272008-11-05 16:20:46 +1100447 case oZeroKnowledgePasswordAuthentication:
448 intptr = &options->zero_knowledge_password_authentication;
449 goto parse_flag;
450
Damien Miller874d77b2000-10-14 16:23:11 +1100451 case oKbdInteractiveAuthentication:
452 intptr = &options->kbd_interactive_authentication;
453 goto parse_flag;
454
455 case oKbdInteractiveDevices:
456 charptr = &options->kbd_interactive_devices;
457 goto parse_string;
458
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 case oPubkeyAuthentication:
460 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000461 goto parse_flag;
462
Damien Miller95def091999-11-25 00:26:21 +1100463 case oRSAAuthentication:
464 intptr = &options->rsa_authentication;
465 goto parse_flag;
466
467 case oRhostsRSAAuthentication:
468 intptr = &options->rhosts_rsa_authentication;
469 goto parse_flag;
470
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000471 case oHostbasedAuthentication:
472 intptr = &options->hostbased_authentication;
473 goto parse_flag;
474
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000475 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000476 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100477 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000478
Darren Tucker0efd1552003-08-26 11:49:55 +1000479 case oGssAuthentication:
480 intptr = &options->gss_authentication;
481 goto parse_flag;
482
483 case oGssDelegateCreds:
484 intptr = &options->gss_deleg_creds;
485 goto parse_flag;
486
Damien Miller95def091999-11-25 00:26:21 +1100487 case oBatchMode:
488 intptr = &options->batch_mode;
489 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 case oCheckHostIP:
492 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000493 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller37876e92003-05-15 10:19:46 +1000495 case oVerifyHostKeyDNS:
496 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100497 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000498
Damien Miller95def091999-11-25 00:26:21 +1100499 case oStrictHostKeyChecking:
500 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100501parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000502 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000503 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000504 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100505 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100506 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000507 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100508 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000509 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100510 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000511 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100512 value = 2;
513 else
514 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
515 if (*activep && *intptr == -1)
516 *intptr = value;
517 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 case oCompression:
520 intptr = &options->compression;
521 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Damien Miller12c150e2003-12-17 16:31:10 +1100523 case oTCPKeepAlive:
524 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100525 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000527 case oNoHostAuthenticationForLocalhost:
528 intptr = &options->no_host_authentication_for_localhost;
529 goto parse_flag;
530
Damien Miller95def091999-11-25 00:26:21 +1100531 case oNumberOfPasswordPrompts:
532 intptr = &options->number_of_password_prompts;
533 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534
Damien Miller95def091999-11-25 00:26:21 +1100535 case oCompressionLevel:
536 intptr = &options->compression_level;
537 goto parse_int;
538
Damien Millera5539d22003-04-09 20:50:06 +1000539 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000540 arg = strdelim(&s);
541 if (!arg || *arg == '\0')
542 fatal("%.200s line %d: Missing argument.", filename, linenum);
543 if (arg[0] < '0' || arg[0] > '9')
544 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100545 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000546 if (arg == endofnumber)
547 fatal("%.200s line %d: Bad number.", filename, linenum);
548 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100549 case '\0':
550 scale = 1;
551 break;
Damien Millera5539d22003-04-09 20:50:06 +1000552 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100553 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000554 break;
555 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100556 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000557 break;
558 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100559 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000560 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100561 default:
562 fatal("%.200s line %d: Invalid RekeyLimit suffix",
563 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000564 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100565 val64 *= scale;
566 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100567 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100568 fatal("%.200s line %d: RekeyLimit too large",
569 filename, linenum);
570 if (val64 < 16)
571 fatal("%.200s line %d: RekeyLimit too small",
572 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100573 if (*activep && options->rekey_limit == -1)
574 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000575 break;
576
Damien Miller95def091999-11-25 00:26:21 +1100577 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000578 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000579 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100580 fatal("%.200s line %d: Missing argument.", filename, linenum);
581 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100582 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000583 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100584 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100585 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100586 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000587 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000588 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100589 }
590 break;
591
Damien Millerd3a18572000-06-07 19:55:44 +1000592 case oXAuthLocation:
593 charptr=&options->xauth_location;
594 goto parse_string;
595
Damien Miller95def091999-11-25 00:26:21 +1100596 case oUser:
597 charptr = &options->user;
598parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000599 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000600 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100601 fatal("%.200s line %d: Missing argument.", filename, linenum);
602 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000603 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100604 break;
605
606 case oGlobalKnownHostsFile:
607 charptr = &options->system_hostfile;
608 goto parse_string;
609
610 case oUserKnownHostsFile:
611 charptr = &options->user_hostfile;
612 goto parse_string;
613
Damien Millereba71ba2000-04-29 23:57:08 +1000614 case oGlobalKnownHostsFile2:
615 charptr = &options->system_hostfile2;
616 goto parse_string;
617
618 case oUserKnownHostsFile2:
619 charptr = &options->user_hostfile2;
620 goto parse_string;
621
Damien Miller95def091999-11-25 00:26:21 +1100622 case oHostName:
623 charptr = &options->hostname;
624 goto parse_string;
625
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000626 case oHostKeyAlias:
627 charptr = &options->host_key_alias;
628 goto parse_string;
629
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000630 case oPreferredAuthentications:
631 charptr = &options->preferred_authentications;
632 goto parse_string;
633
Ben Lindstrome0f88042001-04-30 13:06:24 +0000634 case oBindAddress:
635 charptr = &options->bind_address;
636 goto parse_string;
637
Damien Miller7ea845e2010-02-12 09:21:02 +1100638 case oPKCS11Provider:
639 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000640 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000641
Damien Miller95def091999-11-25 00:26:21 +1100642 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100643 charptr = &options->proxy_command;
644parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000645 if (s == NULL)
646 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100647 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100648 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100649 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100650 return 0;
651
652 case oPort:
653 intptr = &options->port;
654parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000655 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000656 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100657 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000658 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100659 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100660
661 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000662 value = strtol(arg, &endofnumber, 0);
663 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100664 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100665 if (*activep && *intptr == -1)
666 *intptr = value;
667 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
Damien Miller95def091999-11-25 00:26:21 +1100669 case oConnectionAttempts:
670 intptr = &options->connection_attempts;
671 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100672
Damien Miller95def091999-11-25 00:26:21 +1100673 case oCipher:
674 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000675 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000676 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000677 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000678 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100679 if (value == -1)
680 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100681 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100682 if (*activep && *intptr == -1)
683 *intptr = value;
684 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685
Damien Miller78928792000-04-12 20:17:38 +1000686 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000687 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000688 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000689 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000690 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000691 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100692 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000693 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000694 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000695 break;
696
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000697 case oMacs:
698 arg = strdelim(&s);
699 if (!arg || *arg == '\0')
700 fatal("%.200s line %d: Missing argument.", filename, linenum);
701 if (!mac_valid(arg))
702 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100703 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000704 if (*activep && options->macs == NULL)
705 options->macs = xstrdup(arg);
706 break;
707
Damien Millerd5f62bf2010-09-24 22:11:14 +1000708 case oKexAlgorithms:
709 arg = strdelim(&s);
710 if (!arg || *arg == '\0')
711 fatal("%.200s line %d: Missing argument.",
712 filename, linenum);
713 if (!kex_names_valid(arg))
714 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
715 filename, linenum, arg ? arg : "<NONE>");
716 if (*activep && options->kex_algorithms == NULL)
717 options->kex_algorithms = xstrdup(arg);
718 break;
719
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000720 case oHostKeyAlgorithms:
721 arg = strdelim(&s);
722 if (!arg || *arg == '\0')
723 fatal("%.200s line %d: Missing argument.", filename, linenum);
724 if (!key_names_valid2(arg))
725 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100726 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000727 if (*activep && options->hostkeyalgorithms == NULL)
728 options->hostkeyalgorithms = xstrdup(arg);
729 break;
730
Damien Miller78928792000-04-12 20:17:38 +1000731 case oProtocol:
732 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000733 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000734 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000735 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000736 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000737 if (value == SSH_PROTO_UNKNOWN)
738 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100739 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000740 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
741 *intptr = value;
742 break;
743
Damien Miller95def091999-11-25 00:26:21 +1100744 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100745 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000746 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000747 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100748 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000749 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100750 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100751 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
752 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
754
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000755 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100756 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100757 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000758 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100759 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000760 fatal("%.200s line %d: Missing port argument.",
761 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100762
Damien Millera699d952008-11-03 19:27:34 +1100763 if (opcode == oLocalForward ||
764 opcode == oRemoteForward) {
765 arg2 = strdelim(&s);
766 if (arg2 == NULL || *arg2 == '\0')
767 fatal("%.200s line %d: Missing target argument.",
768 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100769
Damien Millera699d952008-11-03 19:27:34 +1100770 /* construct a string for parse_forward */
771 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
772 } else if (opcode == oDynamicForward) {
773 strlcpy(fwdarg, arg, sizeof(fwdarg));
774 }
775
776 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100777 opcode == oDynamicForward ? 1 : 0,
778 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000779 fatal("%.200s line %d: Bad forwarding specification.",
780 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100781
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000782 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100783 if (opcode == oLocalForward ||
784 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100785 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000786 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100787 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000788 }
Damien Miller95def091999-11-25 00:26:21 +1100789 break;
790
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000791 case oClearAllForwardings:
792 intptr = &options->clear_forwardings;
793 goto parse_flag;
794
Damien Miller95def091999-11-25 00:26:21 +1100795 case oHost:
796 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000797 arg2 = NULL;
798 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
799 negated = *arg == '!';
800 if (negated)
801 arg++;
Damien Miller37023962000-07-11 17:31:38 +1000802 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +1000803 if (negated) {
804 debug("%.200s line %d: Skipping Host "
805 "block because of negated match "
806 "for %.100s", filename, linenum,
807 arg);
808 *activep = 0;
809 break;
810 }
811 if (!*activep)
812 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +1100813 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +1100814 }
Damien Millerfe924212011-05-15 08:44:45 +1000815 }
816 if (*activep)
817 debug("%.200s line %d: Applying options for %.100s",
818 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +1000819 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100820 return 0;
821
822 case oEscapeChar:
823 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000824 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000825 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100826 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000827 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000828 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
829 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000830 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000831 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000832 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000833 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100834 else {
835 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100836 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100837 /* NOTREACHED */
838 value = 0; /* Avoid compiler warning. */
839 }
840 if (*activep && *intptr == -1)
841 *intptr = value;
842 break;
843
Damien Miller20a8f972003-05-18 20:50:30 +1000844 case oAddressFamily:
845 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000846 if (!arg || *arg == '\0')
847 fatal("%s line %d: missing address family.",
848 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000849 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000850 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000851 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000852 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000853 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000854 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000855 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000856 else
857 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000858 if (*activep && *intptr == -1)
859 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000860 break;
861
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000862 case oEnableSSHKeysign:
863 intptr = &options->enable_ssh_keysign;
864 goto parse_flag;
865
Damien Millerbd394c32004-03-08 23:12:36 +1100866 case oIdentitiesOnly:
867 intptr = &options->identities_only;
868 goto parse_flag;
869
Damien Miller509b0102003-12-17 16:33:10 +1100870 case oServerAliveInterval:
871 intptr = &options->server_alive_interval;
872 goto parse_time;
873
874 case oServerAliveCountMax:
875 intptr = &options->server_alive_count_max;
876 goto parse_int;
877
Darren Tucker46bc0752004-05-02 22:11:30 +1000878 case oSendEnv:
879 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
880 if (strchr(arg, '=') != NULL)
881 fatal("%s line %d: Invalid environment name.",
882 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100883 if (!*activep)
884 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000885 if (options->num_send_env >= MAX_SEND_ENV)
886 fatal("%s line %d: too many send env.",
887 filename, linenum);
888 options->send_env[options->num_send_env++] =
889 xstrdup(arg);
890 }
891 break;
892
Damien Miller0e220db2004-06-15 10:34:08 +1000893 case oControlPath:
894 charptr = &options->control_path;
895 goto parse_string;
896
897 case oControlMaster:
898 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000899 arg = strdelim(&s);
900 if (!arg || *arg == '\0')
901 fatal("%.200s line %d: Missing ControlMaster argument.",
902 filename, linenum);
903 value = 0; /* To avoid compiler warning... */
904 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
905 value = SSHCTL_MASTER_YES;
906 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
907 value = SSHCTL_MASTER_NO;
908 else if (strcmp(arg, "auto") == 0)
909 value = SSHCTL_MASTER_AUTO;
910 else if (strcmp(arg, "ask") == 0)
911 value = SSHCTL_MASTER_ASK;
912 else if (strcmp(arg, "autoask") == 0)
913 value = SSHCTL_MASTER_AUTO_ASK;
914 else
915 fatal("%.200s line %d: Bad ControlMaster argument.",
916 filename, linenum);
917 if (*activep && *intptr == -1)
918 *intptr = value;
919 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000920
Damien Millere11e1ea2010-08-03 16:04:46 +1000921 case oControlPersist:
922 /* no/false/yes/true, or a time spec */
923 intptr = &options->control_persist;
924 arg = strdelim(&s);
925 if (!arg || *arg == '\0')
926 fatal("%.200s line %d: Missing ControlPersist"
927 " argument.", filename, linenum);
928 value = 0;
929 value2 = 0; /* timeout */
930 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
931 value = 0;
932 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
933 value = 1;
934 else if ((value2 = convtime(arg)) >= 0)
935 value = 1;
936 else
937 fatal("%.200s line %d: Bad ControlPersist argument.",
938 filename, linenum);
939 if (*activep && *intptr == -1) {
940 *intptr = value;
941 options->control_persist_timeout = value2;
942 }
943 break;
944
Damien Millere1776152005-03-01 21:47:37 +1100945 case oHashKnownHosts:
946 intptr = &options->hash_known_hosts;
947 goto parse_flag;
948
Damien Millerd27b9472005-12-13 19:29:02 +1100949 case oTunnel:
950 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100951 arg = strdelim(&s);
952 if (!arg || *arg == '\0')
953 fatal("%s line %d: Missing yes/point-to-point/"
954 "ethernet/no argument.", filename, linenum);
955 value = 0; /* silence compiler */
956 if (strcasecmp(arg, "ethernet") == 0)
957 value = SSH_TUNMODE_ETHERNET;
958 else if (strcasecmp(arg, "point-to-point") == 0)
959 value = SSH_TUNMODE_POINTOPOINT;
960 else if (strcasecmp(arg, "yes") == 0)
961 value = SSH_TUNMODE_DEFAULT;
962 else if (strcasecmp(arg, "no") == 0)
963 value = SSH_TUNMODE_NO;
964 else
965 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
966 "no argument: %s", filename, linenum, arg);
967 if (*activep)
968 *intptr = value;
969 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100970
971 case oTunnelDevice:
972 arg = strdelim(&s);
973 if (!arg || *arg == '\0')
974 fatal("%.200s line %d: Missing argument.", filename, linenum);
975 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100976 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100977 fatal("%.200s line %d: Bad tun device.", filename, linenum);
978 if (*activep) {
979 options->tun_local = value;
980 options->tun_remote = value2;
981 }
982 break;
983
984 case oLocalCommand:
985 charptr = &options->local_command;
986 goto parse_command;
987
988 case oPermitLocalCommand:
989 intptr = &options->permit_local_command;
990 goto parse_flag;
991
Damien Miller10288242008-06-30 00:04:03 +1000992 case oVisualHostKey:
993 intptr = &options->visual_host_key;
994 goto parse_flag;
995
Damien Miller0dac6fb2010-11-20 15:19:38 +1100996 case oIPQoS:
997 arg = strdelim(&s);
998 if ((value = parse_ipqos(arg)) == -1)
999 fatal("%s line %d: Bad IPQoS value: %s",
1000 filename, linenum, arg);
1001 arg = strdelim(&s);
1002 if (arg == NULL)
1003 value2 = value;
1004 else if ((value2 = parse_ipqos(arg)) == -1)
1005 fatal("%s line %d: Bad IPQoS value: %s",
1006 filename, linenum, arg);
1007 if (*activep) {
1008 options->ip_qos_interactive = value;
1009 options->ip_qos_bulk = value2;
1010 }
1011 break;
1012
Darren Tucker71e4d542009-07-06 07:12:27 +10001013 case oUseRoaming:
1014 intptr = &options->use_roaming;
1015 goto parse_flag;
1016
Damien Miller21771e22011-05-15 08:45:50 +10001017 case oRequestTTY:
1018 arg = strdelim(&s);
1019 if (!arg || *arg == '\0')
1020 fatal("%s line %d: missing argument.",
1021 filename, linenum);
1022 intptr = &options->request_tty;
1023 if (strcasecmp(arg, "yes") == 0)
1024 value = REQUEST_TTY_YES;
1025 else if (strcasecmp(arg, "no") == 0)
1026 value = REQUEST_TTY_NO;
1027 else if (strcasecmp(arg, "force") == 0)
1028 value = REQUEST_TTY_FORCE;
1029 else if (strcasecmp(arg, "auto") == 0)
1030 value = REQUEST_TTY_AUTO;
1031 else
1032 fatal("Unsupported RequestTTY \"%s\"", arg);
1033 if (*activep && *intptr == -1)
1034 *intptr = value;
1035 break;
1036
Ben Lindstrom4daea862002-06-09 20:04:02 +00001037 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001038 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001039 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001040 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001041
Damien Millerf9b3feb2003-05-16 11:38:32 +10001042 case oUnsupported:
1043 error("%s line %d: Unsupported option \"%s\"",
1044 filename, linenum, keyword);
1045 return 0;
1046
Damien Miller95def091999-11-25 00:26:21 +11001047 default:
1048 fatal("process_config_line: Unimplemented opcode %d", opcode);
1049 }
1050
1051 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001052 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001053 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001054 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001055 }
Damien Miller95def091999-11-25 00:26:21 +11001056 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057}
1058
1059
Damien Miller5428f641999-11-25 11:54:57 +11001060/*
1061 * Reads the config file and modifies the options accordingly. Options
1062 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001063 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001064 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001066int
Darren Tuckerfc959702004-07-17 16:12:08 +10001067read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +10001068 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069{
Damien Miller95def091999-11-25 00:26:21 +11001070 FILE *f;
1071 char line[1024];
1072 int active, linenum;
1073 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074
Damien Miller57a44762004-04-20 20:11:57 +10001075 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001076 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077
Damien Miller57a44762004-04-20 20:11:57 +10001078 if (checkperm) {
1079 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001080
Damien Miller33793852004-06-15 10:27:55 +10001081 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001082 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001083 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001084 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001085 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001086 }
1087
Damien Miller95def091999-11-25 00:26:21 +11001088 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089
Damien Miller5428f641999-11-25 11:54:57 +11001090 /*
1091 * Mark that we are now processing the options. This flag is turned
1092 * on/off by Host specifications.
1093 */
Damien Miller95def091999-11-25 00:26:21 +11001094 active = 1;
1095 linenum = 0;
1096 while (fgets(line, sizeof(line), f)) {
1097 /* Update line number counter. */
1098 linenum++;
1099 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1100 bad_options++;
1101 }
1102 fclose(f);
1103 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001104 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001105 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001106 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107}
1108
Damien Miller5428f641999-11-25 11:54:57 +11001109/*
1110 * Initializes options to special values that indicate that they have not yet
1111 * been set. Read_config_file will only set options with this value. Options
1112 * are processed in the following order: command line, user config file,
1113 * system config file. Last, fill_default_options is called.
1114 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Damien Miller4af51302000-04-16 11:18:38 +10001116void
Damien Miller95def091999-11-25 00:26:21 +11001117initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118{
Damien Miller95def091999-11-25 00:26:21 +11001119 memset(options, 'X', sizeof(*options));
1120 options->forward_agent = -1;
1121 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001122 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001123 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001124 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001125 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001126 options->gateway_ports = -1;
1127 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001128 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001129 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001130 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001131 options->gss_authentication = -1;
1132 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001133 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001134 options->kbd_interactive_authentication = -1;
1135 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001136 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001137 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001138 options->batch_mode = -1;
1139 options->check_host_ip = -1;
1140 options->strict_host_key_checking = -1;
1141 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001142 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001143 options->compression_level = -1;
1144 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001145 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001146 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001147 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001148 options->number_of_password_prompts = -1;
1149 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001150 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001151 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001152 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001153 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001154 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001155 options->num_identity_files = 0;
1156 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001157 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001158 options->proxy_command = NULL;
1159 options->user = NULL;
1160 options->escape_char = -1;
1161 options->system_hostfile = NULL;
1162 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001163 options->system_hostfile2 = NULL;
1164 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001165 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001166 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001167 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001168 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001169 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001170 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001171 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001172 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001173 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001174 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001175 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001176 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001177 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001178 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001179 options->server_alive_interval = -1;
1180 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001181 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001182 options->control_path = NULL;
1183 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001184 options->control_persist = -1;
1185 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001186 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001187 options->tun_open = -1;
1188 options->tun_local = -1;
1189 options->tun_remote = -1;
1190 options->local_command = NULL;
1191 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001192 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001193 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001194 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001195 options->ip_qos_interactive = -1;
1196 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001197 options->request_tty = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198}
1199
Damien Miller5428f641999-11-25 11:54:57 +11001200/*
1201 * Called after processing other sources of option data, this fills those
1202 * options for which no value has been specified with their default values.
1203 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204
Damien Miller4af51302000-04-16 11:18:38 +10001205void
Damien Miller95def091999-11-25 00:26:21 +11001206fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001208 int len;
1209
Damien Miller95def091999-11-25 00:26:21 +11001210 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001211 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001212 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001213 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001214 if (options->forward_x11_trusted == -1)
1215 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001216 if (options->forward_x11_timeout == -1)
1217 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001218 if (options->exit_on_forward_failure == -1)
1219 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001220 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001221 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001222 if (options->gateway_ports == -1)
1223 options->gateway_ports = 0;
1224 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001225 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001226 if (options->rsa_authentication == -1)
1227 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001228 if (options->pubkey_authentication == -1)
1229 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001230 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001231 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001232 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001233 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001234 if (options->gss_deleg_creds == -1)
1235 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001236 if (options->password_authentication == -1)
1237 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001238 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001239 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001240 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001241 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001242 if (options->hostbased_authentication == -1)
1243 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001244 if (options->batch_mode == -1)
1245 options->batch_mode = 0;
1246 if (options->check_host_ip == -1)
1247 options->check_host_ip = 1;
1248 if (options->strict_host_key_checking == -1)
1249 options->strict_host_key_checking = 2; /* 2 is default */
1250 if (options->compression == -1)
1251 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001252 if (options->tcp_keep_alive == -1)
1253 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001254 if (options->compression_level == -1)
1255 options->compression_level = 6;
1256 if (options->port == -1)
1257 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001258 if (options->address_family == -1)
1259 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001260 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001261 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001262 if (options->number_of_password_prompts == -1)
1263 options->number_of_password_prompts = 3;
1264 /* Selected in ssh_login(). */
1265 if (options->cipher == -1)
1266 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001267 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001268 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001269 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001270 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001271 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001272 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001273 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001274 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001275 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001276 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001277 xmalloc(len);
1278 snprintf(options->identity_files[options->num_identity_files++],
1279 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001280 }
1281 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001282 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1283 options->identity_files[options->num_identity_files] =
1284 xmalloc(len);
1285 snprintf(options->identity_files[options->num_identity_files++],
1286 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1287
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001288 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001289 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001290 xmalloc(len);
1291 snprintf(options->identity_files[options->num_identity_files++],
1292 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001293#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001294 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1295 options->identity_files[options->num_identity_files] =
1296 xmalloc(len);
1297 snprintf(options->identity_files[options->num_identity_files++],
1298 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001299#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001300 }
Damien Millereba71ba2000-04-29 23:57:08 +10001301 }
Damien Miller95def091999-11-25 00:26:21 +11001302 if (options->escape_char == -1)
1303 options->escape_char = '~';
1304 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001305 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001306 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001307 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001308 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001309 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001310 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001311 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001312 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001313 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001314 if (options->clear_forwardings == 1)
1315 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001316 if (options->no_host_authentication_for_localhost == - 1)
1317 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001318 if (options->identities_only == -1)
1319 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001320 if (options->enable_ssh_keysign == -1)
1321 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001322 if (options->rekey_limit == -1)
1323 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001324 if (options->verify_host_key_dns == -1)
1325 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001326 if (options->server_alive_interval == -1)
1327 options->server_alive_interval = 0;
1328 if (options->server_alive_count_max == -1)
1329 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001330 if (options->control_master == -1)
1331 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001332 if (options->control_persist == -1) {
1333 options->control_persist = 0;
1334 options->control_persist_timeout = 0;
1335 }
Damien Millere1776152005-03-01 21:47:37 +11001336 if (options->hash_known_hosts == -1)
1337 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001338 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001339 options->tun_open = SSH_TUNMODE_NO;
1340 if (options->tun_local == -1)
1341 options->tun_local = SSH_TUNID_ANY;
1342 if (options->tun_remote == -1)
1343 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001344 if (options->permit_local_command == -1)
1345 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001346 if (options->use_roaming == -1)
1347 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001348 if (options->visual_host_key == -1)
1349 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001350 if (options->zero_knowledge_password_authentication == -1)
1351 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001352 if (options->ip_qos_interactive == -1)
1353 options->ip_qos_interactive = IPTOS_LOWDELAY;
1354 if (options->ip_qos_bulk == -1)
1355 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001356 if (options->request_tty == -1)
1357 options->request_tty = REQUEST_TTY_AUTO;
Damien Millerd27b9472005-12-13 19:29:02 +11001358 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001359 /* options->proxy_command should not be set by default */
1360 /* options->user will be set in the main program if appropriate */
1361 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001362 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001363 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001365
1366/*
1367 * parse_forward
1368 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001369 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001370 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001371 * dynamicfwd == 1
1372 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001373 * returns number of arguments parsed or zero on error
1374 */
1375int
Damien Miller4bf648f2009-02-14 16:28:21 +11001376parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001377{
1378 int i;
1379 char *p, *cp, *fwdarg[4];
1380
1381 memset(fwd, '\0', sizeof(*fwd));
1382
1383 cp = p = xstrdup(fwdspec);
1384
1385 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001386 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001387 cp++;
1388
1389 for (i = 0; i < 4; ++i)
1390 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1391 break;
1392
Damien Millerf4b39532008-11-03 19:28:21 +11001393 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001394 if (cp != NULL)
1395 i = 0; /* failure */
1396
1397 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001398 case 1:
1399 fwd->listen_host = NULL;
1400 fwd->listen_port = a2port(fwdarg[0]);
1401 fwd->connect_host = xstrdup("socks");
1402 break;
1403
1404 case 2:
1405 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1406 fwd->listen_port = a2port(fwdarg[1]);
1407 fwd->connect_host = xstrdup("socks");
1408 break;
1409
Damien Millerf91ee4c2005-03-01 21:24:33 +11001410 case 3:
1411 fwd->listen_host = NULL;
1412 fwd->listen_port = a2port(fwdarg[0]);
1413 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1414 fwd->connect_port = a2port(fwdarg[2]);
1415 break;
1416
1417 case 4:
1418 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1419 fwd->listen_port = a2port(fwdarg[1]);
1420 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1421 fwd->connect_port = a2port(fwdarg[3]);
1422 break;
1423 default:
1424 i = 0; /* failure */
1425 }
1426
1427 xfree(p);
1428
Damien Millera699d952008-11-03 19:27:34 +11001429 if (dynamicfwd) {
1430 if (!(i == 1 || i == 2))
1431 goto fail_free;
1432 } else {
1433 if (!(i == 3 || i == 4))
1434 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001435 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001436 goto fail_free;
1437 }
1438
Damien Miller4bf648f2009-02-14 16:28:21 +11001439 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001440 goto fail_free;
1441
1442 if (fwd->connect_host != NULL &&
1443 strlen(fwd->connect_host) >= NI_MAXHOST)
1444 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001445 if (fwd->listen_host != NULL &&
1446 strlen(fwd->listen_host) >= NI_MAXHOST)
1447 goto fail_free;
1448
Damien Millerf91ee4c2005-03-01 21:24:33 +11001449
1450 return (i);
1451
1452 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001453 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001454 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001455 fwd->connect_host = NULL;
1456 }
1457 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001458 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001459 fwd->listen_host = NULL;
1460 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001461 return (0);
1462}