blob: 2b1d7cc46d33aa4260bb0e56e98b004c334161a4 [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 Miller0e220db2004-06-15 10:34:08 +100015RCSID("$OpenBSD: readconf.c,v 1.132 2004/06/13 15:03:02 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 Miller0e220db2004-06-15 10:34:08 +1000109 oSendEnv, oControlPath, oControlMaster,
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 },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000200 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100201};
202
Damien Miller5428f641999-11-25 11:54:57 +1100203/*
204 * Adds a local TCP/IP port forward to options. Never returns if there is an
205 * error.
206 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207
Damien Miller4af51302000-04-16 11:18:38 +1000208void
Damien Milleraae6c611999-12-06 11:47:28 +1100209add_local_forward(Options *options, u_short port, const char *host,
210 u_short host_port)
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 Miller95def091999-11-25 00:26:21 +1100215 if (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++];
221 fwd->port = port;
222 fwd->host = xstrdup(host);
223 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
227 * Adds a remote TCP/IP port forward to options. Never returns if there is
228 * an error.
229 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller4af51302000-04-16 11:18:38 +1000231void
Damien Milleraae6c611999-12-06 11:47:28 +1100232add_remote_forward(Options *options, u_short port, const char *host,
233 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234{
Damien Miller95def091999-11-25 00:26:21 +1100235 Forward *fwd;
236 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
237 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100239 fwd = &options->remote_forwards[options->num_remote_forwards++];
240 fwd->port = port;
241 fwd->host = xstrdup(host);
242 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000245static void
246clear_forwardings(Options *options)
247{
248 int i;
249
250 for (i = 0; i < options->num_local_forwards; i++)
251 xfree(options->local_forwards[i].host);
252 options->num_local_forwards = 0;
253 for (i = 0; i < options->num_remote_forwards; i++)
254 xfree(options->remote_forwards[i].host);
255 options->num_remote_forwards = 0;
256}
257
Damien Miller5428f641999-11-25 11:54:57 +1100258/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000259 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100260 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller4af51302000-04-16 11:18:38 +1000262static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100263parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000265 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller95def091999-11-25 00:26:21 +1100267 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100268 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100269 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000271 error("%s: line %d: Bad configuration option: %s",
272 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100273 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274}
275
Damien Miller5428f641999-11-25 11:54:57 +1100276/*
277 * Processes a single option line as used in the configuration files. This
278 * only sets those values that have not already been set.
279 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100280#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller2ccf6611999-11-15 15:25:10 +1100282int
283process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100284 char *line, const char *filename, int linenum,
285 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Damien Miller61f08ac2003-02-24 11:56:27 +1100287 char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100288 int opcode, *intptr, value;
Damien Miller61f08ac2003-02-24 11:56:27 +1100289 size_t len;
Damien Milleraae6c611999-12-06 11:47:28 +1100290 u_short fwd_port, fwd_host_port;
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000291 char sfwd_host_port[6];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Millerc652cac2003-05-14 13:40:54 +1000293 /* Strip trailing whitespace */
294 for(len = strlen(line) - 1; len > 0; len--) {
295 if (strchr(WHITESPACE, line[len]) == NULL)
296 break;
297 line[len] = '\0';
298 }
299
Damien Millerbe484b52000-07-15 14:14:16 +1000300 s = line;
301 /* Get the keyword. (Each line is supposed to begin with a keyword). */
302 keyword = strdelim(&s);
303 /* Ignore leading whitespace. */
304 if (*keyword == '\0')
305 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000306 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100307 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller37023962000-07-11 17:31:38 +1000309 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Miller95def091999-11-25 00:26:21 +1100311 switch (opcode) {
312 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100313 /* don't panic, but count bad options */
314 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100315 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000316 case oConnectTimeout:
317 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100318parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000319 arg = strdelim(&s);
320 if (!arg || *arg == '\0')
321 fatal("%s line %d: missing time value.",
322 filename, linenum);
323 if ((value = convtime(arg)) == -1)
324 fatal("%s line %d: invalid time value.",
325 filename, linenum);
326 if (*intptr == -1)
327 *intptr = value;
328 break;
329
Damien Miller95def091999-11-25 00:26:21 +1100330 case oForwardAgent:
331 intptr = &options->forward_agent;
332parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000333 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000334 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100335 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
336 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000337 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100338 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000339 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100340 value = 0;
341 else
342 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
343 if (*activep && *intptr == -1)
344 *intptr = value;
345 break;
346
347 case oForwardX11:
348 intptr = &options->forward_x11;
349 goto parse_flag;
350
Darren Tucker0a118da2003-10-15 15:54:32 +1000351 case oForwardX11Trusted:
352 intptr = &options->forward_x11_trusted;
353 goto parse_flag;
354
Damien Miller95def091999-11-25 00:26:21 +1100355 case oGatewayPorts:
356 intptr = &options->gateway_ports;
357 goto parse_flag;
358
359 case oUsePrivilegedPort:
360 intptr = &options->use_privileged_port;
361 goto parse_flag;
362
Damien Miller95def091999-11-25 00:26:21 +1100363 case oPasswordAuthentication:
364 intptr = &options->password_authentication;
365 goto parse_flag;
366
Damien Miller874d77b2000-10-14 16:23:11 +1100367 case oKbdInteractiveAuthentication:
368 intptr = &options->kbd_interactive_authentication;
369 goto parse_flag;
370
371 case oKbdInteractiveDevices:
372 charptr = &options->kbd_interactive_devices;
373 goto parse_string;
374
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 case oPubkeyAuthentication:
376 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000377 goto parse_flag;
378
Damien Miller95def091999-11-25 00:26:21 +1100379 case oRSAAuthentication:
380 intptr = &options->rsa_authentication;
381 goto parse_flag;
382
383 case oRhostsRSAAuthentication:
384 intptr = &options->rhosts_rsa_authentication;
385 goto parse_flag;
386
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000387 case oHostbasedAuthentication:
388 intptr = &options->hostbased_authentication;
389 goto parse_flag;
390
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000391 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000392 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100393 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000394
Darren Tucker0efd1552003-08-26 11:49:55 +1000395 case oGssAuthentication:
396 intptr = &options->gss_authentication;
397 goto parse_flag;
398
399 case oGssDelegateCreds:
400 intptr = &options->gss_deleg_creds;
401 goto parse_flag;
402
Damien Miller95def091999-11-25 00:26:21 +1100403 case oBatchMode:
404 intptr = &options->batch_mode;
405 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 case oCheckHostIP:
408 intptr = &options->check_host_ip;
409 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Damien Miller37876e92003-05-15 10:19:46 +1000411 case oVerifyHostKeyDNS:
412 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100413 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 case oStrictHostKeyChecking:
416 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100417parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000418 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000419 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000420 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100421 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100422 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000423 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100424 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000425 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100426 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000427 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100428 value = 2;
429 else
430 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
431 if (*activep && *intptr == -1)
432 *intptr = value;
433 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller95def091999-11-25 00:26:21 +1100435 case oCompression:
436 intptr = &options->compression;
437 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller12c150e2003-12-17 16:31:10 +1100439 case oTCPKeepAlive:
440 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100441 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000443 case oNoHostAuthenticationForLocalhost:
444 intptr = &options->no_host_authentication_for_localhost;
445 goto parse_flag;
446
Damien Miller95def091999-11-25 00:26:21 +1100447 case oNumberOfPasswordPrompts:
448 intptr = &options->number_of_password_prompts;
449 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 case oCompressionLevel:
452 intptr = &options->compression_level;
453 goto parse_int;
454
Damien Millera5539d22003-04-09 20:50:06 +1000455 case oRekeyLimit:
456 intptr = &options->rekey_limit;
457 arg = strdelim(&s);
458 if (!arg || *arg == '\0')
459 fatal("%.200s line %d: Missing argument.", filename, linenum);
460 if (arg[0] < '0' || arg[0] > '9')
461 fatal("%.200s line %d: Bad number.", filename, linenum);
462 value = strtol(arg, &endofnumber, 10);
463 if (arg == endofnumber)
464 fatal("%.200s line %d: Bad number.", filename, linenum);
465 switch (toupper(*endofnumber)) {
466 case 'K':
467 value *= 1<<10;
468 break;
469 case 'M':
470 value *= 1<<20;
471 break;
472 case 'G':
473 value *= 1<<30;
474 break;
475 }
476 if (*activep && *intptr == -1)
477 *intptr = value;
478 break;
479
Damien Miller95def091999-11-25 00:26:21 +1100480 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000481 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000482 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100483 fatal("%.200s line %d: Missing argument.", filename, linenum);
484 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000486 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100487 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100488 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000490 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000491 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100492 }
493 break;
494
Damien Millerd3a18572000-06-07 19:55:44 +1000495 case oXAuthLocation:
496 charptr=&options->xauth_location;
497 goto parse_string;
498
Damien Miller95def091999-11-25 00:26:21 +1100499 case oUser:
500 charptr = &options->user;
501parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000502 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000503 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100504 fatal("%.200s line %d: Missing argument.", filename, linenum);
505 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000506 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100507 break;
508
509 case oGlobalKnownHostsFile:
510 charptr = &options->system_hostfile;
511 goto parse_string;
512
513 case oUserKnownHostsFile:
514 charptr = &options->user_hostfile;
515 goto parse_string;
516
Damien Millereba71ba2000-04-29 23:57:08 +1000517 case oGlobalKnownHostsFile2:
518 charptr = &options->system_hostfile2;
519 goto parse_string;
520
521 case oUserKnownHostsFile2:
522 charptr = &options->user_hostfile2;
523 goto parse_string;
524
Damien Miller95def091999-11-25 00:26:21 +1100525 case oHostName:
526 charptr = &options->hostname;
527 goto parse_string;
528
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000529 case oHostKeyAlias:
530 charptr = &options->host_key_alias;
531 goto parse_string;
532
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000533 case oPreferredAuthentications:
534 charptr = &options->preferred_authentications;
535 goto parse_string;
536
Ben Lindstrome0f88042001-04-30 13:06:24 +0000537 case oBindAddress:
538 charptr = &options->bind_address;
539 goto parse_string;
540
Ben Lindstromae996bf2001-08-06 21:27:53 +0000541 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000542 charptr = &options->smartcard_device;
543 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000544
Damien Miller95def091999-11-25 00:26:21 +1100545 case oProxyCommand:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000546 if (s == NULL)
547 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100548 charptr = &options->proxy_command;
Damien Miller61f08ac2003-02-24 11:56:27 +1100549 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100550 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100551 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100552 return 0;
553
554 case oPort:
555 intptr = &options->port;
556parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000557 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000558 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100559 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000560 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100561 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100562
563 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000564 value = strtol(arg, &endofnumber, 0);
565 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100566 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100567 if (*activep && *intptr == -1)
568 *intptr = value;
569 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller95def091999-11-25 00:26:21 +1100571 case oConnectionAttempts:
572 intptr = &options->connection_attempts;
573 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100574
Damien Miller95def091999-11-25 00:26:21 +1100575 case oCipher:
576 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000577 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000578 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000579 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000580 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100581 if (value == -1)
582 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100583 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100584 if (*activep && *intptr == -1)
585 *intptr = value;
586 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Damien Miller78928792000-04-12 20:17:38 +1000588 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000589 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000590 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000591 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000592 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000593 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000595 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000596 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000597 break;
598
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000599 case oMacs:
600 arg = strdelim(&s);
601 if (!arg || *arg == '\0')
602 fatal("%.200s line %d: Missing argument.", filename, linenum);
603 if (!mac_valid(arg))
604 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000606 if (*activep && options->macs == NULL)
607 options->macs = xstrdup(arg);
608 break;
609
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000610 case oHostKeyAlgorithms:
611 arg = strdelim(&s);
612 if (!arg || *arg == '\0')
613 fatal("%.200s line %d: Missing argument.", filename, linenum);
614 if (!key_names_valid2(arg))
615 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100616 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000617 if (*activep && options->hostkeyalgorithms == NULL)
618 options->hostkeyalgorithms = xstrdup(arg);
619 break;
620
Damien Miller78928792000-04-12 20:17:38 +1000621 case oProtocol:
622 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000623 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000624 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000625 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000626 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000627 if (value == SSH_PROTO_UNKNOWN)
628 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100629 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000630 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
631 *intptr = value;
632 break;
633
Damien Miller95def091999-11-25 00:26:21 +1100634 case oLogLevel:
635 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000636 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000637 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100638 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000639 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100640 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100641 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100642 *intptr = (LogLevel) value;
643 break;
644
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000645 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100646 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000647 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000648 if (!arg || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000649 fatal("%.200s line %d: Missing port argument.",
650 filename, linenum);
651 if ((fwd_port = a2port(arg)) == 0)
652 fatal("%.200s line %d: Bad listen port.",
653 filename, linenum);
Damien Millerbe484b52000-07-15 14:14:16 +1000654 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000655 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100656 fatal("%.200s line %d: Missing second argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000657 filename, linenum);
658 if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
659 sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
660 fatal("%.200s line %d: Bad forwarding specification.",
661 filename, linenum);
662 if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
663 fatal("%.200s line %d: Bad forwarding port.",
664 filename, linenum);
665 if (*activep) {
666 if (opcode == oLocalForward)
667 add_local_forward(options, fwd_port, buf,
668 fwd_host_port);
669 else if (opcode == oRemoteForward)
670 add_remote_forward(options, fwd_port, buf,
671 fwd_host_port);
672 }
Damien Miller95def091999-11-25 00:26:21 +1100673 break;
674
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000675 case oDynamicForward:
676 arg = strdelim(&s);
677 if (!arg || *arg == '\0')
678 fatal("%.200s line %d: Missing port argument.",
679 filename, linenum);
Ben Lindstrom19066a12001-04-12 23:39:26 +0000680 fwd_port = a2port(arg);
681 if (fwd_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000682 fatal("%.200s line %d: Badly formatted port number.",
683 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000684 if (*activep)
Darren Tucker1c52ee32003-08-13 20:38:36 +1000685 add_local_forward(options, fwd_port, "socks", 0);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000686 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000687
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000688 case oClearAllForwardings:
689 intptr = &options->clear_forwardings;
690 goto parse_flag;
691
Damien Miller95def091999-11-25 00:26:21 +1100692 case oHost:
693 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000694 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000695 if (match_pattern(host, arg)) {
696 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100697 *activep = 1;
698 break;
699 }
Damien Millerbe484b52000-07-15 14:14:16 +1000700 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100701 return 0;
702
703 case oEscapeChar:
704 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000705 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000706 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100707 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000708 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000709 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
710 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000711 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000712 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000713 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000714 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100715 else {
716 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100718 /* NOTREACHED */
719 value = 0; /* Avoid compiler warning. */
720 }
721 if (*activep && *intptr == -1)
722 *intptr = value;
723 break;
724
Damien Miller20a8f972003-05-18 20:50:30 +1000725 case oAddressFamily:
726 arg = strdelim(&s);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000727 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000728 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000729 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000730 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000731 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000732 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000733 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000734 else
735 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000736 if (*activep && *intptr == -1)
737 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000738 break;
739
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000740 case oEnableSSHKeysign:
741 intptr = &options->enable_ssh_keysign;
742 goto parse_flag;
743
Damien Millerbd394c32004-03-08 23:12:36 +1100744 case oIdentitiesOnly:
745 intptr = &options->identities_only;
746 goto parse_flag;
747
Damien Miller509b0102003-12-17 16:33:10 +1100748 case oServerAliveInterval:
749 intptr = &options->server_alive_interval;
750 goto parse_time;
751
752 case oServerAliveCountMax:
753 intptr = &options->server_alive_count_max;
754 goto parse_int;
755
Darren Tucker46bc0752004-05-02 22:11:30 +1000756 case oSendEnv:
757 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
758 if (strchr(arg, '=') != NULL)
759 fatal("%s line %d: Invalid environment name.",
760 filename, linenum);
761 if (options->num_send_env >= MAX_SEND_ENV)
762 fatal("%s line %d: too many send env.",
763 filename, linenum);
764 options->send_env[options->num_send_env++] =
765 xstrdup(arg);
766 }
767 break;
768
Damien Miller0e220db2004-06-15 10:34:08 +1000769 case oControlPath:
770 charptr = &options->control_path;
771 goto parse_string;
772
773 case oControlMaster:
774 intptr = &options->control_master;
775 goto parse_flag;
776
Ben Lindstrom4daea862002-06-09 20:04:02 +0000777 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000778 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000779 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000780 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000781
Damien Millerf9b3feb2003-05-16 11:38:32 +1000782 case oUnsupported:
783 error("%s line %d: Unsupported option \"%s\"",
784 filename, linenum, keyword);
785 return 0;
786
Damien Miller95def091999-11-25 00:26:21 +1100787 default:
788 fatal("process_config_line: Unimplemented opcode %d", opcode);
789 }
790
791 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000792 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000793 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100794 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000795 }
Damien Miller95def091999-11-25 00:26:21 +1100796 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797}
798
799
Damien Miller5428f641999-11-25 11:54:57 +1100800/*
801 * Reads the config file and modifies the options accordingly. Options
802 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000803 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100804 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000806int
Damien Miller57a44762004-04-20 20:11:57 +1000807read_config_file(const char *filename, const char *host, Options *options,
808 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809{
Damien Miller95def091999-11-25 00:26:21 +1100810 FILE *f;
811 char line[1024];
812 int active, linenum;
813 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000816 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000817 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller57a44762004-04-20 20:11:57 +1000819 if (checkperm) {
820 struct stat sb;
821
Damien Miller33793852004-06-15 10:27:55 +1000822 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000823 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000824 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000825 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000826 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000827 }
828
Damien Miller95def091999-11-25 00:26:21 +1100829 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830
Damien Miller5428f641999-11-25 11:54:57 +1100831 /*
832 * Mark that we are now processing the options. This flag is turned
833 * on/off by Host specifications.
834 */
Damien Miller95def091999-11-25 00:26:21 +1100835 active = 1;
836 linenum = 0;
837 while (fgets(line, sizeof(line), f)) {
838 /* Update line number counter. */
839 linenum++;
840 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
841 bad_options++;
842 }
843 fclose(f);
844 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000845 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100846 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000847 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848}
849
Damien Miller5428f641999-11-25 11:54:57 +1100850/*
851 * Initializes options to special values that indicate that they have not yet
852 * been set. Read_config_file will only set options with this value. Options
853 * are processed in the following order: command line, user config file,
854 * system config file. Last, fill_default_options is called.
855 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller4af51302000-04-16 11:18:38 +1000857void
Damien Miller95def091999-11-25 00:26:21 +1100858initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859{
Damien Miller95def091999-11-25 00:26:21 +1100860 memset(options, 'X', sizeof(*options));
861 options->forward_agent = -1;
862 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000863 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000864 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100865 options->gateway_ports = -1;
866 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100867 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100868 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000869 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000870 options->gss_authentication = -1;
871 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100872 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100873 options->kbd_interactive_authentication = -1;
874 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100875 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000876 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100877 options->batch_mode = -1;
878 options->check_host_ip = -1;
879 options->strict_host_key_checking = -1;
880 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100881 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +1100882 options->compression_level = -1;
883 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000884 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +1100885 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000886 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +1100887 options->number_of_password_prompts = -1;
888 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000889 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000890 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000891 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000892 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100893 options->num_identity_files = 0;
894 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000895 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100896 options->proxy_command = NULL;
897 options->user = NULL;
898 options->escape_char = -1;
899 options->system_hostfile = NULL;
900 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000901 options->system_hostfile2 = NULL;
902 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100903 options->num_local_forwards = 0;
904 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000905 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100906 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000907 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000908 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000909 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000910 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000911 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +1100912 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +1000913 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +1000914 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +1100915 options->server_alive_interval = -1;
916 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +1000917 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000918 options->control_path = NULL;
919 options->control_master = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920}
921
Damien Miller5428f641999-11-25 11:54:57 +1100922/*
923 * Called after processing other sources of option data, this fills those
924 * options for which no value has been specified with their default values.
925 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller4af51302000-04-16 11:18:38 +1000927void
Damien Miller95def091999-11-25 00:26:21 +1100928fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000930 int len;
931
Damien Miller95def091999-11-25 00:26:21 +1100932 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000933 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100934 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100935 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +1000936 if (options->forward_x11_trusted == -1)
937 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000938 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000939 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100940 if (options->gateway_ports == -1)
941 options->gateway_ports = 0;
942 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +0000943 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +1100944 if (options->rsa_authentication == -1)
945 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946 if (options->pubkey_authentication == -1)
947 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000948 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +0000949 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000950 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +1000951 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000952 if (options->gss_deleg_creds == -1)
953 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100954 if (options->password_authentication == -1)
955 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100956 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000957 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100958 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000959 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000960 if (options->hostbased_authentication == -1)
961 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100962 if (options->batch_mode == -1)
963 options->batch_mode = 0;
964 if (options->check_host_ip == -1)
965 options->check_host_ip = 1;
966 if (options->strict_host_key_checking == -1)
967 options->strict_host_key_checking = 2; /* 2 is default */
968 if (options->compression == -1)
969 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +1100970 if (options->tcp_keep_alive == -1)
971 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +1100972 if (options->compression_level == -1)
973 options->compression_level = 6;
974 if (options->port == -1)
975 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000976 if (options->address_family == -1)
977 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +1100978 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000979 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100980 if (options->number_of_password_prompts == -1)
981 options->number_of_password_prompts = 3;
982 /* Selected in ssh_login(). */
983 if (options->cipher == -1)
984 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000985 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000986 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000987 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000988 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +0000989 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +1100990 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100991 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000992 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100993 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000994 xmalloc(len);
995 snprintf(options->identity_files[options->num_identity_files++],
996 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100997 }
998 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +0000999 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1000 options->identity_files[options->num_identity_files] =
1001 xmalloc(len);
1002 snprintf(options->identity_files[options->num_identity_files++],
1003 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1004
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001005 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001006 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001007 xmalloc(len);
1008 snprintf(options->identity_files[options->num_identity_files++],
1009 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001010 }
Damien Millereba71ba2000-04-29 23:57:08 +10001011 }
Damien Miller95def091999-11-25 00:26:21 +11001012 if (options->escape_char == -1)
1013 options->escape_char = '~';
1014 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001015 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001016 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001017 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001018 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001019 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001020 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001021 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001022 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001023 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001024 if (options->clear_forwardings == 1)
1025 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001026 if (options->no_host_authentication_for_localhost == - 1)
1027 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001028 if (options->identities_only == -1)
1029 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001030 if (options->enable_ssh_keysign == -1)
1031 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001032 if (options->rekey_limit == -1)
1033 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001034 if (options->verify_host_key_dns == -1)
1035 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001036 if (options->server_alive_interval == -1)
1037 options->server_alive_interval = 0;
1038 if (options->server_alive_count_max == -1)
1039 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001040 if (options->control_master == -1)
1041 options->control_master = 0;
Damien Miller95def091999-11-25 00:26:21 +11001042 /* options->proxy_command should not be set by default */
1043 /* options->user will be set in the main program if appropriate */
1044 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001045 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001046 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047}