blob: 6f5ac8ca65f288f49f4ee02607fb9d4ea61aa460 [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"
Ben Lindstrom46c16222000-12-22 01:43:59 +000015RCSID("$OpenBSD: readconf.c,v 1.51 2000/12/19 23:17:57 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018#include "readconf.h"
Damien Millerb38eff82000-04-01 11:09:21 +100019#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100021#include "compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
23/* Format of the configuration file:
24
25 # Configuration data is parsed as follows:
26 # 1. command line options
27 # 2. user-specific file
28 # 3. system-wide file
29 # Any configuration value is only changed the first time it is set.
30 # Thus, host-specific definitions should be at the beginning of the
31 # configuration file, and defaults at the end.
32
33 # Host-specific declarations. These may override anything above. A single
34 # host may match multiple declarations; these are processed in the order
35 # that they are given in.
36
37 Host *.ngs.fi ngs.fi
38 FallBackToRsh no
39
40 Host fake.com
41 HostName another.host.name.real.org
42 User blaah
43 Port 34289
44 ForwardX11 no
45 ForwardAgent no
46
47 Host books.com
48 RemoteForward 9999 shadows.cs.hut.fi:9999
49 Cipher 3des
50
51 Host fascist.blob.com
52 Port 23123
53 User tylonen
54 RhostsAuthentication no
55 PasswordAuthentication no
56
57 Host puukko.hut.fi
58 User t35124p
59 ProxyCommand ssh-proxy %h %p
60
61 Host *.fr
62 UseRsh yes
63
64 Host *.su
65 Cipher none
66 PasswordAuthentication no
67
68 # Defaults for various options
69 Host *
70 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110071 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072 RhostsAuthentication yes
73 PasswordAuthentication yes
74 RSAAuthentication yes
75 RhostsRSAAuthentication yes
76 FallBackToRsh no
77 UseRsh no
78 StrictHostKeyChecking yes
79 KeepAlives no
80 IdentityFile ~/.ssh/identity
81 Port 22
82 EscapeChar ~
83
84*/
85
86/* Keyword tokens. */
87
Damien Miller95def091999-11-25 00:26:21 +110088typedef enum {
89 oBadOption,
90 oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
91 oPasswordAuthentication, oRSAAuthentication, oFallBackToRsh, oUseRsh,
Damien Millerd3a18572000-06-07 19:55:44 +100092 oSkeyAuthentication, oXAuthLocation,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +110094 oKerberosAuthentication,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095#endif /* KRB4 */
96#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +110097 oKerberosTgtPassing, oAFSTokenPassing,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098#endif
Damien Miller95def091999-11-25 00:26:21 +110099 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
100 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
101 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
102 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
103 oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts, oTISAuthentication,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol,
105 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Damien Miller874d77b2000-10-14 16:23:11 +1100106 oKbdInteractiveAuthentication, oKbdInteractiveDevices
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} OpCodes;
108
109/* Textual representations of the tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111static struct {
112 const char *name;
113 OpCodes opcode;
114} keywords[] = {
115 { "forwardagent", oForwardAgent },
116 { "forwardx11", oForwardX11 },
Damien Millerd3a18572000-06-07 19:55:44 +1000117 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100118 { "gatewayports", oGatewayPorts },
119 { "useprivilegedport", oUsePrivilegedPort },
120 { "rhostsauthentication", oRhostsAuthentication },
121 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100122 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
123 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100124 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 { "pubkeyauthentication", oPubkeyAuthentication },
126 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100127 { "skeyauthentication", oSkeyAuthentication },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100129 { "kerberosauthentication", oKerberosAuthentication },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130#endif /* KRB4 */
131#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100132 { "kerberostgtpassing", oKerberosTgtPassing },
133 { "afstokenpassing", oAFSTokenPassing },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134#endif
Damien Miller95def091999-11-25 00:26:21 +1100135 { "fallbacktorsh", oFallBackToRsh },
136 { "usersh", oUseRsh },
137 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138 { "identityfile2", oIdentityFile }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100139 { "hostname", oHostName },
140 { "proxycommand", oProxyCommand },
141 { "port", oPort },
142 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000143 { "ciphers", oCiphers },
144 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100145 { "remoteforward", oRemoteForward },
146 { "localforward", oLocalForward },
147 { "user", oUser },
148 { "host", oHost },
149 { "escapechar", oEscapeChar },
150 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
151 { "globalknownhostsfile", oGlobalKnownHostsFile },
152 { "userknownhostsfile", oUserKnownHostsFile },
Damien Millereba71ba2000-04-29 23:57:08 +1000153 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
154 { "userknownhostsfile2", oUserKnownHostsFile2 },
Damien Miller95def091999-11-25 00:26:21 +1100155 { "connectionattempts", oConnectionAttempts },
156 { "batchmode", oBatchMode },
157 { "checkhostip", oCheckHostIP },
158 { "stricthostkeychecking", oStrictHostKeyChecking },
159 { "compression", oCompression },
160 { "compressionlevel", oCompressionLevel },
161 { "keepalive", oKeepAlives },
162 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
163 { "tisauthentication", oTISAuthentication },
164 { "loglevel", oLogLevel },
165 { NULL, 0 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100166};
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Adds a local TCP/IP port forward to options. Never returns if there is an
170 * error.
171 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller4af51302000-04-16 11:18:38 +1000173void
Damien Milleraae6c611999-12-06 11:47:28 +1100174add_local_forward(Options *options, u_short port, const char *host,
175 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
Damien Miller95def091999-11-25 00:26:21 +1100177 Forward *fwd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100178#ifndef HAVE_CYGWIN
Damien Miller95def091999-11-25 00:26:21 +1100179 extern uid_t original_real_uid;
Damien Miller95def091999-11-25 00:26:21 +1100180 if (port < IPPORT_RESERVED && original_real_uid != 0)
181 fatal("Privileged ports can only be forwarded by root.\n");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100182#endif
Damien Miller95def091999-11-25 00:26:21 +1100183 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
184 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
185 fwd = &options->local_forwards[options->num_local_forwards++];
186 fwd->port = port;
187 fwd->host = xstrdup(host);
188 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
Damien Miller5428f641999-11-25 11:54:57 +1100191/*
192 * Adds a remote TCP/IP port forward to options. Never returns if there is
193 * an error.
194 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Miller4af51302000-04-16 11:18:38 +1000196void
Damien Milleraae6c611999-12-06 11:47:28 +1100197add_remote_forward(Options *options, u_short port, const char *host,
198 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 Forward *fwd;
201 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
202 fatal("Too many remote forwards (max %d).",
203 SSH_MAX_FORWARDS_PER_DIRECTION);
204 fwd = &options->remote_forwards[options->num_remote_forwards++];
205 fwd->port = port;
206 fwd->host = xstrdup(host);
207 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller5428f641999-11-25 11:54:57 +1100210/*
211 * Returns the number of the token pointed to by cp of length len. Never
212 * returns if the token is not known.
213 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller4af51302000-04-16 11:18:38 +1000215static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100216parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000218 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller95def091999-11-25 00:26:21 +1100220 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100221 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100222 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller95def091999-11-25 00:26:21 +1100224 fprintf(stderr, "%s: line %d: Bad configuration option: %s\n",
225 filename, linenum, cp);
226 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller5428f641999-11-25 11:54:57 +1100229/*
230 * Processes a single option line as used in the configuration files. This
231 * only sets those values that have not already been set.
232 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller2ccf6611999-11-15 15:25:10 +1100234int
235process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100236 char *line, const char *filename, int linenum,
237 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Damien Miller37023962000-07-11 17:31:38 +1000239 char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100240 int opcode, *intptr, value;
241 u_short fwd_port, fwd_host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Millerbe484b52000-07-15 14:14:16 +1000243 s = line;
244 /* Get the keyword. (Each line is supposed to begin with a keyword). */
245 keyword = strdelim(&s);
246 /* Ignore leading whitespace. */
247 if (*keyword == '\0')
248 keyword = strdelim(&s);
249 if (!*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100250 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller37023962000-07-11 17:31:38 +1000252 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 switch (opcode) {
255 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100256 /* don't panic, but count bad options */
257 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100258 /* NOTREACHED */
259 case oForwardAgent:
260 intptr = &options->forward_agent;
261parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000262 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000263 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100264 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
265 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000266 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100267 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000268 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100269 value = 0;
270 else
271 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
272 if (*activep && *intptr == -1)
273 *intptr = value;
274 break;
275
276 case oForwardX11:
277 intptr = &options->forward_x11;
278 goto parse_flag;
279
280 case oGatewayPorts:
281 intptr = &options->gateway_ports;
282 goto parse_flag;
283
284 case oUsePrivilegedPort:
285 intptr = &options->use_privileged_port;
286 goto parse_flag;
287
288 case oRhostsAuthentication:
289 intptr = &options->rhosts_authentication;
290 goto parse_flag;
291
292 case oPasswordAuthentication:
293 intptr = &options->password_authentication;
294 goto parse_flag;
295
Damien Miller874d77b2000-10-14 16:23:11 +1100296 case oKbdInteractiveAuthentication:
297 intptr = &options->kbd_interactive_authentication;
298 goto parse_flag;
299
300 case oKbdInteractiveDevices:
301 charptr = &options->kbd_interactive_devices;
302 goto parse_string;
303
Damien Miller0bc1bd82000-11-13 22:57:25 +1100304 case oPubkeyAuthentication:
305 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000306 goto parse_flag;
307
Damien Miller95def091999-11-25 00:26:21 +1100308 case oRSAAuthentication:
309 intptr = &options->rsa_authentication;
310 goto parse_flag;
311
312 case oRhostsRSAAuthentication:
313 intptr = &options->rhosts_rsa_authentication;
314 goto parse_flag;
315
316 case oTISAuthentication:
317 /* fallthrough, there is no difference on the client side */
318 case oSkeyAuthentication:
319 intptr = &options->skey_authentication;
320 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
322#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100323 case oKerberosAuthentication:
324 intptr = &options->kerberos_authentication;
325 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326#endif /* KRB4 */
327
328#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100329 case oKerberosTgtPassing:
330 intptr = &options->kerberos_tgt_passing;
331 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 case oAFSTokenPassing:
334 intptr = &options->afs_token_passing;
335 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Damien Miller95def091999-11-25 00:26:21 +1100338 case oFallBackToRsh:
339 intptr = &options->fallback_to_rsh;
340 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Miller95def091999-11-25 00:26:21 +1100342 case oUseRsh:
343 intptr = &options->use_rsh;
344 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller95def091999-11-25 00:26:21 +1100346 case oBatchMode:
347 intptr = &options->batch_mode;
348 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 case oCheckHostIP:
351 intptr = &options->check_host_ip;
352 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 case oStrictHostKeyChecking:
355 intptr = &options->strict_host_key_checking;
Damien Millerbe484b52000-07-15 14:14:16 +1000356 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000357 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100358 fatal("%.200s line %d: Missing yes/no argument.",
359 filename, linenum);
360 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000361 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100362 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000363 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100364 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000365 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100366 value = 2;
367 else
368 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
369 if (*activep && *intptr == -1)
370 *intptr = value;
371 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller95def091999-11-25 00:26:21 +1100373 case oCompression:
374 intptr = &options->compression;
375 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376
Damien Miller95def091999-11-25 00:26:21 +1100377 case oKeepAlives:
378 intptr = &options->keepalives;
379 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
Damien Miller95def091999-11-25 00:26:21 +1100381 case oNumberOfPasswordPrompts:
382 intptr = &options->number_of_password_prompts;
383 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller95def091999-11-25 00:26:21 +1100385 case oCompressionLevel:
386 intptr = &options->compression_level;
387 goto parse_int;
388
389 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000390 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000391 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100392 fatal("%.200s line %d: Missing argument.", filename, linenum);
393 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100394 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000395 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100396 fatal("%.200s line %d: Too many identity files specified (max %d).",
397 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100398 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000399 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000400 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
402 break;
403
Damien Millerd3a18572000-06-07 19:55:44 +1000404 case oXAuthLocation:
405 charptr=&options->xauth_location;
406 goto parse_string;
407
Damien Miller95def091999-11-25 00:26:21 +1100408 case oUser:
409 charptr = &options->user;
410parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000411 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000412 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100413 fatal("%.200s line %d: Missing argument.", filename, linenum);
414 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000415 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100416 break;
417
418 case oGlobalKnownHostsFile:
419 charptr = &options->system_hostfile;
420 goto parse_string;
421
422 case oUserKnownHostsFile:
423 charptr = &options->user_hostfile;
424 goto parse_string;
425
Damien Millereba71ba2000-04-29 23:57:08 +1000426 case oGlobalKnownHostsFile2:
427 charptr = &options->system_hostfile2;
428 goto parse_string;
429
430 case oUserKnownHostsFile2:
431 charptr = &options->user_hostfile2;
432 goto parse_string;
433
Damien Miller95def091999-11-25 00:26:21 +1100434 case oHostName:
435 charptr = &options->hostname;
436 goto parse_string;
437
438 case oProxyCommand:
439 charptr = &options->proxy_command;
440 string = xstrdup("");
Damien Millerbe484b52000-07-15 14:14:16 +1000441 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000442 string = xrealloc(string, strlen(string) + strlen(arg) + 2);
Damien Miller95def091999-11-25 00:26:21 +1100443 strcat(string, " ");
Damien Miller37023962000-07-11 17:31:38 +1000444 strcat(string, arg);
Damien Miller95def091999-11-25 00:26:21 +1100445 }
446 if (*activep && *charptr == NULL)
447 *charptr = string;
448 else
449 xfree(string);
450 return 0;
451
452 case oPort:
453 intptr = &options->port;
454parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000455 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000456 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100457 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000458 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100459 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100460
461 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000462 value = strtol(arg, &endofnumber, 0);
463 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100464 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100465 if (*activep && *intptr == -1)
466 *intptr = value;
467 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 case oConnectionAttempts:
470 intptr = &options->connection_attempts;
471 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100472
Damien Miller95def091999-11-25 00:26:21 +1100473 case oCipher:
474 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000475 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000476 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000477 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000478 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100479 if (value == -1)
480 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000481 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100482 if (*activep && *intptr == -1)
483 *intptr = value;
484 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485
Damien Miller78928792000-04-12 20:17:38 +1000486 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000487 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000488 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000489 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000490 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000491 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000492 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000493 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000494 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000495 break;
496
497 case oProtocol:
498 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000499 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000500 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000501 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000502 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000503 if (value == SSH_PROTO_UNKNOWN)
504 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000505 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000506 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
507 *intptr = value;
508 break;
509
Damien Miller95def091999-11-25 00:26:21 +1100510 case oLogLevel:
511 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000512 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000513 value = log_level_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100514 if (value == (LogLevel) - 1)
515 fatal("%.200s line %d: unsupported log level '%s'\n",
Damien Miller37023962000-07-11 17:31:38 +1000516 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100517 if (*activep && (LogLevel) * intptr == -1)
518 *intptr = (LogLevel) value;
519 break;
520
521 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000522 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000523 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100524 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000525 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100526 fatal("%.200s line %d: Badly formatted port number.",
527 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000528 fwd_port = atoi(arg);
Damien Millerbe484b52000-07-15 14:14:16 +1000529 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000530 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100531 fatal("%.200s line %d: Missing second argument.",
532 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000533 if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
Damien Miller95def091999-11-25 00:26:21 +1100534 fatal("%.200s line %d: Badly formatted host:port.",
535 filename, linenum);
536 if (*activep)
537 add_remote_forward(options, fwd_port, buf, fwd_host_port);
538 break;
539
540 case oLocalForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000541 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000542 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100543 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000544 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100545 fatal("%.200s line %d: Badly formatted port number.",
546 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000547 fwd_port = atoi(arg);
Damien Millerbe484b52000-07-15 14:14:16 +1000548 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000549 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100550 fatal("%.200s line %d: Missing second argument.",
551 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000552 if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
Damien Miller95def091999-11-25 00:26:21 +1100553 fatal("%.200s line %d: Badly formatted host:port.",
554 filename, linenum);
555 if (*activep)
556 add_local_forward(options, fwd_port, buf, fwd_host_port);
557 break;
558
559 case oHost:
560 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000561 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000562 if (match_pattern(host, arg)) {
563 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100564 *activep = 1;
565 break;
566 }
Damien Millerbe484b52000-07-15 14:14:16 +1000567 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100568 return 0;
569
570 case oEscapeChar:
571 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000572 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000573 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100574 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000575 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000576 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
577 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000578 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000579 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000580 else if (strcmp(arg, "none") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100581 value = -2;
582 else {
583 fatal("%.200s line %d: Bad escape character.",
584 filename, linenum);
585 /* NOTREACHED */
586 value = 0; /* Avoid compiler warning. */
587 }
588 if (*activep && *intptr == -1)
589 *intptr = value;
590 break;
591
592 default:
593 fatal("process_config_line: Unimplemented opcode %d", opcode);
594 }
595
596 /* Check that there is no garbage at end of line. */
Damien Millerbe484b52000-07-15 14:14:16 +1000597 if ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000598 {
599 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
600 filename, linenum, arg);
601 }
Damien Miller95def091999-11-25 00:26:21 +1100602 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603}
604
605
Damien Miller5428f641999-11-25 11:54:57 +1100606/*
607 * Reads the config file and modifies the options accordingly. Options
608 * should already be initialized before this call. This never returns if
609 * there is an error. If the file does not exist, this returns immediately.
610 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller4af51302000-04-16 11:18:38 +1000612void
Damien Miller95def091999-11-25 00:26:21 +1100613read_config_file(const char *filename, const char *host, Options *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Damien Miller95def091999-11-25 00:26:21 +1100615 FILE *f;
616 char line[1024];
617 int active, linenum;
618 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Damien Miller95def091999-11-25 00:26:21 +1100620 /* Open the file. */
621 f = fopen(filename, "r");
622 if (!f)
623 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Damien Miller95def091999-11-25 00:26:21 +1100625 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * Mark that we are now processing the options. This flag is turned
629 * on/off by Host specifications.
630 */
Damien Miller95def091999-11-25 00:26:21 +1100631 active = 1;
632 linenum = 0;
633 while (fgets(line, sizeof(line), f)) {
634 /* Update line number counter. */
635 linenum++;
636 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
637 bad_options++;
638 }
639 fclose(f);
640 if (bad_options > 0)
641 fatal("%s: terminating, %d bad configuration options\n",
642 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643}
644
Damien Miller5428f641999-11-25 11:54:57 +1100645/*
646 * Initializes options to special values that indicate that they have not yet
647 * been set. Read_config_file will only set options with this value. Options
648 * are processed in the following order: command line, user config file,
649 * system config file. Last, fill_default_options is called.
650 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651
Damien Miller4af51302000-04-16 11:18:38 +1000652void
Damien Miller95def091999-11-25 00:26:21 +1100653initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654{
Damien Miller95def091999-11-25 00:26:21 +1100655 memset(options, 'X', sizeof(*options));
656 options->forward_agent = -1;
657 options->forward_x11 = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000658 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100659 options->gateway_ports = -1;
660 options->use_privileged_port = -1;
661 options->rhosts_authentication = -1;
662 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100663 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100664 options->skey_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100666 options->kerberos_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667#endif
668#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100669 options->kerberos_tgt_passing = -1;
670 options->afs_token_passing = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671#endif
Damien Miller95def091999-11-25 00:26:21 +1100672 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100673 options->kbd_interactive_authentication = -1;
674 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100675 options->rhosts_rsa_authentication = -1;
676 options->fallback_to_rsh = -1;
677 options->use_rsh = -1;
678 options->batch_mode = -1;
679 options->check_host_ip = -1;
680 options->strict_host_key_checking = -1;
681 options->compression = -1;
682 options->keepalives = -1;
683 options->compression_level = -1;
684 options->port = -1;
685 options->connection_attempts = -1;
686 options->number_of_password_prompts = -1;
687 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000688 options->ciphers = NULL;
689 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100690 options->num_identity_files = 0;
691 options->hostname = NULL;
692 options->proxy_command = NULL;
693 options->user = NULL;
694 options->escape_char = -1;
695 options->system_hostfile = NULL;
696 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000697 options->system_hostfile2 = NULL;
698 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100699 options->num_local_forwards = 0;
700 options->num_remote_forwards = 0;
701 options->log_level = (LogLevel) - 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702}
703
Damien Miller5428f641999-11-25 11:54:57 +1100704/*
705 * Called after processing other sources of option data, this fills those
706 * options for which no value has been specified with their default values.
707 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Damien Miller4af51302000-04-16 11:18:38 +1000709void
Damien Miller95def091999-11-25 00:26:21 +1100710fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711{
Damien Miller95def091999-11-25 00:26:21 +1100712 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000713 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100714 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100715 options->forward_x11 = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000716#ifdef XAUTH_PATH
717 if (options->xauth_location == NULL)
718 options->xauth_location = XAUTH_PATH;
719#endif /* XAUTH_PATH */
Damien Miller95def091999-11-25 00:26:21 +1100720 if (options->gateway_ports == -1)
721 options->gateway_ports = 0;
722 if (options->use_privileged_port == -1)
723 options->use_privileged_port = 1;
724 if (options->rhosts_authentication == -1)
725 options->rhosts_authentication = 1;
726 if (options->rsa_authentication == -1)
727 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100728 if (options->pubkey_authentication == -1)
729 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100730 if (options->skey_authentication == -1)
731 options->skey_authentication = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100733 if (options->kerberos_authentication == -1)
734 options->kerberos_authentication = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735#endif /* KRB4 */
736#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100737 if (options->kerberos_tgt_passing == -1)
738 options->kerberos_tgt_passing = 1;
739 if (options->afs_token_passing == -1)
740 options->afs_token_passing = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741#endif /* AFS */
Damien Miller95def091999-11-25 00:26:21 +1100742 if (options->password_authentication == -1)
743 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100744 if (options->kbd_interactive_authentication == -1)
745 options->kbd_interactive_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100746 if (options->rhosts_rsa_authentication == -1)
747 options->rhosts_rsa_authentication = 1;
748 if (options->fallback_to_rsh == -1)
Damien Miller182ee6e2000-07-12 09:45:27 +1000749 options->fallback_to_rsh = 0;
Damien Miller95def091999-11-25 00:26:21 +1100750 if (options->use_rsh == -1)
751 options->use_rsh = 0;
752 if (options->batch_mode == -1)
753 options->batch_mode = 0;
754 if (options->check_host_ip == -1)
755 options->check_host_ip = 1;
756 if (options->strict_host_key_checking == -1)
757 options->strict_host_key_checking = 2; /* 2 is default */
758 if (options->compression == -1)
759 options->compression = 0;
760 if (options->keepalives == -1)
761 options->keepalives = 1;
762 if (options->compression_level == -1)
763 options->compression_level = 6;
764 if (options->port == -1)
765 options->port = 0; /* Filled in ssh_connect. */
766 if (options->connection_attempts == -1)
767 options->connection_attempts = 4;
768 if (options->number_of_password_prompts == -1)
769 options->number_of_password_prompts = 3;
770 /* Selected in ssh_login(). */
771 if (options->cipher == -1)
772 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000773 /* options->ciphers, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000774 if (options->protocol == SSH_PROTO_UNKNOWN)
Damien Millereba71ba2000-04-29 23:57:08 +1000775 options->protocol = SSH_PROTO_1|SSH_PROTO_2|SSH_PROTO_1_PREFERRED;
Damien Miller95def091999-11-25 00:26:21 +1100776 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100777 if (options->protocol & SSH_PROTO_1) {
778 options->identity_files[options->num_identity_files] =
779 xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
780 sprintf(options->identity_files[options->num_identity_files++],
781 "~/%.100s", SSH_CLIENT_IDENTITY);
782 }
783 if (options->protocol & SSH_PROTO_2) {
784 options->identity_files[options->num_identity_files] =
785 xmalloc(2 + strlen(SSH_CLIENT_ID_DSA) + 1);
786 sprintf(options->identity_files[options->num_identity_files++],
787 "~/%.100s", SSH_CLIENT_ID_DSA);
788 }
Damien Millereba71ba2000-04-29 23:57:08 +1000789 }
Damien Miller95def091999-11-25 00:26:21 +1100790 if (options->escape_char == -1)
791 options->escape_char = '~';
792 if (options->system_hostfile == NULL)
793 options->system_hostfile = SSH_SYSTEM_HOSTFILE;
794 if (options->user_hostfile == NULL)
795 options->user_hostfile = SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +1000796 if (options->system_hostfile2 == NULL)
797 options->system_hostfile2 = SSH_SYSTEM_HOSTFILE2;
798 if (options->user_hostfile2 == NULL)
799 options->user_hostfile2 = SSH_USER_HOSTFILE2;
Damien Miller95def091999-11-25 00:26:21 +1100800 if (options->log_level == (LogLevel) - 1)
801 options->log_level = SYSLOG_LEVEL_INFO;
802 /* options->proxy_command should not be set by default */
803 /* options->user will be set in the main program if appropriate */
804 /* options->hostname will be set in the main program if appropriate */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805}