blob: 47c77472a837458d322624697a4bc1aa1ff2080d [file] [log] [blame]
Darren Tucker535b5e12010-01-08 18:56:48 +11001/* $OpenBSD: readconf.c,v 1.181 2009/12/29 16:38:41 stevesk 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 Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000114 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000115 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100117 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100121 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000124 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000126 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000127 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000128 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000129 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100130 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100131 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100132 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker34e314d2010-01-08 17:03:46 +1100133 oVisualHostKey, oUseRoaming, oRDomain,
134 oZeroKnowledgePasswordAuthentication, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135} OpCodes;
136
137/* Textual representations of the tokens. */
138
Damien Miller95def091999-11-25 00:26:21 +1100139static struct {
140 const char *name;
141 OpCodes opcode;
142} keywords[] = {
143 { "forwardagent", oForwardAgent },
144 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000145 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000146 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000147 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100148 { "gatewayports", oGatewayPorts },
149 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000150 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100151 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100152 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
153 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000156 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000157 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000158 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000159 { "challengeresponseauthentication", oChallengeResponseAuthentication },
160 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
161 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000162 { "kerberosauthentication", oUnsupported },
163 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000164 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000165#if defined(GSSAPI)
166 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000167 { "gssapidelegatecredentials", oGssDelegateCreds },
168#else
169 { "gssapiauthentication", oUnsupported },
170 { "gssapidelegatecredentials", oUnsupported },
171#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000172 { "fallbacktorsh", oDeprecated },
173 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100174 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100175 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100176 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000178 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "proxycommand", oProxyCommand },
180 { "port", oPort },
181 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000182 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000183 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000184 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "remoteforward", oRemoteForward },
186 { "localforward", oLocalForward },
187 { "user", oUser },
188 { "host", oHost },
189 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100191 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
192 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000193 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100194 { "connectionattempts", oConnectionAttempts },
195 { "batchmode", oBatchMode },
196 { "checkhostip", oCheckHostIP },
197 { "stricthostkeychecking", oStrictHostKeyChecking },
198 { "compression", oCompression },
199 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100200 { "tcpkeepalive", oTCPKeepAlive },
201 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100202 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000204 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000205 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000206 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000207 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000208#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000209 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000210#else
211 { "smartcarddevice", oUnsupported },
212#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100213 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000214 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000215 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100216 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000217 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000218 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000219 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100220 { "serveraliveinterval", oServerAliveInterval },
221 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000222 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000223 { "controlpath", oControlPath },
224 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100225 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100226 { "tunnel", oTunnel },
227 { "tunneldevice", oTunnelDevice },
228 { "localcommand", oLocalCommand },
229 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000230 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000231 { "useroaming", oUseRoaming },
Darren Tucker1f5e3dc2010-01-08 19:53:52 +1100232#ifdef USE_ROUTINGDOMAIN
Darren Tucker535b5e12010-01-08 18:56:48 +1100233 { "routingdomain", oRDomain },
Darren Tucker1f5e3dc2010-01-08 19:53:52 +1100234#else
235 { "routingdomain", oUnsupported },
236#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100237#ifdef JPAKE
238 { "zeroknowledgepasswordauthentication",
239 oZeroKnowledgePasswordAuthentication },
240#else
241 { "zeroknowledgepasswordauthentication", oUnsupported },
242#endif
243
Ben Lindstrom65366a82001-12-06 16:32:47 +0000244 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100245};
246
Damien Miller5428f641999-11-25 11:54:57 +1100247/*
248 * Adds a local TCP/IP port forward to options. Never returns if there is an
249 * error.
250 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller4af51302000-04-16 11:18:38 +1000252void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100253add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254{
Damien Miller95def091999-11-25 00:26:21 +1100255 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000256#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100257 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100258 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000259 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100260#endif
Damien Miller95def091999-11-25 00:26:21 +1100261 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
262 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
263 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264
Damien Miller1a0442f2008-11-05 16:30:06 +1100265 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100266 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100267 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100268 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269}
270
Damien Miller5428f641999-11-25 11:54:57 +1100271/*
272 * Adds a remote TCP/IP port forward to options. Never returns if there is
273 * an error.
274 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller4af51302000-04-16 11:18:38 +1000276void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100277add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278{
Damien Miller95def091999-11-25 00:26:21 +1100279 Forward *fwd;
280 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
281 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100282 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100283 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284
Damien Miller1a0442f2008-11-05 16:30:06 +1100285 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100287 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289}
290
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000291static void
292clear_forwardings(Options *options)
293{
294 int i;
295
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100297 if (options->local_forwards[i].listen_host != NULL)
298 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 xfree(options->local_forwards[i].connect_host);
300 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000301 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100303 if (options->remote_forwards[i].listen_host != NULL)
304 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305 xfree(options->remote_forwards[i].connect_host);
306 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000307 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100308 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000312 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100313 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller4af51302000-04-16 11:18:38 +1000315static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100316parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000318 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Damien Miller95def091999-11-25 00:26:21 +1100320 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100321 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100322 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000324 error("%s: line %d: Bad configuration option: %s",
325 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100326 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327}
328
Damien Miller5428f641999-11-25 11:54:57 +1100329/*
330 * Processes a single option line as used in the configuration files. This
331 * only sets those values that have not already been set.
332 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100333#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Damien Miller2ccf6611999-11-15 15:25:10 +1100335int
336process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100337 char *line, const char *filename, int linenum,
338 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100341 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100342 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100343 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100344 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100345 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Millerc652cac2003-05-14 13:40:54 +1000347 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100348 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000349 if (strchr(WHITESPACE, line[len]) == NULL)
350 break;
351 line[len] = '\0';
352 }
353
Damien Millerbe484b52000-07-15 14:14:16 +1000354 s = line;
355 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100356 if ((keyword = strdelim(&s)) == NULL)
357 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000358 /* Ignore leading whitespace. */
359 if (*keyword == '\0')
360 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000361 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100362 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Miller37023962000-07-11 17:31:38 +1000364 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Damien Miller95def091999-11-25 00:26:21 +1100366 switch (opcode) {
367 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100368 /* don't panic, but count bad options */
369 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100370 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000371 case oConnectTimeout:
372 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100373parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000374 arg = strdelim(&s);
375 if (!arg || *arg == '\0')
376 fatal("%s line %d: missing time value.",
377 filename, linenum);
378 if ((value = convtime(arg)) == -1)
379 fatal("%s line %d: invalid time value.",
380 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100381 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000382 *intptr = value;
383 break;
384
Damien Miller95def091999-11-25 00:26:21 +1100385 case oForwardAgent:
386 intptr = &options->forward_agent;
387parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000388 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000389 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100390 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
391 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000392 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100393 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000394 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100395 value = 0;
396 else
397 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
398 if (*activep && *intptr == -1)
399 *intptr = value;
400 break;
401
402 case oForwardX11:
403 intptr = &options->forward_x11;
404 goto parse_flag;
405
Darren Tucker0a118da2003-10-15 15:54:32 +1000406 case oForwardX11Trusted:
407 intptr = &options->forward_x11_trusted;
408 goto parse_flag;
409
Damien Miller95def091999-11-25 00:26:21 +1100410 case oGatewayPorts:
411 intptr = &options->gateway_ports;
412 goto parse_flag;
413
Darren Tuckere7d4b192006-07-12 22:17:10 +1000414 case oExitOnForwardFailure:
415 intptr = &options->exit_on_forward_failure;
416 goto parse_flag;
417
Damien Miller95def091999-11-25 00:26:21 +1100418 case oUsePrivilegedPort:
419 intptr = &options->use_privileged_port;
420 goto parse_flag;
421
Damien Miller95def091999-11-25 00:26:21 +1100422 case oPasswordAuthentication:
423 intptr = &options->password_authentication;
424 goto parse_flag;
425
Damien Miller01ed2272008-11-05 16:20:46 +1100426 case oZeroKnowledgePasswordAuthentication:
427 intptr = &options->zero_knowledge_password_authentication;
428 goto parse_flag;
429
Damien Miller874d77b2000-10-14 16:23:11 +1100430 case oKbdInteractiveAuthentication:
431 intptr = &options->kbd_interactive_authentication;
432 goto parse_flag;
433
434 case oKbdInteractiveDevices:
435 charptr = &options->kbd_interactive_devices;
436 goto parse_string;
437
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 case oPubkeyAuthentication:
439 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000440 goto parse_flag;
441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oRSAAuthentication:
443 intptr = &options->rsa_authentication;
444 goto parse_flag;
445
446 case oRhostsRSAAuthentication:
447 intptr = &options->rhosts_rsa_authentication;
448 goto parse_flag;
449
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000450 case oHostbasedAuthentication:
451 intptr = &options->hostbased_authentication;
452 goto parse_flag;
453
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000454 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000455 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100456 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000457
Darren Tucker0efd1552003-08-26 11:49:55 +1000458 case oGssAuthentication:
459 intptr = &options->gss_authentication;
460 goto parse_flag;
461
462 case oGssDelegateCreds:
463 intptr = &options->gss_deleg_creds;
464 goto parse_flag;
465
Damien Miller95def091999-11-25 00:26:21 +1100466 case oBatchMode:
467 intptr = &options->batch_mode;
468 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller95def091999-11-25 00:26:21 +1100470 case oCheckHostIP:
471 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000472 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Damien Miller37876e92003-05-15 10:19:46 +1000474 case oVerifyHostKeyDNS:
475 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100476 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 case oStrictHostKeyChecking:
479 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100480parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000481 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000482 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000483 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100484 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100485 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000486 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100487 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000488 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100489 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000490 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100491 value = 2;
492 else
493 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
494 if (*activep && *intptr == -1)
495 *intptr = value;
496 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller95def091999-11-25 00:26:21 +1100498 case oCompression:
499 intptr = &options->compression;
500 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Miller12c150e2003-12-17 16:31:10 +1100502 case oTCPKeepAlive:
503 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100504 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000506 case oNoHostAuthenticationForLocalhost:
507 intptr = &options->no_host_authentication_for_localhost;
508 goto parse_flag;
509
Damien Miller95def091999-11-25 00:26:21 +1100510 case oNumberOfPasswordPrompts:
511 intptr = &options->number_of_password_prompts;
512 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller95def091999-11-25 00:26:21 +1100514 case oCompressionLevel:
515 intptr = &options->compression_level;
516 goto parse_int;
517
Damien Millera5539d22003-04-09 20:50:06 +1000518 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000519 arg = strdelim(&s);
520 if (!arg || *arg == '\0')
521 fatal("%.200s line %d: Missing argument.", filename, linenum);
522 if (arg[0] < '0' || arg[0] > '9')
523 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100524 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000525 if (arg == endofnumber)
526 fatal("%.200s line %d: Bad number.", filename, linenum);
527 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100528 case '\0':
529 scale = 1;
530 break;
Damien Millera5539d22003-04-09 20:50:06 +1000531 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000533 break;
534 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100535 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000536 break;
537 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100538 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000539 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100540 default:
541 fatal("%.200s line %d: Invalid RekeyLimit suffix",
542 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000543 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100544 val64 *= scale;
545 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100546 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100547 fatal("%.200s line %d: RekeyLimit too large",
548 filename, linenum);
549 if (val64 < 16)
550 fatal("%.200s line %d: RekeyLimit too small",
551 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100552 if (*activep && options->rekey_limit == -1)
553 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000554 break;
555
Damien Miller95def091999-11-25 00:26:21 +1100556 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000557 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000558 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100559 fatal("%.200s line %d: Missing argument.", filename, linenum);
560 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100561 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000562 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100563 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100564 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100565 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000566 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000567 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100568 }
569 break;
570
Damien Millerd3a18572000-06-07 19:55:44 +1000571 case oXAuthLocation:
572 charptr=&options->xauth_location;
573 goto parse_string;
574
Damien Miller95def091999-11-25 00:26:21 +1100575 case oUser:
576 charptr = &options->user;
577parse_string:
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 && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000582 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100583 break;
584
585 case oGlobalKnownHostsFile:
586 charptr = &options->system_hostfile;
587 goto parse_string;
588
589 case oUserKnownHostsFile:
590 charptr = &options->user_hostfile;
591 goto parse_string;
592
Damien Millereba71ba2000-04-29 23:57:08 +1000593 case oGlobalKnownHostsFile2:
594 charptr = &options->system_hostfile2;
595 goto parse_string;
596
597 case oUserKnownHostsFile2:
598 charptr = &options->user_hostfile2;
599 goto parse_string;
600
Damien Miller95def091999-11-25 00:26:21 +1100601 case oHostName:
602 charptr = &options->hostname;
603 goto parse_string;
604
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000605 case oHostKeyAlias:
606 charptr = &options->host_key_alias;
607 goto parse_string;
608
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000609 case oPreferredAuthentications:
610 charptr = &options->preferred_authentications;
611 goto parse_string;
612
Ben Lindstrome0f88042001-04-30 13:06:24 +0000613 case oBindAddress:
614 charptr = &options->bind_address;
615 goto parse_string;
616
Ben Lindstromae996bf2001-08-06 21:27:53 +0000617 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000618 charptr = &options->smartcard_device;
619 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000620
Damien Miller95def091999-11-25 00:26:21 +1100621 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100622 charptr = &options->proxy_command;
623parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000624 if (s == NULL)
625 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100626 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100627 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100628 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100629 return 0;
630
631 case oPort:
632 intptr = &options->port;
633parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000634 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000635 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100636 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000637 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100638 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100639
640 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000641 value = strtol(arg, &endofnumber, 0);
642 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100643 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100644 if (*activep && *intptr == -1)
645 *intptr = value;
646 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647
Damien Miller95def091999-11-25 00:26:21 +1100648 case oConnectionAttempts:
649 intptr = &options->connection_attempts;
650 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100651
Damien Miller95def091999-11-25 00:26:21 +1100652 case oCipher:
653 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000654 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000655 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000656 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000657 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100658 if (value == -1)
659 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100660 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100661 if (*activep && *intptr == -1)
662 *intptr = value;
663 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller78928792000-04-12 20:17:38 +1000665 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000666 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000667 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000668 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000669 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000670 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100671 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000672 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000673 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000674 break;
675
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000676 case oMacs:
677 arg = strdelim(&s);
678 if (!arg || *arg == '\0')
679 fatal("%.200s line %d: Missing argument.", filename, linenum);
680 if (!mac_valid(arg))
681 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100682 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000683 if (*activep && options->macs == NULL)
684 options->macs = xstrdup(arg);
685 break;
686
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000687 case oHostKeyAlgorithms:
688 arg = strdelim(&s);
689 if (!arg || *arg == '\0')
690 fatal("%.200s line %d: Missing argument.", filename, linenum);
691 if (!key_names_valid2(arg))
692 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100693 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000694 if (*activep && options->hostkeyalgorithms == NULL)
695 options->hostkeyalgorithms = xstrdup(arg);
696 break;
697
Damien Miller78928792000-04-12 20:17:38 +1000698 case oProtocol:
699 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000700 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000701 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000702 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000703 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000704 if (value == SSH_PROTO_UNKNOWN)
705 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100706 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000707 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
708 *intptr = value;
709 break;
710
Damien Miller95def091999-11-25 00:26:21 +1100711 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100712 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000713 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000714 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100715 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000716 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100718 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
719 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100720 break;
721
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000722 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100723 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100724 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000725 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100726 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000727 fatal("%.200s line %d: Missing port argument.",
728 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100729
Damien Millera699d952008-11-03 19:27:34 +1100730 if (opcode == oLocalForward ||
731 opcode == oRemoteForward) {
732 arg2 = strdelim(&s);
733 if (arg2 == NULL || *arg2 == '\0')
734 fatal("%.200s line %d: Missing target argument.",
735 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100736
Damien Millera699d952008-11-03 19:27:34 +1100737 /* construct a string for parse_forward */
738 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
739 } else if (opcode == oDynamicForward) {
740 strlcpy(fwdarg, arg, sizeof(fwdarg));
741 }
742
743 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100744 opcode == oDynamicForward ? 1 : 0,
745 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000746 fatal("%.200s line %d: Bad forwarding specification.",
747 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100748
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000749 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100750 if (opcode == oLocalForward ||
751 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100752 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000753 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100754 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000755 }
Damien Miller95def091999-11-25 00:26:21 +1100756 break;
757
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000758 case oClearAllForwardings:
759 intptr = &options->clear_forwardings;
760 goto parse_flag;
761
Damien Miller95def091999-11-25 00:26:21 +1100762 case oHost:
763 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000764 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000765 if (match_pattern(host, arg)) {
766 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100767 *activep = 1;
768 break;
769 }
Damien Millerbe484b52000-07-15 14:14:16 +1000770 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100771 return 0;
772
773 case oEscapeChar:
774 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000775 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000776 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100777 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000778 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000779 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
780 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000781 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000782 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000783 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000784 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100785 else {
786 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100787 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100788 /* NOTREACHED */
789 value = 0; /* Avoid compiler warning. */
790 }
791 if (*activep && *intptr == -1)
792 *intptr = value;
793 break;
794
Damien Miller20a8f972003-05-18 20:50:30 +1000795 case oAddressFamily:
796 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000797 if (!arg || *arg == '\0')
798 fatal("%s line %d: missing address family.",
799 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000800 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000801 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000802 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000803 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000804 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000805 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000806 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000807 else
808 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000809 if (*activep && *intptr == -1)
810 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000811 break;
812
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000813 case oEnableSSHKeysign:
814 intptr = &options->enable_ssh_keysign;
815 goto parse_flag;
816
Damien Millerbd394c32004-03-08 23:12:36 +1100817 case oIdentitiesOnly:
818 intptr = &options->identities_only;
819 goto parse_flag;
820
Damien Miller509b0102003-12-17 16:33:10 +1100821 case oServerAliveInterval:
822 intptr = &options->server_alive_interval;
823 goto parse_time;
824
825 case oServerAliveCountMax:
826 intptr = &options->server_alive_count_max;
827 goto parse_int;
828
Darren Tucker46bc0752004-05-02 22:11:30 +1000829 case oSendEnv:
830 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
831 if (strchr(arg, '=') != NULL)
832 fatal("%s line %d: Invalid environment name.",
833 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100834 if (!*activep)
835 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000836 if (options->num_send_env >= MAX_SEND_ENV)
837 fatal("%s line %d: too many send env.",
838 filename, linenum);
839 options->send_env[options->num_send_env++] =
840 xstrdup(arg);
841 }
842 break;
843
Damien Miller0e220db2004-06-15 10:34:08 +1000844 case oControlPath:
845 charptr = &options->control_path;
846 goto parse_string;
847
848 case oControlMaster:
849 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000850 arg = strdelim(&s);
851 if (!arg || *arg == '\0')
852 fatal("%.200s line %d: Missing ControlMaster argument.",
853 filename, linenum);
854 value = 0; /* To avoid compiler warning... */
855 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
856 value = SSHCTL_MASTER_YES;
857 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
858 value = SSHCTL_MASTER_NO;
859 else if (strcmp(arg, "auto") == 0)
860 value = SSHCTL_MASTER_AUTO;
861 else if (strcmp(arg, "ask") == 0)
862 value = SSHCTL_MASTER_ASK;
863 else if (strcmp(arg, "autoask") == 0)
864 value = SSHCTL_MASTER_AUTO_ASK;
865 else
866 fatal("%.200s line %d: Bad ControlMaster argument.",
867 filename, linenum);
868 if (*activep && *intptr == -1)
869 *intptr = value;
870 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000871
Damien Millere1776152005-03-01 21:47:37 +1100872 case oHashKnownHosts:
873 intptr = &options->hash_known_hosts;
874 goto parse_flag;
875
Damien Millerd27b9472005-12-13 19:29:02 +1100876 case oTunnel:
877 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100878 arg = strdelim(&s);
879 if (!arg || *arg == '\0')
880 fatal("%s line %d: Missing yes/point-to-point/"
881 "ethernet/no argument.", filename, linenum);
882 value = 0; /* silence compiler */
883 if (strcasecmp(arg, "ethernet") == 0)
884 value = SSH_TUNMODE_ETHERNET;
885 else if (strcasecmp(arg, "point-to-point") == 0)
886 value = SSH_TUNMODE_POINTOPOINT;
887 else if (strcasecmp(arg, "yes") == 0)
888 value = SSH_TUNMODE_DEFAULT;
889 else if (strcasecmp(arg, "no") == 0)
890 value = SSH_TUNMODE_NO;
891 else
892 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
893 "no argument: %s", filename, linenum, arg);
894 if (*activep)
895 *intptr = value;
896 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100897
898 case oTunnelDevice:
899 arg = strdelim(&s);
900 if (!arg || *arg == '\0')
901 fatal("%.200s line %d: Missing argument.", filename, linenum);
902 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100903 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100904 fatal("%.200s line %d: Bad tun device.", filename, linenum);
905 if (*activep) {
906 options->tun_local = value;
907 options->tun_remote = value2;
908 }
909 break;
910
911 case oLocalCommand:
912 charptr = &options->local_command;
913 goto parse_command;
914
915 case oPermitLocalCommand:
916 intptr = &options->permit_local_command;
917 goto parse_flag;
918
Damien Miller10288242008-06-30 00:04:03 +1000919 case oVisualHostKey:
920 intptr = &options->visual_host_key;
921 goto parse_flag;
922
Darren Tucker71e4d542009-07-06 07:12:27 +1000923 case oUseRoaming:
924 intptr = &options->use_roaming;
925 goto parse_flag;
926
Darren Tucker1f5e3dc2010-01-08 19:53:52 +1100927#ifdef USE_ROUTINGDOMAIN
Darren Tucker34e314d2010-01-08 17:03:46 +1100928 case oRDomain:
929 arg = strdelim(&s);
930 if (!arg || *arg == '\0')
931 fatal("%.200s line %d: Missing argument.",
932 filename, linenum);
Darren Tucker75456e82010-01-08 18:55:58 +1100933 value = a2rdomain(arg);
Darren Tucker34e314d2010-01-08 17:03:46 +1100934 if (value == -1)
935 fatal("%.200s line %d: Bad rdomain.",
936 filename, linenum);
937 if (*activep)
938 options->rdomain = value;
939 break;
Darren Tucker1f5e3dc2010-01-08 19:53:52 +1100940#endif
Darren Tucker34e314d2010-01-08 17:03:46 +1100941
Ben Lindstrom4daea862002-06-09 20:04:02 +0000942 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000943 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000944 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000945 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000946
Damien Millerf9b3feb2003-05-16 11:38:32 +1000947 case oUnsupported:
948 error("%s line %d: Unsupported option \"%s\"",
949 filename, linenum, keyword);
950 return 0;
951
Damien Miller95def091999-11-25 00:26:21 +1100952 default:
953 fatal("process_config_line: Unimplemented opcode %d", opcode);
954 }
955
956 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000957 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000958 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000959 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000960 }
Damien Miller95def091999-11-25 00:26:21 +1100961 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962}
963
964
Damien Miller5428f641999-11-25 11:54:57 +1100965/*
966 * Reads the config file and modifies the options accordingly. Options
967 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000968 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100969 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000971int
Darren Tuckerfc959702004-07-17 16:12:08 +1000972read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000973 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974{
Damien Miller95def091999-11-25 00:26:21 +1100975 FILE *f;
976 char line[1024];
977 int active, linenum;
978 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller57a44762004-04-20 20:11:57 +1000980 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000981 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller57a44762004-04-20 20:11:57 +1000983 if (checkperm) {
984 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000985
Damien Miller33793852004-06-15 10:27:55 +1000986 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000987 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000988 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000989 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000990 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000991 }
992
Damien Miller95def091999-11-25 00:26:21 +1100993 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller5428f641999-11-25 11:54:57 +1100995 /*
996 * Mark that we are now processing the options. This flag is turned
997 * on/off by Host specifications.
998 */
Damien Miller95def091999-11-25 00:26:21 +1100999 active = 1;
1000 linenum = 0;
1001 while (fgets(line, sizeof(line), f)) {
1002 /* Update line number counter. */
1003 linenum++;
1004 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1005 bad_options++;
1006 }
1007 fclose(f);
1008 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001009 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001010 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001011 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012}
1013
Damien Miller5428f641999-11-25 11:54:57 +11001014/*
1015 * Initializes options to special values that indicate that they have not yet
1016 * been set. Read_config_file will only set options with this value. Options
1017 * are processed in the following order: command line, user config file,
1018 * system config file. Last, fill_default_options is called.
1019 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Damien Miller4af51302000-04-16 11:18:38 +10001021void
Damien Miller95def091999-11-25 00:26:21 +11001022initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023{
Damien Miller95def091999-11-25 00:26:21 +11001024 memset(options, 'X', sizeof(*options));
1025 options->forward_agent = -1;
1026 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001027 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001028 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001029 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->gateway_ports = -1;
1031 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001032 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001033 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001034 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001035 options->gss_authentication = -1;
1036 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001037 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001038 options->kbd_interactive_authentication = -1;
1039 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001040 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001041 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001042 options->batch_mode = -1;
1043 options->check_host_ip = -1;
1044 options->strict_host_key_checking = -1;
1045 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001046 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001047 options->compression_level = -1;
1048 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001049 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001050 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001051 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001052 options->number_of_password_prompts = -1;
1053 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001054 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001055 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001056 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001057 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001058 options->num_identity_files = 0;
1059 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001060 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001061 options->proxy_command = NULL;
1062 options->user = NULL;
1063 options->escape_char = -1;
1064 options->system_hostfile = NULL;
1065 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001066 options->system_hostfile2 = NULL;
1067 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001068 options->num_local_forwards = 0;
1069 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001070 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001071 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001072 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001073 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001074 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001075 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001076 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001077 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001078 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001079 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001080 options->server_alive_interval = -1;
1081 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001082 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001083 options->control_path = NULL;
1084 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001085 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001086 options->tun_open = -1;
1087 options->tun_local = -1;
1088 options->tun_remote = -1;
1089 options->local_command = NULL;
1090 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001091 options->use_roaming = -1;
Darren Tucker34e314d2010-01-08 17:03:46 +11001092 options->rdomain = -1;
Damien Miller10288242008-06-30 00:04:03 +10001093 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001094 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095}
1096
Damien Miller5428f641999-11-25 11:54:57 +11001097/*
1098 * Called after processing other sources of option data, this fills those
1099 * options for which no value has been specified with their default values.
1100 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller4af51302000-04-16 11:18:38 +10001102void
Damien Miller95def091999-11-25 00:26:21 +11001103fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001105 int len;
1106
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001108 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001109 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001110 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001111 if (options->forward_x11_trusted == -1)
1112 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001113 if (options->exit_on_forward_failure == -1)
1114 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001115 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001116 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001117 if (options->gateway_ports == -1)
1118 options->gateway_ports = 0;
1119 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001120 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001121 if (options->rsa_authentication == -1)
1122 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001123 if (options->pubkey_authentication == -1)
1124 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001125 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001126 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001127 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001128 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001129 if (options->gss_deleg_creds == -1)
1130 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001131 if (options->password_authentication == -1)
1132 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001133 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001134 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001135 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001136 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001137 if (options->hostbased_authentication == -1)
1138 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001139 if (options->batch_mode == -1)
1140 options->batch_mode = 0;
1141 if (options->check_host_ip == -1)
1142 options->check_host_ip = 1;
1143 if (options->strict_host_key_checking == -1)
1144 options->strict_host_key_checking = 2; /* 2 is default */
1145 if (options->compression == -1)
1146 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001147 if (options->tcp_keep_alive == -1)
1148 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001149 if (options->compression_level == -1)
1150 options->compression_level = 6;
1151 if (options->port == -1)
1152 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001153 if (options->address_family == -1)
1154 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001155 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001156 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001157 if (options->number_of_password_prompts == -1)
1158 options->number_of_password_prompts = 3;
1159 /* Selected in ssh_login(). */
1160 if (options->cipher == -1)
1161 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001162 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001163 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001164 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001165 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001166 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001167 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001169 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001170 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001171 xmalloc(len);
1172 snprintf(options->identity_files[options->num_identity_files++],
1173 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001174 }
1175 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001176 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1177 options->identity_files[options->num_identity_files] =
1178 xmalloc(len);
1179 snprintf(options->identity_files[options->num_identity_files++],
1180 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1181
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001182 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001183 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001184 xmalloc(len);
1185 snprintf(options->identity_files[options->num_identity_files++],
1186 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001187 }
Damien Millereba71ba2000-04-29 23:57:08 +10001188 }
Damien Miller95def091999-11-25 00:26:21 +11001189 if (options->escape_char == -1)
1190 options->escape_char = '~';
1191 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001192 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001193 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001194 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001195 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001196 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001197 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001198 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001199 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001200 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001201 if (options->clear_forwardings == 1)
1202 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001203 if (options->no_host_authentication_for_localhost == - 1)
1204 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001205 if (options->identities_only == -1)
1206 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001207 if (options->enable_ssh_keysign == -1)
1208 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001209 if (options->rekey_limit == -1)
1210 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001211 if (options->verify_host_key_dns == -1)
1212 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001213 if (options->server_alive_interval == -1)
1214 options->server_alive_interval = 0;
1215 if (options->server_alive_count_max == -1)
1216 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001217 if (options->control_master == -1)
1218 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001219 if (options->hash_known_hosts == -1)
1220 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001221 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001222 options->tun_open = SSH_TUNMODE_NO;
1223 if (options->tun_local == -1)
1224 options->tun_local = SSH_TUNID_ANY;
1225 if (options->tun_remote == -1)
1226 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001227 if (options->permit_local_command == -1)
1228 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001229 if (options->use_roaming == -1)
1230 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001231 if (options->visual_host_key == -1)
1232 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001233 if (options->zero_knowledge_password_authentication == -1)
1234 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001235 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001236 /* options->proxy_command should not be set by default */
1237 /* options->user will be set in the main program if appropriate */
1238 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001239 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001240 /* options->preferred_authentications will be set in ssh */
Darren Tucker34e314d2010-01-08 17:03:46 +11001241 /* options->rdomain should not be set by default */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001243
1244/*
1245 * parse_forward
1246 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001247 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001248 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001249 * dynamicfwd == 1
1250 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001251 * returns number of arguments parsed or zero on error
1252 */
1253int
Damien Miller4bf648f2009-02-14 16:28:21 +11001254parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001255{
1256 int i;
1257 char *p, *cp, *fwdarg[4];
1258
1259 memset(fwd, '\0', sizeof(*fwd));
1260
1261 cp = p = xstrdup(fwdspec);
1262
1263 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001264 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001265 cp++;
1266
1267 for (i = 0; i < 4; ++i)
1268 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1269 break;
1270
Damien Millerf4b39532008-11-03 19:28:21 +11001271 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001272 if (cp != NULL)
1273 i = 0; /* failure */
1274
1275 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001276 case 1:
1277 fwd->listen_host = NULL;
1278 fwd->listen_port = a2port(fwdarg[0]);
1279 fwd->connect_host = xstrdup("socks");
1280 break;
1281
1282 case 2:
1283 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1284 fwd->listen_port = a2port(fwdarg[1]);
1285 fwd->connect_host = xstrdup("socks");
1286 break;
1287
Damien Millerf91ee4c2005-03-01 21:24:33 +11001288 case 3:
1289 fwd->listen_host = NULL;
1290 fwd->listen_port = a2port(fwdarg[0]);
1291 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1292 fwd->connect_port = a2port(fwdarg[2]);
1293 break;
1294
1295 case 4:
1296 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1297 fwd->listen_port = a2port(fwdarg[1]);
1298 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1299 fwd->connect_port = a2port(fwdarg[3]);
1300 break;
1301 default:
1302 i = 0; /* failure */
1303 }
1304
1305 xfree(p);
1306
Damien Millera699d952008-11-03 19:27:34 +11001307 if (dynamicfwd) {
1308 if (!(i == 1 || i == 2))
1309 goto fail_free;
1310 } else {
1311 if (!(i == 3 || i == 4))
1312 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001313 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001314 goto fail_free;
1315 }
1316
Damien Miller4bf648f2009-02-14 16:28:21 +11001317 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001318 goto fail_free;
1319
1320 if (fwd->connect_host != NULL &&
1321 strlen(fwd->connect_host) >= NI_MAXHOST)
1322 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001323 if (fwd->listen_host != NULL &&
1324 strlen(fwd->listen_host) >= NI_MAXHOST)
1325 goto fail_free;
1326
Damien Millerf91ee4c2005-03-01 21:24:33 +11001327
1328 return (i);
1329
1330 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001331 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001332 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001333 fwd->connect_host = NULL;
1334 }
1335 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001336 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001337 fwd->listen_host = NULL;
1338 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001339 return (0);
1340}