blob: 5b552815ff8dbc0c109ce4a9efcc5a67bfe0cee3 [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 Lindstroma383baa2001-01-08 06:13:41 +000015RCSID("$OpenBSD: readconf.c,v 1.53 2001/01/07 11:28:05 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,
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000106 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias
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 },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000140 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100141 { "proxycommand", oProxyCommand },
142 { "port", oPort },
143 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000144 { "ciphers", oCiphers },
145 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100146 { "remoteforward", oRemoteForward },
147 { "localforward", oLocalForward },
148 { "user", oUser },
149 { "host", oHost },
150 { "escapechar", oEscapeChar },
151 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
152 { "globalknownhostsfile", oGlobalKnownHostsFile },
153 { "userknownhostsfile", oUserKnownHostsFile },
Damien Millereba71ba2000-04-29 23:57:08 +1000154 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
155 { "userknownhostsfile2", oUserKnownHostsFile2 },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "connectionattempts", oConnectionAttempts },
157 { "batchmode", oBatchMode },
158 { "checkhostip", oCheckHostIP },
159 { "stricthostkeychecking", oStrictHostKeyChecking },
160 { "compression", oCompression },
161 { "compressionlevel", oCompressionLevel },
162 { "keepalive", oKeepAlives },
163 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
164 { "tisauthentication", oTISAuthentication },
165 { "loglevel", oLogLevel },
166 { NULL, 0 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100167};
168
Damien Miller5428f641999-11-25 11:54:57 +1100169/*
170 * Adds a local TCP/IP port forward to options. Never returns if there is an
171 * error.
172 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Damien Miller4af51302000-04-16 11:18:38 +1000174void
Damien Milleraae6c611999-12-06 11:47:28 +1100175add_local_forward(Options *options, u_short port, const char *host,
176 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
Damien Miller95def091999-11-25 00:26:21 +1100178 Forward *fwd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100179#ifndef HAVE_CYGWIN
Damien Miller95def091999-11-25 00:26:21 +1100180 extern uid_t original_real_uid;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (port < IPPORT_RESERVED && original_real_uid != 0)
182 fatal("Privileged ports can only be forwarded by root.\n");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100183#endif
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
185 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
186 fwd = &options->local_forwards[options->num_local_forwards++];
187 fwd->port = port;
188 fwd->host = xstrdup(host);
189 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller5428f641999-11-25 11:54:57 +1100192/*
193 * Adds a remote TCP/IP port forward to options. Never returns if there is
194 * an error.
195 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Miller4af51302000-04-16 11:18:38 +1000197void
Damien Milleraae6c611999-12-06 11:47:28 +1100198add_remote_forward(Options *options, u_short port, const char *host,
199 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200{
Damien Miller95def091999-11-25 00:26:21 +1100201 Forward *fwd;
202 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
203 fatal("Too many remote forwards (max %d).",
204 SSH_MAX_FORWARDS_PER_DIRECTION);
205 fwd = &options->remote_forwards[options->num_remote_forwards++];
206 fwd->port = port;
207 fwd->host = xstrdup(host);
208 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
212 * Returns the number of the token pointed to by cp of length len. Never
213 * returns if the token is not known.
214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller4af51302000-04-16 11:18:38 +1000216static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100217parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000219 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Damien Miller95def091999-11-25 00:26:21 +1100221 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100222 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100223 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225 fprintf(stderr, "%s: line %d: Bad configuration option: %s\n",
226 filename, linenum, cp);
227 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
231 * Processes a single option line as used in the configuration files. This
232 * only sets those values that have not already been set.
233 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
Damien Miller2ccf6611999-11-15 15:25:10 +1100235int
236process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100237 char *line, const char *filename, int linenum,
238 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller37023962000-07-11 17:31:38 +1000240 char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100241 int opcode, *intptr, value;
242 u_short fwd_port, fwd_host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Millerbe484b52000-07-15 14:14:16 +1000244 s = line;
245 /* Get the keyword. (Each line is supposed to begin with a keyword). */
246 keyword = strdelim(&s);
247 /* Ignore leading whitespace. */
248 if (*keyword == '\0')
249 keyword = strdelim(&s);
250 if (!*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100251 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller37023962000-07-11 17:31:38 +1000253 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 switch (opcode) {
256 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100257 /* don't panic, but count bad options */
258 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100259 /* NOTREACHED */
260 case oForwardAgent:
261 intptr = &options->forward_agent;
262parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000263 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000264 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100265 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
266 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000267 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100268 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000269 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100270 value = 0;
271 else
272 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
273 if (*activep && *intptr == -1)
274 *intptr = value;
275 break;
276
277 case oForwardX11:
278 intptr = &options->forward_x11;
279 goto parse_flag;
280
281 case oGatewayPorts:
282 intptr = &options->gateway_ports;
283 goto parse_flag;
284
285 case oUsePrivilegedPort:
286 intptr = &options->use_privileged_port;
287 goto parse_flag;
288
289 case oRhostsAuthentication:
290 intptr = &options->rhosts_authentication;
291 goto parse_flag;
292
293 case oPasswordAuthentication:
294 intptr = &options->password_authentication;
295 goto parse_flag;
296
Damien Miller874d77b2000-10-14 16:23:11 +1100297 case oKbdInteractiveAuthentication:
298 intptr = &options->kbd_interactive_authentication;
299 goto parse_flag;
300
301 case oKbdInteractiveDevices:
302 charptr = &options->kbd_interactive_devices;
303 goto parse_string;
304
Damien Miller0bc1bd82000-11-13 22:57:25 +1100305 case oPubkeyAuthentication:
306 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000307 goto parse_flag;
308
Damien Miller95def091999-11-25 00:26:21 +1100309 case oRSAAuthentication:
310 intptr = &options->rsa_authentication;
311 goto parse_flag;
312
313 case oRhostsRSAAuthentication:
314 intptr = &options->rhosts_rsa_authentication;
315 goto parse_flag;
316
317 case oTISAuthentication:
318 /* fallthrough, there is no difference on the client side */
319 case oSkeyAuthentication:
320 intptr = &options->skey_authentication;
321 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
323#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100324 case oKerberosAuthentication:
325 intptr = &options->kerberos_authentication;
326 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327#endif /* KRB4 */
328
329#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100330 case oKerberosTgtPassing:
331 intptr = &options->kerberos_tgt_passing;
332 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Miller95def091999-11-25 00:26:21 +1100334 case oAFSTokenPassing:
335 intptr = &options->afs_token_passing;
336 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Damien Miller95def091999-11-25 00:26:21 +1100339 case oFallBackToRsh:
340 intptr = &options->fallback_to_rsh;
341 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Miller95def091999-11-25 00:26:21 +1100343 case oUseRsh:
344 intptr = &options->use_rsh;
345 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller95def091999-11-25 00:26:21 +1100347 case oBatchMode:
348 intptr = &options->batch_mode;
349 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 case oCheckHostIP:
352 intptr = &options->check_host_ip;
353 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 case oStrictHostKeyChecking:
356 intptr = &options->strict_host_key_checking;
Damien Millerbe484b52000-07-15 14:14:16 +1000357 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000358 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100359 fatal("%.200s line %d: Missing yes/no argument.",
360 filename, linenum);
361 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000362 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100363 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000364 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100365 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000366 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100367 value = 2;
368 else
369 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
370 if (*activep && *intptr == -1)
371 *intptr = value;
372 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Damien Miller95def091999-11-25 00:26:21 +1100374 case oCompression:
375 intptr = &options->compression;
376 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
Damien Miller95def091999-11-25 00:26:21 +1100378 case oKeepAlives:
379 intptr = &options->keepalives;
380 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381
Damien Miller95def091999-11-25 00:26:21 +1100382 case oNumberOfPasswordPrompts:
383 intptr = &options->number_of_password_prompts;
384 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 case oCompressionLevel:
387 intptr = &options->compression_level;
388 goto parse_int;
389
390 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000391 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000392 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100393 fatal("%.200s line %d: Missing argument.", filename, linenum);
394 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000396 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100397 fatal("%.200s line %d: Too many identity files specified (max %d).",
398 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000400 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000401 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100402 }
403 break;
404
Damien Millerd3a18572000-06-07 19:55:44 +1000405 case oXAuthLocation:
406 charptr=&options->xauth_location;
407 goto parse_string;
408
Damien Miller95def091999-11-25 00:26:21 +1100409 case oUser:
410 charptr = &options->user;
411parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000412 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000413 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100414 fatal("%.200s line %d: Missing argument.", filename, linenum);
415 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000416 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100417 break;
418
419 case oGlobalKnownHostsFile:
420 charptr = &options->system_hostfile;
421 goto parse_string;
422
423 case oUserKnownHostsFile:
424 charptr = &options->user_hostfile;
425 goto parse_string;
426
Damien Millereba71ba2000-04-29 23:57:08 +1000427 case oGlobalKnownHostsFile2:
428 charptr = &options->system_hostfile2;
429 goto parse_string;
430
431 case oUserKnownHostsFile2:
432 charptr = &options->user_hostfile2;
433 goto parse_string;
434
Damien Miller95def091999-11-25 00:26:21 +1100435 case oHostName:
436 charptr = &options->hostname;
437 goto parse_string;
438
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000439 case oHostKeyAlias:
440 charptr = &options->host_key_alias;
441 goto parse_string;
442
Damien Miller95def091999-11-25 00:26:21 +1100443 case oProxyCommand:
444 charptr = &options->proxy_command;
445 string = xstrdup("");
Damien Millerbe484b52000-07-15 14:14:16 +1000446 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000447 string = xrealloc(string, strlen(string) + strlen(arg) + 2);
Damien Miller95def091999-11-25 00:26:21 +1100448 strcat(string, " ");
Damien Miller37023962000-07-11 17:31:38 +1000449 strcat(string, arg);
Damien Miller95def091999-11-25 00:26:21 +1100450 }
451 if (*activep && *charptr == NULL)
452 *charptr = string;
453 else
454 xfree(string);
455 return 0;
456
457 case oPort:
458 intptr = &options->port;
459parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000460 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000461 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100462 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000463 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100464 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100465
466 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000467 value = strtol(arg, &endofnumber, 0);
468 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100469 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100470 if (*activep && *intptr == -1)
471 *intptr = value;
472 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Damien Miller95def091999-11-25 00:26:21 +1100474 case oConnectionAttempts:
475 intptr = &options->connection_attempts;
476 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100477
Damien Miller95def091999-11-25 00:26:21 +1100478 case oCipher:
479 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000480 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000481 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000482 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000483 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100484 if (value == -1)
485 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000486 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100487 if (*activep && *intptr == -1)
488 *intptr = value;
489 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller78928792000-04-12 20:17:38 +1000491 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000492 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000493 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000494 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000495 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000496 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000497 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000498 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000499 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000500 break;
501
502 case oProtocol:
503 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000504 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000505 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000506 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000507 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000508 if (value == SSH_PROTO_UNKNOWN)
509 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller37023962000-07-11 17:31:38 +1000510 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000511 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
512 *intptr = value;
513 break;
514
Damien Miller95def091999-11-25 00:26:21 +1100515 case oLogLevel:
516 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000517 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000518 value = log_level_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100519 if (value == (LogLevel) - 1)
520 fatal("%.200s line %d: unsupported log level '%s'\n",
Damien Miller37023962000-07-11 17:31:38 +1000521 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100522 if (*activep && (LogLevel) * intptr == -1)
523 *intptr = (LogLevel) value;
524 break;
525
526 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000527 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000528 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100529 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000530 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100531 fatal("%.200s line %d: Badly formatted port number.",
532 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000533 fwd_port = atoi(arg);
Damien Millerbe484b52000-07-15 14:14:16 +1000534 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000535 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100536 fatal("%.200s line %d: Missing second argument.",
537 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000538 if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
Damien Miller95def091999-11-25 00:26:21 +1100539 fatal("%.200s line %d: Badly formatted host:port.",
540 filename, linenum);
541 if (*activep)
542 add_remote_forward(options, fwd_port, buf, fwd_host_port);
543 break;
544
545 case oLocalForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000546 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000547 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100548 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000549 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100550 fatal("%.200s line %d: Badly formatted port number.",
551 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000552 fwd_port = atoi(arg);
Damien Millerbe484b52000-07-15 14:14:16 +1000553 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000554 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100555 fatal("%.200s line %d: Missing second argument.",
556 filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000557 if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
Damien Miller95def091999-11-25 00:26:21 +1100558 fatal("%.200s line %d: Badly formatted host:port.",
559 filename, linenum);
560 if (*activep)
561 add_local_forward(options, fwd_port, buf, fwd_host_port);
562 break;
563
564 case oHost:
565 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000566 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000567 if (match_pattern(host, arg)) {
568 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100569 *activep = 1;
570 break;
571 }
Damien Millerbe484b52000-07-15 14:14:16 +1000572 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100573 return 0;
574
575 case oEscapeChar:
576 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000577 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000578 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100579 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000580 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000581 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
582 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000583 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000584 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000585 else if (strcmp(arg, "none") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100586 value = -2;
587 else {
588 fatal("%.200s line %d: Bad escape character.",
589 filename, linenum);
590 /* NOTREACHED */
591 value = 0; /* Avoid compiler warning. */
592 }
593 if (*activep && *intptr == -1)
594 *intptr = value;
595 break;
596
597 default:
598 fatal("process_config_line: Unimplemented opcode %d", opcode);
599 }
600
601 /* Check that there is no garbage at end of line. */
Damien Millerbe484b52000-07-15 14:14:16 +1000602 if ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000603 {
604 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
605 filename, linenum, arg);
606 }
Damien Miller95def091999-11-25 00:26:21 +1100607 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608}
609
610
Damien Miller5428f641999-11-25 11:54:57 +1100611/*
612 * Reads the config file and modifies the options accordingly. Options
613 * should already be initialized before this call. This never returns if
614 * there is an error. If the file does not exist, this returns immediately.
615 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Damien Miller4af51302000-04-16 11:18:38 +1000617void
Damien Miller95def091999-11-25 00:26:21 +1100618read_config_file(const char *filename, const char *host, Options *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619{
Damien Miller95def091999-11-25 00:26:21 +1100620 FILE *f;
621 char line[1024];
622 int active, linenum;
623 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Damien Miller95def091999-11-25 00:26:21 +1100625 /* Open the file. */
626 f = fopen(filename, "r");
627 if (!f)
628 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller95def091999-11-25 00:26:21 +1100630 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller5428f641999-11-25 11:54:57 +1100632 /*
633 * Mark that we are now processing the options. This flag is turned
634 * on/off by Host specifications.
635 */
Damien Miller95def091999-11-25 00:26:21 +1100636 active = 1;
637 linenum = 0;
638 while (fgets(line, sizeof(line), f)) {
639 /* Update line number counter. */
640 linenum++;
641 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
642 bad_options++;
643 }
644 fclose(f);
645 if (bad_options > 0)
646 fatal("%s: terminating, %d bad configuration options\n",
647 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648}
649
Damien Miller5428f641999-11-25 11:54:57 +1100650/*
651 * Initializes options to special values that indicate that they have not yet
652 * been set. Read_config_file will only set options with this value. Options
653 * are processed in the following order: command line, user config file,
654 * system config file. Last, fill_default_options is called.
655 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656
Damien Miller4af51302000-04-16 11:18:38 +1000657void
Damien Miller95def091999-11-25 00:26:21 +1100658initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659{
Damien Miller95def091999-11-25 00:26:21 +1100660 memset(options, 'X', sizeof(*options));
661 options->forward_agent = -1;
662 options->forward_x11 = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000663 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100664 options->gateway_ports = -1;
665 options->use_privileged_port = -1;
666 options->rhosts_authentication = -1;
667 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100669 options->skey_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100671 options->kerberos_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672#endif
673#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100674 options->kerberos_tgt_passing = -1;
675 options->afs_token_passing = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676#endif
Damien Miller95def091999-11-25 00:26:21 +1100677 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100678 options->kbd_interactive_authentication = -1;
679 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100680 options->rhosts_rsa_authentication = -1;
681 options->fallback_to_rsh = -1;
682 options->use_rsh = -1;
683 options->batch_mode = -1;
684 options->check_host_ip = -1;
685 options->strict_host_key_checking = -1;
686 options->compression = -1;
687 options->keepalives = -1;
688 options->compression_level = -1;
689 options->port = -1;
690 options->connection_attempts = -1;
691 options->number_of_password_prompts = -1;
692 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000693 options->ciphers = NULL;
694 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100695 options->num_identity_files = 0;
696 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000697 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100698 options->proxy_command = NULL;
699 options->user = NULL;
700 options->escape_char = -1;
701 options->system_hostfile = NULL;
702 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000703 options->system_hostfile2 = NULL;
704 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100705 options->num_local_forwards = 0;
706 options->num_remote_forwards = 0;
707 options->log_level = (LogLevel) - 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708}
709
Damien Miller5428f641999-11-25 11:54:57 +1100710/*
711 * Called after processing other sources of option data, this fills those
712 * options for which no value has been specified with their default values.
713 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714
Damien Miller4af51302000-04-16 11:18:38 +1000715void
Damien Miller95def091999-11-25 00:26:21 +1100716fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717{
Damien Miller95def091999-11-25 00:26:21 +1100718 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000719 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100720 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100721 options->forward_x11 = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000722#ifdef XAUTH_PATH
723 if (options->xauth_location == NULL)
724 options->xauth_location = XAUTH_PATH;
725#endif /* XAUTH_PATH */
Damien Miller95def091999-11-25 00:26:21 +1100726 if (options->gateway_ports == -1)
727 options->gateway_ports = 0;
728 if (options->use_privileged_port == -1)
729 options->use_privileged_port = 1;
730 if (options->rhosts_authentication == -1)
731 options->rhosts_authentication = 1;
732 if (options->rsa_authentication == -1)
733 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100734 if (options->pubkey_authentication == -1)
735 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100736 if (options->skey_authentication == -1)
737 options->skey_authentication = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100739 if (options->kerberos_authentication == -1)
740 options->kerberos_authentication = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741#endif /* KRB4 */
742#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100743 if (options->kerberos_tgt_passing == -1)
744 options->kerberos_tgt_passing = 1;
745 if (options->afs_token_passing == -1)
746 options->afs_token_passing = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747#endif /* AFS */
Damien Miller95def091999-11-25 00:26:21 +1100748 if (options->password_authentication == -1)
749 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100750 if (options->kbd_interactive_authentication == -1)
751 options->kbd_interactive_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100752 if (options->rhosts_rsa_authentication == -1)
753 options->rhosts_rsa_authentication = 1;
754 if (options->fallback_to_rsh == -1)
Damien Miller182ee6e2000-07-12 09:45:27 +1000755 options->fallback_to_rsh = 0;
Damien Miller95def091999-11-25 00:26:21 +1100756 if (options->use_rsh == -1)
757 options->use_rsh = 0;
758 if (options->batch_mode == -1)
759 options->batch_mode = 0;
760 if (options->check_host_ip == -1)
761 options->check_host_ip = 1;
762 if (options->strict_host_key_checking == -1)
763 options->strict_host_key_checking = 2; /* 2 is default */
764 if (options->compression == -1)
765 options->compression = 0;
766 if (options->keepalives == -1)
767 options->keepalives = 1;
768 if (options->compression_level == -1)
769 options->compression_level = 6;
770 if (options->port == -1)
771 options->port = 0; /* Filled in ssh_connect. */
772 if (options->connection_attempts == -1)
773 options->connection_attempts = 4;
774 if (options->number_of_password_prompts == -1)
775 options->number_of_password_prompts = 3;
776 /* Selected in ssh_login(). */
777 if (options->cipher == -1)
778 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000779 /* options->ciphers, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000780 if (options->protocol == SSH_PROTO_UNKNOWN)
Damien Millereba71ba2000-04-29 23:57:08 +1000781 options->protocol = SSH_PROTO_1|SSH_PROTO_2|SSH_PROTO_1_PREFERRED;
Damien Miller95def091999-11-25 00:26:21 +1100782 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100783 if (options->protocol & SSH_PROTO_1) {
784 options->identity_files[options->num_identity_files] =
785 xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
786 sprintf(options->identity_files[options->num_identity_files++],
787 "~/%.100s", SSH_CLIENT_IDENTITY);
788 }
789 if (options->protocol & SSH_PROTO_2) {
790 options->identity_files[options->num_identity_files] =
791 xmalloc(2 + strlen(SSH_CLIENT_ID_DSA) + 1);
792 sprintf(options->identity_files[options->num_identity_files++],
793 "~/%.100s", SSH_CLIENT_ID_DSA);
794 }
Damien Millereba71ba2000-04-29 23:57:08 +1000795 }
Damien Miller95def091999-11-25 00:26:21 +1100796 if (options->escape_char == -1)
797 options->escape_char = '~';
798 if (options->system_hostfile == NULL)
799 options->system_hostfile = SSH_SYSTEM_HOSTFILE;
800 if (options->user_hostfile == NULL)
801 options->user_hostfile = SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +1000802 if (options->system_hostfile2 == NULL)
803 options->system_hostfile2 = SSH_SYSTEM_HOSTFILE2;
804 if (options->user_hostfile2 == NULL)
805 options->user_hostfile2 = SSH_USER_HOSTFILE2;
Damien Miller95def091999-11-25 00:26:21 +1100806 if (options->log_level == (LogLevel) - 1)
Ben Lindstroma383baa2001-01-08 06:13:41 +0000807 options->log_level = SYSLOG_LEVEL_NOTICE;
Damien Miller95def091999-11-25 00:26:21 +1100808 /* options->proxy_command should not be set by default */
809 /* options->user will be set in the main program if appropriate */
810 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000811 /* options->host_key_alias should not be set by default */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812}