blob: da48ae7dab9d8bb5a05020bafc8deedae0787764 [file] [log] [blame]
Damien Miller1ab6a512010-06-26 10:02:24 +10001/* $OpenBSD: readconf.c,v 1.186 2010/06/25 23:15:36 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 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 },
Damien Miller1ab6a512010-06-26 10:02:24 +1000146 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000147 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000148 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100149 { "gatewayports", oGatewayPorts },
150 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000151 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100152 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100153 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
154 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100155 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100156 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000157 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000158 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000159 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000160 { "challengeresponseauthentication", oChallengeResponseAuthentication },
161 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
162 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000163 { "kerberosauthentication", oUnsupported },
164 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000165 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000166#if defined(GSSAPI)
167 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000168 { "gssapidelegatecredentials", oGssDelegateCreds },
169#else
170 { "gssapiauthentication", oUnsupported },
171 { "gssapidelegatecredentials", oUnsupported },
172#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000173 { "fallbacktorsh", oDeprecated },
174 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100176 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100177 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000179 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "proxycommand", oProxyCommand },
181 { "port", oPort },
182 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000183 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000184 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000185 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100186 { "remoteforward", oRemoteForward },
187 { "localforward", oLocalForward },
188 { "user", oUser },
189 { "host", oHost },
190 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100191 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100192 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
193 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000194 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100195 { "connectionattempts", oConnectionAttempts },
196 { "batchmode", oBatchMode },
197 { "checkhostip", oCheckHostIP },
198 { "stricthostkeychecking", oStrictHostKeyChecking },
199 { "compression", oCompression },
200 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100201 { "tcpkeepalive", oTCPKeepAlive },
202 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100203 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000205 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000206 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000207 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000208 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100209#ifdef ENABLE_PKCS11
210 { "smartcarddevice", oPKCS11Provider },
211 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000212#else
213 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100214 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000215#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100216 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000217 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000218 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100219 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000220 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000221 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000222 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100223 { "serveraliveinterval", oServerAliveInterval },
224 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000225 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000226 { "controlpath", oControlPath },
227 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100228 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100229 { "tunnel", oTunnel },
230 { "tunneldevice", oTunnelDevice },
231 { "localcommand", oLocalCommand },
232 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000233 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000234 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100235#ifdef JPAKE
236 { "zeroknowledgepasswordauthentication",
237 oZeroKnowledgePasswordAuthentication },
238#else
239 { "zeroknowledgepasswordauthentication", oUnsupported },
240#endif
241
Ben Lindstrom65366a82001-12-06 16:32:47 +0000242 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100243};
244
Damien Miller5428f641999-11-25 11:54:57 +1100245/*
246 * Adds a local TCP/IP port forward to options. Never returns if there is an
247 * error.
248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Damien Miller4af51302000-04-16 11:18:38 +1000250void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100251add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Damien Miller95def091999-11-25 00:26:21 +1100253 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000254#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100255 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100256 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000257 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100258#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000259 options->local_forwards = xrealloc(options->local_forwards,
260 options->num_local_forwards + 1,
261 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100262 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263
Damien Miller1a0442f2008-11-05 16:30:06 +1100264 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100266 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
269
Damien Miller5428f641999-11-25 11:54:57 +1100270/*
271 * Adds a remote TCP/IP port forward to options. Never returns if there is
272 * an error.
273 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Miller4af51302000-04-16 11:18:38 +1000275void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100276add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277{
Damien Miller95def091999-11-25 00:26:21 +1100278 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000279
280 options->remote_forwards = xrealloc(options->remote_forwards,
281 options->num_remote_forwards + 1,
282 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100283 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284
Damien Miller1a0442f2008-11-05 16:30:06 +1100285 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100287 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000289 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290}
291
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000292static void
293clear_forwardings(Options *options)
294{
295 int i;
296
Damien Millerf91ee4c2005-03-01 21:24:33 +1100297 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100298 if (options->local_forwards[i].listen_host != NULL)
299 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300 xfree(options->local_forwards[i].connect_host);
301 }
Damien Miller232cfb12010-06-26 09:50:30 +1000302 if (options->num_local_forwards > 0) {
303 xfree(options->local_forwards);
304 options->local_forwards = NULL;
305 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000306 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100307 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100308 if (options->remote_forwards[i].listen_host != NULL)
309 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100310 xfree(options->remote_forwards[i].connect_host);
311 }
Damien Miller232cfb12010-06-26 09:50:30 +1000312 if (options->num_remote_forwards > 0) {
313 xfree(options->remote_forwards);
314 options->remote_forwards = NULL;
315 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000316 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100317 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000318}
319
Damien Miller5428f641999-11-25 11:54:57 +1100320/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000321 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100322 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller4af51302000-04-16 11:18:38 +1000324static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100325parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000327 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
Damien Miller95def091999-11-25 00:26:21 +1100329 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100330 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100331 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000333 error("%s: line %d: Bad configuration option: %s",
334 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100335 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
Damien Miller5428f641999-11-25 11:54:57 +1100338/*
339 * Processes a single option line as used in the configuration files. This
340 * only sets those values that have not already been set.
341 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100342#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Damien Miller2ccf6611999-11-15 15:25:10 +1100344int
345process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100346 char *line, const char *filename, int linenum,
347 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100349 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100350 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100351 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100352 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100353 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100354 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Millerc652cac2003-05-14 13:40:54 +1000356 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100357 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000358 if (strchr(WHITESPACE, line[len]) == NULL)
359 break;
360 line[len] = '\0';
361 }
362
Damien Millerbe484b52000-07-15 14:14:16 +1000363 s = line;
364 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100365 if ((keyword = strdelim(&s)) == NULL)
366 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000367 /* Ignore leading whitespace. */
368 if (*keyword == '\0')
369 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000370 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100371 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller37023962000-07-11 17:31:38 +1000373 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Damien Miller95def091999-11-25 00:26:21 +1100375 switch (opcode) {
376 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100377 /* don't panic, but count bad options */
378 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100379 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000380 case oConnectTimeout:
381 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100382parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000383 arg = strdelim(&s);
384 if (!arg || *arg == '\0')
385 fatal("%s line %d: missing time value.",
386 filename, linenum);
387 if ((value = convtime(arg)) == -1)
388 fatal("%s line %d: invalid time value.",
389 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100390 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000391 *intptr = value;
392 break;
393
Damien Miller95def091999-11-25 00:26:21 +1100394 case oForwardAgent:
395 intptr = &options->forward_agent;
396parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000397 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000398 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100399 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
400 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000401 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100402 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000403 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100404 value = 0;
405 else
406 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
407 if (*activep && *intptr == -1)
408 *intptr = value;
409 break;
410
411 case oForwardX11:
412 intptr = &options->forward_x11;
413 goto parse_flag;
414
Darren Tucker0a118da2003-10-15 15:54:32 +1000415 case oForwardX11Trusted:
416 intptr = &options->forward_x11_trusted;
417 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000418
419 case oForwardX11Timeout:
420 intptr = &options->forward_x11_timeout;
421 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 case oGatewayPorts:
424 intptr = &options->gateway_ports;
425 goto parse_flag;
426
Darren Tuckere7d4b192006-07-12 22:17:10 +1000427 case oExitOnForwardFailure:
428 intptr = &options->exit_on_forward_failure;
429 goto parse_flag;
430
Damien Miller95def091999-11-25 00:26:21 +1100431 case oUsePrivilegedPort:
432 intptr = &options->use_privileged_port;
433 goto parse_flag;
434
Damien Miller95def091999-11-25 00:26:21 +1100435 case oPasswordAuthentication:
436 intptr = &options->password_authentication;
437 goto parse_flag;
438
Damien Miller01ed2272008-11-05 16:20:46 +1100439 case oZeroKnowledgePasswordAuthentication:
440 intptr = &options->zero_knowledge_password_authentication;
441 goto parse_flag;
442
Damien Miller874d77b2000-10-14 16:23:11 +1100443 case oKbdInteractiveAuthentication:
444 intptr = &options->kbd_interactive_authentication;
445 goto parse_flag;
446
447 case oKbdInteractiveDevices:
448 charptr = &options->kbd_interactive_devices;
449 goto parse_string;
450
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451 case oPubkeyAuthentication:
452 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000453 goto parse_flag;
454
Damien Miller95def091999-11-25 00:26:21 +1100455 case oRSAAuthentication:
456 intptr = &options->rsa_authentication;
457 goto parse_flag;
458
459 case oRhostsRSAAuthentication:
460 intptr = &options->rhosts_rsa_authentication;
461 goto parse_flag;
462
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000463 case oHostbasedAuthentication:
464 intptr = &options->hostbased_authentication;
465 goto parse_flag;
466
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000467 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000468 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100469 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000470
Darren Tucker0efd1552003-08-26 11:49:55 +1000471 case oGssAuthentication:
472 intptr = &options->gss_authentication;
473 goto parse_flag;
474
475 case oGssDelegateCreds:
476 intptr = &options->gss_deleg_creds;
477 goto parse_flag;
478
Damien Miller95def091999-11-25 00:26:21 +1100479 case oBatchMode:
480 intptr = &options->batch_mode;
481 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oCheckHostIP:
484 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000485 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller37876e92003-05-15 10:19:46 +1000487 case oVerifyHostKeyDNS:
488 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100489 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 case oStrictHostKeyChecking:
492 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100493parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000494 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000495 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000496 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100497 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100498 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000499 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100500 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000501 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100502 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000503 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100504 value = 2;
505 else
506 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
507 if (*activep && *intptr == -1)
508 *intptr = value;
509 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510
Damien Miller95def091999-11-25 00:26:21 +1100511 case oCompression:
512 intptr = &options->compression;
513 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller12c150e2003-12-17 16:31:10 +1100515 case oTCPKeepAlive:
516 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100517 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000519 case oNoHostAuthenticationForLocalhost:
520 intptr = &options->no_host_authentication_for_localhost;
521 goto parse_flag;
522
Damien Miller95def091999-11-25 00:26:21 +1100523 case oNumberOfPasswordPrompts:
524 intptr = &options->number_of_password_prompts;
525 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller95def091999-11-25 00:26:21 +1100527 case oCompressionLevel:
528 intptr = &options->compression_level;
529 goto parse_int;
530
Damien Millera5539d22003-04-09 20:50:06 +1000531 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000532 arg = strdelim(&s);
533 if (!arg || *arg == '\0')
534 fatal("%.200s line %d: Missing argument.", filename, linenum);
535 if (arg[0] < '0' || arg[0] > '9')
536 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100537 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000538 if (arg == endofnumber)
539 fatal("%.200s line %d: Bad number.", filename, linenum);
540 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100541 case '\0':
542 scale = 1;
543 break;
Damien Millera5539d22003-04-09 20:50:06 +1000544 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100545 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000546 break;
547 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100548 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000549 break;
550 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100551 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000552 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100553 default:
554 fatal("%.200s line %d: Invalid RekeyLimit suffix",
555 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000556 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100557 val64 *= scale;
558 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100559 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100560 fatal("%.200s line %d: RekeyLimit too large",
561 filename, linenum);
562 if (val64 < 16)
563 fatal("%.200s line %d: RekeyLimit too small",
564 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100565 if (*activep && options->rekey_limit == -1)
566 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000567 break;
568
Damien Miller95def091999-11-25 00:26:21 +1100569 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000570 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000571 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100572 fatal("%.200s line %d: Missing argument.", filename, linenum);
573 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100574 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000575 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100576 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100577 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100578 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000579 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000580 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100581 }
582 break;
583
Damien Millerd3a18572000-06-07 19:55:44 +1000584 case oXAuthLocation:
585 charptr=&options->xauth_location;
586 goto parse_string;
587
Damien Miller95def091999-11-25 00:26:21 +1100588 case oUser:
589 charptr = &options->user;
590parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000591 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000592 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100593 fatal("%.200s line %d: Missing argument.", filename, linenum);
594 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000595 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100596 break;
597
598 case oGlobalKnownHostsFile:
599 charptr = &options->system_hostfile;
600 goto parse_string;
601
602 case oUserKnownHostsFile:
603 charptr = &options->user_hostfile;
604 goto parse_string;
605
Damien Millereba71ba2000-04-29 23:57:08 +1000606 case oGlobalKnownHostsFile2:
607 charptr = &options->system_hostfile2;
608 goto parse_string;
609
610 case oUserKnownHostsFile2:
611 charptr = &options->user_hostfile2;
612 goto parse_string;
613
Damien Miller95def091999-11-25 00:26:21 +1100614 case oHostName:
615 charptr = &options->hostname;
616 goto parse_string;
617
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000618 case oHostKeyAlias:
619 charptr = &options->host_key_alias;
620 goto parse_string;
621
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000622 case oPreferredAuthentications:
623 charptr = &options->preferred_authentications;
624 goto parse_string;
625
Ben Lindstrome0f88042001-04-30 13:06:24 +0000626 case oBindAddress:
627 charptr = &options->bind_address;
628 goto parse_string;
629
Damien Miller7ea845e2010-02-12 09:21:02 +1100630 case oPKCS11Provider:
631 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000632 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000633
Damien Miller95def091999-11-25 00:26:21 +1100634 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100635 charptr = &options->proxy_command;
636parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000637 if (s == NULL)
638 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100639 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100640 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100641 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100642 return 0;
643
644 case oPort:
645 intptr = &options->port;
646parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000647 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000648 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100649 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000650 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100651 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100652
653 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000654 value = strtol(arg, &endofnumber, 0);
655 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100656 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100657 if (*activep && *intptr == -1)
658 *intptr = value;
659 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660
Damien Miller95def091999-11-25 00:26:21 +1100661 case oConnectionAttempts:
662 intptr = &options->connection_attempts;
663 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100664
Damien Miller95def091999-11-25 00:26:21 +1100665 case oCipher:
666 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000667 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000668 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000669 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000670 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100671 if (value == -1)
672 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100673 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100674 if (*activep && *intptr == -1)
675 *intptr = value;
676 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Damien Miller78928792000-04-12 20:17:38 +1000678 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000679 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000680 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000681 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000682 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000683 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100684 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000685 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000686 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000687 break;
688
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000689 case oMacs:
690 arg = strdelim(&s);
691 if (!arg || *arg == '\0')
692 fatal("%.200s line %d: Missing argument.", filename, linenum);
693 if (!mac_valid(arg))
694 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100695 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000696 if (*activep && options->macs == NULL)
697 options->macs = xstrdup(arg);
698 break;
699
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000700 case oHostKeyAlgorithms:
701 arg = strdelim(&s);
702 if (!arg || *arg == '\0')
703 fatal("%.200s line %d: Missing argument.", filename, linenum);
704 if (!key_names_valid2(arg))
705 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100706 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000707 if (*activep && options->hostkeyalgorithms == NULL)
708 options->hostkeyalgorithms = xstrdup(arg);
709 break;
710
Damien Miller78928792000-04-12 20:17:38 +1000711 case oProtocol:
712 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000713 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000714 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000715 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000716 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000717 if (value == SSH_PROTO_UNKNOWN)
718 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100719 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000720 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
721 *intptr = value;
722 break;
723
Damien Miller95def091999-11-25 00:26:21 +1100724 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100725 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000726 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000727 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100728 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000729 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100730 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100731 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
732 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100733 break;
734
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000735 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100736 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100737 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000738 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100739 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000740 fatal("%.200s line %d: Missing port argument.",
741 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100742
Damien Millera699d952008-11-03 19:27:34 +1100743 if (opcode == oLocalForward ||
744 opcode == oRemoteForward) {
745 arg2 = strdelim(&s);
746 if (arg2 == NULL || *arg2 == '\0')
747 fatal("%.200s line %d: Missing target argument.",
748 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100749
Damien Millera699d952008-11-03 19:27:34 +1100750 /* construct a string for parse_forward */
751 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
752 } else if (opcode == oDynamicForward) {
753 strlcpy(fwdarg, arg, sizeof(fwdarg));
754 }
755
756 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100757 opcode == oDynamicForward ? 1 : 0,
758 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000759 fatal("%.200s line %d: Bad forwarding specification.",
760 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100761
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000762 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100763 if (opcode == oLocalForward ||
764 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100765 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000766 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100767 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000768 }
Damien Miller95def091999-11-25 00:26:21 +1100769 break;
770
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000771 case oClearAllForwardings:
772 intptr = &options->clear_forwardings;
773 goto parse_flag;
774
Damien Miller95def091999-11-25 00:26:21 +1100775 case oHost:
776 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000777 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000778 if (match_pattern(host, arg)) {
779 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100780 *activep = 1;
781 break;
782 }
Damien Millerbe484b52000-07-15 14:14:16 +1000783 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100784 return 0;
785
786 case oEscapeChar:
787 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000788 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000789 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100790 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000791 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000792 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
793 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000794 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000795 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000796 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000797 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100798 else {
799 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100800 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100801 /* NOTREACHED */
802 value = 0; /* Avoid compiler warning. */
803 }
804 if (*activep && *intptr == -1)
805 *intptr = value;
806 break;
807
Damien Miller20a8f972003-05-18 20:50:30 +1000808 case oAddressFamily:
809 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000810 if (!arg || *arg == '\0')
811 fatal("%s line %d: missing address family.",
812 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000813 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000814 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000815 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000816 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000817 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000818 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000819 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000820 else
821 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000822 if (*activep && *intptr == -1)
823 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000824 break;
825
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000826 case oEnableSSHKeysign:
827 intptr = &options->enable_ssh_keysign;
828 goto parse_flag;
829
Damien Millerbd394c32004-03-08 23:12:36 +1100830 case oIdentitiesOnly:
831 intptr = &options->identities_only;
832 goto parse_flag;
833
Damien Miller509b0102003-12-17 16:33:10 +1100834 case oServerAliveInterval:
835 intptr = &options->server_alive_interval;
836 goto parse_time;
837
838 case oServerAliveCountMax:
839 intptr = &options->server_alive_count_max;
840 goto parse_int;
841
Darren Tucker46bc0752004-05-02 22:11:30 +1000842 case oSendEnv:
843 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
844 if (strchr(arg, '=') != NULL)
845 fatal("%s line %d: Invalid environment name.",
846 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100847 if (!*activep)
848 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000849 if (options->num_send_env >= MAX_SEND_ENV)
850 fatal("%s line %d: too many send env.",
851 filename, linenum);
852 options->send_env[options->num_send_env++] =
853 xstrdup(arg);
854 }
855 break;
856
Damien Miller0e220db2004-06-15 10:34:08 +1000857 case oControlPath:
858 charptr = &options->control_path;
859 goto parse_string;
860
861 case oControlMaster:
862 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000863 arg = strdelim(&s);
864 if (!arg || *arg == '\0')
865 fatal("%.200s line %d: Missing ControlMaster argument.",
866 filename, linenum);
867 value = 0; /* To avoid compiler warning... */
868 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
869 value = SSHCTL_MASTER_YES;
870 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
871 value = SSHCTL_MASTER_NO;
872 else if (strcmp(arg, "auto") == 0)
873 value = SSHCTL_MASTER_AUTO;
874 else if (strcmp(arg, "ask") == 0)
875 value = SSHCTL_MASTER_ASK;
876 else if (strcmp(arg, "autoask") == 0)
877 value = SSHCTL_MASTER_AUTO_ASK;
878 else
879 fatal("%.200s line %d: Bad ControlMaster argument.",
880 filename, linenum);
881 if (*activep && *intptr == -1)
882 *intptr = value;
883 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000884
Damien Millere1776152005-03-01 21:47:37 +1100885 case oHashKnownHosts:
886 intptr = &options->hash_known_hosts;
887 goto parse_flag;
888
Damien Millerd27b9472005-12-13 19:29:02 +1100889 case oTunnel:
890 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100891 arg = strdelim(&s);
892 if (!arg || *arg == '\0')
893 fatal("%s line %d: Missing yes/point-to-point/"
894 "ethernet/no argument.", filename, linenum);
895 value = 0; /* silence compiler */
896 if (strcasecmp(arg, "ethernet") == 0)
897 value = SSH_TUNMODE_ETHERNET;
898 else if (strcasecmp(arg, "point-to-point") == 0)
899 value = SSH_TUNMODE_POINTOPOINT;
900 else if (strcasecmp(arg, "yes") == 0)
901 value = SSH_TUNMODE_DEFAULT;
902 else if (strcasecmp(arg, "no") == 0)
903 value = SSH_TUNMODE_NO;
904 else
905 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
906 "no argument: %s", filename, linenum, arg);
907 if (*activep)
908 *intptr = value;
909 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100910
911 case oTunnelDevice:
912 arg = strdelim(&s);
913 if (!arg || *arg == '\0')
914 fatal("%.200s line %d: Missing argument.", filename, linenum);
915 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100916 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100917 fatal("%.200s line %d: Bad tun device.", filename, linenum);
918 if (*activep) {
919 options->tun_local = value;
920 options->tun_remote = value2;
921 }
922 break;
923
924 case oLocalCommand:
925 charptr = &options->local_command;
926 goto parse_command;
927
928 case oPermitLocalCommand:
929 intptr = &options->permit_local_command;
930 goto parse_flag;
931
Damien Miller10288242008-06-30 00:04:03 +1000932 case oVisualHostKey:
933 intptr = &options->visual_host_key;
934 goto parse_flag;
935
Darren Tucker71e4d542009-07-06 07:12:27 +1000936 case oUseRoaming:
937 intptr = &options->use_roaming;
938 goto parse_flag;
939
Ben Lindstrom4daea862002-06-09 20:04:02 +0000940 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000941 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000942 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000943 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000944
Damien Millerf9b3feb2003-05-16 11:38:32 +1000945 case oUnsupported:
946 error("%s line %d: Unsupported option \"%s\"",
947 filename, linenum, keyword);
948 return 0;
949
Damien Miller95def091999-11-25 00:26:21 +1100950 default:
951 fatal("process_config_line: Unimplemented opcode %d", opcode);
952 }
953
954 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000955 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000956 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000957 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000958 }
Damien Miller95def091999-11-25 00:26:21 +1100959 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
962
Damien Miller5428f641999-11-25 11:54:57 +1100963/*
964 * Reads the config file and modifies the options accordingly. Options
965 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000966 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100967 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000969int
Darren Tuckerfc959702004-07-17 16:12:08 +1000970read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000971 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972{
Damien Miller95def091999-11-25 00:26:21 +1100973 FILE *f;
974 char line[1024];
975 int active, linenum;
976 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
Damien Miller57a44762004-04-20 20:11:57 +1000978 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000979 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller57a44762004-04-20 20:11:57 +1000981 if (checkperm) {
982 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000983
Damien Miller33793852004-06-15 10:27:55 +1000984 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000985 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000986 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000987 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000988 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000989 }
990
Damien Miller95def091999-11-25 00:26:21 +1100991 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller5428f641999-11-25 11:54:57 +1100993 /*
994 * Mark that we are now processing the options. This flag is turned
995 * on/off by Host specifications.
996 */
Damien Miller95def091999-11-25 00:26:21 +1100997 active = 1;
998 linenum = 0;
999 while (fgets(line, sizeof(line), f)) {
1000 /* Update line number counter. */
1001 linenum++;
1002 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1003 bad_options++;
1004 }
1005 fclose(f);
1006 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001007 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001008 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001009 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010}
1011
Damien Miller5428f641999-11-25 11:54:57 +11001012/*
1013 * Initializes options to special values that indicate that they have not yet
1014 * been set. Read_config_file will only set options with this value. Options
1015 * are processed in the following order: command line, user config file,
1016 * system config file. Last, fill_default_options is called.
1017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller4af51302000-04-16 11:18:38 +10001019void
Damien Miller95def091999-11-25 00:26:21 +11001020initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021{
Damien Miller95def091999-11-25 00:26:21 +11001022 memset(options, 'X', sizeof(*options));
1023 options->forward_agent = -1;
1024 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001025 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001026 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001027 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001028 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001029 options->gateway_ports = -1;
1030 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001031 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001032 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001033 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001034 options->gss_authentication = -1;
1035 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001036 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001037 options->kbd_interactive_authentication = -1;
1038 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001039 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001040 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001041 options->batch_mode = -1;
1042 options->check_host_ip = -1;
1043 options->strict_host_key_checking = -1;
1044 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001045 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001046 options->compression_level = -1;
1047 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001048 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001049 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001050 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001051 options->number_of_password_prompts = -1;
1052 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001053 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001054 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001055 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001056 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001057 options->num_identity_files = 0;
1058 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001059 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001060 options->proxy_command = NULL;
1061 options->user = NULL;
1062 options->escape_char = -1;
1063 options->system_hostfile = NULL;
1064 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001065 options->system_hostfile2 = NULL;
1066 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001067 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001068 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001069 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001070 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001071 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001072 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001073 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001074 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001075 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001076 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001077 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001078 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001079 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001080 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001081 options->server_alive_interval = -1;
1082 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001083 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001084 options->control_path = NULL;
1085 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001086 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001087 options->tun_open = -1;
1088 options->tun_local = -1;
1089 options->tun_remote = -1;
1090 options->local_command = NULL;
1091 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001092 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001093 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001094 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095}
1096
Damien Miller5428f641999-11-25 11:54:57 +11001097/*
1098 * Called after processing other sources of option data, this fills those
1099 * options for which no value has been specified with their default values.
1100 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller4af51302000-04-16 11:18:38 +10001102void
Damien Miller95def091999-11-25 00:26:21 +11001103fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001105 int len;
1106
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001108 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001109 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001110 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001111 if (options->forward_x11_trusted == -1)
1112 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001113 if (options->forward_x11_timeout == -1)
1114 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001115 if (options->exit_on_forward_failure == -1)
1116 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001117 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001118 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001119 if (options->gateway_ports == -1)
1120 options->gateway_ports = 0;
1121 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001122 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001123 if (options->rsa_authentication == -1)
1124 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001125 if (options->pubkey_authentication == -1)
1126 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001127 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001128 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001129 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001130 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001131 if (options->gss_deleg_creds == -1)
1132 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001133 if (options->password_authentication == -1)
1134 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001135 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001136 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001137 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001138 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001139 if (options->hostbased_authentication == -1)
1140 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001141 if (options->batch_mode == -1)
1142 options->batch_mode = 0;
1143 if (options->check_host_ip == -1)
1144 options->check_host_ip = 1;
1145 if (options->strict_host_key_checking == -1)
1146 options->strict_host_key_checking = 2; /* 2 is default */
1147 if (options->compression == -1)
1148 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001149 if (options->tcp_keep_alive == -1)
1150 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001151 if (options->compression_level == -1)
1152 options->compression_level = 6;
1153 if (options->port == -1)
1154 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001155 if (options->address_family == -1)
1156 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001157 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001158 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001159 if (options->number_of_password_prompts == -1)
1160 options->number_of_password_prompts = 3;
1161 /* Selected in ssh_login(). */
1162 if (options->cipher == -1)
1163 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001164 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001165 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001166 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001167 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001168 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001169 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001170 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001171 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001172 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001173 xmalloc(len);
1174 snprintf(options->identity_files[options->num_identity_files++],
1175 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001176 }
1177 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001178 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1179 options->identity_files[options->num_identity_files] =
1180 xmalloc(len);
1181 snprintf(options->identity_files[options->num_identity_files++],
1182 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1183
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001184 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001185 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001186 xmalloc(len);
1187 snprintf(options->identity_files[options->num_identity_files++],
1188 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001189 }
Damien Millereba71ba2000-04-29 23:57:08 +10001190 }
Damien Miller95def091999-11-25 00:26:21 +11001191 if (options->escape_char == -1)
1192 options->escape_char = '~';
1193 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001194 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001195 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001196 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001197 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001198 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001199 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001200 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001201 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001202 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001203 if (options->clear_forwardings == 1)
1204 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001205 if (options->no_host_authentication_for_localhost == - 1)
1206 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001207 if (options->identities_only == -1)
1208 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001209 if (options->enable_ssh_keysign == -1)
1210 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001211 if (options->rekey_limit == -1)
1212 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001213 if (options->verify_host_key_dns == -1)
1214 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001215 if (options->server_alive_interval == -1)
1216 options->server_alive_interval = 0;
1217 if (options->server_alive_count_max == -1)
1218 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001219 if (options->control_master == -1)
1220 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001221 if (options->hash_known_hosts == -1)
1222 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001223 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001224 options->tun_open = SSH_TUNMODE_NO;
1225 if (options->tun_local == -1)
1226 options->tun_local = SSH_TUNID_ANY;
1227 if (options->tun_remote == -1)
1228 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001229 if (options->permit_local_command == -1)
1230 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001231 if (options->use_roaming == -1)
1232 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001233 if (options->visual_host_key == -1)
1234 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001235 if (options->zero_knowledge_password_authentication == -1)
1236 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001237 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001238 /* options->proxy_command should not be set by default */
1239 /* options->user will be set in the main program if appropriate */
1240 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001241 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001242 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001244
1245/*
1246 * parse_forward
1247 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001248 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001249 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001250 * dynamicfwd == 1
1251 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001252 * returns number of arguments parsed or zero on error
1253 */
1254int
Damien Miller4bf648f2009-02-14 16:28:21 +11001255parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001256{
1257 int i;
1258 char *p, *cp, *fwdarg[4];
1259
1260 memset(fwd, '\0', sizeof(*fwd));
1261
1262 cp = p = xstrdup(fwdspec);
1263
1264 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001265 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001266 cp++;
1267
1268 for (i = 0; i < 4; ++i)
1269 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1270 break;
1271
Damien Millerf4b39532008-11-03 19:28:21 +11001272 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001273 if (cp != NULL)
1274 i = 0; /* failure */
1275
1276 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001277 case 1:
1278 fwd->listen_host = NULL;
1279 fwd->listen_port = a2port(fwdarg[0]);
1280 fwd->connect_host = xstrdup("socks");
1281 break;
1282
1283 case 2:
1284 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1285 fwd->listen_port = a2port(fwdarg[1]);
1286 fwd->connect_host = xstrdup("socks");
1287 break;
1288
Damien Millerf91ee4c2005-03-01 21:24:33 +11001289 case 3:
1290 fwd->listen_host = NULL;
1291 fwd->listen_port = a2port(fwdarg[0]);
1292 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1293 fwd->connect_port = a2port(fwdarg[2]);
1294 break;
1295
1296 case 4:
1297 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1298 fwd->listen_port = a2port(fwdarg[1]);
1299 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1300 fwd->connect_port = a2port(fwdarg[3]);
1301 break;
1302 default:
1303 i = 0; /* failure */
1304 }
1305
1306 xfree(p);
1307
Damien Millera699d952008-11-03 19:27:34 +11001308 if (dynamicfwd) {
1309 if (!(i == 1 || i == 2))
1310 goto fail_free;
1311 } else {
1312 if (!(i == 3 || i == 4))
1313 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001314 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001315 goto fail_free;
1316 }
1317
Damien Miller4bf648f2009-02-14 16:28:21 +11001318 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001319 goto fail_free;
1320
1321 if (fwd->connect_host != NULL &&
1322 strlen(fwd->connect_host) >= NI_MAXHOST)
1323 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001324 if (fwd->listen_host != NULL &&
1325 strlen(fwd->listen_host) >= NI_MAXHOST)
1326 goto fail_free;
1327
Damien Millerf91ee4c2005-03-01 21:24:33 +11001328
1329 return (i);
1330
1331 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001332 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001333 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001334 fwd->connect_host = NULL;
1335 }
1336 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001337 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001338 fwd->listen_host = NULL;
1339 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001340 return (0);
1341}