blob: 8bdc8caf1abd30fac22ad442060b17f15fbe4dfe [file] [log] [blame]
Damien Miller7ea845e2010-02-12 09:21:02 +11001/* $OpenBSD: readconf.c,v 1.183 2010/02/08 10:50:20 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
20
21#include <netinet/in.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000114 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000115 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100117 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100121 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000124 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100126 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000127 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000128 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000129 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100130 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100131 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100132 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100133 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
134 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135} OpCodes;
136
137/* Textual representations of the tokens. */
138
Damien Miller95def091999-11-25 00:26:21 +1100139static struct {
140 const char *name;
141 OpCodes opcode;
142} keywords[] = {
143 { "forwardagent", oForwardAgent },
144 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000145 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000146 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000147 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100148 { "gatewayports", oGatewayPorts },
149 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000150 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100151 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100152 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
153 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000156 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000157 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000158 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000159 { "challengeresponseauthentication", oChallengeResponseAuthentication },
160 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
161 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000162 { "kerberosauthentication", oUnsupported },
163 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000164 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000165#if defined(GSSAPI)
166 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000167 { "gssapidelegatecredentials", oGssDelegateCreds },
168#else
169 { "gssapiauthentication", oUnsupported },
170 { "gssapidelegatecredentials", oUnsupported },
171#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000172 { "fallbacktorsh", oDeprecated },
173 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100174 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100175 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100176 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000178 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "proxycommand", oProxyCommand },
180 { "port", oPort },
181 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000182 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000183 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000184 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "remoteforward", oRemoteForward },
186 { "localforward", oLocalForward },
187 { "user", oUser },
188 { "host", oHost },
189 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100191 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
192 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000193 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100194 { "connectionattempts", oConnectionAttempts },
195 { "batchmode", oBatchMode },
196 { "checkhostip", oCheckHostIP },
197 { "stricthostkeychecking", oStrictHostKeyChecking },
198 { "compression", oCompression },
199 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100200 { "tcpkeepalive", oTCPKeepAlive },
201 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100202 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000204 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000205 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000206 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000207 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100208#ifdef ENABLE_PKCS11
209 { "smartcarddevice", oPKCS11Provider },
210 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000211#else
212 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100213 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000214#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100215 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000216 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000217 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100218 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000219 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000220 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000221 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100222 { "serveraliveinterval", oServerAliveInterval },
223 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000224 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000225 { "controlpath", oControlPath },
226 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100227 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100228 { "tunnel", oTunnel },
229 { "tunneldevice", oTunnelDevice },
230 { "localcommand", oLocalCommand },
231 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000232 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000233 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100234#ifdef JPAKE
235 { "zeroknowledgepasswordauthentication",
236 oZeroKnowledgePasswordAuthentication },
237#else
238 { "zeroknowledgepasswordauthentication", oUnsupported },
239#endif
240
Ben Lindstrom65366a82001-12-06 16:32:47 +0000241 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100242};
243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Adds a local TCP/IP port forward to options. Never returns if there is an
246 * error.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller4af51302000-04-16 11:18:38 +1000249void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100250add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000253#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100254 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000256 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100257#endif
Damien Miller95def091999-11-25 00:26:21 +1100258 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
259 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
260 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261
Damien Miller1a0442f2008-11-05 16:30:06 +1100262 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100264 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266}
267
Damien Miller5428f641999-11-25 11:54:57 +1100268/*
269 * Adds a remote TCP/IP port forward to options. Never returns if there is
270 * an error.
271 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller4af51302000-04-16 11:18:38 +1000273void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100274add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275{
Damien Miller95def091999-11-25 00:26:21 +1100276 Forward *fwd;
277 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
278 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100279 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100280 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281
Damien Miller1a0442f2008-11-05 16:30:06 +1100282 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100284 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
287
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000288static void
289clear_forwardings(Options *options)
290{
291 int i;
292
Damien Millerf91ee4c2005-03-01 21:24:33 +1100293 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100294 if (options->local_forwards[i].listen_host != NULL)
295 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 xfree(options->local_forwards[i].connect_host);
297 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000298 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100300 if (options->remote_forwards[i].listen_host != NULL)
301 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302 xfree(options->remote_forwards[i].connect_host);
303 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000304 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100305 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000306}
307
Damien Miller5428f641999-11-25 11:54:57 +1100308/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000309 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100310 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Miller4af51302000-04-16 11:18:38 +1000312static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100313parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000315 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller95def091999-11-25 00:26:21 +1100317 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100318 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100319 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000321 error("%s: line %d: Bad configuration option: %s",
322 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100323 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324}
325
Damien Miller5428f641999-11-25 11:54:57 +1100326/*
327 * Processes a single option line as used in the configuration files. This
328 * only sets those values that have not already been set.
329 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100330#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller2ccf6611999-11-15 15:25:10 +1100332int
333process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100334 char *line, const char *filename, int linenum,
335 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100337 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100338 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100339 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100340 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100341 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100342 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Damien Millerc652cac2003-05-14 13:40:54 +1000344 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100345 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000346 if (strchr(WHITESPACE, line[len]) == NULL)
347 break;
348 line[len] = '\0';
349 }
350
Damien Millerbe484b52000-07-15 14:14:16 +1000351 s = line;
352 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100353 if ((keyword = strdelim(&s)) == NULL)
354 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000355 /* Ignore leading whitespace. */
356 if (*keyword == '\0')
357 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000358 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100359 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller37023962000-07-11 17:31:38 +1000361 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller95def091999-11-25 00:26:21 +1100363 switch (opcode) {
364 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100365 /* don't panic, but count bad options */
366 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100367 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000368 case oConnectTimeout:
369 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100370parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000371 arg = strdelim(&s);
372 if (!arg || *arg == '\0')
373 fatal("%s line %d: missing time value.",
374 filename, linenum);
375 if ((value = convtime(arg)) == -1)
376 fatal("%s line %d: invalid time value.",
377 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100378 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000379 *intptr = value;
380 break;
381
Damien Miller95def091999-11-25 00:26:21 +1100382 case oForwardAgent:
383 intptr = &options->forward_agent;
384parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000385 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000386 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100387 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
388 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000389 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100390 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000391 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100392 value = 0;
393 else
394 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
395 if (*activep && *intptr == -1)
396 *intptr = value;
397 break;
398
399 case oForwardX11:
400 intptr = &options->forward_x11;
401 goto parse_flag;
402
Darren Tucker0a118da2003-10-15 15:54:32 +1000403 case oForwardX11Trusted:
404 intptr = &options->forward_x11_trusted;
405 goto parse_flag;
406
Damien Miller95def091999-11-25 00:26:21 +1100407 case oGatewayPorts:
408 intptr = &options->gateway_ports;
409 goto parse_flag;
410
Darren Tuckere7d4b192006-07-12 22:17:10 +1000411 case oExitOnForwardFailure:
412 intptr = &options->exit_on_forward_failure;
413 goto parse_flag;
414
Damien Miller95def091999-11-25 00:26:21 +1100415 case oUsePrivilegedPort:
416 intptr = &options->use_privileged_port;
417 goto parse_flag;
418
Damien Miller95def091999-11-25 00:26:21 +1100419 case oPasswordAuthentication:
420 intptr = &options->password_authentication;
421 goto parse_flag;
422
Damien Miller01ed2272008-11-05 16:20:46 +1100423 case oZeroKnowledgePasswordAuthentication:
424 intptr = &options->zero_knowledge_password_authentication;
425 goto parse_flag;
426
Damien Miller874d77b2000-10-14 16:23:11 +1100427 case oKbdInteractiveAuthentication:
428 intptr = &options->kbd_interactive_authentication;
429 goto parse_flag;
430
431 case oKbdInteractiveDevices:
432 charptr = &options->kbd_interactive_devices;
433 goto parse_string;
434
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435 case oPubkeyAuthentication:
436 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000437 goto parse_flag;
438
Damien Miller95def091999-11-25 00:26:21 +1100439 case oRSAAuthentication:
440 intptr = &options->rsa_authentication;
441 goto parse_flag;
442
443 case oRhostsRSAAuthentication:
444 intptr = &options->rhosts_rsa_authentication;
445 goto parse_flag;
446
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000447 case oHostbasedAuthentication:
448 intptr = &options->hostbased_authentication;
449 goto parse_flag;
450
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000451 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000452 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100453 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000454
Darren Tucker0efd1552003-08-26 11:49:55 +1000455 case oGssAuthentication:
456 intptr = &options->gss_authentication;
457 goto parse_flag;
458
459 case oGssDelegateCreds:
460 intptr = &options->gss_deleg_creds;
461 goto parse_flag;
462
Damien Miller95def091999-11-25 00:26:21 +1100463 case oBatchMode:
464 intptr = &options->batch_mode;
465 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Damien Miller95def091999-11-25 00:26:21 +1100467 case oCheckHostIP:
468 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000469 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller37876e92003-05-15 10:19:46 +1000471 case oVerifyHostKeyDNS:
472 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100473 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 case oStrictHostKeyChecking:
476 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100477parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000478 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000479 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000480 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100481 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100482 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000483 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100484 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000485 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100486 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000487 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100488 value = 2;
489 else
490 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
491 if (*activep && *intptr == -1)
492 *intptr = value;
493 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 case oCompression:
496 intptr = &options->compression;
497 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller12c150e2003-12-17 16:31:10 +1100499 case oTCPKeepAlive:
500 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100501 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000503 case oNoHostAuthenticationForLocalhost:
504 intptr = &options->no_host_authentication_for_localhost;
505 goto parse_flag;
506
Damien Miller95def091999-11-25 00:26:21 +1100507 case oNumberOfPasswordPrompts:
508 intptr = &options->number_of_password_prompts;
509 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510
Damien Miller95def091999-11-25 00:26:21 +1100511 case oCompressionLevel:
512 intptr = &options->compression_level;
513 goto parse_int;
514
Damien Millera5539d22003-04-09 20:50:06 +1000515 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000516 arg = strdelim(&s);
517 if (!arg || *arg == '\0')
518 fatal("%.200s line %d: Missing argument.", filename, linenum);
519 if (arg[0] < '0' || arg[0] > '9')
520 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100521 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000522 if (arg == endofnumber)
523 fatal("%.200s line %d: Bad number.", filename, linenum);
524 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100525 case '\0':
526 scale = 1;
527 break;
Damien Millera5539d22003-04-09 20:50:06 +1000528 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100529 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000530 break;
531 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000533 break;
534 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100535 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000536 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100537 default:
538 fatal("%.200s line %d: Invalid RekeyLimit suffix",
539 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000540 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100541 val64 *= scale;
542 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100543 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100544 fatal("%.200s line %d: RekeyLimit too large",
545 filename, linenum);
546 if (val64 < 16)
547 fatal("%.200s line %d: RekeyLimit too small",
548 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100549 if (*activep && options->rekey_limit == -1)
550 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000551 break;
552
Damien Miller95def091999-11-25 00:26:21 +1100553 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000554 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000555 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100556 fatal("%.200s line %d: Missing argument.", filename, linenum);
557 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100558 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000559 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100560 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100561 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100562 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000563 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000564 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100565 }
566 break;
567
Damien Millerd3a18572000-06-07 19:55:44 +1000568 case oXAuthLocation:
569 charptr=&options->xauth_location;
570 goto parse_string;
571
Damien Miller95def091999-11-25 00:26:21 +1100572 case oUser:
573 charptr = &options->user;
574parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000575 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000576 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100577 fatal("%.200s line %d: Missing argument.", filename, linenum);
578 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000579 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100580 break;
581
582 case oGlobalKnownHostsFile:
583 charptr = &options->system_hostfile;
584 goto parse_string;
585
586 case oUserKnownHostsFile:
587 charptr = &options->user_hostfile;
588 goto parse_string;
589
Damien Millereba71ba2000-04-29 23:57:08 +1000590 case oGlobalKnownHostsFile2:
591 charptr = &options->system_hostfile2;
592 goto parse_string;
593
594 case oUserKnownHostsFile2:
595 charptr = &options->user_hostfile2;
596 goto parse_string;
597
Damien Miller95def091999-11-25 00:26:21 +1100598 case oHostName:
599 charptr = &options->hostname;
600 goto parse_string;
601
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000602 case oHostKeyAlias:
603 charptr = &options->host_key_alias;
604 goto parse_string;
605
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000606 case oPreferredAuthentications:
607 charptr = &options->preferred_authentications;
608 goto parse_string;
609
Ben Lindstrome0f88042001-04-30 13:06:24 +0000610 case oBindAddress:
611 charptr = &options->bind_address;
612 goto parse_string;
613
Damien Miller7ea845e2010-02-12 09:21:02 +1100614 case oPKCS11Provider:
615 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000616 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000617
Damien Miller95def091999-11-25 00:26:21 +1100618 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100619 charptr = &options->proxy_command;
620parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000621 if (s == NULL)
622 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100623 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100624 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100625 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100626 return 0;
627
628 case oPort:
629 intptr = &options->port;
630parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000631 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000632 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100633 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000634 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100635 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100636
637 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000638 value = strtol(arg, &endofnumber, 0);
639 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100640 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100641 if (*activep && *intptr == -1)
642 *intptr = value;
643 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller95def091999-11-25 00:26:21 +1100645 case oConnectionAttempts:
646 intptr = &options->connection_attempts;
647 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100648
Damien Miller95def091999-11-25 00:26:21 +1100649 case oCipher:
650 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000651 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000652 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000653 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000654 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100655 if (value == -1)
656 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100657 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100658 if (*activep && *intptr == -1)
659 *intptr = value;
660 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller78928792000-04-12 20:17:38 +1000662 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000663 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000664 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000665 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000666 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000667 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000669 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000670 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000671 break;
672
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000673 case oMacs:
674 arg = strdelim(&s);
675 if (!arg || *arg == '\0')
676 fatal("%.200s line %d: Missing argument.", filename, linenum);
677 if (!mac_valid(arg))
678 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100679 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000680 if (*activep && options->macs == NULL)
681 options->macs = xstrdup(arg);
682 break;
683
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000684 case oHostKeyAlgorithms:
685 arg = strdelim(&s);
686 if (!arg || *arg == '\0')
687 fatal("%.200s line %d: Missing argument.", filename, linenum);
688 if (!key_names_valid2(arg))
689 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100690 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000691 if (*activep && options->hostkeyalgorithms == NULL)
692 options->hostkeyalgorithms = xstrdup(arg);
693 break;
694
Damien Miller78928792000-04-12 20:17:38 +1000695 case oProtocol:
696 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000697 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000698 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000699 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000700 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000701 if (value == SSH_PROTO_UNKNOWN)
702 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100703 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000704 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
705 *intptr = value;
706 break;
707
Damien Miller95def091999-11-25 00:26:21 +1100708 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100709 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000710 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000711 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100712 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000713 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100714 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100715 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
716 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100717 break;
718
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000719 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100720 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100721 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000722 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100723 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000724 fatal("%.200s line %d: Missing port argument.",
725 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100726
Damien Millera699d952008-11-03 19:27:34 +1100727 if (opcode == oLocalForward ||
728 opcode == oRemoteForward) {
729 arg2 = strdelim(&s);
730 if (arg2 == NULL || *arg2 == '\0')
731 fatal("%.200s line %d: Missing target argument.",
732 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100733
Damien Millera699d952008-11-03 19:27:34 +1100734 /* construct a string for parse_forward */
735 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
736 } else if (opcode == oDynamicForward) {
737 strlcpy(fwdarg, arg, sizeof(fwdarg));
738 }
739
740 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100741 opcode == oDynamicForward ? 1 : 0,
742 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000743 fatal("%.200s line %d: Bad forwarding specification.",
744 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100745
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000746 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100747 if (opcode == oLocalForward ||
748 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100749 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000750 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100751 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000752 }
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
754
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000755 case oClearAllForwardings:
756 intptr = &options->clear_forwardings;
757 goto parse_flag;
758
Damien Miller95def091999-11-25 00:26:21 +1100759 case oHost:
760 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000761 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000762 if (match_pattern(host, arg)) {
763 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100764 *activep = 1;
765 break;
766 }
Damien Millerbe484b52000-07-15 14:14:16 +1000767 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100768 return 0;
769
770 case oEscapeChar:
771 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000772 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000773 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100774 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000775 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000776 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
777 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000778 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000779 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000780 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000781 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100782 else {
783 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100784 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100785 /* NOTREACHED */
786 value = 0; /* Avoid compiler warning. */
787 }
788 if (*activep && *intptr == -1)
789 *intptr = value;
790 break;
791
Damien Miller20a8f972003-05-18 20:50:30 +1000792 case oAddressFamily:
793 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000794 if (!arg || *arg == '\0')
795 fatal("%s line %d: missing address family.",
796 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000797 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000798 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000799 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000800 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000801 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000802 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000803 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000804 else
805 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000806 if (*activep && *intptr == -1)
807 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000808 break;
809
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000810 case oEnableSSHKeysign:
811 intptr = &options->enable_ssh_keysign;
812 goto parse_flag;
813
Damien Millerbd394c32004-03-08 23:12:36 +1100814 case oIdentitiesOnly:
815 intptr = &options->identities_only;
816 goto parse_flag;
817
Damien Miller509b0102003-12-17 16:33:10 +1100818 case oServerAliveInterval:
819 intptr = &options->server_alive_interval;
820 goto parse_time;
821
822 case oServerAliveCountMax:
823 intptr = &options->server_alive_count_max;
824 goto parse_int;
825
Darren Tucker46bc0752004-05-02 22:11:30 +1000826 case oSendEnv:
827 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
828 if (strchr(arg, '=') != NULL)
829 fatal("%s line %d: Invalid environment name.",
830 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100831 if (!*activep)
832 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000833 if (options->num_send_env >= MAX_SEND_ENV)
834 fatal("%s line %d: too many send env.",
835 filename, linenum);
836 options->send_env[options->num_send_env++] =
837 xstrdup(arg);
838 }
839 break;
840
Damien Miller0e220db2004-06-15 10:34:08 +1000841 case oControlPath:
842 charptr = &options->control_path;
843 goto parse_string;
844
845 case oControlMaster:
846 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000847 arg = strdelim(&s);
848 if (!arg || *arg == '\0')
849 fatal("%.200s line %d: Missing ControlMaster argument.",
850 filename, linenum);
851 value = 0; /* To avoid compiler warning... */
852 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
853 value = SSHCTL_MASTER_YES;
854 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
855 value = SSHCTL_MASTER_NO;
856 else if (strcmp(arg, "auto") == 0)
857 value = SSHCTL_MASTER_AUTO;
858 else if (strcmp(arg, "ask") == 0)
859 value = SSHCTL_MASTER_ASK;
860 else if (strcmp(arg, "autoask") == 0)
861 value = SSHCTL_MASTER_AUTO_ASK;
862 else
863 fatal("%.200s line %d: Bad ControlMaster argument.",
864 filename, linenum);
865 if (*activep && *intptr == -1)
866 *intptr = value;
867 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000868
Damien Millere1776152005-03-01 21:47:37 +1100869 case oHashKnownHosts:
870 intptr = &options->hash_known_hosts;
871 goto parse_flag;
872
Damien Millerd27b9472005-12-13 19:29:02 +1100873 case oTunnel:
874 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100875 arg = strdelim(&s);
876 if (!arg || *arg == '\0')
877 fatal("%s line %d: Missing yes/point-to-point/"
878 "ethernet/no argument.", filename, linenum);
879 value = 0; /* silence compiler */
880 if (strcasecmp(arg, "ethernet") == 0)
881 value = SSH_TUNMODE_ETHERNET;
882 else if (strcasecmp(arg, "point-to-point") == 0)
883 value = SSH_TUNMODE_POINTOPOINT;
884 else if (strcasecmp(arg, "yes") == 0)
885 value = SSH_TUNMODE_DEFAULT;
886 else if (strcasecmp(arg, "no") == 0)
887 value = SSH_TUNMODE_NO;
888 else
889 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
890 "no argument: %s", filename, linenum, arg);
891 if (*activep)
892 *intptr = value;
893 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100894
895 case oTunnelDevice:
896 arg = strdelim(&s);
897 if (!arg || *arg == '\0')
898 fatal("%.200s line %d: Missing argument.", filename, linenum);
899 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100900 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100901 fatal("%.200s line %d: Bad tun device.", filename, linenum);
902 if (*activep) {
903 options->tun_local = value;
904 options->tun_remote = value2;
905 }
906 break;
907
908 case oLocalCommand:
909 charptr = &options->local_command;
910 goto parse_command;
911
912 case oPermitLocalCommand:
913 intptr = &options->permit_local_command;
914 goto parse_flag;
915
Damien Miller10288242008-06-30 00:04:03 +1000916 case oVisualHostKey:
917 intptr = &options->visual_host_key;
918 goto parse_flag;
919
Darren Tucker71e4d542009-07-06 07:12:27 +1000920 case oUseRoaming:
921 intptr = &options->use_roaming;
922 goto parse_flag;
923
Ben Lindstrom4daea862002-06-09 20:04:02 +0000924 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000925 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000926 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000927 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000928
Damien Millerf9b3feb2003-05-16 11:38:32 +1000929 case oUnsupported:
930 error("%s line %d: Unsupported option \"%s\"",
931 filename, linenum, keyword);
932 return 0;
933
Damien Miller95def091999-11-25 00:26:21 +1100934 default:
935 fatal("process_config_line: Unimplemented opcode %d", opcode);
936 }
937
938 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000939 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000940 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000941 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000942 }
Damien Miller95def091999-11-25 00:26:21 +1100943 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944}
945
946
Damien Miller5428f641999-11-25 11:54:57 +1100947/*
948 * Reads the config file and modifies the options accordingly. Options
949 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000950 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100951 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000953int
Darren Tuckerfc959702004-07-17 16:12:08 +1000954read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000955 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956{
Damien Miller95def091999-11-25 00:26:21 +1100957 FILE *f;
958 char line[1024];
959 int active, linenum;
960 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
Damien Miller57a44762004-04-20 20:11:57 +1000962 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000963 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Damien Miller57a44762004-04-20 20:11:57 +1000965 if (checkperm) {
966 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000967
Damien Miller33793852004-06-15 10:27:55 +1000968 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000969 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000970 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000971 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000972 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000973 }
974
Damien Miller95def091999-11-25 00:26:21 +1100975 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller5428f641999-11-25 11:54:57 +1100977 /*
978 * Mark that we are now processing the options. This flag is turned
979 * on/off by Host specifications.
980 */
Damien Miller95def091999-11-25 00:26:21 +1100981 active = 1;
982 linenum = 0;
983 while (fgets(line, sizeof(line), f)) {
984 /* Update line number counter. */
985 linenum++;
986 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
987 bad_options++;
988 }
989 fclose(f);
990 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000991 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100992 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000993 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994}
995
Damien Miller5428f641999-11-25 11:54:57 +1100996/*
997 * Initializes options to special values that indicate that they have not yet
998 * been set. Read_config_file will only set options with this value. Options
999 * are processed in the following order: command line, user config file,
1000 * system config file. Last, fill_default_options is called.
1001 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller4af51302000-04-16 11:18:38 +10001003void
Damien Miller95def091999-11-25 00:26:21 +11001004initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005{
Damien Miller95def091999-11-25 00:26:21 +11001006 memset(options, 'X', sizeof(*options));
1007 options->forward_agent = -1;
1008 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001009 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001010 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001011 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001012 options->gateway_ports = -1;
1013 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001014 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001015 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001016 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001017 options->gss_authentication = -1;
1018 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001019 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001020 options->kbd_interactive_authentication = -1;
1021 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001022 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001023 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001024 options->batch_mode = -1;
1025 options->check_host_ip = -1;
1026 options->strict_host_key_checking = -1;
1027 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001028 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001029 options->compression_level = -1;
1030 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001031 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001032 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001033 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001034 options->number_of_password_prompts = -1;
1035 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001036 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001037 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001038 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001039 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001040 options->num_identity_files = 0;
1041 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001042 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001043 options->proxy_command = NULL;
1044 options->user = NULL;
1045 options->escape_char = -1;
1046 options->system_hostfile = NULL;
1047 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001048 options->system_hostfile2 = NULL;
1049 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001050 options->num_local_forwards = 0;
1051 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001052 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001053 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001054 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001055 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001056 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001057 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001058 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001059 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001060 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001061 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001062 options->server_alive_interval = -1;
1063 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001064 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001065 options->control_path = NULL;
1066 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001067 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001068 options->tun_open = -1;
1069 options->tun_local = -1;
1070 options->tun_remote = -1;
1071 options->local_command = NULL;
1072 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001073 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001074 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001075 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076}
1077
Damien Miller5428f641999-11-25 11:54:57 +11001078/*
1079 * Called after processing other sources of option data, this fills those
1080 * options for which no value has been specified with their default values.
1081 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082
Damien Miller4af51302000-04-16 11:18:38 +10001083void
Damien Miller95def091999-11-25 00:26:21 +11001084fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001085{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001086 int len;
1087
Damien Miller95def091999-11-25 00:26:21 +11001088 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001089 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001090 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001091 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001092 if (options->forward_x11_trusted == -1)
1093 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001094 if (options->exit_on_forward_failure == -1)
1095 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001096 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001097 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001098 if (options->gateway_ports == -1)
1099 options->gateway_ports = 0;
1100 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001101 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001102 if (options->rsa_authentication == -1)
1103 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001104 if (options->pubkey_authentication == -1)
1105 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001106 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001107 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001108 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001109 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001110 if (options->gss_deleg_creds == -1)
1111 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001112 if (options->password_authentication == -1)
1113 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001114 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001115 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001116 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001117 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001118 if (options->hostbased_authentication == -1)
1119 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001120 if (options->batch_mode == -1)
1121 options->batch_mode = 0;
1122 if (options->check_host_ip == -1)
1123 options->check_host_ip = 1;
1124 if (options->strict_host_key_checking == -1)
1125 options->strict_host_key_checking = 2; /* 2 is default */
1126 if (options->compression == -1)
1127 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001128 if (options->tcp_keep_alive == -1)
1129 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001130 if (options->compression_level == -1)
1131 options->compression_level = 6;
1132 if (options->port == -1)
1133 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001134 if (options->address_family == -1)
1135 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001136 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001137 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001138 if (options->number_of_password_prompts == -1)
1139 options->number_of_password_prompts = 3;
1140 /* Selected in ssh_login(). */
1141 if (options->cipher == -1)
1142 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001143 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001144 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001145 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001146 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001147 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001148 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001149 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001150 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001151 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001152 xmalloc(len);
1153 snprintf(options->identity_files[options->num_identity_files++],
1154 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155 }
1156 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001157 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1158 options->identity_files[options->num_identity_files] =
1159 xmalloc(len);
1160 snprintf(options->identity_files[options->num_identity_files++],
1161 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1162
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001163 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001164 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001165 xmalloc(len);
1166 snprintf(options->identity_files[options->num_identity_files++],
1167 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 }
Damien Millereba71ba2000-04-29 23:57:08 +10001169 }
Damien Miller95def091999-11-25 00:26:21 +11001170 if (options->escape_char == -1)
1171 options->escape_char = '~';
1172 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001173 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001174 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001176 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001177 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001178 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001179 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001180 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001181 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001182 if (options->clear_forwardings == 1)
1183 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001184 if (options->no_host_authentication_for_localhost == - 1)
1185 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001186 if (options->identities_only == -1)
1187 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001188 if (options->enable_ssh_keysign == -1)
1189 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001190 if (options->rekey_limit == -1)
1191 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001192 if (options->verify_host_key_dns == -1)
1193 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001194 if (options->server_alive_interval == -1)
1195 options->server_alive_interval = 0;
1196 if (options->server_alive_count_max == -1)
1197 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001198 if (options->control_master == -1)
1199 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001200 if (options->hash_known_hosts == -1)
1201 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001202 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001203 options->tun_open = SSH_TUNMODE_NO;
1204 if (options->tun_local == -1)
1205 options->tun_local = SSH_TUNID_ANY;
1206 if (options->tun_remote == -1)
1207 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001208 if (options->permit_local_command == -1)
1209 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001210 if (options->use_roaming == -1)
1211 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001212 if (options->visual_host_key == -1)
1213 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001214 if (options->zero_knowledge_password_authentication == -1)
1215 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001216 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001217 /* options->proxy_command should not be set by default */
1218 /* options->user will be set in the main program if appropriate */
1219 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001220 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001221 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001223
1224/*
1225 * parse_forward
1226 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001227 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001228 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001229 * dynamicfwd == 1
1230 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001231 * returns number of arguments parsed or zero on error
1232 */
1233int
Damien Miller4bf648f2009-02-14 16:28:21 +11001234parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001235{
1236 int i;
1237 char *p, *cp, *fwdarg[4];
1238
1239 memset(fwd, '\0', sizeof(*fwd));
1240
1241 cp = p = xstrdup(fwdspec);
1242
1243 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001244 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001245 cp++;
1246
1247 for (i = 0; i < 4; ++i)
1248 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1249 break;
1250
Damien Millerf4b39532008-11-03 19:28:21 +11001251 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001252 if (cp != NULL)
1253 i = 0; /* failure */
1254
1255 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001256 case 1:
1257 fwd->listen_host = NULL;
1258 fwd->listen_port = a2port(fwdarg[0]);
1259 fwd->connect_host = xstrdup("socks");
1260 break;
1261
1262 case 2:
1263 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1264 fwd->listen_port = a2port(fwdarg[1]);
1265 fwd->connect_host = xstrdup("socks");
1266 break;
1267
Damien Millerf91ee4c2005-03-01 21:24:33 +11001268 case 3:
1269 fwd->listen_host = NULL;
1270 fwd->listen_port = a2port(fwdarg[0]);
1271 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1272 fwd->connect_port = a2port(fwdarg[2]);
1273 break;
1274
1275 case 4:
1276 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1277 fwd->listen_port = a2port(fwdarg[1]);
1278 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1279 fwd->connect_port = a2port(fwdarg[3]);
1280 break;
1281 default:
1282 i = 0; /* failure */
1283 }
1284
1285 xfree(p);
1286
Damien Millera699d952008-11-03 19:27:34 +11001287 if (dynamicfwd) {
1288 if (!(i == 1 || i == 2))
1289 goto fail_free;
1290 } else {
1291 if (!(i == 3 || i == 4))
1292 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001293 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001294 goto fail_free;
1295 }
1296
Damien Miller4bf648f2009-02-14 16:28:21 +11001297 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001298 goto fail_free;
1299
1300 if (fwd->connect_host != NULL &&
1301 strlen(fwd->connect_host) >= NI_MAXHOST)
1302 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001303 if (fwd->listen_host != NULL &&
1304 strlen(fwd->listen_host) >= NI_MAXHOST)
1305 goto fail_free;
1306
Damien Millerf91ee4c2005-03-01 21:24:33 +11001307
1308 return (i);
1309
1310 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001311 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001312 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001313 fwd->connect_host = NULL;
1314 }
1315 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001316 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001317 fwd->listen_host = NULL;
1318 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001319 return (0);
1320}