blob: 586422930cd1a10476e0750711041953f218882f [file] [log] [blame]
Damien Millereb8b60e2010-08-31 22:41:14 +10001/* $OpenBSD: readconf.c,v 1.188 2010/08/31 11:54:45 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 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,
Damien Miller1ab6a512010-06-26 10:02:24 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
114 oGatewayPorts, 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,
Damien Miller7ea845e2010-02-12 09:21:02 +1100126 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
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 Millere11e1ea2010-08-03 16:04:46 +1000131 oSendEnv, oControlPath, oControlMaster, oControlPersist,
132 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100133 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100134 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
135 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136} OpCodes;
137
138/* Textual representations of the tokens. */
139
Damien Miller95def091999-11-25 00:26:21 +1100140static struct {
141 const char *name;
142 OpCodes opcode;
143} keywords[] = {
144 { "forwardagent", oForwardAgent },
145 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000146 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000147 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000148 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000149 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100150 { "gatewayports", oGatewayPorts },
151 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000152 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100153 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100154 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
155 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000158 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000159 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000160 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000161 { "challengeresponseauthentication", oChallengeResponseAuthentication },
162 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
163 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000164 { "kerberosauthentication", oUnsupported },
165 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000166 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000167#if defined(GSSAPI)
168 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000169 { "gssapidelegatecredentials", oGssDelegateCreds },
170#else
171 { "gssapiauthentication", oUnsupported },
172 { "gssapidelegatecredentials", oUnsupported },
173#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000174 { "fallbacktorsh", oDeprecated },
175 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100176 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100177 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100178 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000180 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100181 { "proxycommand", oProxyCommand },
182 { "port", oPort },
183 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000184 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000185 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000186 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100187 { "remoteforward", oRemoteForward },
188 { "localforward", oLocalForward },
189 { "user", oUser },
190 { "host", oHost },
191 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100192 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100193 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
194 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000195 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100196 { "connectionattempts", oConnectionAttempts },
197 { "batchmode", oBatchMode },
198 { "checkhostip", oCheckHostIP },
199 { "stricthostkeychecking", oStrictHostKeyChecking },
200 { "compression", oCompression },
201 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100202 { "tcpkeepalive", oTCPKeepAlive },
203 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100204 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100205 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000206 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000207 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000208 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000209 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100210#ifdef ENABLE_PKCS11
211 { "smartcarddevice", oPKCS11Provider },
212 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000213#else
214 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100215 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000216#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100217 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000218 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000219 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100220 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000221 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000222 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000223 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100224 { "serveraliveinterval", oServerAliveInterval },
225 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000226 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000227 { "controlpath", oControlPath },
228 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000229 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100230 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100231 { "tunnel", oTunnel },
232 { "tunneldevice", oTunnelDevice },
233 { "localcommand", oLocalCommand },
234 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000235 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000236 { "useroaming", oUseRoaming },
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 Miller232cfb12010-06-26 09:50:30 +1000261 options->local_forwards = xrealloc(options->local_forwards,
262 options->num_local_forwards + 1,
263 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100264 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265
Damien Miller1a0442f2008-11-05 16:30:06 +1100266 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100268 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100269 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Miller5428f641999-11-25 11:54:57 +1100272/*
273 * Adds a remote TCP/IP port forward to options. Never returns if there is
274 * an error.
275 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller4af51302000-04-16 11:18:38 +1000277void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
Damien Miller95def091999-11-25 00:26:21 +1100280 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000281
282 options->remote_forwards = xrealloc(options->remote_forwards,
283 options->num_remote_forwards + 1,
284 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100285 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286
Damien Miller1a0442f2008-11-05 16:30:06 +1100287 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100289 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000291 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000294static void
295clear_forwardings(Options *options)
296{
297 int i;
298
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100300 if (options->local_forwards[i].listen_host != NULL)
301 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302 xfree(options->local_forwards[i].connect_host);
303 }
Damien Miller232cfb12010-06-26 09:50:30 +1000304 if (options->num_local_forwards > 0) {
305 xfree(options->local_forwards);
306 options->local_forwards = NULL;
307 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000308 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100309 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100310 if (options->remote_forwards[i].listen_host != NULL)
311 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100312 xfree(options->remote_forwards[i].connect_host);
313 }
Damien Miller232cfb12010-06-26 09:50:30 +1000314 if (options->num_remote_forwards > 0) {
315 xfree(options->remote_forwards);
316 options->remote_forwards = NULL;
317 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000318 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100319 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000320}
321
Damien Miller5428f641999-11-25 11:54:57 +1100322/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000323 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100324 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Damien Miller4af51302000-04-16 11:18:38 +1000326static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100327parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000329 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller95def091999-11-25 00:26:21 +1100331 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100332 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100333 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000335 error("%s: line %d: Bad configuration option: %s",
336 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100337 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
339
Damien Miller5428f641999-11-25 11:54:57 +1100340/*
341 * Processes a single option line as used in the configuration files. This
342 * only sets those values that have not already been set.
343 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100344#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller2ccf6611999-11-15 15:25:10 +1100346int
347process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100348 char *line, const char *filename, int linenum,
349 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100351 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100352 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100353 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100354 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100355 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100356 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Millerc652cac2003-05-14 13:40:54 +1000358 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100359 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000360 if (strchr(WHITESPACE, line[len]) == NULL)
361 break;
362 line[len] = '\0';
363 }
364
Damien Millerbe484b52000-07-15 14:14:16 +1000365 s = line;
366 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100367 if ((keyword = strdelim(&s)) == NULL)
368 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000369 /* Ignore leading whitespace. */
370 if (*keyword == '\0')
371 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000372 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100373 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Damien Miller37023962000-07-11 17:31:38 +1000375 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376
Damien Miller95def091999-11-25 00:26:21 +1100377 switch (opcode) {
378 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100379 /* don't panic, but count bad options */
380 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100381 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000382 case oConnectTimeout:
383 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100384parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000385 arg = strdelim(&s);
386 if (!arg || *arg == '\0')
387 fatal("%s line %d: missing time value.",
388 filename, linenum);
389 if ((value = convtime(arg)) == -1)
390 fatal("%s line %d: invalid time value.",
391 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100392 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000393 *intptr = value;
394 break;
395
Damien Miller95def091999-11-25 00:26:21 +1100396 case oForwardAgent:
397 intptr = &options->forward_agent;
398parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000399 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000400 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100401 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
402 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000403 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100404 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000405 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100406 value = 0;
407 else
408 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
409 if (*activep && *intptr == -1)
410 *intptr = value;
411 break;
412
413 case oForwardX11:
414 intptr = &options->forward_x11;
415 goto parse_flag;
416
Darren Tucker0a118da2003-10-15 15:54:32 +1000417 case oForwardX11Trusted:
418 intptr = &options->forward_x11_trusted;
419 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000420
421 case oForwardX11Timeout:
422 intptr = &options->forward_x11_timeout;
423 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 case oGatewayPorts:
426 intptr = &options->gateway_ports;
427 goto parse_flag;
428
Darren Tuckere7d4b192006-07-12 22:17:10 +1000429 case oExitOnForwardFailure:
430 intptr = &options->exit_on_forward_failure;
431 goto parse_flag;
432
Damien Miller95def091999-11-25 00:26:21 +1100433 case oUsePrivilegedPort:
434 intptr = &options->use_privileged_port;
435 goto parse_flag;
436
Damien Miller95def091999-11-25 00:26:21 +1100437 case oPasswordAuthentication:
438 intptr = &options->password_authentication;
439 goto parse_flag;
440
Damien Miller01ed2272008-11-05 16:20:46 +1100441 case oZeroKnowledgePasswordAuthentication:
442 intptr = &options->zero_knowledge_password_authentication;
443 goto parse_flag;
444
Damien Miller874d77b2000-10-14 16:23:11 +1100445 case oKbdInteractiveAuthentication:
446 intptr = &options->kbd_interactive_authentication;
447 goto parse_flag;
448
449 case oKbdInteractiveDevices:
450 charptr = &options->kbd_interactive_devices;
451 goto parse_string;
452
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 case oPubkeyAuthentication:
454 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000455 goto parse_flag;
456
Damien Miller95def091999-11-25 00:26:21 +1100457 case oRSAAuthentication:
458 intptr = &options->rsa_authentication;
459 goto parse_flag;
460
461 case oRhostsRSAAuthentication:
462 intptr = &options->rhosts_rsa_authentication;
463 goto parse_flag;
464
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000465 case oHostbasedAuthentication:
466 intptr = &options->hostbased_authentication;
467 goto parse_flag;
468
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000469 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000470 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100471 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000472
Darren Tucker0efd1552003-08-26 11:49:55 +1000473 case oGssAuthentication:
474 intptr = &options->gss_authentication;
475 goto parse_flag;
476
477 case oGssDelegateCreds:
478 intptr = &options->gss_deleg_creds;
479 goto parse_flag;
480
Damien Miller95def091999-11-25 00:26:21 +1100481 case oBatchMode:
482 intptr = &options->batch_mode;
483 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Damien Miller95def091999-11-25 00:26:21 +1100485 case oCheckHostIP:
486 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000487 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488
Damien Miller37876e92003-05-15 10:19:46 +1000489 case oVerifyHostKeyDNS:
490 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100491 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000492
Damien Miller95def091999-11-25 00:26:21 +1100493 case oStrictHostKeyChecking:
494 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100495parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000496 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000497 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000498 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100499 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100500 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000501 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100502 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000503 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100504 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000505 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100506 value = 2;
507 else
508 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
509 if (*activep && *intptr == -1)
510 *intptr = value;
511 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 case oCompression:
514 intptr = &options->compression;
515 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Damien Miller12c150e2003-12-17 16:31:10 +1100517 case oTCPKeepAlive:
518 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100519 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000521 case oNoHostAuthenticationForLocalhost:
522 intptr = &options->no_host_authentication_for_localhost;
523 goto parse_flag;
524
Damien Miller95def091999-11-25 00:26:21 +1100525 case oNumberOfPasswordPrompts:
526 intptr = &options->number_of_password_prompts;
527 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 case oCompressionLevel:
530 intptr = &options->compression_level;
531 goto parse_int;
532
Damien Millera5539d22003-04-09 20:50:06 +1000533 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000534 arg = strdelim(&s);
535 if (!arg || *arg == '\0')
536 fatal("%.200s line %d: Missing argument.", filename, linenum);
537 if (arg[0] < '0' || arg[0] > '9')
538 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100539 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000540 if (arg == endofnumber)
541 fatal("%.200s line %d: Bad number.", filename, linenum);
542 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100543 case '\0':
544 scale = 1;
545 break;
Damien Millera5539d22003-04-09 20:50:06 +1000546 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100547 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000548 break;
549 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100550 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000551 break;
552 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100553 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000554 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100555 default:
556 fatal("%.200s line %d: Invalid RekeyLimit suffix",
557 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000558 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100559 val64 *= scale;
560 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100561 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100562 fatal("%.200s line %d: RekeyLimit too large",
563 filename, linenum);
564 if (val64 < 16)
565 fatal("%.200s line %d: RekeyLimit too small",
566 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100567 if (*activep && options->rekey_limit == -1)
568 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000569 break;
570
Damien Miller95def091999-11-25 00:26:21 +1100571 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000572 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000573 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100574 fatal("%.200s line %d: Missing argument.", filename, linenum);
575 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100576 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000577 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100578 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100579 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100580 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000581 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000582 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100583 }
584 break;
585
Damien Millerd3a18572000-06-07 19:55:44 +1000586 case oXAuthLocation:
587 charptr=&options->xauth_location;
588 goto parse_string;
589
Damien Miller95def091999-11-25 00:26:21 +1100590 case oUser:
591 charptr = &options->user;
592parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000593 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000594 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100595 fatal("%.200s line %d: Missing argument.", filename, linenum);
596 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000597 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100598 break;
599
600 case oGlobalKnownHostsFile:
601 charptr = &options->system_hostfile;
602 goto parse_string;
603
604 case oUserKnownHostsFile:
605 charptr = &options->user_hostfile;
606 goto parse_string;
607
Damien Millereba71ba2000-04-29 23:57:08 +1000608 case oGlobalKnownHostsFile2:
609 charptr = &options->system_hostfile2;
610 goto parse_string;
611
612 case oUserKnownHostsFile2:
613 charptr = &options->user_hostfile2;
614 goto parse_string;
615
Damien Miller95def091999-11-25 00:26:21 +1100616 case oHostName:
617 charptr = &options->hostname;
618 goto parse_string;
619
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000620 case oHostKeyAlias:
621 charptr = &options->host_key_alias;
622 goto parse_string;
623
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000624 case oPreferredAuthentications:
625 charptr = &options->preferred_authentications;
626 goto parse_string;
627
Ben Lindstrome0f88042001-04-30 13:06:24 +0000628 case oBindAddress:
629 charptr = &options->bind_address;
630 goto parse_string;
631
Damien Miller7ea845e2010-02-12 09:21:02 +1100632 case oPKCS11Provider:
633 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000634 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000635
Damien Miller95def091999-11-25 00:26:21 +1100636 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100637 charptr = &options->proxy_command;
638parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000639 if (s == NULL)
640 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100641 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100642 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100643 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100644 return 0;
645
646 case oPort:
647 intptr = &options->port;
648parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000649 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000650 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100651 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000652 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100653 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100654
655 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000656 value = strtol(arg, &endofnumber, 0);
657 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100658 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100659 if (*activep && *intptr == -1)
660 *intptr = value;
661 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 case oConnectionAttempts:
664 intptr = &options->connection_attempts;
665 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100666
Damien Miller95def091999-11-25 00:26:21 +1100667 case oCipher:
668 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000669 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000670 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000671 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000672 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100673 if (value == -1)
674 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100675 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100676 if (*activep && *intptr == -1)
677 *intptr = value;
678 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller78928792000-04-12 20:17:38 +1000680 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000681 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000682 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000683 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000684 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000685 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100686 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000687 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000688 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000689 break;
690
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000691 case oMacs:
692 arg = strdelim(&s);
693 if (!arg || *arg == '\0')
694 fatal("%.200s line %d: Missing argument.", filename, linenum);
695 if (!mac_valid(arg))
696 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100697 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000698 if (*activep && options->macs == NULL)
699 options->macs = xstrdup(arg);
700 break;
701
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000702 case oHostKeyAlgorithms:
703 arg = strdelim(&s);
704 if (!arg || *arg == '\0')
705 fatal("%.200s line %d: Missing argument.", filename, linenum);
706 if (!key_names_valid2(arg))
707 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000709 if (*activep && options->hostkeyalgorithms == NULL)
710 options->hostkeyalgorithms = xstrdup(arg);
711 break;
712
Damien Miller78928792000-04-12 20:17:38 +1000713 case oProtocol:
714 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000715 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000716 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000717 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000718 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000719 if (value == SSH_PROTO_UNKNOWN)
720 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100721 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000722 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
723 *intptr = value;
724 break;
725
Damien Miller95def091999-11-25 00:26:21 +1100726 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100727 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000728 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000729 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100730 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000731 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100732 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100733 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
734 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100735 break;
736
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000737 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100738 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100739 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000740 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100741 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000742 fatal("%.200s line %d: Missing port argument.",
743 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100744
Damien Millera699d952008-11-03 19:27:34 +1100745 if (opcode == oLocalForward ||
746 opcode == oRemoteForward) {
747 arg2 = strdelim(&s);
748 if (arg2 == NULL || *arg2 == '\0')
749 fatal("%.200s line %d: Missing target argument.",
750 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100751
Damien Millera699d952008-11-03 19:27:34 +1100752 /* construct a string for parse_forward */
753 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
754 } else if (opcode == oDynamicForward) {
755 strlcpy(fwdarg, arg, sizeof(fwdarg));
756 }
757
758 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100759 opcode == oDynamicForward ? 1 : 0,
760 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000761 fatal("%.200s line %d: Bad forwarding specification.",
762 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100763
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000764 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100765 if (opcode == oLocalForward ||
766 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100767 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000768 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100769 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000770 }
Damien Miller95def091999-11-25 00:26:21 +1100771 break;
772
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000773 case oClearAllForwardings:
774 intptr = &options->clear_forwardings;
775 goto parse_flag;
776
Damien Miller95def091999-11-25 00:26:21 +1100777 case oHost:
778 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000779 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000780 if (match_pattern(host, arg)) {
781 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100782 *activep = 1;
783 break;
784 }
Damien Millerbe484b52000-07-15 14:14:16 +1000785 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100786 return 0;
787
788 case oEscapeChar:
789 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000790 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000791 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100792 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000793 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000794 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
795 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000796 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000797 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000798 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000799 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100800 else {
801 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100802 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100803 /* NOTREACHED */
804 value = 0; /* Avoid compiler warning. */
805 }
806 if (*activep && *intptr == -1)
807 *intptr = value;
808 break;
809
Damien Miller20a8f972003-05-18 20:50:30 +1000810 case oAddressFamily:
811 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000812 if (!arg || *arg == '\0')
813 fatal("%s line %d: missing address family.",
814 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000815 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000816 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000817 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000818 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000819 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000820 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000821 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000822 else
823 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000824 if (*activep && *intptr == -1)
825 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000826 break;
827
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000828 case oEnableSSHKeysign:
829 intptr = &options->enable_ssh_keysign;
830 goto parse_flag;
831
Damien Millerbd394c32004-03-08 23:12:36 +1100832 case oIdentitiesOnly:
833 intptr = &options->identities_only;
834 goto parse_flag;
835
Damien Miller509b0102003-12-17 16:33:10 +1100836 case oServerAliveInterval:
837 intptr = &options->server_alive_interval;
838 goto parse_time;
839
840 case oServerAliveCountMax:
841 intptr = &options->server_alive_count_max;
842 goto parse_int;
843
Darren Tucker46bc0752004-05-02 22:11:30 +1000844 case oSendEnv:
845 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
846 if (strchr(arg, '=') != NULL)
847 fatal("%s line %d: Invalid environment name.",
848 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100849 if (!*activep)
850 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000851 if (options->num_send_env >= MAX_SEND_ENV)
852 fatal("%s line %d: too many send env.",
853 filename, linenum);
854 options->send_env[options->num_send_env++] =
855 xstrdup(arg);
856 }
857 break;
858
Damien Miller0e220db2004-06-15 10:34:08 +1000859 case oControlPath:
860 charptr = &options->control_path;
861 goto parse_string;
862
863 case oControlMaster:
864 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000865 arg = strdelim(&s);
866 if (!arg || *arg == '\0')
867 fatal("%.200s line %d: Missing ControlMaster argument.",
868 filename, linenum);
869 value = 0; /* To avoid compiler warning... */
870 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
871 value = SSHCTL_MASTER_YES;
872 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
873 value = SSHCTL_MASTER_NO;
874 else if (strcmp(arg, "auto") == 0)
875 value = SSHCTL_MASTER_AUTO;
876 else if (strcmp(arg, "ask") == 0)
877 value = SSHCTL_MASTER_ASK;
878 else if (strcmp(arg, "autoask") == 0)
879 value = SSHCTL_MASTER_AUTO_ASK;
880 else
881 fatal("%.200s line %d: Bad ControlMaster argument.",
882 filename, linenum);
883 if (*activep && *intptr == -1)
884 *intptr = value;
885 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000886
Damien Millere11e1ea2010-08-03 16:04:46 +1000887 case oControlPersist:
888 /* no/false/yes/true, or a time spec */
889 intptr = &options->control_persist;
890 arg = strdelim(&s);
891 if (!arg || *arg == '\0')
892 fatal("%.200s line %d: Missing ControlPersist"
893 " argument.", filename, linenum);
894 value = 0;
895 value2 = 0; /* timeout */
896 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
897 value = 0;
898 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
899 value = 1;
900 else if ((value2 = convtime(arg)) >= 0)
901 value = 1;
902 else
903 fatal("%.200s line %d: Bad ControlPersist argument.",
904 filename, linenum);
905 if (*activep && *intptr == -1) {
906 *intptr = value;
907 options->control_persist_timeout = value2;
908 }
909 break;
910
Damien Millere1776152005-03-01 21:47:37 +1100911 case oHashKnownHosts:
912 intptr = &options->hash_known_hosts;
913 goto parse_flag;
914
Damien Millerd27b9472005-12-13 19:29:02 +1100915 case oTunnel:
916 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100917 arg = strdelim(&s);
918 if (!arg || *arg == '\0')
919 fatal("%s line %d: Missing yes/point-to-point/"
920 "ethernet/no argument.", filename, linenum);
921 value = 0; /* silence compiler */
922 if (strcasecmp(arg, "ethernet") == 0)
923 value = SSH_TUNMODE_ETHERNET;
924 else if (strcasecmp(arg, "point-to-point") == 0)
925 value = SSH_TUNMODE_POINTOPOINT;
926 else if (strcasecmp(arg, "yes") == 0)
927 value = SSH_TUNMODE_DEFAULT;
928 else if (strcasecmp(arg, "no") == 0)
929 value = SSH_TUNMODE_NO;
930 else
931 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
932 "no argument: %s", filename, linenum, arg);
933 if (*activep)
934 *intptr = value;
935 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100936
937 case oTunnelDevice:
938 arg = strdelim(&s);
939 if (!arg || *arg == '\0')
940 fatal("%.200s line %d: Missing argument.", filename, linenum);
941 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100942 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100943 fatal("%.200s line %d: Bad tun device.", filename, linenum);
944 if (*activep) {
945 options->tun_local = value;
946 options->tun_remote = value2;
947 }
948 break;
949
950 case oLocalCommand:
951 charptr = &options->local_command;
952 goto parse_command;
953
954 case oPermitLocalCommand:
955 intptr = &options->permit_local_command;
956 goto parse_flag;
957
Damien Miller10288242008-06-30 00:04:03 +1000958 case oVisualHostKey:
959 intptr = &options->visual_host_key;
960 goto parse_flag;
961
Darren Tucker71e4d542009-07-06 07:12:27 +1000962 case oUseRoaming:
963 intptr = &options->use_roaming;
964 goto parse_flag;
965
Ben Lindstrom4daea862002-06-09 20:04:02 +0000966 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000967 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000968 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000969 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000970
Damien Millerf9b3feb2003-05-16 11:38:32 +1000971 case oUnsupported:
972 error("%s line %d: Unsupported option \"%s\"",
973 filename, linenum, keyword);
974 return 0;
975
Damien Miller95def091999-11-25 00:26:21 +1100976 default:
977 fatal("process_config_line: Unimplemented opcode %d", opcode);
978 }
979
980 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000981 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000982 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000983 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000984 }
Damien Miller95def091999-11-25 00:26:21 +1100985 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
987
988
Damien Miller5428f641999-11-25 11:54:57 +1100989/*
990 * Reads the config file and modifies the options accordingly. Options
991 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000992 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100993 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000995int
Darren Tuckerfc959702004-07-17 16:12:08 +1000996read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000997 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998{
Damien Miller95def091999-11-25 00:26:21 +1100999 FILE *f;
1000 char line[1024];
1001 int active, linenum;
1002 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Miller57a44762004-04-20 20:11:57 +10001004 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001005 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Damien Miller57a44762004-04-20 20:11:57 +10001007 if (checkperm) {
1008 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001009
Damien Miller33793852004-06-15 10:27:55 +10001010 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001011 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001012 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001013 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001014 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001015 }
1016
Damien Miller95def091999-11-25 00:26:21 +11001017 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller5428f641999-11-25 11:54:57 +11001019 /*
1020 * Mark that we are now processing the options. This flag is turned
1021 * on/off by Host specifications.
1022 */
Damien Miller95def091999-11-25 00:26:21 +11001023 active = 1;
1024 linenum = 0;
1025 while (fgets(line, sizeof(line), f)) {
1026 /* Update line number counter. */
1027 linenum++;
1028 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1029 bad_options++;
1030 }
1031 fclose(f);
1032 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001033 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001034 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001035 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036}
1037
Damien Miller5428f641999-11-25 11:54:57 +11001038/*
1039 * Initializes options to special values that indicate that they have not yet
1040 * been set. Read_config_file will only set options with this value. Options
1041 * are processed in the following order: command line, user config file,
1042 * system config file. Last, fill_default_options is called.
1043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
Damien Miller4af51302000-04-16 11:18:38 +10001045void
Damien Miller95def091999-11-25 00:26:21 +11001046initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047{
Damien Miller95def091999-11-25 00:26:21 +11001048 memset(options, 'X', sizeof(*options));
1049 options->forward_agent = -1;
1050 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001051 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001052 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001053 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001054 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001055 options->gateway_ports = -1;
1056 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001057 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001058 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001059 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001060 options->gss_authentication = -1;
1061 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001062 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001063 options->kbd_interactive_authentication = -1;
1064 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001065 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001066 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001067 options->batch_mode = -1;
1068 options->check_host_ip = -1;
1069 options->strict_host_key_checking = -1;
1070 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001071 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001072 options->compression_level = -1;
1073 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001074 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001075 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001076 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001077 options->number_of_password_prompts = -1;
1078 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001079 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001080 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001081 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001082 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001083 options->num_identity_files = 0;
1084 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001085 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001086 options->proxy_command = NULL;
1087 options->user = NULL;
1088 options->escape_char = -1;
1089 options->system_hostfile = NULL;
1090 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001091 options->system_hostfile2 = NULL;
1092 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001093 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001094 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001095 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001096 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001097 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001098 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001099 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001100 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001101 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001102 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001103 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001104 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001105 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001106 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001107 options->server_alive_interval = -1;
1108 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001109 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001110 options->control_path = NULL;
1111 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001112 options->control_persist = -1;
1113 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001114 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001115 options->tun_open = -1;
1116 options->tun_local = -1;
1117 options->tun_remote = -1;
1118 options->local_command = NULL;
1119 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001120 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001121 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001122 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123}
1124
Damien Miller5428f641999-11-25 11:54:57 +11001125/*
1126 * Called after processing other sources of option data, this fills those
1127 * options for which no value has been specified with their default values.
1128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
Damien Miller4af51302000-04-16 11:18:38 +10001130void
Damien Miller95def091999-11-25 00:26:21 +11001131fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001133 int len;
1134
Damien Miller95def091999-11-25 00:26:21 +11001135 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001136 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001137 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001138 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001139 if (options->forward_x11_trusted == -1)
1140 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001141 if (options->forward_x11_timeout == -1)
1142 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001143 if (options->exit_on_forward_failure == -1)
1144 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001145 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001146 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001147 if (options->gateway_ports == -1)
1148 options->gateway_ports = 0;
1149 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001150 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001151 if (options->rsa_authentication == -1)
1152 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001153 if (options->pubkey_authentication == -1)
1154 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001155 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001156 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001157 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001158 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001159 if (options->gss_deleg_creds == -1)
1160 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001161 if (options->password_authentication == -1)
1162 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001163 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001164 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001165 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001166 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001167 if (options->hostbased_authentication == -1)
1168 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001169 if (options->batch_mode == -1)
1170 options->batch_mode = 0;
1171 if (options->check_host_ip == -1)
1172 options->check_host_ip = 1;
1173 if (options->strict_host_key_checking == -1)
1174 options->strict_host_key_checking = 2; /* 2 is default */
1175 if (options->compression == -1)
1176 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001177 if (options->tcp_keep_alive == -1)
1178 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001179 if (options->compression_level == -1)
1180 options->compression_level = 6;
1181 if (options->port == -1)
1182 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001183 if (options->address_family == -1)
1184 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001185 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001186 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001187 if (options->number_of_password_prompts == -1)
1188 options->number_of_password_prompts = 3;
1189 /* Selected in ssh_login(). */
1190 if (options->cipher == -1)
1191 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001192 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001193 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001194 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001195 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001196 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001197 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001198 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001199 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001200 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001201 xmalloc(len);
1202 snprintf(options->identity_files[options->num_identity_files++],
1203 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001204 }
1205 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001206 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1207 options->identity_files[options->num_identity_files] =
1208 xmalloc(len);
1209 snprintf(options->identity_files[options->num_identity_files++],
1210 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1211
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001212 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001213 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001214 xmalloc(len);
1215 snprintf(options->identity_files[options->num_identity_files++],
1216 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001217#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001218 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1219 options->identity_files[options->num_identity_files] =
1220 xmalloc(len);
1221 snprintf(options->identity_files[options->num_identity_files++],
1222 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001223#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001224 }
Damien Millereba71ba2000-04-29 23:57:08 +10001225 }
Damien Miller95def091999-11-25 00:26:21 +11001226 if (options->escape_char == -1)
1227 options->escape_char = '~';
1228 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001229 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001230 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001231 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001232 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001233 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001234 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001235 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001236 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001237 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001238 if (options->clear_forwardings == 1)
1239 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001240 if (options->no_host_authentication_for_localhost == - 1)
1241 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001242 if (options->identities_only == -1)
1243 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001244 if (options->enable_ssh_keysign == -1)
1245 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001246 if (options->rekey_limit == -1)
1247 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001248 if (options->verify_host_key_dns == -1)
1249 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001250 if (options->server_alive_interval == -1)
1251 options->server_alive_interval = 0;
1252 if (options->server_alive_count_max == -1)
1253 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001254 if (options->control_master == -1)
1255 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001256 if (options->control_persist == -1) {
1257 options->control_persist = 0;
1258 options->control_persist_timeout = 0;
1259 }
Damien Millere1776152005-03-01 21:47:37 +11001260 if (options->hash_known_hosts == -1)
1261 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001262 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001263 options->tun_open = SSH_TUNMODE_NO;
1264 if (options->tun_local == -1)
1265 options->tun_local = SSH_TUNID_ANY;
1266 if (options->tun_remote == -1)
1267 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001268 if (options->permit_local_command == -1)
1269 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001270 if (options->use_roaming == -1)
1271 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001272 if (options->visual_host_key == -1)
1273 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001274 if (options->zero_knowledge_password_authentication == -1)
1275 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001276 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001277 /* options->proxy_command should not be set by default */
1278 /* options->user will be set in the main program if appropriate */
1279 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001280 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001281 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001283
1284/*
1285 * parse_forward
1286 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001287 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001288 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001289 * dynamicfwd == 1
1290 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001291 * returns number of arguments parsed or zero on error
1292 */
1293int
Damien Miller4bf648f2009-02-14 16:28:21 +11001294parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001295{
1296 int i;
1297 char *p, *cp, *fwdarg[4];
1298
1299 memset(fwd, '\0', sizeof(*fwd));
1300
1301 cp = p = xstrdup(fwdspec);
1302
1303 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001304 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001305 cp++;
1306
1307 for (i = 0; i < 4; ++i)
1308 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1309 break;
1310
Damien Millerf4b39532008-11-03 19:28:21 +11001311 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001312 if (cp != NULL)
1313 i = 0; /* failure */
1314
1315 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001316 case 1:
1317 fwd->listen_host = NULL;
1318 fwd->listen_port = a2port(fwdarg[0]);
1319 fwd->connect_host = xstrdup("socks");
1320 break;
1321
1322 case 2:
1323 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1324 fwd->listen_port = a2port(fwdarg[1]);
1325 fwd->connect_host = xstrdup("socks");
1326 break;
1327
Damien Millerf91ee4c2005-03-01 21:24:33 +11001328 case 3:
1329 fwd->listen_host = NULL;
1330 fwd->listen_port = a2port(fwdarg[0]);
1331 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1332 fwd->connect_port = a2port(fwdarg[2]);
1333 break;
1334
1335 case 4:
1336 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1337 fwd->listen_port = a2port(fwdarg[1]);
1338 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1339 fwd->connect_port = a2port(fwdarg[3]);
1340 break;
1341 default:
1342 i = 0; /* failure */
1343 }
1344
1345 xfree(p);
1346
Damien Millera699d952008-11-03 19:27:34 +11001347 if (dynamicfwd) {
1348 if (!(i == 1 || i == 2))
1349 goto fail_free;
1350 } else {
1351 if (!(i == 3 || i == 4))
1352 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001353 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001354 goto fail_free;
1355 }
1356
Damien Miller4bf648f2009-02-14 16:28:21 +11001357 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001358 goto fail_free;
1359
1360 if (fwd->connect_host != NULL &&
1361 strlen(fwd->connect_host) >= NI_MAXHOST)
1362 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001363 if (fwd->listen_host != NULL &&
1364 strlen(fwd->listen_host) >= NI_MAXHOST)
1365 goto fail_free;
1366
Damien Millerf91ee4c2005-03-01 21:24:33 +11001367
1368 return (i);
1369
1370 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001371 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001372 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001373 fwd->connect_host = NULL;
1374 }
1375 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001376 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001377 fwd->listen_host = NULL;
1378 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001379 return (0);
1380}