blob: e50a422222c41b36af05ca44dcfb94a62f0de661 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millere1776152005-03-01 21:47:37 +110015RCSID("$OpenBSD: readconf.c,v 1.136 2005/03/01 10:40:26 djm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100019#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "cipher.h"
21#include "pathnames.h"
22#include "log.h"
23#include "readconf.h"
24#include "match.h"
25#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000026#include "kex.h"
27#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
29/* Format of the configuration file:
30
31 # Configuration data is parsed as follows:
32 # 1. command line options
33 # 2. user-specific file
34 # 3. system-wide file
35 # Any configuration value is only changed the first time it is set.
36 # Thus, host-specific definitions should be at the beginning of the
37 # configuration file, and defaults at the end.
38
39 # Host-specific declarations. These may override anything above. A single
40 # host may match multiple declarations; these are processed in the order
41 # that they are given in.
42
43 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000044 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46 Host fake.com
47 HostName another.host.name.real.org
48 User blaah
49 Port 34289
50 ForwardX11 no
51 ForwardAgent no
52
53 Host books.com
54 RemoteForward 9999 shadows.cs.hut.fi:9999
55 Cipher 3des
56
57 Host fascist.blob.com
58 Port 23123
59 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060 PasswordAuthentication no
61
62 Host puukko.hut.fi
63 User t35124p
64 ProxyCommand ssh-proxy %h %p
65
66 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000067 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
69 Host *.su
70 Cipher none
71 PasswordAuthentication no
72
73 # Defaults for various options
74 Host *
75 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110076 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication yes
78 RSAAuthentication yes
79 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110081 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 IdentityFile ~/.ssh/identity
83 Port 22
84 EscapeChar ~
85
86*/
87
88/* Keyword tokens. */
89
Damien Miller95def091999-11-25 00:26:21 +110090typedef enum {
91 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +100092 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000093 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +000094 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +110095 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
96 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
97 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
98 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +110099 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000100 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000102 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000103 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000104 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000105 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000106 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000107 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100108 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100109 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000110 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111} OpCodes;
112
113/* Textual representations of the tokens. */
114
Damien Miller95def091999-11-25 00:26:21 +1100115static struct {
116 const char *name;
117 OpCodes opcode;
118} keywords[] = {
119 { "forwardagent", oForwardAgent },
120 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000121 { "forwardx11trusted", oForwardX11Trusted },
Damien Millerd3a18572000-06-07 19:55:44 +1000122 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100123 { "gatewayports", oGatewayPorts },
124 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000125 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100126 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100127 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
128 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100129 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100130 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000131 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000132 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000133 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000134 { "challengeresponseauthentication", oChallengeResponseAuthentication },
135 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
136 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000137 { "kerberosauthentication", oUnsupported },
138 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000139 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000140#if defined(GSSAPI)
141 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000142 { "gssapidelegatecredentials", oGssDelegateCreds },
143#else
144 { "gssapiauthentication", oUnsupported },
145 { "gssapidelegatecredentials", oUnsupported },
146#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000147 { "fallbacktorsh", oDeprecated },
148 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100149 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100151 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100152 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000153 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "proxycommand", oProxyCommand },
155 { "port", oPort },
156 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000157 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000158 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000159 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100160 { "remoteforward", oRemoteForward },
161 { "localforward", oLocalForward },
162 { "user", oUser },
163 { "host", oHost },
164 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000166 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000167 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000168 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100169 { "connectionattempts", oConnectionAttempts },
170 { "batchmode", oBatchMode },
171 { "checkhostip", oCheckHostIP },
172 { "stricthostkeychecking", oStrictHostKeyChecking },
173 { "compression", oCompression },
174 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100175 { "tcpkeepalive", oTCPKeepAlive },
176 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100177 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000179 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000180 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000181 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000182 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000183#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000184 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000185#else
186 { "smartcarddevice", oUnsupported },
187#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000189 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000190 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100191 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000192 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000193 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000194 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100195 { "serveraliveinterval", oServerAliveInterval },
196 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000197 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000198 { "controlpath", oControlPath },
199 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100200 { "hashknownhosts", oHashKnownHosts },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000201 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100202};
203
Damien Miller5428f641999-11-25 11:54:57 +1100204/*
205 * Adds a local TCP/IP port forward to options. Never returns if there is an
206 * error.
207 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Miller4af51302000-04-16 11:18:38 +1000209void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100210add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Damien Miller95def091999-11-25 00:26:21 +1100212 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000213#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100214 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100215 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000216 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100217#endif
Damien Miller95def091999-11-25 00:26:21 +1100218 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
219 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
220 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100221
222 fwd->listen_host = (newfwd->listen_host == NULL) ?
223 NULL : xstrdup(newfwd->listen_host);
224 fwd->listen_port = newfwd->listen_port;
225 fwd->connect_host = xstrdup(newfwd->connect_host);
226 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller5428f641999-11-25 11:54:57 +1100229/*
230 * Adds a remote TCP/IP port forward to options. Never returns if there is
231 * an error.
232 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller4af51302000-04-16 11:18:38 +1000234void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100235add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Damien Miller95def091999-11-25 00:26:21 +1100237 Forward *fwd;
238 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
239 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100240 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100241 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100242
243 fwd->listen_host = (newfwd->listen_host == NULL) ?
244 NULL : xstrdup(newfwd->listen_host);
245 fwd->listen_port = newfwd->listen_port;
246 fwd->connect_host = xstrdup(newfwd->connect_host);
247 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248}
249
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000250static void
251clear_forwardings(Options *options)
252{
253 int i;
254
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255 for (i = 0; i < options->num_local_forwards; i++) {
256 xfree(options->local_forwards[i].listen_host);
257 xfree(options->local_forwards[i].connect_host);
258 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000259 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100260 for (i = 0; i < options->num_remote_forwards; i++) {
261 xfree(options->remote_forwards[i].listen_host);
262 xfree(options->remote_forwards[i].connect_host);
263 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000264 options->num_remote_forwards = 0;
265}
266
Damien Miller5428f641999-11-25 11:54:57 +1100267/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000268 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100269 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller4af51302000-04-16 11:18:38 +1000271static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100272parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000274 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller95def091999-11-25 00:26:21 +1100276 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100277 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100278 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000280 error("%s: line %d: Bad configuration option: %s",
281 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100282 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283}
284
Damien Miller5428f641999-11-25 11:54:57 +1100285/*
286 * Processes a single option line as used in the configuration files. This
287 * only sets those values that have not already been set.
288 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100289#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller2ccf6611999-11-15 15:25:10 +1100291int
292process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100293 char *line, const char *filename, int linenum,
294 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Milleraae6c611999-12-06 11:47:28 +1100297 int opcode, *intptr, value;
Damien Miller61f08ac2003-02-24 11:56:27 +1100298 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Millerc652cac2003-05-14 13:40:54 +1000301 /* Strip trailing whitespace */
302 for(len = strlen(line) - 1; len > 0; len--) {
303 if (strchr(WHITESPACE, line[len]) == NULL)
304 break;
305 line[len] = '\0';
306 }
307
Damien Millerbe484b52000-07-15 14:14:16 +1000308 s = line;
309 /* Get the keyword. (Each line is supposed to begin with a keyword). */
310 keyword = strdelim(&s);
311 /* Ignore leading whitespace. */
312 if (*keyword == '\0')
313 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000314 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100315 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller37023962000-07-11 17:31:38 +1000317 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller95def091999-11-25 00:26:21 +1100319 switch (opcode) {
320 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100321 /* don't panic, but count bad options */
322 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100323 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000324 case oConnectTimeout:
325 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100326parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000327 arg = strdelim(&s);
328 if (!arg || *arg == '\0')
329 fatal("%s line %d: missing time value.",
330 filename, linenum);
331 if ((value = convtime(arg)) == -1)
332 fatal("%s line %d: invalid time value.",
333 filename, linenum);
334 if (*intptr == -1)
335 *intptr = value;
336 break;
337
Damien Miller95def091999-11-25 00:26:21 +1100338 case oForwardAgent:
339 intptr = &options->forward_agent;
340parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000341 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000342 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100343 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
344 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000345 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100346 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000347 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100348 value = 0;
349 else
350 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
351 if (*activep && *intptr == -1)
352 *intptr = value;
353 break;
354
355 case oForwardX11:
356 intptr = &options->forward_x11;
357 goto parse_flag;
358
Darren Tucker0a118da2003-10-15 15:54:32 +1000359 case oForwardX11Trusted:
360 intptr = &options->forward_x11_trusted;
361 goto parse_flag;
362
Damien Miller95def091999-11-25 00:26:21 +1100363 case oGatewayPorts:
364 intptr = &options->gateway_ports;
365 goto parse_flag;
366
367 case oUsePrivilegedPort:
368 intptr = &options->use_privileged_port;
369 goto parse_flag;
370
Damien Miller95def091999-11-25 00:26:21 +1100371 case oPasswordAuthentication:
372 intptr = &options->password_authentication;
373 goto parse_flag;
374
Damien Miller874d77b2000-10-14 16:23:11 +1100375 case oKbdInteractiveAuthentication:
376 intptr = &options->kbd_interactive_authentication;
377 goto parse_flag;
378
379 case oKbdInteractiveDevices:
380 charptr = &options->kbd_interactive_devices;
381 goto parse_string;
382
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 case oPubkeyAuthentication:
384 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000385 goto parse_flag;
386
Damien Miller95def091999-11-25 00:26:21 +1100387 case oRSAAuthentication:
388 intptr = &options->rsa_authentication;
389 goto parse_flag;
390
391 case oRhostsRSAAuthentication:
392 intptr = &options->rhosts_rsa_authentication;
393 goto parse_flag;
394
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000395 case oHostbasedAuthentication:
396 intptr = &options->hostbased_authentication;
397 goto parse_flag;
398
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000399 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000400 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100401 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000402
Darren Tucker0efd1552003-08-26 11:49:55 +1000403 case oGssAuthentication:
404 intptr = &options->gss_authentication;
405 goto parse_flag;
406
407 case oGssDelegateCreds:
408 intptr = &options->gss_deleg_creds;
409 goto parse_flag;
410
Damien Miller95def091999-11-25 00:26:21 +1100411 case oBatchMode:
412 intptr = &options->batch_mode;
413 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 case oCheckHostIP:
416 intptr = &options->check_host_ip;
417 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller37876e92003-05-15 10:19:46 +1000419 case oVerifyHostKeyDNS:
420 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100421 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 case oStrictHostKeyChecking:
424 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100425parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000426 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000427 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000428 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100429 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100430 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000431 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100432 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000433 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100434 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000435 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100436 value = 2;
437 else
438 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
439 if (*activep && *intptr == -1)
440 *intptr = value;
441 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Damien Miller95def091999-11-25 00:26:21 +1100443 case oCompression:
444 intptr = &options->compression;
445 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller12c150e2003-12-17 16:31:10 +1100447 case oTCPKeepAlive:
448 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100449 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000451 case oNoHostAuthenticationForLocalhost:
452 intptr = &options->no_host_authentication_for_localhost;
453 goto parse_flag;
454
Damien Miller95def091999-11-25 00:26:21 +1100455 case oNumberOfPasswordPrompts:
456 intptr = &options->number_of_password_prompts;
457 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Damien Miller95def091999-11-25 00:26:21 +1100459 case oCompressionLevel:
460 intptr = &options->compression_level;
461 goto parse_int;
462
Damien Millera5539d22003-04-09 20:50:06 +1000463 case oRekeyLimit:
464 intptr = &options->rekey_limit;
465 arg = strdelim(&s);
466 if (!arg || *arg == '\0')
467 fatal("%.200s line %d: Missing argument.", filename, linenum);
468 if (arg[0] < '0' || arg[0] > '9')
469 fatal("%.200s line %d: Bad number.", filename, linenum);
470 value = strtol(arg, &endofnumber, 10);
471 if (arg == endofnumber)
472 fatal("%.200s line %d: Bad number.", filename, linenum);
473 switch (toupper(*endofnumber)) {
474 case 'K':
475 value *= 1<<10;
476 break;
477 case 'M':
478 value *= 1<<20;
479 break;
480 case 'G':
481 value *= 1<<30;
482 break;
483 }
484 if (*activep && *intptr == -1)
485 *intptr = value;
486 break;
487
Damien Miller95def091999-11-25 00:26:21 +1100488 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000489 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000490 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100491 fatal("%.200s line %d: Missing argument.", filename, linenum);
492 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000494 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100495 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100496 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000498 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000499 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100500 }
501 break;
502
Damien Millerd3a18572000-06-07 19:55:44 +1000503 case oXAuthLocation:
504 charptr=&options->xauth_location;
505 goto parse_string;
506
Damien Miller95def091999-11-25 00:26:21 +1100507 case oUser:
508 charptr = &options->user;
509parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000510 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000511 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100512 fatal("%.200s line %d: Missing argument.", filename, linenum);
513 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000514 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100515 break;
516
517 case oGlobalKnownHostsFile:
518 charptr = &options->system_hostfile;
519 goto parse_string;
520
521 case oUserKnownHostsFile:
522 charptr = &options->user_hostfile;
523 goto parse_string;
524
Damien Millereba71ba2000-04-29 23:57:08 +1000525 case oGlobalKnownHostsFile2:
526 charptr = &options->system_hostfile2;
527 goto parse_string;
528
529 case oUserKnownHostsFile2:
530 charptr = &options->user_hostfile2;
531 goto parse_string;
532
Damien Miller95def091999-11-25 00:26:21 +1100533 case oHostName:
534 charptr = &options->hostname;
535 goto parse_string;
536
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000537 case oHostKeyAlias:
538 charptr = &options->host_key_alias;
539 goto parse_string;
540
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000541 case oPreferredAuthentications:
542 charptr = &options->preferred_authentications;
543 goto parse_string;
544
Ben Lindstrome0f88042001-04-30 13:06:24 +0000545 case oBindAddress:
546 charptr = &options->bind_address;
547 goto parse_string;
548
Ben Lindstromae996bf2001-08-06 21:27:53 +0000549 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000550 charptr = &options->smartcard_device;
551 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000552
Damien Miller95def091999-11-25 00:26:21 +1100553 case oProxyCommand:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000554 if (s == NULL)
555 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100556 charptr = &options->proxy_command;
Damien Miller61f08ac2003-02-24 11:56:27 +1100557 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100558 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100559 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100560 return 0;
561
562 case oPort:
563 intptr = &options->port;
564parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000565 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000566 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100567 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000568 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100569 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100570
571 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000572 value = strtol(arg, &endofnumber, 0);
573 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100574 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100575 if (*activep && *intptr == -1)
576 *intptr = value;
577 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 case oConnectionAttempts:
580 intptr = &options->connection_attempts;
581 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100582
Damien Miller95def091999-11-25 00:26:21 +1100583 case oCipher:
584 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000585 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000586 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000587 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000588 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100589 if (value == -1)
590 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100592 if (*activep && *intptr == -1)
593 *intptr = value;
594 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Damien Miller78928792000-04-12 20:17:38 +1000596 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000597 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000598 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000599 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000600 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000601 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100602 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000603 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000604 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000605 break;
606
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000607 case oMacs:
608 arg = strdelim(&s);
609 if (!arg || *arg == '\0')
610 fatal("%.200s line %d: Missing argument.", filename, linenum);
611 if (!mac_valid(arg))
612 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100613 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000614 if (*activep && options->macs == NULL)
615 options->macs = xstrdup(arg);
616 break;
617
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000618 case oHostKeyAlgorithms:
619 arg = strdelim(&s);
620 if (!arg || *arg == '\0')
621 fatal("%.200s line %d: Missing argument.", filename, linenum);
622 if (!key_names_valid2(arg))
623 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100624 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000625 if (*activep && options->hostkeyalgorithms == NULL)
626 options->hostkeyalgorithms = xstrdup(arg);
627 break;
628
Damien Miller78928792000-04-12 20:17:38 +1000629 case oProtocol:
630 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000631 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000632 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000633 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000634 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000635 if (value == SSH_PROTO_UNKNOWN)
636 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100637 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000638 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
639 *intptr = value;
640 break;
641
Damien Miller95def091999-11-25 00:26:21 +1100642 case oLogLevel:
643 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000644 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000645 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100646 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000647 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100648 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100649 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100650 *intptr = (LogLevel) value;
651 break;
652
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000653 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100654 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000655 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100656 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000657 fatal("%.200s line %d: Missing port argument.",
658 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100659 arg2 = strdelim(&s);
660 if (arg2 == NULL || *arg2 == '\0')
661 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000662 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100663
664 /* construct a string for parse_forward */
665 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
666
667 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000668 fatal("%.200s line %d: Bad forwarding specification.",
669 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100670
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000671 if (*activep) {
672 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100673 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000674 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100675 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000676 }
Damien Miller95def091999-11-25 00:26:21 +1100677 break;
678
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000679 case oDynamicForward:
680 arg = strdelim(&s);
681 if (!arg || *arg == '\0')
682 fatal("%.200s line %d: Missing port argument.",
683 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100684 memset(&fwd, '\0', sizeof(fwd));
685 fwd.connect_host = "socks";
686 fwd.listen_host = hpdelim(&arg);
687 if (fwd.listen_host == NULL ||
688 strlen(fwd.listen_host) >= NI_MAXHOST)
689 fatal("%.200s line %d: Bad forwarding specification.",
690 filename, linenum);
691 if (arg) {
692 fwd.listen_port = a2port(arg);
693 fwd.listen_host = cleanhostname(fwd.listen_host);
694 } else {
695 fwd.listen_port = a2port(fwd.listen_host);
696 fwd.listen_host = "";
697 }
698 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000699 fatal("%.200s line %d: Badly formatted port number.",
700 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000701 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100702 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000703 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000704
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000705 case oClearAllForwardings:
706 intptr = &options->clear_forwardings;
707 goto parse_flag;
708
Damien Miller95def091999-11-25 00:26:21 +1100709 case oHost:
710 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000711 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000712 if (match_pattern(host, arg)) {
713 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100714 *activep = 1;
715 break;
716 }
Damien Millerbe484b52000-07-15 14:14:16 +1000717 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100718 return 0;
719
720 case oEscapeChar:
721 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000722 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000723 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100724 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000725 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000726 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
727 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000728 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000729 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000730 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000731 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100732 else {
733 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100734 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100735 /* NOTREACHED */
736 value = 0; /* Avoid compiler warning. */
737 }
738 if (*activep && *intptr == -1)
739 *intptr = value;
740 break;
741
Damien Miller20a8f972003-05-18 20:50:30 +1000742 case oAddressFamily:
743 arg = strdelim(&s);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000744 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000745 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000746 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000747 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000748 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000749 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000750 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000751 else
752 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000753 if (*activep && *intptr == -1)
754 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000755 break;
756
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000757 case oEnableSSHKeysign:
758 intptr = &options->enable_ssh_keysign;
759 goto parse_flag;
760
Damien Millerbd394c32004-03-08 23:12:36 +1100761 case oIdentitiesOnly:
762 intptr = &options->identities_only;
763 goto parse_flag;
764
Damien Miller509b0102003-12-17 16:33:10 +1100765 case oServerAliveInterval:
766 intptr = &options->server_alive_interval;
767 goto parse_time;
768
769 case oServerAliveCountMax:
770 intptr = &options->server_alive_count_max;
771 goto parse_int;
772
Darren Tucker46bc0752004-05-02 22:11:30 +1000773 case oSendEnv:
774 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
775 if (strchr(arg, '=') != NULL)
776 fatal("%s line %d: Invalid environment name.",
777 filename, linenum);
778 if (options->num_send_env >= MAX_SEND_ENV)
779 fatal("%s line %d: too many send env.",
780 filename, linenum);
781 options->send_env[options->num_send_env++] =
782 xstrdup(arg);
783 }
784 break;
785
Damien Miller0e220db2004-06-15 10:34:08 +1000786 case oControlPath:
787 charptr = &options->control_path;
788 goto parse_string;
789
790 case oControlMaster:
791 intptr = &options->control_master;
Damien Miller23f07702004-06-18 01:19:03 +1000792 goto parse_yesnoask;
Damien Miller0e220db2004-06-15 10:34:08 +1000793
Damien Millere1776152005-03-01 21:47:37 +1100794 case oHashKnownHosts:
795 intptr = &options->hash_known_hosts;
796 goto parse_flag;
797
Ben Lindstrom4daea862002-06-09 20:04:02 +0000798 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000799 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000800 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000801 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000802
Damien Millerf9b3feb2003-05-16 11:38:32 +1000803 case oUnsupported:
804 error("%s line %d: Unsupported option \"%s\"",
805 filename, linenum, keyword);
806 return 0;
807
Damien Miller95def091999-11-25 00:26:21 +1100808 default:
809 fatal("process_config_line: Unimplemented opcode %d", opcode);
810 }
811
812 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000813 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000814 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100815 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000816 }
Damien Miller95def091999-11-25 00:26:21 +1100817 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818}
819
820
Damien Miller5428f641999-11-25 11:54:57 +1100821/*
822 * Reads the config file and modifies the options accordingly. Options
823 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000824 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100825 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000827int
Darren Tuckerfc959702004-07-17 16:12:08 +1000828read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000829 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830{
Damien Miller95def091999-11-25 00:26:21 +1100831 FILE *f;
832 char line[1024];
833 int active, linenum;
834 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835
Damien Miller95def091999-11-25 00:26:21 +1100836 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000837 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000838 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839
Damien Miller57a44762004-04-20 20:11:57 +1000840 if (checkperm) {
841 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000842
Damien Miller33793852004-06-15 10:27:55 +1000843 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000844 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000845 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000846 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000847 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000848 }
849
Damien Miller95def091999-11-25 00:26:21 +1100850 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller5428f641999-11-25 11:54:57 +1100852 /*
853 * Mark that we are now processing the options. This flag is turned
854 * on/off by Host specifications.
855 */
Damien Miller95def091999-11-25 00:26:21 +1100856 active = 1;
857 linenum = 0;
858 while (fgets(line, sizeof(line), f)) {
859 /* Update line number counter. */
860 linenum++;
861 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
862 bad_options++;
863 }
864 fclose(f);
865 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000866 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100867 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000868 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869}
870
Damien Miller5428f641999-11-25 11:54:57 +1100871/*
872 * Initializes options to special values that indicate that they have not yet
873 * been set. Read_config_file will only set options with this value. Options
874 * are processed in the following order: command line, user config file,
875 * system config file. Last, fill_default_options is called.
876 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller4af51302000-04-16 11:18:38 +1000878void
Damien Miller95def091999-11-25 00:26:21 +1100879initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
Damien Miller95def091999-11-25 00:26:21 +1100881 memset(options, 'X', sizeof(*options));
882 options->forward_agent = -1;
883 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000884 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000885 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100886 options->gateway_ports = -1;
887 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100888 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100889 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000890 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000891 options->gss_authentication = -1;
892 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100893 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100894 options->kbd_interactive_authentication = -1;
895 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100896 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000897 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100898 options->batch_mode = -1;
899 options->check_host_ip = -1;
900 options->strict_host_key_checking = -1;
901 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100902 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +1100903 options->compression_level = -1;
904 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000905 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +1100906 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000907 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +1100908 options->number_of_password_prompts = -1;
909 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000910 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000911 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000912 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000913 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100914 options->num_identity_files = 0;
915 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000916 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100917 options->proxy_command = NULL;
918 options->user = NULL;
919 options->escape_char = -1;
920 options->system_hostfile = NULL;
921 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000922 options->system_hostfile2 = NULL;
923 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100924 options->num_local_forwards = 0;
925 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000926 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100927 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000928 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000929 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000930 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000931 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000932 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +1100933 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +1000934 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +1000935 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +1100936 options->server_alive_interval = -1;
937 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +1000938 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000939 options->control_path = NULL;
940 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +1100941 options->hash_known_hosts = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942}
943
Damien Miller5428f641999-11-25 11:54:57 +1100944/*
945 * Called after processing other sources of option data, this fills those
946 * options for which no value has been specified with their default values.
947 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller4af51302000-04-16 11:18:38 +1000949void
Damien Miller95def091999-11-25 00:26:21 +1100950fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000952 int len;
953
Damien Miller95def091999-11-25 00:26:21 +1100954 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000955 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100956 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100957 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +1000958 if (options->forward_x11_trusted == -1)
959 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000960 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000961 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100962 if (options->gateway_ports == -1)
963 options->gateway_ports = 0;
964 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +0000965 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +1100966 if (options->rsa_authentication == -1)
967 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100968 if (options->pubkey_authentication == -1)
969 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000970 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +0000971 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000972 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +1000973 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000974 if (options->gss_deleg_creds == -1)
975 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100976 if (options->password_authentication == -1)
977 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100978 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000979 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100980 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000981 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000982 if (options->hostbased_authentication == -1)
983 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100984 if (options->batch_mode == -1)
985 options->batch_mode = 0;
986 if (options->check_host_ip == -1)
987 options->check_host_ip = 1;
988 if (options->strict_host_key_checking == -1)
989 options->strict_host_key_checking = 2; /* 2 is default */
990 if (options->compression == -1)
991 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +1100992 if (options->tcp_keep_alive == -1)
993 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +1100994 if (options->compression_level == -1)
995 options->compression_level = 6;
996 if (options->port == -1)
997 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000998 if (options->address_family == -1)
999 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001000 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001001 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001002 if (options->number_of_password_prompts == -1)
1003 options->number_of_password_prompts = 3;
1004 /* Selected in ssh_login(). */
1005 if (options->cipher == -1)
1006 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001007 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001008 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001009 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001010 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001011 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001012 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001013 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001014 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001015 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001016 xmalloc(len);
1017 snprintf(options->identity_files[options->num_identity_files++],
1018 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001019 }
1020 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001021 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1022 options->identity_files[options->num_identity_files] =
1023 xmalloc(len);
1024 snprintf(options->identity_files[options->num_identity_files++],
1025 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1026
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001027 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001028 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001029 xmalloc(len);
1030 snprintf(options->identity_files[options->num_identity_files++],
1031 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001032 }
Damien Millereba71ba2000-04-29 23:57:08 +10001033 }
Damien Miller95def091999-11-25 00:26:21 +11001034 if (options->escape_char == -1)
1035 options->escape_char = '~';
1036 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001037 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001038 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001039 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001040 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001041 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001042 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001043 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001044 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001045 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001046 if (options->clear_forwardings == 1)
1047 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001048 if (options->no_host_authentication_for_localhost == - 1)
1049 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001050 if (options->identities_only == -1)
1051 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001052 if (options->enable_ssh_keysign == -1)
1053 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001054 if (options->rekey_limit == -1)
1055 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001056 if (options->verify_host_key_dns == -1)
1057 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001058 if (options->server_alive_interval == -1)
1059 options->server_alive_interval = 0;
1060 if (options->server_alive_count_max == -1)
1061 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001062 if (options->control_master == -1)
1063 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001064 if (options->hash_known_hosts == -1)
1065 options->hash_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +11001066 /* options->proxy_command should not be set by default */
1067 /* options->user will be set in the main program if appropriate */
1068 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001069 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001070 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001072
1073/*
1074 * parse_forward
1075 * parses a string containing a port forwarding specification of the form:
1076 * [listenhost:]listenport:connecthost:connectport
1077 * returns number of arguments parsed or zero on error
1078 */
1079int
1080parse_forward(Forward *fwd, const char *fwdspec)
1081{
1082 int i;
1083 char *p, *cp, *fwdarg[4];
1084
1085 memset(fwd, '\0', sizeof(*fwd));
1086
1087 cp = p = xstrdup(fwdspec);
1088
1089 /* skip leading spaces */
1090 while (*cp && isspace(*cp))
1091 cp++;
1092
1093 for (i = 0; i < 4; ++i)
1094 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1095 break;
1096
1097 /* Check for trailing garbage in 4-arg case*/
1098 if (cp != NULL)
1099 i = 0; /* failure */
1100
1101 switch (i) {
1102 case 3:
1103 fwd->listen_host = NULL;
1104 fwd->listen_port = a2port(fwdarg[0]);
1105 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1106 fwd->connect_port = a2port(fwdarg[2]);
1107 break;
1108
1109 case 4:
1110 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1111 fwd->listen_port = a2port(fwdarg[1]);
1112 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1113 fwd->connect_port = a2port(fwdarg[3]);
1114 break;
1115 default:
1116 i = 0; /* failure */
1117 }
1118
1119 xfree(p);
1120
1121 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1122 goto fail_free;
1123
1124 if (fwd->connect_host != NULL &&
1125 strlen(fwd->connect_host) >= NI_MAXHOST)
1126 goto fail_free;
1127
1128 return (i);
1129
1130 fail_free:
1131 if (fwd->connect_host != NULL)
1132 xfree(fwd->connect_host);
1133 if (fwd->listen_host != NULL)
1134 xfree(fwd->listen_host);
1135 return (0);
1136}