blob: aae9cef4b143d86026429307e62dd5e783bdfd0e [file] [log] [blame]
Damien Miller232cfb12010-06-26 09:50:30 +10001/* $OpenBSD: readconf.c,v 1.185 2010/06/25 07:14:46 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,
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,
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 Millere1776152005-03-01 21:47:37 +1100131 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100132 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100133 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
134 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 Miller7ea845e2010-02-12 09:21:02 +1100208#ifdef ENABLE_PKCS11
209 { "smartcarddevice", oPKCS11Provider },
210 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000211#else
212 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100213 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000214#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100215 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000216 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000217 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100218 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000219 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000220 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000221 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100222 { "serveraliveinterval", oServerAliveInterval },
223 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000224 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000225 { "controlpath", oControlPath },
226 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100227 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100228 { "tunnel", oTunnel },
229 { "tunneldevice", oTunnelDevice },
230 { "localcommand", oLocalCommand },
231 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000232 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000233 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100234#ifdef JPAKE
235 { "zeroknowledgepasswordauthentication",
236 oZeroKnowledgePasswordAuthentication },
237#else
238 { "zeroknowledgepasswordauthentication", oUnsupported },
239#endif
240
Ben Lindstrom65366a82001-12-06 16:32:47 +0000241 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100242};
243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Adds a local TCP/IP port forward to options. Never returns if there is an
246 * error.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller4af51302000-04-16 11:18:38 +1000249void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100250add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000253#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100254 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000256 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100257#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000258 options->local_forwards = xrealloc(options->local_forwards,
259 options->num_local_forwards + 1,
260 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100261 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100262
Damien Miller1a0442f2008-11-05 16:30:06 +1100263 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100265 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100266 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267}
268
Damien Miller5428f641999-11-25 11:54:57 +1100269/*
270 * Adds a remote TCP/IP port forward to options. Never returns if there is
271 * an error.
272 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller4af51302000-04-16 11:18:38 +1000274void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Damien Miller95def091999-11-25 00:26:21 +1100277 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000278
279 options->remote_forwards = xrealloc(options->remote_forwards,
280 options->num_remote_forwards + 1,
281 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100282 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283
Damien Miller1a0442f2008-11-05 16:30:06 +1100284 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100286 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000288 fwd->allocated_port = 0;
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 }
Damien Miller232cfb12010-06-26 09:50:30 +1000301 if (options->num_local_forwards > 0) {
302 xfree(options->local_forwards);
303 options->local_forwards = NULL;
304 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000305 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100306 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100307 if (options->remote_forwards[i].listen_host != NULL)
308 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100309 xfree(options->remote_forwards[i].connect_host);
310 }
Damien Miller232cfb12010-06-26 09:50:30 +1000311 if (options->num_remote_forwards > 0) {
312 xfree(options->remote_forwards);
313 options->remote_forwards = NULL;
314 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000315 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100316 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000317}
318
Damien Miller5428f641999-11-25 11:54:57 +1100319/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000320 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100321 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller4af51302000-04-16 11:18:38 +1000323static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100324parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000326 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller95def091999-11-25 00:26:21 +1100328 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100329 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100330 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000332 error("%s: line %d: Bad configuration option: %s",
333 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100334 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335}
336
Damien Miller5428f641999-11-25 11:54:57 +1100337/*
338 * Processes a single option line as used in the configuration files. This
339 * only sets those values that have not already been set.
340 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100341#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Miller2ccf6611999-11-15 15:25:10 +1100343int
344process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100345 char *line, const char *filename, int linenum,
346 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100348 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100349 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100350 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100351 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100352 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100353 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Millerc652cac2003-05-14 13:40:54 +1000355 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100356 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000357 if (strchr(WHITESPACE, line[len]) == NULL)
358 break;
359 line[len] = '\0';
360 }
361
Damien Millerbe484b52000-07-15 14:14:16 +1000362 s = line;
363 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100364 if ((keyword = strdelim(&s)) == NULL)
365 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000366 /* Ignore leading whitespace. */
367 if (*keyword == '\0')
368 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000369 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100370 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Damien Miller37023962000-07-11 17:31:38 +1000372 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Damien Miller95def091999-11-25 00:26:21 +1100374 switch (opcode) {
375 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100376 /* don't panic, but count bad options */
377 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100378 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000379 case oConnectTimeout:
380 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100381parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000382 arg = strdelim(&s);
383 if (!arg || *arg == '\0')
384 fatal("%s line %d: missing time value.",
385 filename, linenum);
386 if ((value = convtime(arg)) == -1)
387 fatal("%s line %d: invalid time value.",
388 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100389 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000390 *intptr = value;
391 break;
392
Damien Miller95def091999-11-25 00:26:21 +1100393 case oForwardAgent:
394 intptr = &options->forward_agent;
395parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000396 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000397 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100398 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
399 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000400 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100401 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000402 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100403 value = 0;
404 else
405 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
406 if (*activep && *intptr == -1)
407 *intptr = value;
408 break;
409
410 case oForwardX11:
411 intptr = &options->forward_x11;
412 goto parse_flag;
413
Darren Tucker0a118da2003-10-15 15:54:32 +1000414 case oForwardX11Trusted:
415 intptr = &options->forward_x11_trusted;
416 goto parse_flag;
417
Damien Miller95def091999-11-25 00:26:21 +1100418 case oGatewayPorts:
419 intptr = &options->gateway_ports;
420 goto parse_flag;
421
Darren Tuckere7d4b192006-07-12 22:17:10 +1000422 case oExitOnForwardFailure:
423 intptr = &options->exit_on_forward_failure;
424 goto parse_flag;
425
Damien Miller95def091999-11-25 00:26:21 +1100426 case oUsePrivilegedPort:
427 intptr = &options->use_privileged_port;
428 goto parse_flag;
429
Damien Miller95def091999-11-25 00:26:21 +1100430 case oPasswordAuthentication:
431 intptr = &options->password_authentication;
432 goto parse_flag;
433
Damien Miller01ed2272008-11-05 16:20:46 +1100434 case oZeroKnowledgePasswordAuthentication:
435 intptr = &options->zero_knowledge_password_authentication;
436 goto parse_flag;
437
Damien Miller874d77b2000-10-14 16:23:11 +1100438 case oKbdInteractiveAuthentication:
439 intptr = &options->kbd_interactive_authentication;
440 goto parse_flag;
441
442 case oKbdInteractiveDevices:
443 charptr = &options->kbd_interactive_devices;
444 goto parse_string;
445
Damien Miller0bc1bd82000-11-13 22:57:25 +1100446 case oPubkeyAuthentication:
447 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000448 goto parse_flag;
449
Damien Miller95def091999-11-25 00:26:21 +1100450 case oRSAAuthentication:
451 intptr = &options->rsa_authentication;
452 goto parse_flag;
453
454 case oRhostsRSAAuthentication:
455 intptr = &options->rhosts_rsa_authentication;
456 goto parse_flag;
457
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000458 case oHostbasedAuthentication:
459 intptr = &options->hostbased_authentication;
460 goto parse_flag;
461
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000462 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000463 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100464 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000465
Darren Tucker0efd1552003-08-26 11:49:55 +1000466 case oGssAuthentication:
467 intptr = &options->gss_authentication;
468 goto parse_flag;
469
470 case oGssDelegateCreds:
471 intptr = &options->gss_deleg_creds;
472 goto parse_flag;
473
Damien Miller95def091999-11-25 00:26:21 +1100474 case oBatchMode:
475 intptr = &options->batch_mode;
476 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 case oCheckHostIP:
479 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000480 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481
Damien Miller37876e92003-05-15 10:19:46 +1000482 case oVerifyHostKeyDNS:
483 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100484 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000485
Damien Miller95def091999-11-25 00:26:21 +1100486 case oStrictHostKeyChecking:
487 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100488parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000489 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000490 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000491 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100492 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100493 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000494 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100495 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000496 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100497 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000498 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100499 value = 2;
500 else
501 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
502 if (*activep && *intptr == -1)
503 *intptr = value;
504 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller95def091999-11-25 00:26:21 +1100506 case oCompression:
507 intptr = &options->compression;
508 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509
Damien Miller12c150e2003-12-17 16:31:10 +1100510 case oTCPKeepAlive:
511 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100512 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000514 case oNoHostAuthenticationForLocalhost:
515 intptr = &options->no_host_authentication_for_localhost;
516 goto parse_flag;
517
Damien Miller95def091999-11-25 00:26:21 +1100518 case oNumberOfPasswordPrompts:
519 intptr = &options->number_of_password_prompts;
520 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller95def091999-11-25 00:26:21 +1100522 case oCompressionLevel:
523 intptr = &options->compression_level;
524 goto parse_int;
525
Damien Millera5539d22003-04-09 20:50:06 +1000526 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000527 arg = strdelim(&s);
528 if (!arg || *arg == '\0')
529 fatal("%.200s line %d: Missing argument.", filename, linenum);
530 if (arg[0] < '0' || arg[0] > '9')
531 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000533 if (arg == endofnumber)
534 fatal("%.200s line %d: Bad number.", filename, linenum);
535 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100536 case '\0':
537 scale = 1;
538 break;
Damien Millera5539d22003-04-09 20:50:06 +1000539 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100540 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000541 break;
542 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100543 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000544 break;
545 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100546 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000547 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100548 default:
549 fatal("%.200s line %d: Invalid RekeyLimit suffix",
550 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000551 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100552 val64 *= scale;
553 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100554 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100555 fatal("%.200s line %d: RekeyLimit too large",
556 filename, linenum);
557 if (val64 < 16)
558 fatal("%.200s line %d: RekeyLimit too small",
559 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100560 if (*activep && options->rekey_limit == -1)
561 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000562 break;
563
Damien Miller95def091999-11-25 00:26:21 +1100564 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000565 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000566 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100567 fatal("%.200s line %d: Missing argument.", filename, linenum);
568 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000570 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100571 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100572 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100573 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000574 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000575 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100576 }
577 break;
578
Damien Millerd3a18572000-06-07 19:55:44 +1000579 case oXAuthLocation:
580 charptr=&options->xauth_location;
581 goto parse_string;
582
Damien Miller95def091999-11-25 00:26:21 +1100583 case oUser:
584 charptr = &options->user;
585parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000586 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000587 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100588 fatal("%.200s line %d: Missing argument.", filename, linenum);
589 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000590 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100591 break;
592
593 case oGlobalKnownHostsFile:
594 charptr = &options->system_hostfile;
595 goto parse_string;
596
597 case oUserKnownHostsFile:
598 charptr = &options->user_hostfile;
599 goto parse_string;
600
Damien Millereba71ba2000-04-29 23:57:08 +1000601 case oGlobalKnownHostsFile2:
602 charptr = &options->system_hostfile2;
603 goto parse_string;
604
605 case oUserKnownHostsFile2:
606 charptr = &options->user_hostfile2;
607 goto parse_string;
608
Damien Miller95def091999-11-25 00:26:21 +1100609 case oHostName:
610 charptr = &options->hostname;
611 goto parse_string;
612
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000613 case oHostKeyAlias:
614 charptr = &options->host_key_alias;
615 goto parse_string;
616
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000617 case oPreferredAuthentications:
618 charptr = &options->preferred_authentications;
619 goto parse_string;
620
Ben Lindstrome0f88042001-04-30 13:06:24 +0000621 case oBindAddress:
622 charptr = &options->bind_address;
623 goto parse_string;
624
Damien Miller7ea845e2010-02-12 09:21:02 +1100625 case oPKCS11Provider:
626 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000627 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000628
Damien Miller95def091999-11-25 00:26:21 +1100629 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100630 charptr = &options->proxy_command;
631parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000632 if (s == NULL)
633 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100634 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100635 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100636 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100637 return 0;
638
639 case oPort:
640 intptr = &options->port;
641parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000642 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000643 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100644 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000645 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100646 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100647
648 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000649 value = strtol(arg, &endofnumber, 0);
650 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100651 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100652 if (*activep && *intptr == -1)
653 *intptr = value;
654 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656 case oConnectionAttempts:
657 intptr = &options->connection_attempts;
658 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100659
Damien Miller95def091999-11-25 00:26:21 +1100660 case oCipher:
661 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000662 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000663 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000664 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000665 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100666 if (value == -1)
667 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100669 if (*activep && *intptr == -1)
670 *intptr = value;
671 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller78928792000-04-12 20:17:38 +1000673 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000674 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000675 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000676 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000677 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000678 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100679 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000680 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000681 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000682 break;
683
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000684 case oMacs:
685 arg = strdelim(&s);
686 if (!arg || *arg == '\0')
687 fatal("%.200s line %d: Missing argument.", filename, linenum);
688 if (!mac_valid(arg))
689 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100690 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000691 if (*activep && options->macs == NULL)
692 options->macs = xstrdup(arg);
693 break;
694
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000695 case oHostKeyAlgorithms:
696 arg = strdelim(&s);
697 if (!arg || *arg == '\0')
698 fatal("%.200s line %d: Missing argument.", filename, linenum);
699 if (!key_names_valid2(arg))
700 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100701 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000702 if (*activep && options->hostkeyalgorithms == NULL)
703 options->hostkeyalgorithms = xstrdup(arg);
704 break;
705
Damien Miller78928792000-04-12 20:17:38 +1000706 case oProtocol:
707 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000708 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000709 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000710 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000711 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000712 if (value == SSH_PROTO_UNKNOWN)
713 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100714 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000715 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
716 *intptr = value;
717 break;
718
Damien Miller95def091999-11-25 00:26:21 +1100719 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100720 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000721 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000722 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100723 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000724 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100725 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100726 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
727 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100728 break;
729
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000730 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100731 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100732 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000733 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100734 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000735 fatal("%.200s line %d: Missing port argument.",
736 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100737
Damien Millera699d952008-11-03 19:27:34 +1100738 if (opcode == oLocalForward ||
739 opcode == oRemoteForward) {
740 arg2 = strdelim(&s);
741 if (arg2 == NULL || *arg2 == '\0')
742 fatal("%.200s line %d: Missing target argument.",
743 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100744
Damien Millera699d952008-11-03 19:27:34 +1100745 /* construct a string for parse_forward */
746 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
747 } else if (opcode == oDynamicForward) {
748 strlcpy(fwdarg, arg, sizeof(fwdarg));
749 }
750
751 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100752 opcode == oDynamicForward ? 1 : 0,
753 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000754 fatal("%.200s line %d: Bad forwarding specification.",
755 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100756
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000757 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100758 if (opcode == oLocalForward ||
759 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100760 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000761 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100762 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000763 }
Damien Miller95def091999-11-25 00:26:21 +1100764 break;
765
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000766 case oClearAllForwardings:
767 intptr = &options->clear_forwardings;
768 goto parse_flag;
769
Damien Miller95def091999-11-25 00:26:21 +1100770 case oHost:
771 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000772 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000773 if (match_pattern(host, arg)) {
774 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100775 *activep = 1;
776 break;
777 }
Damien Millerbe484b52000-07-15 14:14:16 +1000778 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100779 return 0;
780
781 case oEscapeChar:
782 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000783 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000784 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100785 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000786 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000787 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
788 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000789 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000790 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000791 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000792 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100793 else {
794 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100795 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100796 /* NOTREACHED */
797 value = 0; /* Avoid compiler warning. */
798 }
799 if (*activep && *intptr == -1)
800 *intptr = value;
801 break;
802
Damien Miller20a8f972003-05-18 20:50:30 +1000803 case oAddressFamily:
804 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000805 if (!arg || *arg == '\0')
806 fatal("%s line %d: missing address family.",
807 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000808 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000809 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000810 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000811 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000812 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000813 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000814 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000815 else
816 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000817 if (*activep && *intptr == -1)
818 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000819 break;
820
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000821 case oEnableSSHKeysign:
822 intptr = &options->enable_ssh_keysign;
823 goto parse_flag;
824
Damien Millerbd394c32004-03-08 23:12:36 +1100825 case oIdentitiesOnly:
826 intptr = &options->identities_only;
827 goto parse_flag;
828
Damien Miller509b0102003-12-17 16:33:10 +1100829 case oServerAliveInterval:
830 intptr = &options->server_alive_interval;
831 goto parse_time;
832
833 case oServerAliveCountMax:
834 intptr = &options->server_alive_count_max;
835 goto parse_int;
836
Darren Tucker46bc0752004-05-02 22:11:30 +1000837 case oSendEnv:
838 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
839 if (strchr(arg, '=') != NULL)
840 fatal("%s line %d: Invalid environment name.",
841 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100842 if (!*activep)
843 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000844 if (options->num_send_env >= MAX_SEND_ENV)
845 fatal("%s line %d: too many send env.",
846 filename, linenum);
847 options->send_env[options->num_send_env++] =
848 xstrdup(arg);
849 }
850 break;
851
Damien Miller0e220db2004-06-15 10:34:08 +1000852 case oControlPath:
853 charptr = &options->control_path;
854 goto parse_string;
855
856 case oControlMaster:
857 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000858 arg = strdelim(&s);
859 if (!arg || *arg == '\0')
860 fatal("%.200s line %d: Missing ControlMaster argument.",
861 filename, linenum);
862 value = 0; /* To avoid compiler warning... */
863 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
864 value = SSHCTL_MASTER_YES;
865 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
866 value = SSHCTL_MASTER_NO;
867 else if (strcmp(arg, "auto") == 0)
868 value = SSHCTL_MASTER_AUTO;
869 else if (strcmp(arg, "ask") == 0)
870 value = SSHCTL_MASTER_ASK;
871 else if (strcmp(arg, "autoask") == 0)
872 value = SSHCTL_MASTER_AUTO_ASK;
873 else
874 fatal("%.200s line %d: Bad ControlMaster argument.",
875 filename, linenum);
876 if (*activep && *intptr == -1)
877 *intptr = value;
878 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000879
Damien Millere1776152005-03-01 21:47:37 +1100880 case oHashKnownHosts:
881 intptr = &options->hash_known_hosts;
882 goto parse_flag;
883
Damien Millerd27b9472005-12-13 19:29:02 +1100884 case oTunnel:
885 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100886 arg = strdelim(&s);
887 if (!arg || *arg == '\0')
888 fatal("%s line %d: Missing yes/point-to-point/"
889 "ethernet/no argument.", filename, linenum);
890 value = 0; /* silence compiler */
891 if (strcasecmp(arg, "ethernet") == 0)
892 value = SSH_TUNMODE_ETHERNET;
893 else if (strcasecmp(arg, "point-to-point") == 0)
894 value = SSH_TUNMODE_POINTOPOINT;
895 else if (strcasecmp(arg, "yes") == 0)
896 value = SSH_TUNMODE_DEFAULT;
897 else if (strcasecmp(arg, "no") == 0)
898 value = SSH_TUNMODE_NO;
899 else
900 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
901 "no argument: %s", filename, linenum, arg);
902 if (*activep)
903 *intptr = value;
904 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100905
906 case oTunnelDevice:
907 arg = strdelim(&s);
908 if (!arg || *arg == '\0')
909 fatal("%.200s line %d: Missing argument.", filename, linenum);
910 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100911 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100912 fatal("%.200s line %d: Bad tun device.", filename, linenum);
913 if (*activep) {
914 options->tun_local = value;
915 options->tun_remote = value2;
916 }
917 break;
918
919 case oLocalCommand:
920 charptr = &options->local_command;
921 goto parse_command;
922
923 case oPermitLocalCommand:
924 intptr = &options->permit_local_command;
925 goto parse_flag;
926
Damien Miller10288242008-06-30 00:04:03 +1000927 case oVisualHostKey:
928 intptr = &options->visual_host_key;
929 goto parse_flag;
930
Darren Tucker71e4d542009-07-06 07:12:27 +1000931 case oUseRoaming:
932 intptr = &options->use_roaming;
933 goto parse_flag;
934
Ben Lindstrom4daea862002-06-09 20:04:02 +0000935 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000936 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000937 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000938 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000939
Damien Millerf9b3feb2003-05-16 11:38:32 +1000940 case oUnsupported:
941 error("%s line %d: Unsupported option \"%s\"",
942 filename, linenum, keyword);
943 return 0;
944
Damien Miller95def091999-11-25 00:26:21 +1100945 default:
946 fatal("process_config_line: Unimplemented opcode %d", opcode);
947 }
948
949 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000950 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000951 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000952 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000953 }
Damien Miller95def091999-11-25 00:26:21 +1100954 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955}
956
957
Damien Miller5428f641999-11-25 11:54:57 +1100958/*
959 * Reads the config file and modifies the options accordingly. Options
960 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000961 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100962 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000964int
Darren Tuckerfc959702004-07-17 16:12:08 +1000965read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000966 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967{
Damien Miller95def091999-11-25 00:26:21 +1100968 FILE *f;
969 char line[1024];
970 int active, linenum;
971 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller57a44762004-04-20 20:11:57 +1000973 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000974 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller57a44762004-04-20 20:11:57 +1000976 if (checkperm) {
977 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000978
Damien Miller33793852004-06-15 10:27:55 +1000979 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000980 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000981 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000982 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000983 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000984 }
985
Damien Miller95def091999-11-25 00:26:21 +1100986 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller5428f641999-11-25 11:54:57 +1100988 /*
989 * Mark that we are now processing the options. This flag is turned
990 * on/off by Host specifications.
991 */
Damien Miller95def091999-11-25 00:26:21 +1100992 active = 1;
993 linenum = 0;
994 while (fgets(line, sizeof(line), f)) {
995 /* Update line number counter. */
996 linenum++;
997 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
998 bad_options++;
999 }
1000 fclose(f);
1001 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001002 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001003 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001004 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005}
1006
Damien Miller5428f641999-11-25 11:54:57 +11001007/*
1008 * Initializes options to special values that indicate that they have not yet
1009 * been set. Read_config_file will only set options with this value. Options
1010 * are processed in the following order: command line, user config file,
1011 * system config file. Last, fill_default_options is called.
1012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller4af51302000-04-16 11:18:38 +10001014void
Damien Miller95def091999-11-25 00:26:21 +11001015initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016{
Damien Miller95def091999-11-25 00:26:21 +11001017 memset(options, 'X', sizeof(*options));
1018 options->forward_agent = -1;
1019 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001020 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001021 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001022 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001023 options->gateway_ports = -1;
1024 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001025 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001027 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001028 options->gss_authentication = -1;
1029 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001031 options->kbd_interactive_authentication = -1;
1032 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001033 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001034 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001035 options->batch_mode = -1;
1036 options->check_host_ip = -1;
1037 options->strict_host_key_checking = -1;
1038 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001039 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001040 options->compression_level = -1;
1041 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001042 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001043 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001044 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001045 options->number_of_password_prompts = -1;
1046 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001047 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001048 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001049 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001050 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001051 options->num_identity_files = 0;
1052 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001053 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001054 options->proxy_command = NULL;
1055 options->user = NULL;
1056 options->escape_char = -1;
1057 options->system_hostfile = NULL;
1058 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001059 options->system_hostfile2 = NULL;
1060 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001061 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001062 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001063 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001064 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001065 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001066 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001067 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001068 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001069 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001070 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001071 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001072 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001073 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001074 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001075 options->server_alive_interval = -1;
1076 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001077 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001078 options->control_path = NULL;
1079 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001080 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001081 options->tun_open = -1;
1082 options->tun_local = -1;
1083 options->tun_remote = -1;
1084 options->local_command = NULL;
1085 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001086 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001087 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001088 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089}
1090
Damien Miller5428f641999-11-25 11:54:57 +11001091/*
1092 * Called after processing other sources of option data, this fills those
1093 * options for which no value has been specified with their default values.
1094 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Damien Miller4af51302000-04-16 11:18:38 +10001096void
Damien Miller95def091999-11-25 00:26:21 +11001097fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001099 int len;
1100
Damien Miller95def091999-11-25 00:26:21 +11001101 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001102 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001104 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001105 if (options->forward_x11_trusted == -1)
1106 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001107 if (options->exit_on_forward_failure == -1)
1108 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001109 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001110 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001111 if (options->gateway_ports == -1)
1112 options->gateway_ports = 0;
1113 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001114 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001115 if (options->rsa_authentication == -1)
1116 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001117 if (options->pubkey_authentication == -1)
1118 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001119 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001120 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001121 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001122 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001123 if (options->gss_deleg_creds == -1)
1124 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001125 if (options->password_authentication == -1)
1126 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001127 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001128 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001129 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001130 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001131 if (options->hostbased_authentication == -1)
1132 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001133 if (options->batch_mode == -1)
1134 options->batch_mode = 0;
1135 if (options->check_host_ip == -1)
1136 options->check_host_ip = 1;
1137 if (options->strict_host_key_checking == -1)
1138 options->strict_host_key_checking = 2; /* 2 is default */
1139 if (options->compression == -1)
1140 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001141 if (options->tcp_keep_alive == -1)
1142 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001143 if (options->compression_level == -1)
1144 options->compression_level = 6;
1145 if (options->port == -1)
1146 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001147 if (options->address_family == -1)
1148 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001149 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001150 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001151 if (options->number_of_password_prompts == -1)
1152 options->number_of_password_prompts = 3;
1153 /* Selected in ssh_login(). */
1154 if (options->cipher == -1)
1155 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001156 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001157 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001158 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001159 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001160 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001161 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001162 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001163 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001164 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001165 xmalloc(len);
1166 snprintf(options->identity_files[options->num_identity_files++],
1167 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 }
1169 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001170 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1171 options->identity_files[options->num_identity_files] =
1172 xmalloc(len);
1173 snprintf(options->identity_files[options->num_identity_files++],
1174 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1175
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001176 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001177 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001178 xmalloc(len);
1179 snprintf(options->identity_files[options->num_identity_files++],
1180 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001181 }
Damien Millereba71ba2000-04-29 23:57:08 +10001182 }
Damien Miller95def091999-11-25 00:26:21 +11001183 if (options->escape_char == -1)
1184 options->escape_char = '~';
1185 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001186 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001187 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001188 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001189 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001190 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001191 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001192 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001193 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001194 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001195 if (options->clear_forwardings == 1)
1196 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001197 if (options->no_host_authentication_for_localhost == - 1)
1198 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001199 if (options->identities_only == -1)
1200 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001201 if (options->enable_ssh_keysign == -1)
1202 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001203 if (options->rekey_limit == -1)
1204 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001205 if (options->verify_host_key_dns == -1)
1206 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001207 if (options->server_alive_interval == -1)
1208 options->server_alive_interval = 0;
1209 if (options->server_alive_count_max == -1)
1210 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001211 if (options->control_master == -1)
1212 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001213 if (options->hash_known_hosts == -1)
1214 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001215 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001216 options->tun_open = SSH_TUNMODE_NO;
1217 if (options->tun_local == -1)
1218 options->tun_local = SSH_TUNID_ANY;
1219 if (options->tun_remote == -1)
1220 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001221 if (options->permit_local_command == -1)
1222 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001223 if (options->use_roaming == -1)
1224 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001225 if (options->visual_host_key == -1)
1226 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001227 if (options->zero_knowledge_password_authentication == -1)
1228 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001229 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001230 /* options->proxy_command should not be set by default */
1231 /* options->user will be set in the main program if appropriate */
1232 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001233 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001234 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001236
1237/*
1238 * parse_forward
1239 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001240 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001241 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001242 * dynamicfwd == 1
1243 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001244 * returns number of arguments parsed or zero on error
1245 */
1246int
Damien Miller4bf648f2009-02-14 16:28:21 +11001247parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001248{
1249 int i;
1250 char *p, *cp, *fwdarg[4];
1251
1252 memset(fwd, '\0', sizeof(*fwd));
1253
1254 cp = p = xstrdup(fwdspec);
1255
1256 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001257 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001258 cp++;
1259
1260 for (i = 0; i < 4; ++i)
1261 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1262 break;
1263
Damien Millerf4b39532008-11-03 19:28:21 +11001264 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001265 if (cp != NULL)
1266 i = 0; /* failure */
1267
1268 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001269 case 1:
1270 fwd->listen_host = NULL;
1271 fwd->listen_port = a2port(fwdarg[0]);
1272 fwd->connect_host = xstrdup("socks");
1273 break;
1274
1275 case 2:
1276 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1277 fwd->listen_port = a2port(fwdarg[1]);
1278 fwd->connect_host = xstrdup("socks");
1279 break;
1280
Damien Millerf91ee4c2005-03-01 21:24:33 +11001281 case 3:
1282 fwd->listen_host = NULL;
1283 fwd->listen_port = a2port(fwdarg[0]);
1284 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1285 fwd->connect_port = a2port(fwdarg[2]);
1286 break;
1287
1288 case 4:
1289 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1290 fwd->listen_port = a2port(fwdarg[1]);
1291 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1292 fwd->connect_port = a2port(fwdarg[3]);
1293 break;
1294 default:
1295 i = 0; /* failure */
1296 }
1297
1298 xfree(p);
1299
Damien Millera699d952008-11-03 19:27:34 +11001300 if (dynamicfwd) {
1301 if (!(i == 1 || i == 2))
1302 goto fail_free;
1303 } else {
1304 if (!(i == 3 || i == 4))
1305 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001306 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001307 goto fail_free;
1308 }
1309
Damien Miller4bf648f2009-02-14 16:28:21 +11001310 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001311 goto fail_free;
1312
1313 if (fwd->connect_host != NULL &&
1314 strlen(fwd->connect_host) >= NI_MAXHOST)
1315 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001316 if (fwd->listen_host != NULL &&
1317 strlen(fwd->listen_host) >= NI_MAXHOST)
1318 goto fail_free;
1319
Damien Millerf91ee4c2005-03-01 21:24:33 +11001320
1321 return (i);
1322
1323 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001324 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001325 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001326 fwd->connect_host = NULL;
1327 }
1328 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001329 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001330 fwd->listen_host = NULL;
1331 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001332 return (0);
1333}