blob: 4a16974b874a9e8fd94821121c1e47e3f4375089 [file] [log] [blame]
Darren Tuckerbad50762009-10-11 21:51:08 +11001/* $OpenBSD: readconf.c,v 1.178 2009/10/08 14:03:41 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,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000126 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
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 Tucker71e4d542009-07-06 07:12:27 +1000133 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000134 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 Millerf9b3feb2003-05-16 11:38:32 +1000208#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000209 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000210#else
211 { "smartcarddevice", oUnsupported },
212#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100213 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000214 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000215 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100216 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000217 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000218 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000219 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100220 { "serveraliveinterval", oServerAliveInterval },
221 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000222 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000223 { "controlpath", oControlPath },
224 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100225 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100226 { "tunnel", oTunnel },
227 { "tunneldevice", oTunnelDevice },
228 { "localcommand", oLocalCommand },
229 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000230 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000231 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100232#ifdef JPAKE
233 { "zeroknowledgepasswordauthentication",
234 oZeroKnowledgePasswordAuthentication },
235#else
236 { "zeroknowledgepasswordauthentication", oUnsupported },
237#endif
238
Ben Lindstrom65366a82001-12-06 16:32:47 +0000239 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100240};
241
Damien Miller5428f641999-11-25 11:54:57 +1100242/*
243 * Adds a local TCP/IP port forward to options. Never returns if there is an
244 * error.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller4af51302000-04-16 11:18:38 +1000247void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100248add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000251#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100252 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100253 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000254 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100255#endif
Damien Miller95def091999-11-25 00:26:21 +1100256 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
257 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
258 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100259
Damien Miller1a0442f2008-11-05 16:30:06 +1100260 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100262 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264}
265
Damien Miller5428f641999-11-25 11:54:57 +1100266/*
267 * Adds a remote TCP/IP port forward to options. Never returns if there is
268 * an error.
269 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller4af51302000-04-16 11:18:38 +1000271void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273{
Damien Miller95def091999-11-25 00:26:21 +1100274 Forward *fwd;
275 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
276 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100277 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100278 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100279
Damien Miller1a0442f2008-11-05 16:30:06 +1100280 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100282 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284}
285
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000286static void
287clear_forwardings(Options *options)
288{
289 int i;
290
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100292 if (options->local_forwards[i].listen_host != NULL)
293 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294 xfree(options->local_forwards[i].connect_host);
295 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000296 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100297 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100298 if (options->remote_forwards[i].listen_host != NULL)
299 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300 xfree(options->remote_forwards[i].connect_host);
301 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000302 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100303 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000304}
305
Damien Miller5428f641999-11-25 11:54:57 +1100306/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000307 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100308 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Miller4af51302000-04-16 11:18:38 +1000310static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100311parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000313 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller95def091999-11-25 00:26:21 +1100315 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100316 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100317 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000319 error("%s: line %d: Bad configuration option: %s",
320 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100321 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
323
Damien Miller5428f641999-11-25 11:54:57 +1100324/*
325 * Processes a single option line as used in the configuration files. This
326 * only sets those values that have not already been set.
327 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100328#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller2ccf6611999-11-15 15:25:10 +1100330int
331process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100332 char *line, const char *filename, int linenum,
333 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100335 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100336 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100337 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100338 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100339 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Millerc652cac2003-05-14 13:40:54 +1000342 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100343 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000344 if (strchr(WHITESPACE, line[len]) == NULL)
345 break;
346 line[len] = '\0';
347 }
348
Damien Millerbe484b52000-07-15 14:14:16 +1000349 s = line;
350 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100351 if ((keyword = strdelim(&s)) == NULL)
352 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000353 /* Ignore leading whitespace. */
354 if (*keyword == '\0')
355 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000356 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100357 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
Damien Miller37023962000-07-11 17:31:38 +1000359 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 switch (opcode) {
362 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100363 /* don't panic, but count bad options */
364 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100365 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000366 case oConnectTimeout:
367 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100368parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000369 arg = strdelim(&s);
370 if (!arg || *arg == '\0')
371 fatal("%s line %d: missing time value.",
372 filename, linenum);
373 if ((value = convtime(arg)) == -1)
374 fatal("%s line %d: invalid time value.",
375 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100376 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000377 *intptr = value;
378 break;
379
Damien Miller95def091999-11-25 00:26:21 +1100380 case oForwardAgent:
381 intptr = &options->forward_agent;
382parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000383 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000384 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100385 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
386 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000387 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100388 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000389 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100390 value = 0;
391 else
392 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
393 if (*activep && *intptr == -1)
394 *intptr = value;
395 break;
396
397 case oForwardX11:
398 intptr = &options->forward_x11;
399 goto parse_flag;
400
Darren Tucker0a118da2003-10-15 15:54:32 +1000401 case oForwardX11Trusted:
402 intptr = &options->forward_x11_trusted;
403 goto parse_flag;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oGatewayPorts:
406 intptr = &options->gateway_ports;
407 goto parse_flag;
408
Darren Tuckere7d4b192006-07-12 22:17:10 +1000409 case oExitOnForwardFailure:
410 intptr = &options->exit_on_forward_failure;
411 goto parse_flag;
412
Damien Miller95def091999-11-25 00:26:21 +1100413 case oUsePrivilegedPort:
414 intptr = &options->use_privileged_port;
415 goto parse_flag;
416
Damien Miller95def091999-11-25 00:26:21 +1100417 case oPasswordAuthentication:
418 intptr = &options->password_authentication;
419 goto parse_flag;
420
Damien Miller01ed2272008-11-05 16:20:46 +1100421 case oZeroKnowledgePasswordAuthentication:
422 intptr = &options->zero_knowledge_password_authentication;
423 goto parse_flag;
424
Damien Miller874d77b2000-10-14 16:23:11 +1100425 case oKbdInteractiveAuthentication:
426 intptr = &options->kbd_interactive_authentication;
427 goto parse_flag;
428
429 case oKbdInteractiveDevices:
430 charptr = &options->kbd_interactive_devices;
431 goto parse_string;
432
Damien Miller0bc1bd82000-11-13 22:57:25 +1100433 case oPubkeyAuthentication:
434 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000435 goto parse_flag;
436
Damien Miller95def091999-11-25 00:26:21 +1100437 case oRSAAuthentication:
438 intptr = &options->rsa_authentication;
439 goto parse_flag;
440
441 case oRhostsRSAAuthentication:
442 intptr = &options->rhosts_rsa_authentication;
443 goto parse_flag;
444
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000445 case oHostbasedAuthentication:
446 intptr = &options->hostbased_authentication;
447 goto parse_flag;
448
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000449 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000450 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100451 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000452
Darren Tucker0efd1552003-08-26 11:49:55 +1000453 case oGssAuthentication:
454 intptr = &options->gss_authentication;
455 goto parse_flag;
456
457 case oGssDelegateCreds:
458 intptr = &options->gss_deleg_creds;
459 goto parse_flag;
460
Damien Miller95def091999-11-25 00:26:21 +1100461 case oBatchMode:
462 intptr = &options->batch_mode;
463 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 case oCheckHostIP:
466 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000467 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller37876e92003-05-15 10:19:46 +1000469 case oVerifyHostKeyDNS:
470 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100471 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000472
Damien Miller95def091999-11-25 00:26:21 +1100473 case oStrictHostKeyChecking:
474 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100475parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000476 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000477 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000478 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100479 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100480 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000481 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100482 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000483 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100484 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000485 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100486 value = 2;
487 else
488 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
489 if (*activep && *intptr == -1)
490 *intptr = value;
491 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
Damien Miller95def091999-11-25 00:26:21 +1100493 case oCompression:
494 intptr = &options->compression;
495 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Damien Miller12c150e2003-12-17 16:31:10 +1100497 case oTCPKeepAlive:
498 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100499 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000501 case oNoHostAuthenticationForLocalhost:
502 intptr = &options->no_host_authentication_for_localhost;
503 goto parse_flag;
504
Damien Miller95def091999-11-25 00:26:21 +1100505 case oNumberOfPasswordPrompts:
506 intptr = &options->number_of_password_prompts;
507 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508
Damien Miller95def091999-11-25 00:26:21 +1100509 case oCompressionLevel:
510 intptr = &options->compression_level;
511 goto parse_int;
512
Damien Millera5539d22003-04-09 20:50:06 +1000513 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000514 arg = strdelim(&s);
515 if (!arg || *arg == '\0')
516 fatal("%.200s line %d: Missing argument.", filename, linenum);
517 if (arg[0] < '0' || arg[0] > '9')
518 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100519 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000520 if (arg == endofnumber)
521 fatal("%.200s line %d: Bad number.", filename, linenum);
522 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100523 case '\0':
524 scale = 1;
525 break;
Damien Millera5539d22003-04-09 20:50:06 +1000526 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100527 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000528 break;
529 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100530 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000531 break;
532 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100533 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000534 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100535 default:
536 fatal("%.200s line %d: Invalid RekeyLimit suffix",
537 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000538 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100539 val64 *= scale;
540 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100541 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100542 fatal("%.200s line %d: RekeyLimit too large",
543 filename, linenum);
544 if (val64 < 16)
545 fatal("%.200s line %d: RekeyLimit too small",
546 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100547 if (*activep && options->rekey_limit == -1)
548 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000549 break;
550
Damien Miller95def091999-11-25 00:26:21 +1100551 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000552 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000553 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100554 fatal("%.200s line %d: Missing argument.", filename, linenum);
555 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100556 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000557 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100558 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100559 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100560 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000561 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000562 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100563 }
564 break;
565
Damien Millerd3a18572000-06-07 19:55:44 +1000566 case oXAuthLocation:
567 charptr=&options->xauth_location;
568 goto parse_string;
569
Damien Miller95def091999-11-25 00:26:21 +1100570 case oUser:
571 charptr = &options->user;
572parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000573 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000574 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100575 fatal("%.200s line %d: Missing argument.", filename, linenum);
576 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000577 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100578 break;
579
580 case oGlobalKnownHostsFile:
581 charptr = &options->system_hostfile;
582 goto parse_string;
583
584 case oUserKnownHostsFile:
585 charptr = &options->user_hostfile;
586 goto parse_string;
587
Damien Millereba71ba2000-04-29 23:57:08 +1000588 case oGlobalKnownHostsFile2:
589 charptr = &options->system_hostfile2;
590 goto parse_string;
591
592 case oUserKnownHostsFile2:
593 charptr = &options->user_hostfile2;
594 goto parse_string;
595
Damien Miller95def091999-11-25 00:26:21 +1100596 case oHostName:
597 charptr = &options->hostname;
598 goto parse_string;
599
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000600 case oHostKeyAlias:
601 charptr = &options->host_key_alias;
602 goto parse_string;
603
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000604 case oPreferredAuthentications:
605 charptr = &options->preferred_authentications;
606 goto parse_string;
607
Ben Lindstrome0f88042001-04-30 13:06:24 +0000608 case oBindAddress:
609 charptr = &options->bind_address;
610 goto parse_string;
611
Ben Lindstromae996bf2001-08-06 21:27:53 +0000612 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000613 charptr = &options->smartcard_device;
614 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000615
Damien Miller95def091999-11-25 00:26:21 +1100616 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100617 charptr = &options->proxy_command;
618parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000619 if (s == NULL)
620 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100621 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100622 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100623 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100624 return 0;
625
626 case oPort:
627 intptr = &options->port;
628parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000629 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000630 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100631 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000632 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100633 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100634
635 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000636 value = strtol(arg, &endofnumber, 0);
637 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100638 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100639 if (*activep && *intptr == -1)
640 *intptr = value;
641 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 case oConnectionAttempts:
644 intptr = &options->connection_attempts;
645 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100646
Damien Miller95def091999-11-25 00:26:21 +1100647 case oCipher:
648 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000649 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000650 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000651 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000652 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100653 if (value == -1)
654 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100655 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100656 if (*activep && *intptr == -1)
657 *intptr = value;
658 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659
Damien Miller78928792000-04-12 20:17:38 +1000660 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000661 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000662 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000663 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000664 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000665 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100666 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000667 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000668 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000669 break;
670
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000671 case oMacs:
672 arg = strdelim(&s);
673 if (!arg || *arg == '\0')
674 fatal("%.200s line %d: Missing argument.", filename, linenum);
675 if (!mac_valid(arg))
676 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100677 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000678 if (*activep && options->macs == NULL)
679 options->macs = xstrdup(arg);
680 break;
681
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000682 case oHostKeyAlgorithms:
683 arg = strdelim(&s);
684 if (!arg || *arg == '\0')
685 fatal("%.200s line %d: Missing argument.", filename, linenum);
686 if (!key_names_valid2(arg))
687 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100688 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000689 if (*activep && options->hostkeyalgorithms == NULL)
690 options->hostkeyalgorithms = xstrdup(arg);
691 break;
692
Damien Miller78928792000-04-12 20:17:38 +1000693 case oProtocol:
694 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000695 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000696 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000697 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000698 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000699 if (value == SSH_PROTO_UNKNOWN)
700 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100701 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000702 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
703 *intptr = value;
704 break;
705
Damien Miller95def091999-11-25 00:26:21 +1100706 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100707 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000708 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000709 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100710 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000711 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100712 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100713 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
714 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100715 break;
716
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000717 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100718 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100719 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000720 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100721 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000722 fatal("%.200s line %d: Missing port argument.",
723 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100724
Damien Millera699d952008-11-03 19:27:34 +1100725 if (opcode == oLocalForward ||
726 opcode == oRemoteForward) {
727 arg2 = strdelim(&s);
728 if (arg2 == NULL || *arg2 == '\0')
729 fatal("%.200s line %d: Missing target argument.",
730 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100731
Damien Millera699d952008-11-03 19:27:34 +1100732 /* construct a string for parse_forward */
733 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
734 } else if (opcode == oDynamicForward) {
735 strlcpy(fwdarg, arg, sizeof(fwdarg));
736 }
737
738 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100739 opcode == oDynamicForward ? 1 : 0,
740 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000741 fatal("%.200s line %d: Bad forwarding specification.",
742 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100743
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000744 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100745 if (opcode == oLocalForward ||
746 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100747 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000748 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100749 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000750 }
Damien Miller95def091999-11-25 00:26:21 +1100751 break;
752
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000753 case oClearAllForwardings:
754 intptr = &options->clear_forwardings;
755 goto parse_flag;
756
Damien Miller95def091999-11-25 00:26:21 +1100757 case oHost:
758 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000759 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000760 if (match_pattern(host, arg)) {
761 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100762 *activep = 1;
763 break;
764 }
Damien Millerbe484b52000-07-15 14:14:16 +1000765 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100766 return 0;
767
768 case oEscapeChar:
769 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000770 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000771 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100772 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000773 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000774 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
775 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000776 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000777 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000778 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000779 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100780 else {
781 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100782 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100783 /* NOTREACHED */
784 value = 0; /* Avoid compiler warning. */
785 }
786 if (*activep && *intptr == -1)
787 *intptr = value;
788 break;
789
Damien Miller20a8f972003-05-18 20:50:30 +1000790 case oAddressFamily:
791 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000792 if (!arg || *arg == '\0')
793 fatal("%s line %d: missing address family.",
794 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000795 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000796 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000797 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000798 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000799 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000800 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000801 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000802 else
803 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000804 if (*activep && *intptr == -1)
805 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000806 break;
807
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000808 case oEnableSSHKeysign:
809 intptr = &options->enable_ssh_keysign;
810 goto parse_flag;
811
Damien Millerbd394c32004-03-08 23:12:36 +1100812 case oIdentitiesOnly:
813 intptr = &options->identities_only;
814 goto parse_flag;
815
Damien Miller509b0102003-12-17 16:33:10 +1100816 case oServerAliveInterval:
817 intptr = &options->server_alive_interval;
818 goto parse_time;
819
820 case oServerAliveCountMax:
821 intptr = &options->server_alive_count_max;
822 goto parse_int;
823
Darren Tucker46bc0752004-05-02 22:11:30 +1000824 case oSendEnv:
825 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
826 if (strchr(arg, '=') != NULL)
827 fatal("%s line %d: Invalid environment name.",
828 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100829 if (!*activep)
830 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000831 if (options->num_send_env >= MAX_SEND_ENV)
832 fatal("%s line %d: too many send env.",
833 filename, linenum);
834 options->send_env[options->num_send_env++] =
835 xstrdup(arg);
836 }
837 break;
838
Damien Miller0e220db2004-06-15 10:34:08 +1000839 case oControlPath:
840 charptr = &options->control_path;
841 goto parse_string;
842
843 case oControlMaster:
844 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000845 arg = strdelim(&s);
846 if (!arg || *arg == '\0')
847 fatal("%.200s line %d: Missing ControlMaster argument.",
848 filename, linenum);
849 value = 0; /* To avoid compiler warning... */
850 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
851 value = SSHCTL_MASTER_YES;
852 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
853 value = SSHCTL_MASTER_NO;
854 else if (strcmp(arg, "auto") == 0)
855 value = SSHCTL_MASTER_AUTO;
856 else if (strcmp(arg, "ask") == 0)
857 value = SSHCTL_MASTER_ASK;
858 else if (strcmp(arg, "autoask") == 0)
859 value = SSHCTL_MASTER_AUTO_ASK;
860 else
861 fatal("%.200s line %d: Bad ControlMaster argument.",
862 filename, linenum);
863 if (*activep && *intptr == -1)
864 *intptr = value;
865 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000866
Damien Millere1776152005-03-01 21:47:37 +1100867 case oHashKnownHosts:
868 intptr = &options->hash_known_hosts;
869 goto parse_flag;
870
Damien Millerd27b9472005-12-13 19:29:02 +1100871 case oTunnel:
872 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100873 arg = strdelim(&s);
874 if (!arg || *arg == '\0')
875 fatal("%s line %d: Missing yes/point-to-point/"
876 "ethernet/no argument.", filename, linenum);
877 value = 0; /* silence compiler */
878 if (strcasecmp(arg, "ethernet") == 0)
879 value = SSH_TUNMODE_ETHERNET;
880 else if (strcasecmp(arg, "point-to-point") == 0)
881 value = SSH_TUNMODE_POINTOPOINT;
882 else if (strcasecmp(arg, "yes") == 0)
883 value = SSH_TUNMODE_DEFAULT;
884 else if (strcasecmp(arg, "no") == 0)
885 value = SSH_TUNMODE_NO;
886 else
887 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
888 "no argument: %s", filename, linenum, arg);
889 if (*activep)
890 *intptr = value;
891 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100892
893 case oTunnelDevice:
894 arg = strdelim(&s);
895 if (!arg || *arg == '\0')
896 fatal("%.200s line %d: Missing argument.", filename, linenum);
897 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100898 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100899 fatal("%.200s line %d: Bad tun device.", filename, linenum);
900 if (*activep) {
901 options->tun_local = value;
902 options->tun_remote = value2;
903 }
904 break;
905
906 case oLocalCommand:
907 charptr = &options->local_command;
908 goto parse_command;
909
910 case oPermitLocalCommand:
911 intptr = &options->permit_local_command;
912 goto parse_flag;
913
Damien Miller10288242008-06-30 00:04:03 +1000914 case oVisualHostKey:
915 intptr = &options->visual_host_key;
916 goto parse_flag;
917
Darren Tucker71e4d542009-07-06 07:12:27 +1000918 case oUseRoaming:
919 intptr = &options->use_roaming;
920 goto parse_flag;
921
Ben Lindstrom4daea862002-06-09 20:04:02 +0000922 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000923 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000924 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000925 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000926
Damien Millerf9b3feb2003-05-16 11:38:32 +1000927 case oUnsupported:
928 error("%s line %d: Unsupported option \"%s\"",
929 filename, linenum, keyword);
930 return 0;
931
Damien Miller95def091999-11-25 00:26:21 +1100932 default:
933 fatal("process_config_line: Unimplemented opcode %d", opcode);
934 }
935
936 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000937 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000938 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000939 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000940 }
Damien Miller95def091999-11-25 00:26:21 +1100941 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942}
943
944
Damien Miller5428f641999-11-25 11:54:57 +1100945/*
946 * Reads the config file and modifies the options accordingly. Options
947 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000948 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100949 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000951int
Darren Tuckerfc959702004-07-17 16:12:08 +1000952read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000953 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954{
Damien Miller95def091999-11-25 00:26:21 +1100955 FILE *f;
956 char line[1024];
957 int active, linenum;
958 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959
Damien Miller57a44762004-04-20 20:11:57 +1000960 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000961 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Miller57a44762004-04-20 20:11:57 +1000963 if (checkperm) {
964 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000965
Damien Miller33793852004-06-15 10:27:55 +1000966 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000967 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000968 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000969 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000970 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000971 }
972
Damien Miller95def091999-11-25 00:26:21 +1100973 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller5428f641999-11-25 11:54:57 +1100975 /*
976 * Mark that we are now processing the options. This flag is turned
977 * on/off by Host specifications.
978 */
Damien Miller95def091999-11-25 00:26:21 +1100979 active = 1;
980 linenum = 0;
981 while (fgets(line, sizeof(line), f)) {
982 /* Update line number counter. */
983 linenum++;
984 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
985 bad_options++;
986 }
987 fclose(f);
988 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000989 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100990 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000991 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
Damien Miller5428f641999-11-25 11:54:57 +1100994/*
995 * Initializes options to special values that indicate that they have not yet
996 * been set. Read_config_file will only set options with this value. Options
997 * are processed in the following order: command line, user config file,
998 * system config file. Last, fill_default_options is called.
999 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller4af51302000-04-16 11:18:38 +10001001void
Damien Miller95def091999-11-25 00:26:21 +11001002initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003{
Damien Miller95def091999-11-25 00:26:21 +11001004 memset(options, 'X', sizeof(*options));
1005 options->forward_agent = -1;
1006 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001007 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001008 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001009 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001010 options->gateway_ports = -1;
1011 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001012 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001013 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001014 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001015 options->gss_authentication = -1;
1016 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001017 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001018 options->kbd_interactive_authentication = -1;
1019 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001020 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001021 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001022 options->batch_mode = -1;
1023 options->check_host_ip = -1;
1024 options->strict_host_key_checking = -1;
1025 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001026 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001027 options->compression_level = -1;
1028 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001029 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001031 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001032 options->number_of_password_prompts = -1;
1033 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001034 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001035 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001036 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001037 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001038 options->num_identity_files = 0;
1039 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001040 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001041 options->proxy_command = NULL;
1042 options->user = NULL;
1043 options->escape_char = -1;
1044 options->system_hostfile = NULL;
1045 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001046 options->system_hostfile2 = NULL;
1047 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001048 options->num_local_forwards = 0;
1049 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001050 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001051 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001052 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001053 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001054 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001055 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001056 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001057 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001058 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001059 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001060 options->server_alive_interval = -1;
1061 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001062 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001063 options->control_path = NULL;
1064 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001065 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001066 options->tun_open = -1;
1067 options->tun_local = -1;
1068 options->tun_remote = -1;
1069 options->local_command = NULL;
1070 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001071 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001072 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001073 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074}
1075
Damien Miller5428f641999-11-25 11:54:57 +11001076/*
1077 * Called after processing other sources of option data, this fills those
1078 * options for which no value has been specified with their default values.
1079 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080
Damien Miller4af51302000-04-16 11:18:38 +10001081void
Damien Miller95def091999-11-25 00:26:21 +11001082fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001084 int len;
1085
Damien Miller95def091999-11-25 00:26:21 +11001086 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001087 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001088 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001089 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001090 if (options->forward_x11_trusted == -1)
1091 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001092 if (options->exit_on_forward_failure == -1)
1093 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001094 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001095 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001096 if (options->gateway_ports == -1)
1097 options->gateway_ports = 0;
1098 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001099 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001100 if (options->rsa_authentication == -1)
1101 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001102 if (options->pubkey_authentication == -1)
1103 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001104 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001105 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001106 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001107 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001108 if (options->gss_deleg_creds == -1)
1109 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001110 if (options->password_authentication == -1)
1111 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001112 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001113 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001114 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001115 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001116 if (options->hostbased_authentication == -1)
1117 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001118 if (options->batch_mode == -1)
1119 options->batch_mode = 0;
1120 if (options->check_host_ip == -1)
1121 options->check_host_ip = 1;
1122 if (options->strict_host_key_checking == -1)
1123 options->strict_host_key_checking = 2; /* 2 is default */
1124 if (options->compression == -1)
1125 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001126 if (options->tcp_keep_alive == -1)
1127 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001128 if (options->compression_level == -1)
1129 options->compression_level = 6;
1130 if (options->port == -1)
1131 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001132 if (options->address_family == -1)
1133 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001134 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001135 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001136 if (options->number_of_password_prompts == -1)
1137 options->number_of_password_prompts = 3;
1138 /* Selected in ssh_login(). */
1139 if (options->cipher == -1)
1140 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001141 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001142 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001143 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001144 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001145 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001146 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001147 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001148 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001149 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001150 xmalloc(len);
1151 snprintf(options->identity_files[options->num_identity_files++],
1152 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001153 }
1154 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001155 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1156 options->identity_files[options->num_identity_files] =
1157 xmalloc(len);
1158 snprintf(options->identity_files[options->num_identity_files++],
1159 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1160
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001161 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001162 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001163 xmalloc(len);
1164 snprintf(options->identity_files[options->num_identity_files++],
1165 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001166 }
Damien Millereba71ba2000-04-29 23:57:08 +10001167 }
Damien Miller95def091999-11-25 00:26:21 +11001168 if (options->escape_char == -1)
1169 options->escape_char = '~';
1170 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001171 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001172 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001173 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001174 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001176 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001177 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001178 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001179 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001180 if (options->clear_forwardings == 1)
1181 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001182 if (options->no_host_authentication_for_localhost == - 1)
1183 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001184 if (options->identities_only == -1)
1185 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001186 if (options->enable_ssh_keysign == -1)
1187 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001188 if (options->rekey_limit == -1)
1189 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001190 if (options->verify_host_key_dns == -1)
1191 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001192 if (options->server_alive_interval == -1)
1193 options->server_alive_interval = 0;
1194 if (options->server_alive_count_max == -1)
1195 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001196 if (options->control_master == -1)
1197 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001198 if (options->hash_known_hosts == -1)
1199 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001200 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001201 options->tun_open = SSH_TUNMODE_NO;
1202 if (options->tun_local == -1)
1203 options->tun_local = SSH_TUNID_ANY;
1204 if (options->tun_remote == -1)
1205 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001206 if (options->permit_local_command == -1)
1207 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001208 if (options->use_roaming == -1)
1209 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001210 if (options->visual_host_key == -1)
1211 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001212 if (options->zero_knowledge_password_authentication == -1)
1213 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001214 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001215 /* options->proxy_command should not be set by default */
1216 /* options->user will be set in the main program if appropriate */
1217 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001218 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001219 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001221
1222/*
1223 * parse_forward
1224 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001225 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001226 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001227 * dynamicfwd == 1
1228 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001229 * returns number of arguments parsed or zero on error
1230 */
1231int
Damien Miller4bf648f2009-02-14 16:28:21 +11001232parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001233{
1234 int i;
1235 char *p, *cp, *fwdarg[4];
1236
1237 memset(fwd, '\0', sizeof(*fwd));
1238
1239 cp = p = xstrdup(fwdspec);
1240
1241 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001242 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001243 cp++;
1244
1245 for (i = 0; i < 4; ++i)
1246 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1247 break;
1248
Damien Millerf4b39532008-11-03 19:28:21 +11001249 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001250 if (cp != NULL)
1251 i = 0; /* failure */
1252
1253 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001254 case 1:
1255 fwd->listen_host = NULL;
1256 fwd->listen_port = a2port(fwdarg[0]);
1257 fwd->connect_host = xstrdup("socks");
1258 break;
1259
1260 case 2:
1261 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1262 fwd->listen_port = a2port(fwdarg[1]);
1263 fwd->connect_host = xstrdup("socks");
1264 break;
1265
Damien Millerf91ee4c2005-03-01 21:24:33 +11001266 case 3:
1267 fwd->listen_host = NULL;
1268 fwd->listen_port = a2port(fwdarg[0]);
1269 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1270 fwd->connect_port = a2port(fwdarg[2]);
1271 break;
1272
1273 case 4:
1274 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1275 fwd->listen_port = a2port(fwdarg[1]);
1276 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1277 fwd->connect_port = a2port(fwdarg[3]);
1278 break;
1279 default:
1280 i = 0; /* failure */
1281 }
1282
1283 xfree(p);
1284
Damien Millera699d952008-11-03 19:27:34 +11001285 if (dynamicfwd) {
1286 if (!(i == 1 || i == 2))
1287 goto fail_free;
1288 } else {
1289 if (!(i == 3 || i == 4))
1290 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001291 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001292 goto fail_free;
1293 }
1294
Damien Miller4bf648f2009-02-14 16:28:21 +11001295 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001296 goto fail_free;
1297
1298 if (fwd->connect_host != NULL &&
1299 strlen(fwd->connect_host) >= NI_MAXHOST)
1300 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001301 if (fwd->listen_host != NULL &&
1302 strlen(fwd->listen_host) >= NI_MAXHOST)
1303 goto fail_free;
1304
Damien Millerf91ee4c2005-03-01 21:24:33 +11001305
1306 return (i);
1307
1308 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001309 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001310 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001311 fwd->connect_host = NULL;
1312 }
1313 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001314 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001315 fwd->listen_host = NULL;
1316 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001317 return (0);
1318}