blob: 0a8be140072d4d5cd0fa36e1ceef34730397074b [file] [log] [blame]
Damien Miller3dc71ad2009-01-28 16:31:22 +11001/* $OpenBSD: readconf.c,v 1.175 2009/01/22 10:02:34 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
20
21#include <netinet/in.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
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,
Damien Miller01ed2272008-11-05 16:20:46 +1100133 oVisualHostKey, 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 },
Damien Miller01ed2272008-11-05 16:20:46 +1100231#ifdef JPAKE
232 { "zeroknowledgepasswordauthentication",
233 oZeroKnowledgePasswordAuthentication },
234#else
235 { "zeroknowledgepasswordauthentication", oUnsupported },
236#endif
237
Ben Lindstrom65366a82001-12-06 16:32:47 +0000238 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100239};
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Adds a local TCP/IP port forward to options. Never returns if there is an
243 * error.
244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller4af51302000-04-16 11:18:38 +1000246void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100247add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Miller95def091999-11-25 00:26:21 +1100249 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000250#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100251 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100252 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000253 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100254#endif
Damien Miller95def091999-11-25 00:26:21 +1100255 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
256 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
257 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100258
Damien Miller1a0442f2008-11-05 16:30:06 +1100259 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100260 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100261 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100262 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263}
264
Damien Miller5428f641999-11-25 11:54:57 +1100265/*
266 * Adds a remote TCP/IP port forward to options. Never returns if there is
267 * an error.
268 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller4af51302000-04-16 11:18:38 +1000270void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272{
Damien Miller95def091999-11-25 00:26:21 +1100273 Forward *fwd;
274 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
275 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100276 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100277 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278
Damien Miller1a0442f2008-11-05 16:30:06 +1100279 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100281 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100282 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283}
284
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000285static void
286clear_forwardings(Options *options)
287{
288 int i;
289
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100291 if (options->local_forwards[i].listen_host != NULL)
292 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100293 xfree(options->local_forwards[i].connect_host);
294 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000295 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100297 if (options->remote_forwards[i].listen_host != NULL)
298 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 xfree(options->remote_forwards[i].connect_host);
300 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000301 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100302 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000303}
304
Damien Miller5428f641999-11-25 11:54:57 +1100305/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000306 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100307 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller4af51302000-04-16 11:18:38 +1000309static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100310parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000312 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller95def091999-11-25 00:26:21 +1100314 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100315 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100316 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000318 error("%s: line %d: Bad configuration option: %s",
319 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100320 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321}
322
Damien Miller5428f641999-11-25 11:54:57 +1100323/*
324 * Processes a single option line as used in the configuration files. This
325 * only sets those values that have not already been set.
326 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100327#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
Damien Miller2ccf6611999-11-15 15:25:10 +1100329int
330process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100331 char *line, const char *filename, int linenum,
332 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100335 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100336 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100337 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100338 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Millerc652cac2003-05-14 13:40:54 +1000341 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100342 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000343 if (strchr(WHITESPACE, line[len]) == NULL)
344 break;
345 line[len] = '\0';
346 }
347
Damien Millerbe484b52000-07-15 14:14:16 +1000348 s = line;
349 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100350 if ((keyword = strdelim(&s)) == NULL)
351 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000352 /* Ignore leading whitespace. */
353 if (*keyword == '\0')
354 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000355 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100356 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Miller37023962000-07-11 17:31:38 +1000358 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Miller95def091999-11-25 00:26:21 +1100360 switch (opcode) {
361 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100362 /* don't panic, but count bad options */
363 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100364 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000365 case oConnectTimeout:
366 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100367parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000368 arg = strdelim(&s);
369 if (!arg || *arg == '\0')
370 fatal("%s line %d: missing time value.",
371 filename, linenum);
372 if ((value = convtime(arg)) == -1)
373 fatal("%s line %d: invalid time value.",
374 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100375 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000376 *intptr = value;
377 break;
378
Damien Miller95def091999-11-25 00:26:21 +1100379 case oForwardAgent:
380 intptr = &options->forward_agent;
381parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000382 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000383 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100384 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
385 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000386 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100387 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000388 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100389 value = 0;
390 else
391 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
392 if (*activep && *intptr == -1)
393 *intptr = value;
394 break;
395
396 case oForwardX11:
397 intptr = &options->forward_x11;
398 goto parse_flag;
399
Darren Tucker0a118da2003-10-15 15:54:32 +1000400 case oForwardX11Trusted:
401 intptr = &options->forward_x11_trusted;
402 goto parse_flag;
403
Damien Miller95def091999-11-25 00:26:21 +1100404 case oGatewayPorts:
405 intptr = &options->gateway_ports;
406 goto parse_flag;
407
Darren Tuckere7d4b192006-07-12 22:17:10 +1000408 case oExitOnForwardFailure:
409 intptr = &options->exit_on_forward_failure;
410 goto parse_flag;
411
Damien Miller95def091999-11-25 00:26:21 +1100412 case oUsePrivilegedPort:
413 intptr = &options->use_privileged_port;
414 goto parse_flag;
415
Damien Miller95def091999-11-25 00:26:21 +1100416 case oPasswordAuthentication:
417 intptr = &options->password_authentication;
418 goto parse_flag;
419
Damien Miller01ed2272008-11-05 16:20:46 +1100420 case oZeroKnowledgePasswordAuthentication:
421 intptr = &options->zero_knowledge_password_authentication;
422 goto parse_flag;
423
Damien Miller874d77b2000-10-14 16:23:11 +1100424 case oKbdInteractiveAuthentication:
425 intptr = &options->kbd_interactive_authentication;
426 goto parse_flag;
427
428 case oKbdInteractiveDevices:
429 charptr = &options->kbd_interactive_devices;
430 goto parse_string;
431
Damien Miller0bc1bd82000-11-13 22:57:25 +1100432 case oPubkeyAuthentication:
433 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000434 goto parse_flag;
435
Damien Miller95def091999-11-25 00:26:21 +1100436 case oRSAAuthentication:
437 intptr = &options->rsa_authentication;
438 goto parse_flag;
439
440 case oRhostsRSAAuthentication:
441 intptr = &options->rhosts_rsa_authentication;
442 goto parse_flag;
443
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000444 case oHostbasedAuthentication:
445 intptr = &options->hostbased_authentication;
446 goto parse_flag;
447
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000448 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000449 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100450 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000451
Darren Tucker0efd1552003-08-26 11:49:55 +1000452 case oGssAuthentication:
453 intptr = &options->gss_authentication;
454 goto parse_flag;
455
456 case oGssDelegateCreds:
457 intptr = &options->gss_deleg_creds;
458 goto parse_flag;
459
Damien Miller95def091999-11-25 00:26:21 +1100460 case oBatchMode:
461 intptr = &options->batch_mode;
462 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Damien Miller95def091999-11-25 00:26:21 +1100464 case oCheckHostIP:
465 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000466 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller37876e92003-05-15 10:19:46 +1000468 case oVerifyHostKeyDNS:
469 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100470 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 case oStrictHostKeyChecking:
473 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100474parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000475 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000476 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000477 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100478 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100479 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000480 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100481 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000482 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100483 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000484 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100485 value = 2;
486 else
487 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
488 if (*activep && *intptr == -1)
489 *intptr = value;
490 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Miller95def091999-11-25 00:26:21 +1100492 case oCompression:
493 intptr = &options->compression;
494 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495
Damien Miller12c150e2003-12-17 16:31:10 +1100496 case oTCPKeepAlive:
497 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100498 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000500 case oNoHostAuthenticationForLocalhost:
501 intptr = &options->no_host_authentication_for_localhost;
502 goto parse_flag;
503
Damien Miller95def091999-11-25 00:26:21 +1100504 case oNumberOfPasswordPrompts:
505 intptr = &options->number_of_password_prompts;
506 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Miller95def091999-11-25 00:26:21 +1100508 case oCompressionLevel:
509 intptr = &options->compression_level;
510 goto parse_int;
511
Damien Millera5539d22003-04-09 20:50:06 +1000512 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000513 arg = strdelim(&s);
514 if (!arg || *arg == '\0')
515 fatal("%.200s line %d: Missing argument.", filename, linenum);
516 if (arg[0] < '0' || arg[0] > '9')
517 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100518 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000519 if (arg == endofnumber)
520 fatal("%.200s line %d: Bad number.", filename, linenum);
521 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100522 case '\0':
523 scale = 1;
524 break;
Damien Millera5539d22003-04-09 20:50:06 +1000525 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100526 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000527 break;
528 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100529 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000530 break;
531 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000533 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100534 default:
535 fatal("%.200s line %d: Invalid RekeyLimit suffix",
536 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000537 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100538 val64 *= scale;
539 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100540 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100541 fatal("%.200s line %d: RekeyLimit too large",
542 filename, linenum);
543 if (val64 < 16)
544 fatal("%.200s line %d: RekeyLimit too small",
545 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100546 if (*activep && options->rekey_limit == -1)
547 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000548 break;
549
Damien Miller95def091999-11-25 00:26:21 +1100550 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000551 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000552 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100553 fatal("%.200s line %d: Missing argument.", filename, linenum);
554 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100555 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000556 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100557 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100558 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100559 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000560 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000561 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100562 }
563 break;
564
Damien Millerd3a18572000-06-07 19:55:44 +1000565 case oXAuthLocation:
566 charptr=&options->xauth_location;
567 goto parse_string;
568
Damien Miller95def091999-11-25 00:26:21 +1100569 case oUser:
570 charptr = &options->user;
571parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000572 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000573 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100574 fatal("%.200s line %d: Missing argument.", filename, linenum);
575 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000576 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100577 break;
578
579 case oGlobalKnownHostsFile:
580 charptr = &options->system_hostfile;
581 goto parse_string;
582
583 case oUserKnownHostsFile:
584 charptr = &options->user_hostfile;
585 goto parse_string;
586
Damien Millereba71ba2000-04-29 23:57:08 +1000587 case oGlobalKnownHostsFile2:
588 charptr = &options->system_hostfile2;
589 goto parse_string;
590
591 case oUserKnownHostsFile2:
592 charptr = &options->user_hostfile2;
593 goto parse_string;
594
Damien Miller95def091999-11-25 00:26:21 +1100595 case oHostName:
596 charptr = &options->hostname;
597 goto parse_string;
598
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000599 case oHostKeyAlias:
600 charptr = &options->host_key_alias;
601 goto parse_string;
602
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000603 case oPreferredAuthentications:
604 charptr = &options->preferred_authentications;
605 goto parse_string;
606
Ben Lindstrome0f88042001-04-30 13:06:24 +0000607 case oBindAddress:
608 charptr = &options->bind_address;
609 goto parse_string;
610
Ben Lindstromae996bf2001-08-06 21:27:53 +0000611 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000612 charptr = &options->smartcard_device;
613 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000614
Damien Miller95def091999-11-25 00:26:21 +1100615 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100616 charptr = &options->proxy_command;
617parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000618 if (s == NULL)
619 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100620 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100621 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100622 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100623 return 0;
624
625 case oPort:
626 intptr = &options->port;
627parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000628 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000629 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100630 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000631 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100632 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100633
634 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000635 value = strtol(arg, &endofnumber, 0);
636 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100637 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100638 if (*activep && *intptr == -1)
639 *intptr = value;
640 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641
Damien Miller95def091999-11-25 00:26:21 +1100642 case oConnectionAttempts:
643 intptr = &options->connection_attempts;
644 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100645
Damien Miller95def091999-11-25 00:26:21 +1100646 case oCipher:
647 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000648 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000649 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000650 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000651 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100652 if (value == -1)
653 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100654 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100655 if (*activep && *intptr == -1)
656 *intptr = value;
657 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658
Damien Miller78928792000-04-12 20:17:38 +1000659 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000660 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000661 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000662 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000663 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000664 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100665 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000666 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000667 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000668 break;
669
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000670 case oMacs:
671 arg = strdelim(&s);
672 if (!arg || *arg == '\0')
673 fatal("%.200s line %d: Missing argument.", filename, linenum);
674 if (!mac_valid(arg))
675 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100676 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000677 if (*activep && options->macs == NULL)
678 options->macs = xstrdup(arg);
679 break;
680
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000681 case oHostKeyAlgorithms:
682 arg = strdelim(&s);
683 if (!arg || *arg == '\0')
684 fatal("%.200s line %d: Missing argument.", filename, linenum);
685 if (!key_names_valid2(arg))
686 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100687 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000688 if (*activep && options->hostkeyalgorithms == NULL)
689 options->hostkeyalgorithms = xstrdup(arg);
690 break;
691
Damien Miller78928792000-04-12 20:17:38 +1000692 case oProtocol:
693 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000694 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000695 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000696 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000697 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000698 if (value == SSH_PROTO_UNKNOWN)
699 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100700 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000701 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
702 *intptr = value;
703 break;
704
Damien Miller95def091999-11-25 00:26:21 +1100705 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100706 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000707 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000708 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100709 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000710 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100711 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100712 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
713 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100714 break;
715
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000716 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100717 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100718 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000719 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100720 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000721 fatal("%.200s line %d: Missing port argument.",
722 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100723
Damien Millera699d952008-11-03 19:27:34 +1100724 if (opcode == oLocalForward ||
725 opcode == oRemoteForward) {
726 arg2 = strdelim(&s);
727 if (arg2 == NULL || *arg2 == '\0')
728 fatal("%.200s line %d: Missing target argument.",
729 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100730
Damien Millera699d952008-11-03 19:27:34 +1100731 /* construct a string for parse_forward */
732 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
733 } else if (opcode == oDynamicForward) {
734 strlcpy(fwdarg, arg, sizeof(fwdarg));
735 }
736
737 if (parse_forward(&fwd, fwdarg,
738 opcode == oDynamicForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000739 fatal("%.200s line %d: Bad forwarding specification.",
740 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100741
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000742 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100743 if (opcode == oLocalForward ||
744 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100745 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000746 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100747 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000748 }
Damien Miller95def091999-11-25 00:26:21 +1100749 break;
750
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000751 case oClearAllForwardings:
752 intptr = &options->clear_forwardings;
753 goto parse_flag;
754
Damien Miller95def091999-11-25 00:26:21 +1100755 case oHost:
756 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000757 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000758 if (match_pattern(host, arg)) {
759 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100760 *activep = 1;
761 break;
762 }
Damien Millerbe484b52000-07-15 14:14:16 +1000763 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100764 return 0;
765
766 case oEscapeChar:
767 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000768 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000769 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100770 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000771 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000772 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
773 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000774 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000775 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000776 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000777 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100778 else {
779 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100780 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100781 /* NOTREACHED */
782 value = 0; /* Avoid compiler warning. */
783 }
784 if (*activep && *intptr == -1)
785 *intptr = value;
786 break;
787
Damien Miller20a8f972003-05-18 20:50:30 +1000788 case oAddressFamily:
789 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000790 if (!arg || *arg == '\0')
791 fatal("%s line %d: missing address family.",
792 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000793 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000794 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000795 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000796 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000797 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000798 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000799 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000800 else
801 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000802 if (*activep && *intptr == -1)
803 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000804 break;
805
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000806 case oEnableSSHKeysign:
807 intptr = &options->enable_ssh_keysign;
808 goto parse_flag;
809
Damien Millerbd394c32004-03-08 23:12:36 +1100810 case oIdentitiesOnly:
811 intptr = &options->identities_only;
812 goto parse_flag;
813
Damien Miller509b0102003-12-17 16:33:10 +1100814 case oServerAliveInterval:
815 intptr = &options->server_alive_interval;
816 goto parse_time;
817
818 case oServerAliveCountMax:
819 intptr = &options->server_alive_count_max;
820 goto parse_int;
821
Darren Tucker46bc0752004-05-02 22:11:30 +1000822 case oSendEnv:
823 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
824 if (strchr(arg, '=') != NULL)
825 fatal("%s line %d: Invalid environment name.",
826 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100827 if (!*activep)
828 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000829 if (options->num_send_env >= MAX_SEND_ENV)
830 fatal("%s line %d: too many send env.",
831 filename, linenum);
832 options->send_env[options->num_send_env++] =
833 xstrdup(arg);
834 }
835 break;
836
Damien Miller0e220db2004-06-15 10:34:08 +1000837 case oControlPath:
838 charptr = &options->control_path;
839 goto parse_string;
840
841 case oControlMaster:
842 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000843 arg = strdelim(&s);
844 if (!arg || *arg == '\0')
845 fatal("%.200s line %d: Missing ControlMaster argument.",
846 filename, linenum);
847 value = 0; /* To avoid compiler warning... */
848 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
849 value = SSHCTL_MASTER_YES;
850 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
851 value = SSHCTL_MASTER_NO;
852 else if (strcmp(arg, "auto") == 0)
853 value = SSHCTL_MASTER_AUTO;
854 else if (strcmp(arg, "ask") == 0)
855 value = SSHCTL_MASTER_ASK;
856 else if (strcmp(arg, "autoask") == 0)
857 value = SSHCTL_MASTER_AUTO_ASK;
858 else
859 fatal("%.200s line %d: Bad ControlMaster argument.",
860 filename, linenum);
861 if (*activep && *intptr == -1)
862 *intptr = value;
863 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000864
Damien Millere1776152005-03-01 21:47:37 +1100865 case oHashKnownHosts:
866 intptr = &options->hash_known_hosts;
867 goto parse_flag;
868
Damien Millerd27b9472005-12-13 19:29:02 +1100869 case oTunnel:
870 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100871 arg = strdelim(&s);
872 if (!arg || *arg == '\0')
873 fatal("%s line %d: Missing yes/point-to-point/"
874 "ethernet/no argument.", filename, linenum);
875 value = 0; /* silence compiler */
876 if (strcasecmp(arg, "ethernet") == 0)
877 value = SSH_TUNMODE_ETHERNET;
878 else if (strcasecmp(arg, "point-to-point") == 0)
879 value = SSH_TUNMODE_POINTOPOINT;
880 else if (strcasecmp(arg, "yes") == 0)
881 value = SSH_TUNMODE_DEFAULT;
882 else if (strcasecmp(arg, "no") == 0)
883 value = SSH_TUNMODE_NO;
884 else
885 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
886 "no argument: %s", filename, linenum, arg);
887 if (*activep)
888 *intptr = value;
889 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100890
891 case oTunnelDevice:
892 arg = strdelim(&s);
893 if (!arg || *arg == '\0')
894 fatal("%.200s line %d: Missing argument.", filename, linenum);
895 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100896 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100897 fatal("%.200s line %d: Bad tun device.", filename, linenum);
898 if (*activep) {
899 options->tun_local = value;
900 options->tun_remote = value2;
901 }
902 break;
903
904 case oLocalCommand:
905 charptr = &options->local_command;
906 goto parse_command;
907
908 case oPermitLocalCommand:
909 intptr = &options->permit_local_command;
910 goto parse_flag;
911
Damien Miller10288242008-06-30 00:04:03 +1000912 case oVisualHostKey:
913 intptr = &options->visual_host_key;
914 goto parse_flag;
915
Ben Lindstrom4daea862002-06-09 20:04:02 +0000916 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000917 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000918 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000919 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000920
Damien Millerf9b3feb2003-05-16 11:38:32 +1000921 case oUnsupported:
922 error("%s line %d: Unsupported option \"%s\"",
923 filename, linenum, keyword);
924 return 0;
925
Damien Miller95def091999-11-25 00:26:21 +1100926 default:
927 fatal("process_config_line: Unimplemented opcode %d", opcode);
928 }
929
930 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000931 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000932 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000933 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000934 }
Damien Miller95def091999-11-25 00:26:21 +1100935 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936}
937
938
Damien Miller5428f641999-11-25 11:54:57 +1100939/*
940 * Reads the config file and modifies the options accordingly. Options
941 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000942 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100943 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000945int
Darren Tuckerfc959702004-07-17 16:12:08 +1000946read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000947 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948{
Damien Miller95def091999-11-25 00:26:21 +1100949 FILE *f;
950 char line[1024];
951 int active, linenum;
952 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller57a44762004-04-20 20:11:57 +1000954 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000955 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller57a44762004-04-20 20:11:57 +1000957 if (checkperm) {
958 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000959
Damien Miller33793852004-06-15 10:27:55 +1000960 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000961 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000962 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000963 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000964 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000965 }
966
Damien Miller95def091999-11-25 00:26:21 +1100967 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller5428f641999-11-25 11:54:57 +1100969 /*
970 * Mark that we are now processing the options. This flag is turned
971 * on/off by Host specifications.
972 */
Damien Miller95def091999-11-25 00:26:21 +1100973 active = 1;
974 linenum = 0;
975 while (fgets(line, sizeof(line), f)) {
976 /* Update line number counter. */
977 linenum++;
978 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
979 bad_options++;
980 }
981 fclose(f);
982 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000983 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100984 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000985 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
987
Damien Miller5428f641999-11-25 11:54:57 +1100988/*
989 * Initializes options to special values that indicate that they have not yet
990 * been set. Read_config_file will only set options with this value. Options
991 * are processed in the following order: command line, user config file,
992 * system config file. Last, fill_default_options is called.
993 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller4af51302000-04-16 11:18:38 +1000995void
Damien Miller95def091999-11-25 00:26:21 +1100996initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997{
Damien Miller95def091999-11-25 00:26:21 +1100998 memset(options, 'X', sizeof(*options));
999 options->forward_agent = -1;
1000 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001001 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001002 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001003 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001004 options->gateway_ports = -1;
1005 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001006 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001007 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001008 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001009 options->gss_authentication = -1;
1010 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001011 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001012 options->kbd_interactive_authentication = -1;
1013 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001014 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001015 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->batch_mode = -1;
1017 options->check_host_ip = -1;
1018 options->strict_host_key_checking = -1;
1019 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001020 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001021 options->compression_level = -1;
1022 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001023 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001024 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001025 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001026 options->number_of_password_prompts = -1;
1027 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001028 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001029 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001030 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001031 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001032 options->num_identity_files = 0;
1033 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001034 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001035 options->proxy_command = NULL;
1036 options->user = NULL;
1037 options->escape_char = -1;
1038 options->system_hostfile = NULL;
1039 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001040 options->system_hostfile2 = NULL;
1041 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001042 options->num_local_forwards = 0;
1043 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001044 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001045 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001046 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001047 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001048 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001049 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001050 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001051 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001052 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001053 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001054 options->server_alive_interval = -1;
1055 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001056 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001057 options->control_path = NULL;
1058 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001059 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001060 options->tun_open = -1;
1061 options->tun_local = -1;
1062 options->tun_remote = -1;
1063 options->local_command = NULL;
1064 options->permit_local_command = -1;
Damien Miller10288242008-06-30 00:04:03 +10001065 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001066 options->zero_knowledge_password_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067}
1068
Damien Miller5428f641999-11-25 11:54:57 +11001069/*
1070 * Called after processing other sources of option data, this fills those
1071 * options for which no value has been specified with their default values.
1072 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073
Damien Miller4af51302000-04-16 11:18:38 +10001074void
Damien Miller95def091999-11-25 00:26:21 +11001075fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001077 int len;
1078
Damien Miller95def091999-11-25 00:26:21 +11001079 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001080 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001081 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001082 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001083 if (options->forward_x11_trusted == -1)
1084 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001085 if (options->exit_on_forward_failure == -1)
1086 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001087 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001088 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001089 if (options->gateway_ports == -1)
1090 options->gateway_ports = 0;
1091 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001092 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001093 if (options->rsa_authentication == -1)
1094 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001095 if (options->pubkey_authentication == -1)
1096 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001097 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001098 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001099 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001100 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001101 if (options->gss_deleg_creds == -1)
1102 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->password_authentication == -1)
1104 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001105 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001106 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001108 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001109 if (options->hostbased_authentication == -1)
1110 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001111 if (options->batch_mode == -1)
1112 options->batch_mode = 0;
1113 if (options->check_host_ip == -1)
1114 options->check_host_ip = 1;
1115 if (options->strict_host_key_checking == -1)
1116 options->strict_host_key_checking = 2; /* 2 is default */
1117 if (options->compression == -1)
1118 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001119 if (options->tcp_keep_alive == -1)
1120 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001121 if (options->compression_level == -1)
1122 options->compression_level = 6;
1123 if (options->port == -1)
1124 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001125 if (options->address_family == -1)
1126 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001127 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001128 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001129 if (options->number_of_password_prompts == -1)
1130 options->number_of_password_prompts = 3;
1131 /* Selected in ssh_login(). */
1132 if (options->cipher == -1)
1133 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001134 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001135 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001136 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001137 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001138 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001139 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001140 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001141 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001143 xmalloc(len);
1144 snprintf(options->identity_files[options->num_identity_files++],
1145 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001146 }
1147 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001148 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1149 options->identity_files[options->num_identity_files] =
1150 xmalloc(len);
1151 snprintf(options->identity_files[options->num_identity_files++],
1152 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1153
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001154 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001156 xmalloc(len);
1157 snprintf(options->identity_files[options->num_identity_files++],
1158 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001159 }
Damien Millereba71ba2000-04-29 23:57:08 +10001160 }
Damien Miller95def091999-11-25 00:26:21 +11001161 if (options->escape_char == -1)
1162 options->escape_char = '~';
1163 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001165 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001167 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001168 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001169 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001170 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001171 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001172 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001173 if (options->clear_forwardings == 1)
1174 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001175 if (options->no_host_authentication_for_localhost == - 1)
1176 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001177 if (options->identities_only == -1)
1178 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001179 if (options->enable_ssh_keysign == -1)
1180 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001181 if (options->rekey_limit == -1)
1182 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001183 if (options->verify_host_key_dns == -1)
1184 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001185 if (options->server_alive_interval == -1)
1186 options->server_alive_interval = 0;
1187 if (options->server_alive_count_max == -1)
1188 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001189 if (options->control_master == -1)
1190 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001191 if (options->hash_known_hosts == -1)
1192 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001193 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001194 options->tun_open = SSH_TUNMODE_NO;
1195 if (options->tun_local == -1)
1196 options->tun_local = SSH_TUNID_ANY;
1197 if (options->tun_remote == -1)
1198 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001199 if (options->permit_local_command == -1)
1200 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001201 if (options->visual_host_key == -1)
1202 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001203 if (options->zero_knowledge_password_authentication == -1)
1204 options->zero_knowledge_password_authentication = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001205 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001206 /* options->proxy_command should not be set by default */
1207 /* options->user will be set in the main program if appropriate */
1208 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001209 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001210 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001212
1213/*
1214 * parse_forward
1215 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001216 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001217 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001218 * dynamicfwd == 1
1219 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001220 * returns number of arguments parsed or zero on error
1221 */
1222int
Damien Millera699d952008-11-03 19:27:34 +11001223parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001224{
1225 int i;
1226 char *p, *cp, *fwdarg[4];
1227
1228 memset(fwd, '\0', sizeof(*fwd));
1229
1230 cp = p = xstrdup(fwdspec);
1231
1232 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001233 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001234 cp++;
1235
1236 for (i = 0; i < 4; ++i)
1237 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1238 break;
1239
Damien Millerf4b39532008-11-03 19:28:21 +11001240 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001241 if (cp != NULL)
1242 i = 0; /* failure */
1243
1244 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001245 case 1:
1246 fwd->listen_host = NULL;
1247 fwd->listen_port = a2port(fwdarg[0]);
1248 fwd->connect_host = xstrdup("socks");
1249 break;
1250
1251 case 2:
1252 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1253 fwd->listen_port = a2port(fwdarg[1]);
1254 fwd->connect_host = xstrdup("socks");
1255 break;
1256
Damien Millerf91ee4c2005-03-01 21:24:33 +11001257 case 3:
1258 fwd->listen_host = NULL;
1259 fwd->listen_port = a2port(fwdarg[0]);
1260 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1261 fwd->connect_port = a2port(fwdarg[2]);
1262 break;
1263
1264 case 4:
1265 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1266 fwd->listen_port = a2port(fwdarg[1]);
1267 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1268 fwd->connect_port = a2port(fwdarg[3]);
1269 break;
1270 default:
1271 i = 0; /* failure */
1272 }
1273
1274 xfree(p);
1275
Damien Millera699d952008-11-03 19:27:34 +11001276 if (dynamicfwd) {
1277 if (!(i == 1 || i == 2))
1278 goto fail_free;
1279 } else {
1280 if (!(i == 3 || i == 4))
1281 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001282 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001283 goto fail_free;
1284 }
1285
Damien Miller3dc71ad2009-01-28 16:31:22 +11001286 if (fwd->listen_port <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001287 goto fail_free;
1288
1289 if (fwd->connect_host != NULL &&
1290 strlen(fwd->connect_host) >= NI_MAXHOST)
1291 goto fail_free;
1292
1293 return (i);
1294
1295 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001296 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001297 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001298 fwd->connect_host = NULL;
1299 }
1300 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001301 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001302 fwd->listen_host = NULL;
1303 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001304 return (0);
1305}