blob: da7efd19391ce6996c6761ba907fc7752585c52f [file] [log] [blame]
Damien Millerd5f62bf2010-09-24 22:11:14 +10001/* $OpenBSD: readconf.c,v 1.189 2010/09/22 05:01:29 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
20
21#include <netinet/in.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
114 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000115 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100117 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100121 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000124 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100126 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000127 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000128 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000129 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100130 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000131 oSendEnv, oControlPath, oControlMaster, oControlPersist,
132 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100133 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100134 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Millerd5f62bf2010-09-24 22:11:14 +1000135 oKexAlgorithms,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100136 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137} OpCodes;
138
139/* Textual representations of the tokens. */
140
Damien Miller95def091999-11-25 00:26:21 +1100141static struct {
142 const char *name;
143 OpCodes opcode;
144} keywords[] = {
145 { "forwardagent", oForwardAgent },
146 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000147 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000148 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000149 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000150 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100151 { "gatewayports", oGatewayPorts },
152 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000153 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100155 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
156 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100157 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000159 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000160 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000161 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000162 { "challengeresponseauthentication", oChallengeResponseAuthentication },
163 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
164 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000165 { "kerberosauthentication", oUnsupported },
166 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000167 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000168#if defined(GSSAPI)
169 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000170 { "gssapidelegatecredentials", oGssDelegateCreds },
171#else
172 { "gssapiauthentication", oUnsupported },
173 { "gssapidelegatecredentials", oUnsupported },
174#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000175 { "fallbacktorsh", oDeprecated },
176 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100178 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100179 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000181 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "proxycommand", oProxyCommand },
183 { "port", oPort },
184 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000185 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000186 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000187 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100188 { "remoteforward", oRemoteForward },
189 { "localforward", oLocalForward },
190 { "user", oUser },
191 { "host", oHost },
192 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100194 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
195 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000196 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100197 { "connectionattempts", oConnectionAttempts },
198 { "batchmode", oBatchMode },
199 { "checkhostip", oCheckHostIP },
200 { "stricthostkeychecking", oStrictHostKeyChecking },
201 { "compression", oCompression },
202 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100203 { "tcpkeepalive", oTCPKeepAlive },
204 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100205 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100206 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000207 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000208 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000209 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000210 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100211#ifdef ENABLE_PKCS11
212 { "smartcarddevice", oPKCS11Provider },
213 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000214#else
215 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100216 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000217#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100218 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000219 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000220 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100221 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000222 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000223 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000224 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100225 { "serveraliveinterval", oServerAliveInterval },
226 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000227 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000228 { "controlpath", oControlPath },
229 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000230 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100231 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100232 { "tunnel", oTunnel },
233 { "tunneldevice", oTunnelDevice },
234 { "localcommand", oLocalCommand },
235 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000236 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000237 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100238#ifdef JPAKE
239 { "zeroknowledgepasswordauthentication",
240 oZeroKnowledgePasswordAuthentication },
241#else
242 { "zeroknowledgepasswordauthentication", oUnsupported },
243#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000244 { "kexalgorithms", oKexAlgorithms },
Damien Miller01ed2272008-11-05 16:20:46 +1100245
Ben Lindstrom65366a82001-12-06 16:32:47 +0000246 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100247};
248
Damien Miller5428f641999-11-25 11:54:57 +1100249/*
250 * Adds a local TCP/IP port forward to options. Never returns if there is an
251 * error.
252 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller4af51302000-04-16 11:18:38 +1000254void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256{
Damien Miller95def091999-11-25 00:26:21 +1100257 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000258#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100259 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100260 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000261 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100262#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000263 options->local_forwards = xrealloc(options->local_forwards,
264 options->num_local_forwards + 1,
265 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100266 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267
Damien Miller1a0442f2008-11-05 16:30:06 +1100268 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100269 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100270 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272}
273
Damien Miller5428f641999-11-25 11:54:57 +1100274/*
275 * Adds a remote TCP/IP port forward to options. Never returns if there is
276 * an error.
277 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller4af51302000-04-16 11:18:38 +1000279void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281{
Damien Miller95def091999-11-25 00:26:21 +1100282 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000283
284 options->remote_forwards = xrealloc(options->remote_forwards,
285 options->num_remote_forwards + 1,
286 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100287 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288
Damien Miller1a0442f2008-11-05 16:30:06 +1100289 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100291 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100292 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000293 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000296static void
297clear_forwardings(Options *options)
298{
299 int i;
300
Damien Millerf91ee4c2005-03-01 21:24:33 +1100301 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100302 if (options->local_forwards[i].listen_host != NULL)
303 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 xfree(options->local_forwards[i].connect_host);
305 }
Damien Miller232cfb12010-06-26 09:50:30 +1000306 if (options->num_local_forwards > 0) {
307 xfree(options->local_forwards);
308 options->local_forwards = NULL;
309 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000310 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100311 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100312 if (options->remote_forwards[i].listen_host != NULL)
313 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100314 xfree(options->remote_forwards[i].connect_host);
315 }
Damien Miller232cfb12010-06-26 09:50:30 +1000316 if (options->num_remote_forwards > 0) {
317 xfree(options->remote_forwards);
318 options->remote_forwards = NULL;
319 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000320 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100321 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000322}
323
Damien Miller5428f641999-11-25 11:54:57 +1100324/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000325 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100326 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller4af51302000-04-16 11:18:38 +1000328static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100329parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000331 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100334 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100335 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000337 error("%s: line %d: Bad configuration option: %s",
338 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100339 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340}
341
Damien Miller5428f641999-11-25 11:54:57 +1100342/*
343 * Processes a single option line as used in the configuration files. This
344 * only sets those values that have not already been set.
345 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100346#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller2ccf6611999-11-15 15:25:10 +1100348int
349process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100350 char *line, const char *filename, int linenum,
351 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100353 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100354 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100355 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100356 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100357 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100358 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Millerc652cac2003-05-14 13:40:54 +1000360 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100361 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000362 if (strchr(WHITESPACE, line[len]) == NULL)
363 break;
364 line[len] = '\0';
365 }
366
Damien Millerbe484b52000-07-15 14:14:16 +1000367 s = line;
368 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100369 if ((keyword = strdelim(&s)) == NULL)
370 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000371 /* Ignore leading whitespace. */
372 if (*keyword == '\0')
373 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000374 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100375 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376
Damien Miller37023962000-07-11 17:31:38 +1000377 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378
Damien Miller95def091999-11-25 00:26:21 +1100379 switch (opcode) {
380 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100381 /* don't panic, but count bad options */
382 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100383 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000384 case oConnectTimeout:
385 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100386parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000387 arg = strdelim(&s);
388 if (!arg || *arg == '\0')
389 fatal("%s line %d: missing time value.",
390 filename, linenum);
391 if ((value = convtime(arg)) == -1)
392 fatal("%s line %d: invalid time value.",
393 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100394 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000395 *intptr = value;
396 break;
397
Damien Miller95def091999-11-25 00:26:21 +1100398 case oForwardAgent:
399 intptr = &options->forward_agent;
400parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000401 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000402 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100403 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
404 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000405 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100406 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000407 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100408 value = 0;
409 else
410 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
411 if (*activep && *intptr == -1)
412 *intptr = value;
413 break;
414
415 case oForwardX11:
416 intptr = &options->forward_x11;
417 goto parse_flag;
418
Darren Tucker0a118da2003-10-15 15:54:32 +1000419 case oForwardX11Trusted:
420 intptr = &options->forward_x11_trusted;
421 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000422
423 case oForwardX11Timeout:
424 intptr = &options->forward_x11_timeout;
425 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000426
Damien Miller95def091999-11-25 00:26:21 +1100427 case oGatewayPorts:
428 intptr = &options->gateway_ports;
429 goto parse_flag;
430
Darren Tuckere7d4b192006-07-12 22:17:10 +1000431 case oExitOnForwardFailure:
432 intptr = &options->exit_on_forward_failure;
433 goto parse_flag;
434
Damien Miller95def091999-11-25 00:26:21 +1100435 case oUsePrivilegedPort:
436 intptr = &options->use_privileged_port;
437 goto parse_flag;
438
Damien Miller95def091999-11-25 00:26:21 +1100439 case oPasswordAuthentication:
440 intptr = &options->password_authentication;
441 goto parse_flag;
442
Damien Miller01ed2272008-11-05 16:20:46 +1100443 case oZeroKnowledgePasswordAuthentication:
444 intptr = &options->zero_knowledge_password_authentication;
445 goto parse_flag;
446
Damien Miller874d77b2000-10-14 16:23:11 +1100447 case oKbdInteractiveAuthentication:
448 intptr = &options->kbd_interactive_authentication;
449 goto parse_flag;
450
451 case oKbdInteractiveDevices:
452 charptr = &options->kbd_interactive_devices;
453 goto parse_string;
454
Damien Miller0bc1bd82000-11-13 22:57:25 +1100455 case oPubkeyAuthentication:
456 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000457 goto parse_flag;
458
Damien Miller95def091999-11-25 00:26:21 +1100459 case oRSAAuthentication:
460 intptr = &options->rsa_authentication;
461 goto parse_flag;
462
463 case oRhostsRSAAuthentication:
464 intptr = &options->rhosts_rsa_authentication;
465 goto parse_flag;
466
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000467 case oHostbasedAuthentication:
468 intptr = &options->hostbased_authentication;
469 goto parse_flag;
470
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000471 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000472 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100473 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000474
Darren Tucker0efd1552003-08-26 11:49:55 +1000475 case oGssAuthentication:
476 intptr = &options->gss_authentication;
477 goto parse_flag;
478
479 case oGssDelegateCreds:
480 intptr = &options->gss_deleg_creds;
481 goto parse_flag;
482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oBatchMode:
484 intptr = &options->batch_mode;
485 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 case oCheckHostIP:
488 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000489 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller37876e92003-05-15 10:19:46 +1000491 case oVerifyHostKeyDNS:
492 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100493 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 case oStrictHostKeyChecking:
496 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100497parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000498 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000499 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000500 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100501 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100502 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000503 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100504 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000505 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100506 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000507 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100508 value = 2;
509 else
510 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
511 if (*activep && *intptr == -1)
512 *intptr = value;
513 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller95def091999-11-25 00:26:21 +1100515 case oCompression:
516 intptr = &options->compression;
517 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller12c150e2003-12-17 16:31:10 +1100519 case oTCPKeepAlive:
520 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100521 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000523 case oNoHostAuthenticationForLocalhost:
524 intptr = &options->no_host_authentication_for_localhost;
525 goto parse_flag;
526
Damien Miller95def091999-11-25 00:26:21 +1100527 case oNumberOfPasswordPrompts:
528 intptr = &options->number_of_password_prompts;
529 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Miller95def091999-11-25 00:26:21 +1100531 case oCompressionLevel:
532 intptr = &options->compression_level;
533 goto parse_int;
534
Damien Millera5539d22003-04-09 20:50:06 +1000535 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000536 arg = strdelim(&s);
537 if (!arg || *arg == '\0')
538 fatal("%.200s line %d: Missing argument.", filename, linenum);
539 if (arg[0] < '0' || arg[0] > '9')
540 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100541 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000542 if (arg == endofnumber)
543 fatal("%.200s line %d: Bad number.", filename, linenum);
544 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100545 case '\0':
546 scale = 1;
547 break;
Damien Millera5539d22003-04-09 20:50:06 +1000548 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100549 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000550 break;
551 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100552 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000553 break;
554 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100555 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000556 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100557 default:
558 fatal("%.200s line %d: Invalid RekeyLimit suffix",
559 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000560 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100561 val64 *= scale;
562 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100563 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100564 fatal("%.200s line %d: RekeyLimit too large",
565 filename, linenum);
566 if (val64 < 16)
567 fatal("%.200s line %d: RekeyLimit too small",
568 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100569 if (*activep && options->rekey_limit == -1)
570 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000571 break;
572
Damien Miller95def091999-11-25 00:26:21 +1100573 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000574 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000575 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100576 fatal("%.200s line %d: Missing argument.", filename, linenum);
577 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100578 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000579 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100580 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100581 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100582 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000583 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000584 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100585 }
586 break;
587
Damien Millerd3a18572000-06-07 19:55:44 +1000588 case oXAuthLocation:
589 charptr=&options->xauth_location;
590 goto parse_string;
591
Damien Miller95def091999-11-25 00:26:21 +1100592 case oUser:
593 charptr = &options->user;
594parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000595 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000596 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100597 fatal("%.200s line %d: Missing argument.", filename, linenum);
598 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000599 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100600 break;
601
602 case oGlobalKnownHostsFile:
603 charptr = &options->system_hostfile;
604 goto parse_string;
605
606 case oUserKnownHostsFile:
607 charptr = &options->user_hostfile;
608 goto parse_string;
609
Damien Millereba71ba2000-04-29 23:57:08 +1000610 case oGlobalKnownHostsFile2:
611 charptr = &options->system_hostfile2;
612 goto parse_string;
613
614 case oUserKnownHostsFile2:
615 charptr = &options->user_hostfile2;
616 goto parse_string;
617
Damien Miller95def091999-11-25 00:26:21 +1100618 case oHostName:
619 charptr = &options->hostname;
620 goto parse_string;
621
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000622 case oHostKeyAlias:
623 charptr = &options->host_key_alias;
624 goto parse_string;
625
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000626 case oPreferredAuthentications:
627 charptr = &options->preferred_authentications;
628 goto parse_string;
629
Ben Lindstrome0f88042001-04-30 13:06:24 +0000630 case oBindAddress:
631 charptr = &options->bind_address;
632 goto parse_string;
633
Damien Miller7ea845e2010-02-12 09:21:02 +1100634 case oPKCS11Provider:
635 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000636 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000637
Damien Miller95def091999-11-25 00:26:21 +1100638 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100639 charptr = &options->proxy_command;
640parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000641 if (s == NULL)
642 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100643 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100644 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100645 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100646 return 0;
647
648 case oPort:
649 intptr = &options->port;
650parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000651 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000652 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100653 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000654 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100655 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100656
657 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000658 value = strtol(arg, &endofnumber, 0);
659 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100660 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100661 if (*activep && *intptr == -1)
662 *intptr = value;
663 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller95def091999-11-25 00:26:21 +1100665 case oConnectionAttempts:
666 intptr = &options->connection_attempts;
667 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100668
Damien Miller95def091999-11-25 00:26:21 +1100669 case oCipher:
670 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000671 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000672 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000673 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000674 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100675 if (value == -1)
676 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100677 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100678 if (*activep && *intptr == -1)
679 *intptr = value;
680 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681
Damien Miller78928792000-04-12 20:17:38 +1000682 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000683 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000684 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000685 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000686 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000687 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100688 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000689 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000690 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000691 break;
692
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000693 case oMacs:
694 arg = strdelim(&s);
695 if (!arg || *arg == '\0')
696 fatal("%.200s line %d: Missing argument.", filename, linenum);
697 if (!mac_valid(arg))
698 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000700 if (*activep && options->macs == NULL)
701 options->macs = xstrdup(arg);
702 break;
703
Damien Millerd5f62bf2010-09-24 22:11:14 +1000704 case oKexAlgorithms:
705 arg = strdelim(&s);
706 if (!arg || *arg == '\0')
707 fatal("%.200s line %d: Missing argument.",
708 filename, linenum);
709 if (!kex_names_valid(arg))
710 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
711 filename, linenum, arg ? arg : "<NONE>");
712 if (*activep && options->kex_algorithms == NULL)
713 options->kex_algorithms = xstrdup(arg);
714 break;
715
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000716 case oHostKeyAlgorithms:
717 arg = strdelim(&s);
718 if (!arg || *arg == '\0')
719 fatal("%.200s line %d: Missing argument.", filename, linenum);
720 if (!key_names_valid2(arg))
721 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100722 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000723 if (*activep && options->hostkeyalgorithms == NULL)
724 options->hostkeyalgorithms = xstrdup(arg);
725 break;
726
Damien Miller78928792000-04-12 20:17:38 +1000727 case oProtocol:
728 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000729 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000730 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000731 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000732 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000733 if (value == SSH_PROTO_UNKNOWN)
734 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100735 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000736 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
737 *intptr = value;
738 break;
739
Damien Miller95def091999-11-25 00:26:21 +1100740 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100741 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000742 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000743 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100744 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000745 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100746 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100747 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
748 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100749 break;
750
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000751 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100752 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100753 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000754 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100755 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000756 fatal("%.200s line %d: Missing port argument.",
757 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100758
Damien Millera699d952008-11-03 19:27:34 +1100759 if (opcode == oLocalForward ||
760 opcode == oRemoteForward) {
761 arg2 = strdelim(&s);
762 if (arg2 == NULL || *arg2 == '\0')
763 fatal("%.200s line %d: Missing target argument.",
764 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100765
Damien Millera699d952008-11-03 19:27:34 +1100766 /* construct a string for parse_forward */
767 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
768 } else if (opcode == oDynamicForward) {
769 strlcpy(fwdarg, arg, sizeof(fwdarg));
770 }
771
772 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100773 opcode == oDynamicForward ? 1 : 0,
774 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000775 fatal("%.200s line %d: Bad forwarding specification.",
776 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100777
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000778 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100779 if (opcode == oLocalForward ||
780 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100781 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000782 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100783 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000784 }
Damien Miller95def091999-11-25 00:26:21 +1100785 break;
786
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000787 case oClearAllForwardings:
788 intptr = &options->clear_forwardings;
789 goto parse_flag;
790
Damien Miller95def091999-11-25 00:26:21 +1100791 case oHost:
792 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000793 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000794 if (match_pattern(host, arg)) {
795 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100796 *activep = 1;
797 break;
798 }
Damien Millerbe484b52000-07-15 14:14:16 +1000799 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100800 return 0;
801
802 case oEscapeChar:
803 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000804 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000805 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100806 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000807 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000808 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
809 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000810 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000811 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000812 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000813 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100814 else {
815 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100816 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100817 /* NOTREACHED */
818 value = 0; /* Avoid compiler warning. */
819 }
820 if (*activep && *intptr == -1)
821 *intptr = value;
822 break;
823
Damien Miller20a8f972003-05-18 20:50:30 +1000824 case oAddressFamily:
825 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000826 if (!arg || *arg == '\0')
827 fatal("%s line %d: missing address family.",
828 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000829 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000830 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000831 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000832 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000833 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000834 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000835 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000836 else
837 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000838 if (*activep && *intptr == -1)
839 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000840 break;
841
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000842 case oEnableSSHKeysign:
843 intptr = &options->enable_ssh_keysign;
844 goto parse_flag;
845
Damien Millerbd394c32004-03-08 23:12:36 +1100846 case oIdentitiesOnly:
847 intptr = &options->identities_only;
848 goto parse_flag;
849
Damien Miller509b0102003-12-17 16:33:10 +1100850 case oServerAliveInterval:
851 intptr = &options->server_alive_interval;
852 goto parse_time;
853
854 case oServerAliveCountMax:
855 intptr = &options->server_alive_count_max;
856 goto parse_int;
857
Darren Tucker46bc0752004-05-02 22:11:30 +1000858 case oSendEnv:
859 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
860 if (strchr(arg, '=') != NULL)
861 fatal("%s line %d: Invalid environment name.",
862 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100863 if (!*activep)
864 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000865 if (options->num_send_env >= MAX_SEND_ENV)
866 fatal("%s line %d: too many send env.",
867 filename, linenum);
868 options->send_env[options->num_send_env++] =
869 xstrdup(arg);
870 }
871 break;
872
Damien Miller0e220db2004-06-15 10:34:08 +1000873 case oControlPath:
874 charptr = &options->control_path;
875 goto parse_string;
876
877 case oControlMaster:
878 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000879 arg = strdelim(&s);
880 if (!arg || *arg == '\0')
881 fatal("%.200s line %d: Missing ControlMaster argument.",
882 filename, linenum);
883 value = 0; /* To avoid compiler warning... */
884 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
885 value = SSHCTL_MASTER_YES;
886 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
887 value = SSHCTL_MASTER_NO;
888 else if (strcmp(arg, "auto") == 0)
889 value = SSHCTL_MASTER_AUTO;
890 else if (strcmp(arg, "ask") == 0)
891 value = SSHCTL_MASTER_ASK;
892 else if (strcmp(arg, "autoask") == 0)
893 value = SSHCTL_MASTER_AUTO_ASK;
894 else
895 fatal("%.200s line %d: Bad ControlMaster argument.",
896 filename, linenum);
897 if (*activep && *intptr == -1)
898 *intptr = value;
899 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000900
Damien Millere11e1ea2010-08-03 16:04:46 +1000901 case oControlPersist:
902 /* no/false/yes/true, or a time spec */
903 intptr = &options->control_persist;
904 arg = strdelim(&s);
905 if (!arg || *arg == '\0')
906 fatal("%.200s line %d: Missing ControlPersist"
907 " argument.", filename, linenum);
908 value = 0;
909 value2 = 0; /* timeout */
910 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
911 value = 0;
912 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
913 value = 1;
914 else if ((value2 = convtime(arg)) >= 0)
915 value = 1;
916 else
917 fatal("%.200s line %d: Bad ControlPersist argument.",
918 filename, linenum);
919 if (*activep && *intptr == -1) {
920 *intptr = value;
921 options->control_persist_timeout = value2;
922 }
923 break;
924
Damien Millere1776152005-03-01 21:47:37 +1100925 case oHashKnownHosts:
926 intptr = &options->hash_known_hosts;
927 goto parse_flag;
928
Damien Millerd27b9472005-12-13 19:29:02 +1100929 case oTunnel:
930 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100931 arg = strdelim(&s);
932 if (!arg || *arg == '\0')
933 fatal("%s line %d: Missing yes/point-to-point/"
934 "ethernet/no argument.", filename, linenum);
935 value = 0; /* silence compiler */
936 if (strcasecmp(arg, "ethernet") == 0)
937 value = SSH_TUNMODE_ETHERNET;
938 else if (strcasecmp(arg, "point-to-point") == 0)
939 value = SSH_TUNMODE_POINTOPOINT;
940 else if (strcasecmp(arg, "yes") == 0)
941 value = SSH_TUNMODE_DEFAULT;
942 else if (strcasecmp(arg, "no") == 0)
943 value = SSH_TUNMODE_NO;
944 else
945 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
946 "no argument: %s", filename, linenum, arg);
947 if (*activep)
948 *intptr = value;
949 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100950
951 case oTunnelDevice:
952 arg = strdelim(&s);
953 if (!arg || *arg == '\0')
954 fatal("%.200s line %d: Missing argument.", filename, linenum);
955 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100956 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100957 fatal("%.200s line %d: Bad tun device.", filename, linenum);
958 if (*activep) {
959 options->tun_local = value;
960 options->tun_remote = value2;
961 }
962 break;
963
964 case oLocalCommand:
965 charptr = &options->local_command;
966 goto parse_command;
967
968 case oPermitLocalCommand:
969 intptr = &options->permit_local_command;
970 goto parse_flag;
971
Damien Miller10288242008-06-30 00:04:03 +1000972 case oVisualHostKey:
973 intptr = &options->visual_host_key;
974 goto parse_flag;
975
Darren Tucker71e4d542009-07-06 07:12:27 +1000976 case oUseRoaming:
977 intptr = &options->use_roaming;
978 goto parse_flag;
979
Ben Lindstrom4daea862002-06-09 20:04:02 +0000980 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000981 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000982 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000983 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000984
Damien Millerf9b3feb2003-05-16 11:38:32 +1000985 case oUnsupported:
986 error("%s line %d: Unsupported option \"%s\"",
987 filename, linenum, keyword);
988 return 0;
989
Damien Miller95def091999-11-25 00:26:21 +1100990 default:
991 fatal("process_config_line: Unimplemented opcode %d", opcode);
992 }
993
994 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000995 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000996 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000997 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000998 }
Damien Miller95def091999-11-25 00:26:21 +1100999 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000}
1001
1002
Damien Miller5428f641999-11-25 11:54:57 +11001003/*
1004 * Reads the config file and modifies the options accordingly. Options
1005 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001006 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001007 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001009int
Darren Tuckerfc959702004-07-17 16:12:08 +10001010read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +10001011 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012{
Damien Miller95def091999-11-25 00:26:21 +11001013 FILE *f;
1014 char line[1024];
1015 int active, linenum;
1016 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Miller57a44762004-04-20 20:11:57 +10001018 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001019 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Damien Miller57a44762004-04-20 20:11:57 +10001021 if (checkperm) {
1022 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001023
Damien Miller33793852004-06-15 10:27:55 +10001024 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001025 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001026 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001027 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001028 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001029 }
1030
Damien Miller95def091999-11-25 00:26:21 +11001031 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032
Damien Miller5428f641999-11-25 11:54:57 +11001033 /*
1034 * Mark that we are now processing the options. This flag is turned
1035 * on/off by Host specifications.
1036 */
Damien Miller95def091999-11-25 00:26:21 +11001037 active = 1;
1038 linenum = 0;
1039 while (fgets(line, sizeof(line), f)) {
1040 /* Update line number counter. */
1041 linenum++;
1042 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1043 bad_options++;
1044 }
1045 fclose(f);
1046 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001047 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001048 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001049 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050}
1051
Damien Miller5428f641999-11-25 11:54:57 +11001052/*
1053 * Initializes options to special values that indicate that they have not yet
1054 * been set. Read_config_file will only set options with this value. Options
1055 * are processed in the following order: command line, user config file,
1056 * system config file. Last, fill_default_options is called.
1057 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller4af51302000-04-16 11:18:38 +10001059void
Damien Miller95def091999-11-25 00:26:21 +11001060initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061{
Damien Miller95def091999-11-25 00:26:21 +11001062 memset(options, 'X', sizeof(*options));
1063 options->forward_agent = -1;
1064 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001065 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001066 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001067 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001068 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001069 options->gateway_ports = -1;
1070 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001071 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001072 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001073 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001074 options->gss_authentication = -1;
1075 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001076 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001077 options->kbd_interactive_authentication = -1;
1078 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001079 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001080 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001081 options->batch_mode = -1;
1082 options->check_host_ip = -1;
1083 options->strict_host_key_checking = -1;
1084 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001085 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001086 options->compression_level = -1;
1087 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001088 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001089 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001090 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001091 options->number_of_password_prompts = -1;
1092 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001093 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001094 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001095 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001096 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001097 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001098 options->num_identity_files = 0;
1099 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001100 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001101 options->proxy_command = NULL;
1102 options->user = NULL;
1103 options->escape_char = -1;
1104 options->system_hostfile = NULL;
1105 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001106 options->system_hostfile2 = NULL;
1107 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001108 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001109 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001110 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001111 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001112 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001113 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001114 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001115 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001116 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001117 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001118 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001119 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001120 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001121 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001122 options->server_alive_interval = -1;
1123 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001124 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001125 options->control_path = NULL;
1126 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001127 options->control_persist = -1;
1128 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001129 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001130 options->tun_open = -1;
1131 options->tun_local = -1;
1132 options->tun_remote = -1;
1133 options->local_command = NULL;
1134 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001135 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001136 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001137 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138}
1139
Damien Miller5428f641999-11-25 11:54:57 +11001140/*
1141 * Called after processing other sources of option data, this fills those
1142 * options for which no value has been specified with their default values.
1143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
Damien Miller4af51302000-04-16 11:18:38 +10001145void
Damien Miller95def091999-11-25 00:26:21 +11001146fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001148 int len;
1149
Damien Miller95def091999-11-25 00:26:21 +11001150 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001151 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001152 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001153 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001154 if (options->forward_x11_trusted == -1)
1155 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001156 if (options->forward_x11_timeout == -1)
1157 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001158 if (options->exit_on_forward_failure == -1)
1159 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001160 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001161 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001162 if (options->gateway_ports == -1)
1163 options->gateway_ports = 0;
1164 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001165 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001166 if (options->rsa_authentication == -1)
1167 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 if (options->pubkey_authentication == -1)
1169 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001170 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001171 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001172 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001173 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001174 if (options->gss_deleg_creds == -1)
1175 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001176 if (options->password_authentication == -1)
1177 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001178 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001179 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001180 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001181 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001182 if (options->hostbased_authentication == -1)
1183 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001184 if (options->batch_mode == -1)
1185 options->batch_mode = 0;
1186 if (options->check_host_ip == -1)
1187 options->check_host_ip = 1;
1188 if (options->strict_host_key_checking == -1)
1189 options->strict_host_key_checking = 2; /* 2 is default */
1190 if (options->compression == -1)
1191 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001192 if (options->tcp_keep_alive == -1)
1193 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001194 if (options->compression_level == -1)
1195 options->compression_level = 6;
1196 if (options->port == -1)
1197 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001198 if (options->address_family == -1)
1199 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001200 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001201 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001202 if (options->number_of_password_prompts == -1)
1203 options->number_of_password_prompts = 3;
1204 /* Selected in ssh_login(). */
1205 if (options->cipher == -1)
1206 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001207 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001208 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001209 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001210 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001211 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001212 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001213 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001214 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001215 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001217 xmalloc(len);
1218 snprintf(options->identity_files[options->num_identity_files++],
1219 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001220 }
1221 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001222 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1223 options->identity_files[options->num_identity_files] =
1224 xmalloc(len);
1225 snprintf(options->identity_files[options->num_identity_files++],
1226 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1227
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001228 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001229 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001230 xmalloc(len);
1231 snprintf(options->identity_files[options->num_identity_files++],
1232 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001233#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001234 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1235 options->identity_files[options->num_identity_files] =
1236 xmalloc(len);
1237 snprintf(options->identity_files[options->num_identity_files++],
1238 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001239#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001240 }
Damien Millereba71ba2000-04-29 23:57:08 +10001241 }
Damien Miller95def091999-11-25 00:26:21 +11001242 if (options->escape_char == -1)
1243 options->escape_char = '~';
1244 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001245 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001246 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001247 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001248 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001249 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001250 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001251 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001252 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001253 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001254 if (options->clear_forwardings == 1)
1255 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001256 if (options->no_host_authentication_for_localhost == - 1)
1257 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001258 if (options->identities_only == -1)
1259 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001260 if (options->enable_ssh_keysign == -1)
1261 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001262 if (options->rekey_limit == -1)
1263 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001264 if (options->verify_host_key_dns == -1)
1265 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001266 if (options->server_alive_interval == -1)
1267 options->server_alive_interval = 0;
1268 if (options->server_alive_count_max == -1)
1269 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001270 if (options->control_master == -1)
1271 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001272 if (options->control_persist == -1) {
1273 options->control_persist = 0;
1274 options->control_persist_timeout = 0;
1275 }
Damien Millere1776152005-03-01 21:47:37 +11001276 if (options->hash_known_hosts == -1)
1277 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001278 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001279 options->tun_open = SSH_TUNMODE_NO;
1280 if (options->tun_local == -1)
1281 options->tun_local = SSH_TUNID_ANY;
1282 if (options->tun_remote == -1)
1283 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001284 if (options->permit_local_command == -1)
1285 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001286 if (options->use_roaming == -1)
1287 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001288 if (options->visual_host_key == -1)
1289 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001290 if (options->zero_knowledge_password_authentication == -1)
1291 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001292 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001293 /* options->proxy_command should not be set by default */
1294 /* options->user will be set in the main program if appropriate */
1295 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001296 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001297 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001299
1300/*
1301 * parse_forward
1302 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001303 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001304 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001305 * dynamicfwd == 1
1306 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001307 * returns number of arguments parsed or zero on error
1308 */
1309int
Damien Miller4bf648f2009-02-14 16:28:21 +11001310parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001311{
1312 int i;
1313 char *p, *cp, *fwdarg[4];
1314
1315 memset(fwd, '\0', sizeof(*fwd));
1316
1317 cp = p = xstrdup(fwdspec);
1318
1319 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001320 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001321 cp++;
1322
1323 for (i = 0; i < 4; ++i)
1324 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1325 break;
1326
Damien Millerf4b39532008-11-03 19:28:21 +11001327 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001328 if (cp != NULL)
1329 i = 0; /* failure */
1330
1331 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001332 case 1:
1333 fwd->listen_host = NULL;
1334 fwd->listen_port = a2port(fwdarg[0]);
1335 fwd->connect_host = xstrdup("socks");
1336 break;
1337
1338 case 2:
1339 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1340 fwd->listen_port = a2port(fwdarg[1]);
1341 fwd->connect_host = xstrdup("socks");
1342 break;
1343
Damien Millerf91ee4c2005-03-01 21:24:33 +11001344 case 3:
1345 fwd->listen_host = NULL;
1346 fwd->listen_port = a2port(fwdarg[0]);
1347 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1348 fwd->connect_port = a2port(fwdarg[2]);
1349 break;
1350
1351 case 4:
1352 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1353 fwd->listen_port = a2port(fwdarg[1]);
1354 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1355 fwd->connect_port = a2port(fwdarg[3]);
1356 break;
1357 default:
1358 i = 0; /* failure */
1359 }
1360
1361 xfree(p);
1362
Damien Millera699d952008-11-03 19:27:34 +11001363 if (dynamicfwd) {
1364 if (!(i == 1 || i == 2))
1365 goto fail_free;
1366 } else {
1367 if (!(i == 3 || i == 4))
1368 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001369 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001370 goto fail_free;
1371 }
1372
Damien Miller4bf648f2009-02-14 16:28:21 +11001373 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001374 goto fail_free;
1375
1376 if (fwd->connect_host != NULL &&
1377 strlen(fwd->connect_host) >= NI_MAXHOST)
1378 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001379 if (fwd->listen_host != NULL &&
1380 strlen(fwd->listen_host) >= NI_MAXHOST)
1381 goto fail_free;
1382
Damien Millerf91ee4c2005-03-01 21:24:33 +11001383
1384 return (i);
1385
1386 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001387 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001388 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001389 fwd->connect_host = NULL;
1390 }
1391 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001392 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001393 fwd->listen_host = NULL;
1394 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001395 return (0);
1396}