blob: 927e7fefac58c2e4f7b492441013283f4d58a094 [file] [log] [blame]
Damien Millerfe924212011-05-15 08:44:45 +10001/* $OpenBSD: readconf.c,v 1.191 2011/05/06 21:31:38 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 Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in_systm.h>
23#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Millerc7b06362006-03-15 11:53:45 +110025#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100027#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100029#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100030#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100032#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110033
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100036#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "cipher.h"
38#include "pathnames.h"
39#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "readconf.h"
42#include "match.h"
43#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100044#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000045#include "kex.h"
46#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48/* Format of the configuration file:
49
50 # Configuration data is parsed as follows:
51 # 1. command line options
52 # 2. user-specific file
53 # 3. system-wide file
54 # Any configuration value is only changed the first time it is set.
55 # Thus, host-specific definitions should be at the beginning of the
56 # configuration file, and defaults at the end.
57
58 # Host-specific declarations. These may override anything above. A single
59 # host may match multiple declarations; these are processed in the order
60 # that they are given in.
61
62 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000063 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65 Host fake.com
66 HostName another.host.name.real.org
67 User blaah
68 Port 34289
69 ForwardX11 no
70 ForwardAgent no
71
72 Host books.com
73 RemoteForward 9999 shadows.cs.hut.fi:9999
74 Cipher 3des
75
76 Host fascist.blob.com
77 Port 23123
78 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079 PasswordAuthentication no
80
81 Host puukko.hut.fi
82 User t35124p
83 ProxyCommand ssh-proxy %h %p
84
85 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host *.su
89 Cipher none
90 PasswordAuthentication no
91
Damien Millerd27b9472005-12-13 19:29:02 +110092 Host vpn.fake.com
93 Tunnel yes
94 TunnelDevice 3
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 # Defaults for various options
97 Host *
98 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110099 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 PasswordAuthentication yes
101 RSAAuthentication yes
102 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100104 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105 IdentityFile ~/.ssh/identity
106 Port 22
107 EscapeChar ~
108
109*/
110
111/* Keyword tokens. */
112
Damien Miller95def091999-11-25 00:26:21 +1100113typedef enum {
114 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000115 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
116 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000117 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000118 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100119 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
120 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
121 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
122 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100123 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000124 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000126 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000127 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100128 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000129 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000130 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000131 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100132 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000133 oSendEnv, oControlPath, oControlMaster, oControlPersist,
134 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100135 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100136 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100137 oKexAlgorithms, oIPQoS,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100138 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139} OpCodes;
140
141/* Textual representations of the tokens. */
142
Damien Miller95def091999-11-25 00:26:21 +1100143static struct {
144 const char *name;
145 OpCodes opcode;
146} keywords[] = {
147 { "forwardagent", oForwardAgent },
148 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000149 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000150 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000151 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000152 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100153 { "gatewayports", oGatewayPorts },
154 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000155 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100157 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
158 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000161 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000162 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000163 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000164 { "challengeresponseauthentication", oChallengeResponseAuthentication },
165 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
166 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000167 { "kerberosauthentication", oUnsupported },
168 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000169 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000170#if defined(GSSAPI)
171 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000172 { "gssapidelegatecredentials", oGssDelegateCreds },
173#else
174 { "gssapiauthentication", oUnsupported },
175 { "gssapidelegatecredentials", oUnsupported },
176#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000177 { "fallbacktorsh", oDeprecated },
178 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100180 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100181 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000183 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100184 { "proxycommand", oProxyCommand },
185 { "port", oPort },
186 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000187 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000188 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000189 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "remoteforward", oRemoteForward },
191 { "localforward", oLocalForward },
192 { "user", oUser },
193 { "host", oHost },
194 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100196 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
197 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000198 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100199 { "connectionattempts", oConnectionAttempts },
200 { "batchmode", oBatchMode },
201 { "checkhostip", oCheckHostIP },
202 { "stricthostkeychecking", oStrictHostKeyChecking },
203 { "compression", oCompression },
204 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100205 { "tcpkeepalive", oTCPKeepAlive },
206 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100207 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000209 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000210 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000211 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000212 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100213#ifdef ENABLE_PKCS11
214 { "smartcarddevice", oPKCS11Provider },
215 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000216#else
217 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100218 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000219#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100220 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000221 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000222 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000224 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000225 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000226 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100227 { "serveraliveinterval", oServerAliveInterval },
228 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000229 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000230 { "controlpath", oControlPath },
231 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000232 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100233 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100234 { "tunnel", oTunnel },
235 { "tunneldevice", oTunnelDevice },
236 { "localcommand", oLocalCommand },
237 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000238 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000239 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100240#ifdef JPAKE
241 { "zeroknowledgepasswordauthentication",
242 oZeroKnowledgePasswordAuthentication },
243#else
244 { "zeroknowledgepasswordauthentication", oUnsupported },
245#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000246 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100247 { "ipqos", oIPQoS },
Damien Miller01ed2272008-11-05 16:20:46 +1100248
Ben Lindstrom65366a82001-12-06 16:32:47 +0000249 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100250};
251
Damien Miller5428f641999-11-25 11:54:57 +1100252/*
253 * Adds a local TCP/IP port forward to options. Never returns if there is an
254 * error.
255 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller4af51302000-04-16 11:18:38 +1000257void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100258add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259{
Damien Miller95def091999-11-25 00:26:21 +1100260 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000261#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100262 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000264 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100265#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000266 options->local_forwards = xrealloc(options->local_forwards,
267 options->num_local_forwards + 1,
268 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100269 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270
Damien Miller1a0442f2008-11-05 16:30:06 +1100271 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100273 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100274 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Damien Miller5428f641999-11-25 11:54:57 +1100277/*
278 * Adds a remote TCP/IP port forward to options. Never returns if there is
279 * an error.
280 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller4af51302000-04-16 11:18:38 +1000282void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
Damien Miller95def091999-11-25 00:26:21 +1100285 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000286
287 options->remote_forwards = xrealloc(options->remote_forwards,
288 options->num_remote_forwards + 1,
289 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100290 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291
Damien Miller1a0442f2008-11-05 16:30:06 +1100292 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100293 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100294 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100295 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000296 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000299static void
300clear_forwardings(Options *options)
301{
302 int i;
303
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100305 if (options->local_forwards[i].listen_host != NULL)
306 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100307 xfree(options->local_forwards[i].connect_host);
308 }
Damien Miller232cfb12010-06-26 09:50:30 +1000309 if (options->num_local_forwards > 0) {
310 xfree(options->local_forwards);
311 options->local_forwards = NULL;
312 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000313 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100314 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100315 if (options->remote_forwards[i].listen_host != NULL)
316 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100317 xfree(options->remote_forwards[i].connect_host);
318 }
Damien Miller232cfb12010-06-26 09:50:30 +1000319 if (options->num_remote_forwards > 0) {
320 xfree(options->remote_forwards);
321 options->remote_forwards = NULL;
322 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000323 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100324 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000325}
326
Damien Miller5428f641999-11-25 11:54:57 +1100327/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000328 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100329 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller4af51302000-04-16 11:18:38 +1000331static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100332parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000334 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller95def091999-11-25 00:26:21 +1100336 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100337 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100338 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000340 error("%s: line %d: Bad configuration option: %s",
341 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100342 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
Damien Miller5428f641999-11-25 11:54:57 +1100345/*
346 * Processes a single option line as used in the configuration files. This
347 * only sets those values that have not already been set.
348 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100349#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller2ccf6611999-11-15 15:25:10 +1100351int
352process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100353 char *line, const char *filename, int linenum,
354 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100356 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerfe924212011-05-15 08:44:45 +1000357 int negated, opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100358 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100359 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100360 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100361 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Millerc652cac2003-05-14 13:40:54 +1000363 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100364 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000365 if (strchr(WHITESPACE, line[len]) == NULL)
366 break;
367 line[len] = '\0';
368 }
369
Damien Millerbe484b52000-07-15 14:14:16 +1000370 s = line;
371 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100372 if ((keyword = strdelim(&s)) == NULL)
373 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000374 /* Ignore leading whitespace. */
375 if (*keyword == '\0')
376 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000377 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100378 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Damien Miller37023962000-07-11 17:31:38 +1000380 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381
Damien Miller95def091999-11-25 00:26:21 +1100382 switch (opcode) {
383 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100384 /* don't panic, but count bad options */
385 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100386 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000387 case oConnectTimeout:
388 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100389parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000390 arg = strdelim(&s);
391 if (!arg || *arg == '\0')
392 fatal("%s line %d: missing time value.",
393 filename, linenum);
394 if ((value = convtime(arg)) == -1)
395 fatal("%s line %d: invalid time value.",
396 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100397 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000398 *intptr = value;
399 break;
400
Damien Miller95def091999-11-25 00:26:21 +1100401 case oForwardAgent:
402 intptr = &options->forward_agent;
403parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000404 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000405 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100406 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
407 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000408 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100409 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000410 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100411 value = 0;
412 else
413 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
414 if (*activep && *intptr == -1)
415 *intptr = value;
416 break;
417
418 case oForwardX11:
419 intptr = &options->forward_x11;
420 goto parse_flag;
421
Darren Tucker0a118da2003-10-15 15:54:32 +1000422 case oForwardX11Trusted:
423 intptr = &options->forward_x11_trusted;
424 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000425
426 case oForwardX11Timeout:
427 intptr = &options->forward_x11_timeout;
428 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000429
Damien Miller95def091999-11-25 00:26:21 +1100430 case oGatewayPorts:
431 intptr = &options->gateway_ports;
432 goto parse_flag;
433
Darren Tuckere7d4b192006-07-12 22:17:10 +1000434 case oExitOnForwardFailure:
435 intptr = &options->exit_on_forward_failure;
436 goto parse_flag;
437
Damien Miller95def091999-11-25 00:26:21 +1100438 case oUsePrivilegedPort:
439 intptr = &options->use_privileged_port;
440 goto parse_flag;
441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oPasswordAuthentication:
443 intptr = &options->password_authentication;
444 goto parse_flag;
445
Damien Miller01ed2272008-11-05 16:20:46 +1100446 case oZeroKnowledgePasswordAuthentication:
447 intptr = &options->zero_knowledge_password_authentication;
448 goto parse_flag;
449
Damien Miller874d77b2000-10-14 16:23:11 +1100450 case oKbdInteractiveAuthentication:
451 intptr = &options->kbd_interactive_authentication;
452 goto parse_flag;
453
454 case oKbdInteractiveDevices:
455 charptr = &options->kbd_interactive_devices;
456 goto parse_string;
457
Damien Miller0bc1bd82000-11-13 22:57:25 +1100458 case oPubkeyAuthentication:
459 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000460 goto parse_flag;
461
Damien Miller95def091999-11-25 00:26:21 +1100462 case oRSAAuthentication:
463 intptr = &options->rsa_authentication;
464 goto parse_flag;
465
466 case oRhostsRSAAuthentication:
467 intptr = &options->rhosts_rsa_authentication;
468 goto parse_flag;
469
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000470 case oHostbasedAuthentication:
471 intptr = &options->hostbased_authentication;
472 goto parse_flag;
473
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000474 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000475 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100476 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000477
Darren Tucker0efd1552003-08-26 11:49:55 +1000478 case oGssAuthentication:
479 intptr = &options->gss_authentication;
480 goto parse_flag;
481
482 case oGssDelegateCreds:
483 intptr = &options->gss_deleg_creds;
484 goto parse_flag;
485
Damien Miller95def091999-11-25 00:26:21 +1100486 case oBatchMode:
487 intptr = &options->batch_mode;
488 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller95def091999-11-25 00:26:21 +1100490 case oCheckHostIP:
491 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000492 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller37876e92003-05-15 10:19:46 +1000494 case oVerifyHostKeyDNS:
495 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100496 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000497
Damien Miller95def091999-11-25 00:26:21 +1100498 case oStrictHostKeyChecking:
499 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100500parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000501 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000502 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000503 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100504 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100505 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000506 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100507 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000508 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100509 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000510 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100511 value = 2;
512 else
513 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
514 if (*activep && *intptr == -1)
515 *intptr = value;
516 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller95def091999-11-25 00:26:21 +1100518 case oCompression:
519 intptr = &options->compression;
520 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller12c150e2003-12-17 16:31:10 +1100522 case oTCPKeepAlive:
523 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100524 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000526 case oNoHostAuthenticationForLocalhost:
527 intptr = &options->no_host_authentication_for_localhost;
528 goto parse_flag;
529
Damien Miller95def091999-11-25 00:26:21 +1100530 case oNumberOfPasswordPrompts:
531 intptr = &options->number_of_password_prompts;
532 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oCompressionLevel:
535 intptr = &options->compression_level;
536 goto parse_int;
537
Damien Millera5539d22003-04-09 20:50:06 +1000538 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000539 arg = strdelim(&s);
540 if (!arg || *arg == '\0')
541 fatal("%.200s line %d: Missing argument.", filename, linenum);
542 if (arg[0] < '0' || arg[0] > '9')
543 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100544 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000545 if (arg == endofnumber)
546 fatal("%.200s line %d: Bad number.", filename, linenum);
547 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100548 case '\0':
549 scale = 1;
550 break;
Damien Millera5539d22003-04-09 20:50:06 +1000551 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100552 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000553 break;
554 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100555 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000556 break;
557 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100558 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000559 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100560 default:
561 fatal("%.200s line %d: Invalid RekeyLimit suffix",
562 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000563 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100564 val64 *= scale;
565 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100566 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100567 fatal("%.200s line %d: RekeyLimit too large",
568 filename, linenum);
569 if (val64 < 16)
570 fatal("%.200s line %d: RekeyLimit too small",
571 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100572 if (*activep && options->rekey_limit == -1)
573 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000574 break;
575
Damien Miller95def091999-11-25 00:26:21 +1100576 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000577 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000578 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100579 fatal("%.200s line %d: Missing argument.", filename, linenum);
580 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100581 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000582 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100583 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100585 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000586 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000587 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100588 }
589 break;
590
Damien Millerd3a18572000-06-07 19:55:44 +1000591 case oXAuthLocation:
592 charptr=&options->xauth_location;
593 goto parse_string;
594
Damien Miller95def091999-11-25 00:26:21 +1100595 case oUser:
596 charptr = &options->user;
597parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000598 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000599 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100600 fatal("%.200s line %d: Missing argument.", filename, linenum);
601 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000602 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100603 break;
604
605 case oGlobalKnownHostsFile:
606 charptr = &options->system_hostfile;
607 goto parse_string;
608
609 case oUserKnownHostsFile:
610 charptr = &options->user_hostfile;
611 goto parse_string;
612
Damien Millereba71ba2000-04-29 23:57:08 +1000613 case oGlobalKnownHostsFile2:
614 charptr = &options->system_hostfile2;
615 goto parse_string;
616
617 case oUserKnownHostsFile2:
618 charptr = &options->user_hostfile2;
619 goto parse_string;
620
Damien Miller95def091999-11-25 00:26:21 +1100621 case oHostName:
622 charptr = &options->hostname;
623 goto parse_string;
624
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000625 case oHostKeyAlias:
626 charptr = &options->host_key_alias;
627 goto parse_string;
628
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000629 case oPreferredAuthentications:
630 charptr = &options->preferred_authentications;
631 goto parse_string;
632
Ben Lindstrome0f88042001-04-30 13:06:24 +0000633 case oBindAddress:
634 charptr = &options->bind_address;
635 goto parse_string;
636
Damien Miller7ea845e2010-02-12 09:21:02 +1100637 case oPKCS11Provider:
638 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000639 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000640
Damien Miller95def091999-11-25 00:26:21 +1100641 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100642 charptr = &options->proxy_command;
643parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000644 if (s == NULL)
645 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100646 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100647 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100648 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100649 return 0;
650
651 case oPort:
652 intptr = &options->port;
653parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000654 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000655 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100656 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000657 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100658 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100659
660 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000661 value = strtol(arg, &endofnumber, 0);
662 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100663 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100664 if (*activep && *intptr == -1)
665 *intptr = value;
666 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 case oConnectionAttempts:
669 intptr = &options->connection_attempts;
670 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100671
Damien Miller95def091999-11-25 00:26:21 +1100672 case oCipher:
673 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000674 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000675 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000676 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000677 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100678 if (value == -1)
679 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100681 if (*activep && *intptr == -1)
682 *intptr = value;
683 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller78928792000-04-12 20:17:38 +1000685 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000686 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000687 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000688 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000689 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000690 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100691 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000692 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000693 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000694 break;
695
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000696 case oMacs:
697 arg = strdelim(&s);
698 if (!arg || *arg == '\0')
699 fatal("%.200s line %d: Missing argument.", filename, linenum);
700 if (!mac_valid(arg))
701 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000703 if (*activep && options->macs == NULL)
704 options->macs = xstrdup(arg);
705 break;
706
Damien Millerd5f62bf2010-09-24 22:11:14 +1000707 case oKexAlgorithms:
708 arg = strdelim(&s);
709 if (!arg || *arg == '\0')
710 fatal("%.200s line %d: Missing argument.",
711 filename, linenum);
712 if (!kex_names_valid(arg))
713 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
714 filename, linenum, arg ? arg : "<NONE>");
715 if (*activep && options->kex_algorithms == NULL)
716 options->kex_algorithms = xstrdup(arg);
717 break;
718
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000719 case oHostKeyAlgorithms:
720 arg = strdelim(&s);
721 if (!arg || *arg == '\0')
722 fatal("%.200s line %d: Missing argument.", filename, linenum);
723 if (!key_names_valid2(arg))
724 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100725 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000726 if (*activep && options->hostkeyalgorithms == NULL)
727 options->hostkeyalgorithms = xstrdup(arg);
728 break;
729
Damien Miller78928792000-04-12 20:17:38 +1000730 case oProtocol:
731 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000732 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000733 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000734 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000735 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000736 if (value == SSH_PROTO_UNKNOWN)
737 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100738 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000739 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
740 *intptr = value;
741 break;
742
Damien Miller95def091999-11-25 00:26:21 +1100743 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100744 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000745 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000746 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100747 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000748 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100749 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100750 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
751 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100752 break;
753
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000754 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100755 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100756 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000757 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100758 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000759 fatal("%.200s line %d: Missing port argument.",
760 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100761
Damien Millera699d952008-11-03 19:27:34 +1100762 if (opcode == oLocalForward ||
763 opcode == oRemoteForward) {
764 arg2 = strdelim(&s);
765 if (arg2 == NULL || *arg2 == '\0')
766 fatal("%.200s line %d: Missing target argument.",
767 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100768
Damien Millera699d952008-11-03 19:27:34 +1100769 /* construct a string for parse_forward */
770 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
771 } else if (opcode == oDynamicForward) {
772 strlcpy(fwdarg, arg, sizeof(fwdarg));
773 }
774
775 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100776 opcode == oDynamicForward ? 1 : 0,
777 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000778 fatal("%.200s line %d: Bad forwarding specification.",
779 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100780
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000781 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100782 if (opcode == oLocalForward ||
783 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100784 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000785 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100786 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000787 }
Damien Miller95def091999-11-25 00:26:21 +1100788 break;
789
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000790 case oClearAllForwardings:
791 intptr = &options->clear_forwardings;
792 goto parse_flag;
793
Damien Miller95def091999-11-25 00:26:21 +1100794 case oHost:
795 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +1000796 arg2 = NULL;
797 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
798 negated = *arg == '!';
799 if (negated)
800 arg++;
Damien Miller37023962000-07-11 17:31:38 +1000801 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +1000802 if (negated) {
803 debug("%.200s line %d: Skipping Host "
804 "block because of negated match "
805 "for %.100s", filename, linenum,
806 arg);
807 *activep = 0;
808 break;
809 }
810 if (!*activep)
811 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +1100812 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +1100813 }
Damien Millerfe924212011-05-15 08:44:45 +1000814 }
815 if (*activep)
816 debug("%.200s line %d: Applying options for %.100s",
817 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +1000818 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100819 return 0;
820
821 case oEscapeChar:
822 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000823 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000824 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100825 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000826 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000827 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
828 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000829 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000830 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000831 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000832 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100833 else {
834 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100835 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100836 /* NOTREACHED */
837 value = 0; /* Avoid compiler warning. */
838 }
839 if (*activep && *intptr == -1)
840 *intptr = value;
841 break;
842
Damien Miller20a8f972003-05-18 20:50:30 +1000843 case oAddressFamily:
844 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000845 if (!arg || *arg == '\0')
846 fatal("%s line %d: missing address family.",
847 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000848 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000849 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000850 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000851 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000852 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000853 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000854 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000855 else
856 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000857 if (*activep && *intptr == -1)
858 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000859 break;
860
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000861 case oEnableSSHKeysign:
862 intptr = &options->enable_ssh_keysign;
863 goto parse_flag;
864
Damien Millerbd394c32004-03-08 23:12:36 +1100865 case oIdentitiesOnly:
866 intptr = &options->identities_only;
867 goto parse_flag;
868
Damien Miller509b0102003-12-17 16:33:10 +1100869 case oServerAliveInterval:
870 intptr = &options->server_alive_interval;
871 goto parse_time;
872
873 case oServerAliveCountMax:
874 intptr = &options->server_alive_count_max;
875 goto parse_int;
876
Darren Tucker46bc0752004-05-02 22:11:30 +1000877 case oSendEnv:
878 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
879 if (strchr(arg, '=') != NULL)
880 fatal("%s line %d: Invalid environment name.",
881 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100882 if (!*activep)
883 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000884 if (options->num_send_env >= MAX_SEND_ENV)
885 fatal("%s line %d: too many send env.",
886 filename, linenum);
887 options->send_env[options->num_send_env++] =
888 xstrdup(arg);
889 }
890 break;
891
Damien Miller0e220db2004-06-15 10:34:08 +1000892 case oControlPath:
893 charptr = &options->control_path;
894 goto parse_string;
895
896 case oControlMaster:
897 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000898 arg = strdelim(&s);
899 if (!arg || *arg == '\0')
900 fatal("%.200s line %d: Missing ControlMaster argument.",
901 filename, linenum);
902 value = 0; /* To avoid compiler warning... */
903 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
904 value = SSHCTL_MASTER_YES;
905 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
906 value = SSHCTL_MASTER_NO;
907 else if (strcmp(arg, "auto") == 0)
908 value = SSHCTL_MASTER_AUTO;
909 else if (strcmp(arg, "ask") == 0)
910 value = SSHCTL_MASTER_ASK;
911 else if (strcmp(arg, "autoask") == 0)
912 value = SSHCTL_MASTER_AUTO_ASK;
913 else
914 fatal("%.200s line %d: Bad ControlMaster argument.",
915 filename, linenum);
916 if (*activep && *intptr == -1)
917 *intptr = value;
918 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000919
Damien Millere11e1ea2010-08-03 16:04:46 +1000920 case oControlPersist:
921 /* no/false/yes/true, or a time spec */
922 intptr = &options->control_persist;
923 arg = strdelim(&s);
924 if (!arg || *arg == '\0')
925 fatal("%.200s line %d: Missing ControlPersist"
926 " argument.", filename, linenum);
927 value = 0;
928 value2 = 0; /* timeout */
929 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
930 value = 0;
931 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
932 value = 1;
933 else if ((value2 = convtime(arg)) >= 0)
934 value = 1;
935 else
936 fatal("%.200s line %d: Bad ControlPersist argument.",
937 filename, linenum);
938 if (*activep && *intptr == -1) {
939 *intptr = value;
940 options->control_persist_timeout = value2;
941 }
942 break;
943
Damien Millere1776152005-03-01 21:47:37 +1100944 case oHashKnownHosts:
945 intptr = &options->hash_known_hosts;
946 goto parse_flag;
947
Damien Millerd27b9472005-12-13 19:29:02 +1100948 case oTunnel:
949 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100950 arg = strdelim(&s);
951 if (!arg || *arg == '\0')
952 fatal("%s line %d: Missing yes/point-to-point/"
953 "ethernet/no argument.", filename, linenum);
954 value = 0; /* silence compiler */
955 if (strcasecmp(arg, "ethernet") == 0)
956 value = SSH_TUNMODE_ETHERNET;
957 else if (strcasecmp(arg, "point-to-point") == 0)
958 value = SSH_TUNMODE_POINTOPOINT;
959 else if (strcasecmp(arg, "yes") == 0)
960 value = SSH_TUNMODE_DEFAULT;
961 else if (strcasecmp(arg, "no") == 0)
962 value = SSH_TUNMODE_NO;
963 else
964 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
965 "no argument: %s", filename, linenum, arg);
966 if (*activep)
967 *intptr = value;
968 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100969
970 case oTunnelDevice:
971 arg = strdelim(&s);
972 if (!arg || *arg == '\0')
973 fatal("%.200s line %d: Missing argument.", filename, linenum);
974 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100975 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100976 fatal("%.200s line %d: Bad tun device.", filename, linenum);
977 if (*activep) {
978 options->tun_local = value;
979 options->tun_remote = value2;
980 }
981 break;
982
983 case oLocalCommand:
984 charptr = &options->local_command;
985 goto parse_command;
986
987 case oPermitLocalCommand:
988 intptr = &options->permit_local_command;
989 goto parse_flag;
990
Damien Miller10288242008-06-30 00:04:03 +1000991 case oVisualHostKey:
992 intptr = &options->visual_host_key;
993 goto parse_flag;
994
Damien Miller0dac6fb2010-11-20 15:19:38 +1100995 case oIPQoS:
996 arg = strdelim(&s);
997 if ((value = parse_ipqos(arg)) == -1)
998 fatal("%s line %d: Bad IPQoS value: %s",
999 filename, linenum, arg);
1000 arg = strdelim(&s);
1001 if (arg == NULL)
1002 value2 = value;
1003 else if ((value2 = parse_ipqos(arg)) == -1)
1004 fatal("%s line %d: Bad IPQoS value: %s",
1005 filename, linenum, arg);
1006 if (*activep) {
1007 options->ip_qos_interactive = value;
1008 options->ip_qos_bulk = value2;
1009 }
1010 break;
1011
Darren Tucker71e4d542009-07-06 07:12:27 +10001012 case oUseRoaming:
1013 intptr = &options->use_roaming;
1014 goto parse_flag;
1015
Ben Lindstrom4daea862002-06-09 20:04:02 +00001016 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001017 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001018 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001019 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001020
Damien Millerf9b3feb2003-05-16 11:38:32 +10001021 case oUnsupported:
1022 error("%s line %d: Unsupported option \"%s\"",
1023 filename, linenum, keyword);
1024 return 0;
1025
Damien Miller95def091999-11-25 00:26:21 +11001026 default:
1027 fatal("process_config_line: Unimplemented opcode %d", opcode);
1028 }
1029
1030 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001031 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001032 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001033 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001034 }
Damien Miller95def091999-11-25 00:26:21 +11001035 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036}
1037
1038
Damien Miller5428f641999-11-25 11:54:57 +11001039/*
1040 * Reads the config file and modifies the options accordingly. Options
1041 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001042 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001045int
Darren Tuckerfc959702004-07-17 16:12:08 +10001046read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +10001047 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048{
Damien Miller95def091999-11-25 00:26:21 +11001049 FILE *f;
1050 char line[1024];
1051 int active, linenum;
1052 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053
Damien Miller57a44762004-04-20 20:11:57 +10001054 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001055 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056
Damien Miller57a44762004-04-20 20:11:57 +10001057 if (checkperm) {
1058 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001059
Damien Miller33793852004-06-15 10:27:55 +10001060 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001061 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001062 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001063 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001064 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001065 }
1066
Damien Miller95def091999-11-25 00:26:21 +11001067 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068
Damien Miller5428f641999-11-25 11:54:57 +11001069 /*
1070 * Mark that we are now processing the options. This flag is turned
1071 * on/off by Host specifications.
1072 */
Damien Miller95def091999-11-25 00:26:21 +11001073 active = 1;
1074 linenum = 0;
1075 while (fgets(line, sizeof(line), f)) {
1076 /* Update line number counter. */
1077 linenum++;
1078 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1079 bad_options++;
1080 }
1081 fclose(f);
1082 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001083 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001084 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001085 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086}
1087
Damien Miller5428f641999-11-25 11:54:57 +11001088/*
1089 * Initializes options to special values that indicate that they have not yet
1090 * been set. Read_config_file will only set options with this value. Options
1091 * are processed in the following order: command line, user config file,
1092 * system config file. Last, fill_default_options is called.
1093 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094
Damien Miller4af51302000-04-16 11:18:38 +10001095void
Damien Miller95def091999-11-25 00:26:21 +11001096initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097{
Damien Miller95def091999-11-25 00:26:21 +11001098 memset(options, 'X', sizeof(*options));
1099 options->forward_agent = -1;
1100 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001101 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001102 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001103 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001104 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001105 options->gateway_ports = -1;
1106 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001107 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001108 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001109 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001110 options->gss_authentication = -1;
1111 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001112 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001113 options->kbd_interactive_authentication = -1;
1114 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001115 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001116 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001117 options->batch_mode = -1;
1118 options->check_host_ip = -1;
1119 options->strict_host_key_checking = -1;
1120 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001121 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001122 options->compression_level = -1;
1123 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001124 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001125 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001126 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001127 options->number_of_password_prompts = -1;
1128 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001129 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001130 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001131 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001132 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001133 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001134 options->num_identity_files = 0;
1135 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001136 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001137 options->proxy_command = NULL;
1138 options->user = NULL;
1139 options->escape_char = -1;
1140 options->system_hostfile = NULL;
1141 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001142 options->system_hostfile2 = NULL;
1143 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001144 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001145 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001146 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001147 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001148 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001149 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001150 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001151 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001152 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001153 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001154 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001155 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001156 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001157 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001158 options->server_alive_interval = -1;
1159 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001160 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001161 options->control_path = NULL;
1162 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001163 options->control_persist = -1;
1164 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001165 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001166 options->tun_open = -1;
1167 options->tun_local = -1;
1168 options->tun_remote = -1;
1169 options->local_command = NULL;
1170 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001171 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001172 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001173 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001174 options->ip_qos_interactive = -1;
1175 options->ip_qos_bulk = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176}
1177
Damien Miller5428f641999-11-25 11:54:57 +11001178/*
1179 * Called after processing other sources of option data, this fills those
1180 * options for which no value has been specified with their default values.
1181 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182
Damien Miller4af51302000-04-16 11:18:38 +10001183void
Damien Miller95def091999-11-25 00:26:21 +11001184fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001186 int len;
1187
Damien Miller95def091999-11-25 00:26:21 +11001188 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001189 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001190 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001191 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001192 if (options->forward_x11_trusted == -1)
1193 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001194 if (options->forward_x11_timeout == -1)
1195 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001196 if (options->exit_on_forward_failure == -1)
1197 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001198 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001199 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001200 if (options->gateway_ports == -1)
1201 options->gateway_ports = 0;
1202 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001203 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001204 if (options->rsa_authentication == -1)
1205 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001206 if (options->pubkey_authentication == -1)
1207 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001208 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001209 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001210 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001211 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001212 if (options->gss_deleg_creds == -1)
1213 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001214 if (options->password_authentication == -1)
1215 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001216 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001217 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001218 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001219 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001220 if (options->hostbased_authentication == -1)
1221 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001222 if (options->batch_mode == -1)
1223 options->batch_mode = 0;
1224 if (options->check_host_ip == -1)
1225 options->check_host_ip = 1;
1226 if (options->strict_host_key_checking == -1)
1227 options->strict_host_key_checking = 2; /* 2 is default */
1228 if (options->compression == -1)
1229 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001230 if (options->tcp_keep_alive == -1)
1231 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001232 if (options->compression_level == -1)
1233 options->compression_level = 6;
1234 if (options->port == -1)
1235 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001236 if (options->address_family == -1)
1237 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001238 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001239 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001240 if (options->number_of_password_prompts == -1)
1241 options->number_of_password_prompts = 3;
1242 /* Selected in ssh_login(). */
1243 if (options->cipher == -1)
1244 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001245 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001246 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001247 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001248 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001249 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001250 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001251 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001252 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001253 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001254 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001255 xmalloc(len);
1256 snprintf(options->identity_files[options->num_identity_files++],
1257 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001258 }
1259 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001260 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1261 options->identity_files[options->num_identity_files] =
1262 xmalloc(len);
1263 snprintf(options->identity_files[options->num_identity_files++],
1264 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1265
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001266 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001267 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001268 xmalloc(len);
1269 snprintf(options->identity_files[options->num_identity_files++],
1270 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001271#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001272 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1273 options->identity_files[options->num_identity_files] =
1274 xmalloc(len);
1275 snprintf(options->identity_files[options->num_identity_files++],
1276 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001277#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001278 }
Damien Millereba71ba2000-04-29 23:57:08 +10001279 }
Damien Miller95def091999-11-25 00:26:21 +11001280 if (options->escape_char == -1)
1281 options->escape_char = '~';
1282 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001283 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001284 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001285 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001286 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001287 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001288 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001289 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001290 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001291 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001292 if (options->clear_forwardings == 1)
1293 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001294 if (options->no_host_authentication_for_localhost == - 1)
1295 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001296 if (options->identities_only == -1)
1297 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001298 if (options->enable_ssh_keysign == -1)
1299 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001300 if (options->rekey_limit == -1)
1301 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001302 if (options->verify_host_key_dns == -1)
1303 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001304 if (options->server_alive_interval == -1)
1305 options->server_alive_interval = 0;
1306 if (options->server_alive_count_max == -1)
1307 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001308 if (options->control_master == -1)
1309 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001310 if (options->control_persist == -1) {
1311 options->control_persist = 0;
1312 options->control_persist_timeout = 0;
1313 }
Damien Millere1776152005-03-01 21:47:37 +11001314 if (options->hash_known_hosts == -1)
1315 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001316 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001317 options->tun_open = SSH_TUNMODE_NO;
1318 if (options->tun_local == -1)
1319 options->tun_local = SSH_TUNID_ANY;
1320 if (options->tun_remote == -1)
1321 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001322 if (options->permit_local_command == -1)
1323 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001324 if (options->use_roaming == -1)
1325 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001326 if (options->visual_host_key == -1)
1327 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001328 if (options->zero_knowledge_password_authentication == -1)
1329 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001330 if (options->ip_qos_interactive == -1)
1331 options->ip_qos_interactive = IPTOS_LOWDELAY;
1332 if (options->ip_qos_bulk == -1)
1333 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Millerd27b9472005-12-13 19:29:02 +11001334 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001335 /* options->proxy_command should not be set by default */
1336 /* options->user will be set in the main program if appropriate */
1337 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001338 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001339 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001341
1342/*
1343 * parse_forward
1344 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001345 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001346 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001347 * dynamicfwd == 1
1348 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001349 * returns number of arguments parsed or zero on error
1350 */
1351int
Damien Miller4bf648f2009-02-14 16:28:21 +11001352parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001353{
1354 int i;
1355 char *p, *cp, *fwdarg[4];
1356
1357 memset(fwd, '\0', sizeof(*fwd));
1358
1359 cp = p = xstrdup(fwdspec);
1360
1361 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001362 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001363 cp++;
1364
1365 for (i = 0; i < 4; ++i)
1366 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1367 break;
1368
Damien Millerf4b39532008-11-03 19:28:21 +11001369 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001370 if (cp != NULL)
1371 i = 0; /* failure */
1372
1373 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001374 case 1:
1375 fwd->listen_host = NULL;
1376 fwd->listen_port = a2port(fwdarg[0]);
1377 fwd->connect_host = xstrdup("socks");
1378 break;
1379
1380 case 2:
1381 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1382 fwd->listen_port = a2port(fwdarg[1]);
1383 fwd->connect_host = xstrdup("socks");
1384 break;
1385
Damien Millerf91ee4c2005-03-01 21:24:33 +11001386 case 3:
1387 fwd->listen_host = NULL;
1388 fwd->listen_port = a2port(fwdarg[0]);
1389 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1390 fwd->connect_port = a2port(fwdarg[2]);
1391 break;
1392
1393 case 4:
1394 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1395 fwd->listen_port = a2port(fwdarg[1]);
1396 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1397 fwd->connect_port = a2port(fwdarg[3]);
1398 break;
1399 default:
1400 i = 0; /* failure */
1401 }
1402
1403 xfree(p);
1404
Damien Millera699d952008-11-03 19:27:34 +11001405 if (dynamicfwd) {
1406 if (!(i == 1 || i == 2))
1407 goto fail_free;
1408 } else {
1409 if (!(i == 3 || i == 4))
1410 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001411 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001412 goto fail_free;
1413 }
1414
Damien Miller4bf648f2009-02-14 16:28:21 +11001415 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001416 goto fail_free;
1417
1418 if (fwd->connect_host != NULL &&
1419 strlen(fwd->connect_host) >= NI_MAXHOST)
1420 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001421 if (fwd->listen_host != NULL &&
1422 strlen(fwd->listen_host) >= NI_MAXHOST)
1423 goto fail_free;
1424
Damien Millerf91ee4c2005-03-01 21:24:33 +11001425
1426 return (i);
1427
1428 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001429 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001430 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001431 fwd->connect_host = NULL;
1432 }
1433 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001434 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001435 fwd->listen_host = NULL;
1436 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001437 return (0);
1438}