Annotation of src/usr.bin/ssh/readconf.c, Revision 1.58
1.1 deraadt 1: /*
1.18 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Functions for reading the configuration files.
1.26 markus 6: *
1.46 deraadt 7: * 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".
1.18 deraadt 12: */
1.1 deraadt 13:
14: #include "includes.h"
1.58 ! markus 15: RCSID("$OpenBSD: readconf.c,v 1.57 2001/01/20 23:02:07 djm Exp $");
1.1 deraadt 16:
17: #include "ssh.h"
18: #include "xmalloc.h"
1.25 markus 19: #include "compat.h"
1.58 ! markus 20: #include "cipher.h"
1.55 markus 21: #include "pathnames.h"
1.58 ! markus 22: #include "log.h"
! 23: #include "readconf.h"
! 24: #include "match.h"
! 25: #include "misc.h"
1.1 deraadt 26:
27: /* Format of the configuration file:
28:
29: # Configuration data is parsed as follows:
30: # 1. command line options
31: # 2. user-specific file
32: # 3. system-wide file
33: # Any configuration value is only changed the first time it is set.
34: # Thus, host-specific definitions should be at the beginning of the
35: # configuration file, and defaults at the end.
36:
37: # Host-specific declarations. These may override anything above. A single
38: # host may match multiple declarations; these are processed in the order
39: # that they are given in.
40:
41: Host *.ngs.fi ngs.fi
42: FallBackToRsh no
43:
44: Host fake.com
45: HostName another.host.name.real.org
46: User blaah
47: Port 34289
48: ForwardX11 no
49: ForwardAgent no
50:
51: Host books.com
52: RemoteForward 9999 shadows.cs.hut.fi:9999
53: Cipher 3des
54:
55: Host fascist.blob.com
56: Port 23123
57: User tylonen
58: RhostsAuthentication no
59: PasswordAuthentication no
60:
61: Host puukko.hut.fi
62: User t35124p
63: ProxyCommand ssh-proxy %h %p
64:
65: Host *.fr
66: UseRsh yes
67:
68: Host *.su
69: Cipher none
70: PasswordAuthentication no
71:
72: # Defaults for various options
73: Host *
74: ForwardAgent no
1.50 markus 75: ForwardX11 no
1.1 deraadt 76: RhostsAuthentication yes
77: PasswordAuthentication yes
78: RSAAuthentication yes
79: RhostsRSAAuthentication yes
80: FallBackToRsh no
81: UseRsh no
82: StrictHostKeyChecking yes
83: KeepAlives no
84: IdentityFile ~/.ssh/identity
85: Port 22
86: EscapeChar ~
87:
88: */
89:
90: /* Keyword tokens. */
91:
1.17 markus 92: typedef enum {
93: oBadOption,
94: oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
95: oPasswordAuthentication, oRSAAuthentication, oFallBackToRsh, oUseRsh,
1.34 markus 96: oSkeyAuthentication, oXAuthLocation,
1.1 deraadt 97: #ifdef KRB4
1.17 markus 98: oKerberosAuthentication,
1.1 deraadt 99: #endif /* KRB4 */
100: #ifdef AFS
1.17 markus 101: oKerberosTgtPassing, oAFSTokenPassing,
1.1 deraadt 102: #endif
1.17 markus 103: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
104: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
105: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
106: oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
107: oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts, oTISAuthentication,
1.50 markus 108: oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol,
109: oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.52 markus 110: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias
1.1 deraadt 111: } OpCodes;
112:
113: /* Textual representations of the tokens. */
114:
1.17 markus 115: static struct {
116: const char *name;
117: OpCodes opcode;
118: } keywords[] = {
119: { "forwardagent", oForwardAgent },
120: { "forwardx11", oForwardX11 },
1.34 markus 121: { "xauthlocation", oXAuthLocation },
1.17 markus 122: { "gatewayports", oGatewayPorts },
123: { "useprivilegedport", oUsePrivilegedPort },
124: { "rhostsauthentication", oRhostsAuthentication },
125: { "passwordauthentication", oPasswordAuthentication },
1.48 markus 126: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
127: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17 markus 128: { "rsaauthentication", oRSAAuthentication },
1.50 markus 129: { "pubkeyauthentication", oPubkeyAuthentication },
130: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
1.17 markus 131: { "skeyauthentication", oSkeyAuthentication },
1.1 deraadt 132: #ifdef KRB4
1.17 markus 133: { "kerberosauthentication", oKerberosAuthentication },
1.1 deraadt 134: #endif /* KRB4 */
1.5 dugsong 135: #ifdef AFS
1.17 markus 136: { "kerberostgtpassing", oKerberosTgtPassing },
137: { "afstokenpassing", oAFSTokenPassing },
1.1 deraadt 138: #endif
1.17 markus 139: { "fallbacktorsh", oFallBackToRsh },
140: { "usersh", oUseRsh },
141: { "identityfile", oIdentityFile },
1.50 markus 142: { "identityfile2", oIdentityFile }, /* alias */
1.17 markus 143: { "hostname", oHostName },
1.52 markus 144: { "hostkeyalias", oHostKeyAlias },
1.17 markus 145: { "proxycommand", oProxyCommand },
146: { "port", oPort },
147: { "cipher", oCipher },
1.25 markus 148: { "ciphers", oCiphers },
149: { "protocol", oProtocol },
1.17 markus 150: { "remoteforward", oRemoteForward },
151: { "localforward", oLocalForward },
152: { "user", oUser },
153: { "host", oHost },
154: { "escapechar", oEscapeChar },
155: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
156: { "globalknownhostsfile", oGlobalKnownHostsFile },
157: { "userknownhostsfile", oUserKnownHostsFile },
1.27 markus 158: { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
159: { "userknownhostsfile2", oUserKnownHostsFile2 },
1.17 markus 160: { "connectionattempts", oConnectionAttempts },
161: { "batchmode", oBatchMode },
162: { "checkhostip", oCheckHostIP },
163: { "stricthostkeychecking", oStrictHostKeyChecking },
164: { "compression", oCompression },
165: { "compressionlevel", oCompressionLevel },
166: { "keepalive", oKeepAlives },
167: { "numberofpasswordprompts", oNumberOfPasswordPrompts },
168: { "tisauthentication", oTISAuthentication },
169: { "loglevel", oLogLevel },
170: { NULL, 0 }
1.13 markus 171: };
172:
1.19 markus 173: /*
174: * Adds a local TCP/IP port forward to options. Never returns if there is an
175: * error.
176: */
1.1 deraadt 177:
1.26 markus 178: void
1.22 markus 179: add_local_forward(Options *options, u_short port, const char *host,
180: u_short host_port)
1.1 deraadt 181: {
1.17 markus 182: Forward *fwd;
183: extern uid_t original_real_uid;
184: if (port < IPPORT_RESERVED && original_real_uid != 0)
185: fatal("Privileged ports can only be forwarded by root.\n");
186: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
187: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
188: fwd = &options->local_forwards[options->num_local_forwards++];
189: fwd->port = port;
190: fwd->host = xstrdup(host);
191: fwd->host_port = host_port;
1.1 deraadt 192: }
193:
1.19 markus 194: /*
195: * Adds a remote TCP/IP port forward to options. Never returns if there is
196: * an error.
197: */
1.1 deraadt 198:
1.26 markus 199: void
1.22 markus 200: add_remote_forward(Options *options, u_short port, const char *host,
201: u_short host_port)
1.1 deraadt 202: {
1.17 markus 203: Forward *fwd;
204: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
205: fatal("Too many remote forwards (max %d).",
206: SSH_MAX_FORWARDS_PER_DIRECTION);
207: fwd = &options->remote_forwards[options->num_remote_forwards++];
208: fwd->port = port;
209: fwd->host = xstrdup(host);
210: fwd->host_port = host_port;
1.1 deraadt 211: }
212:
1.19 markus 213: /*
214: * Returns the number of the token pointed to by cp of length len. Never
215: * returns if the token is not known.
216: */
1.1 deraadt 217:
1.26 markus 218: static OpCodes
1.17 markus 219: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 220: {
1.51 markus 221: u_int i;
1.1 deraadt 222:
1.17 markus 223: for (i = 0; keywords[i].name; i++)
1.20 markus 224: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 225: return keywords[i].opcode;
226:
227: fprintf(stderr, "%s: line %d: Bad configuration option: %s\n",
228: filename, linenum, cp);
229: return oBadOption;
1.1 deraadt 230: }
231:
1.19 markus 232: /*
233: * Processes a single option line as used in the configuration files. This
234: * only sets those values that have not already been set.
235: */
1.1 deraadt 236:
1.14 markus 237: int
238: process_config_line(Options *options, const char *host,
1.17 markus 239: char *line, const char *filename, int linenum,
240: int *activep)
1.1 deraadt 241: {
1.38 provos 242: char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 243: int opcode, *intptr, value;
244: u_short fwd_port, fwd_host_port;
1.1 deraadt 245:
1.42 provos 246: s = line;
247: /* Get the keyword. (Each line is supposed to begin with a keyword). */
248: keyword = strdelim(&s);
249: /* Ignore leading whitespace. */
250: if (*keyword == '\0')
1.43 markus 251: keyword = strdelim(&s);
1.56 deraadt 252: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 253: return 0;
254:
1.38 provos 255: opcode = parse_token(keyword, filename, linenum);
1.17 markus 256:
257: switch (opcode) {
258: case oBadOption:
1.19 markus 259: /* don't panic, but count bad options */
260: return -1;
1.17 markus 261: /* NOTREACHED */
262: case oForwardAgent:
263: intptr = &options->forward_agent;
264: parse_flag:
1.42 provos 265: arg = strdelim(&s);
1.40 ho 266: if (!arg || *arg == '\0')
1.17 markus 267: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
268: value = 0; /* To avoid compiler warning... */
1.38 provos 269: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 270: value = 1;
1.38 provos 271: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 272: value = 0;
273: else
274: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
275: if (*activep && *intptr == -1)
276: *intptr = value;
277: break;
278:
279: case oForwardX11:
280: intptr = &options->forward_x11;
281: goto parse_flag;
282:
283: case oGatewayPorts:
284: intptr = &options->gateway_ports;
285: goto parse_flag;
286:
287: case oUsePrivilegedPort:
288: intptr = &options->use_privileged_port;
289: goto parse_flag;
290:
291: case oRhostsAuthentication:
292: intptr = &options->rhosts_authentication;
293: goto parse_flag;
294:
295: case oPasswordAuthentication:
296: intptr = &options->password_authentication;
297: goto parse_flag;
298:
1.48 markus 299: case oKbdInteractiveAuthentication:
300: intptr = &options->kbd_interactive_authentication;
301: goto parse_flag;
302:
303: case oKbdInteractiveDevices:
304: charptr = &options->kbd_interactive_devices;
305: goto parse_string;
306:
1.50 markus 307: case oPubkeyAuthentication:
308: intptr = &options->pubkey_authentication;
1.30 markus 309: goto parse_flag;
310:
1.17 markus 311: case oRSAAuthentication:
312: intptr = &options->rsa_authentication;
313: goto parse_flag;
314:
315: case oRhostsRSAAuthentication:
316: intptr = &options->rhosts_rsa_authentication;
317: goto parse_flag;
318:
319: case oTISAuthentication:
320: /* fallthrough, there is no difference on the client side */
321: case oSkeyAuthentication:
322: intptr = &options->skey_authentication;
323: goto parse_flag;
1.16 markus 324:
1.1 deraadt 325: #ifdef KRB4
1.17 markus 326: case oKerberosAuthentication:
327: intptr = &options->kerberos_authentication;
328: goto parse_flag;
1.1 deraadt 329: #endif /* KRB4 */
330:
1.5 dugsong 331: #ifdef AFS
1.17 markus 332: case oKerberosTgtPassing:
333: intptr = &options->kerberos_tgt_passing;
334: goto parse_flag;
335:
336: case oAFSTokenPassing:
337: intptr = &options->afs_token_passing;
338: goto parse_flag;
1.1 deraadt 339: #endif
1.17 markus 340:
341: case oFallBackToRsh:
342: intptr = &options->fallback_to_rsh;
343: goto parse_flag;
344:
345: case oUseRsh:
346: intptr = &options->use_rsh;
347: goto parse_flag;
348:
349: case oBatchMode:
350: intptr = &options->batch_mode;
351: goto parse_flag;
352:
353: case oCheckHostIP:
354: intptr = &options->check_host_ip;
355: goto parse_flag;
356:
357: case oStrictHostKeyChecking:
358: intptr = &options->strict_host_key_checking;
1.42 provos 359: arg = strdelim(&s);
1.40 ho 360: if (!arg || *arg == '\0')
1.17 markus 361: fatal("%.200s line %d: Missing yes/no argument.",
362: filename, linenum);
363: value = 0; /* To avoid compiler warning... */
1.38 provos 364: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 365: value = 1;
1.38 provos 366: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 367: value = 0;
1.38 provos 368: else if (strcmp(arg, "ask") == 0)
1.17 markus 369: value = 2;
370: else
371: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
372: if (*activep && *intptr == -1)
373: *intptr = value;
374: break;
375:
376: case oCompression:
377: intptr = &options->compression;
378: goto parse_flag;
379:
380: case oKeepAlives:
381: intptr = &options->keepalives;
382: goto parse_flag;
383:
384: case oNumberOfPasswordPrompts:
385: intptr = &options->number_of_password_prompts;
386: goto parse_int;
387:
388: case oCompressionLevel:
389: intptr = &options->compression_level;
390: goto parse_int;
391:
392: case oIdentityFile:
1.42 provos 393: arg = strdelim(&s);
1.40 ho 394: if (!arg || *arg == '\0')
1.17 markus 395: fatal("%.200s line %d: Missing argument.", filename, linenum);
396: if (*activep) {
1.50 markus 397: intptr = &options->num_identity_files;
1.27 markus 398: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 399: fatal("%.200s line %d: Too many identity files specified (max %d).",
400: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 401: charptr = &options->identity_files[*intptr];
1.38 provos 402: *charptr = xstrdup(arg);
1.27 markus 403: *intptr = *intptr + 1;
1.17 markus 404: }
405: break;
406:
1.34 markus 407: case oXAuthLocation:
408: charptr=&options->xauth_location;
409: goto parse_string;
410:
1.17 markus 411: case oUser:
412: charptr = &options->user;
413: parse_string:
1.42 provos 414: arg = strdelim(&s);
1.40 ho 415: if (!arg || *arg == '\0')
1.17 markus 416: fatal("%.200s line %d: Missing argument.", filename, linenum);
417: if (*activep && *charptr == NULL)
1.38 provos 418: *charptr = xstrdup(arg);
1.17 markus 419: break;
420:
421: case oGlobalKnownHostsFile:
422: charptr = &options->system_hostfile;
423: goto parse_string;
424:
425: case oUserKnownHostsFile:
426: charptr = &options->user_hostfile;
427: goto parse_string;
428:
1.27 markus 429: case oGlobalKnownHostsFile2:
430: charptr = &options->system_hostfile2;
431: goto parse_string;
432:
433: case oUserKnownHostsFile2:
434: charptr = &options->user_hostfile2;
435: goto parse_string;
436:
1.17 markus 437: case oHostName:
438: charptr = &options->hostname;
439: goto parse_string;
440:
1.52 markus 441: case oHostKeyAlias:
442: charptr = &options->host_key_alias;
443: goto parse_string;
444:
1.17 markus 445: case oProxyCommand:
446: charptr = &options->proxy_command;
447: string = xstrdup("");
1.42 provos 448: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.38 provos 449: string = xrealloc(string, strlen(string) + strlen(arg) + 2);
1.17 markus 450: strcat(string, " ");
1.38 provos 451: strcat(string, arg);
1.17 markus 452: }
453: if (*activep && *charptr == NULL)
454: *charptr = string;
455: else
456: xfree(string);
457: return 0;
458:
459: case oPort:
460: intptr = &options->port;
461: parse_int:
1.42 provos 462: arg = strdelim(&s);
1.40 ho 463: if (!arg || *arg == '\0')
1.17 markus 464: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 465: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 466: fatal("%.200s line %d: Bad number.", filename, linenum);
1.21 markus 467:
468: /* Octal, decimal, or hex format? */
1.38 provos 469: value = strtol(arg, &endofnumber, 0);
470: if (arg == endofnumber)
1.21 markus 471: fatal("%.200s line %d: Bad number.", filename, linenum);
1.17 markus 472: if (*activep && *intptr == -1)
473: *intptr = value;
474: break;
475:
476: case oConnectionAttempts:
477: intptr = &options->connection_attempts;
478: goto parse_int;
479:
480: case oCipher:
481: intptr = &options->cipher;
1.42 provos 482: arg = strdelim(&s);
1.40 ho 483: if (!arg || *arg == '\0')
1.32 markus 484: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 485: value = cipher_number(arg);
1.17 markus 486: if (value == -1)
487: fatal("%.200s line %d: Bad cipher '%s'.",
1.38 provos 488: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 489: if (*activep && *intptr == -1)
490: *intptr = value;
491: break;
492:
1.25 markus 493: case oCiphers:
1.42 provos 494: arg = strdelim(&s);
1.40 ho 495: if (!arg || *arg == '\0')
1.32 markus 496: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 497: if (!ciphers_valid(arg))
1.31 markus 498: fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.38 provos 499: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 500: if (*activep && options->ciphers == NULL)
1.38 provos 501: options->ciphers = xstrdup(arg);
1.25 markus 502: break;
503:
504: case oProtocol:
505: intptr = &options->protocol;
1.42 provos 506: arg = strdelim(&s);
1.40 ho 507: if (!arg || *arg == '\0')
1.32 markus 508: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 509: value = proto_spec(arg);
1.25 markus 510: if (value == SSH_PROTO_UNKNOWN)
511: fatal("%.200s line %d: Bad protocol spec '%s'.",
1.38 provos 512: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 513: if (*activep && *intptr == SSH_PROTO_UNKNOWN)
514: *intptr = value;
515: break;
516:
1.17 markus 517: case oLogLevel:
518: intptr = (int *) &options->log_level;
1.42 provos 519: arg = strdelim(&s);
1.38 provos 520: value = log_level_number(arg);
1.17 markus 521: if (value == (LogLevel) - 1)
522: fatal("%.200s line %d: unsupported log level '%s'\n",
1.38 provos 523: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 524: if (*activep && (LogLevel) * intptr == -1)
525: *intptr = (LogLevel) value;
526: break;
527:
528: case oRemoteForward:
1.42 provos 529: arg = strdelim(&s);
1.40 ho 530: if (!arg || *arg == '\0')
1.17 markus 531: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 532: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 533: fatal("%.200s line %d: Badly formatted port number.",
534: filename, linenum);
1.38 provos 535: fwd_port = atoi(arg);
1.42 provos 536: arg = strdelim(&s);
1.40 ho 537: if (!arg || *arg == '\0')
1.17 markus 538: fatal("%.200s line %d: Missing second argument.",
539: filename, linenum);
1.38 provos 540: if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17 markus 541: fatal("%.200s line %d: Badly formatted host:port.",
542: filename, linenum);
543: if (*activep)
544: add_remote_forward(options, fwd_port, buf, fwd_host_port);
545: break;
546:
547: case oLocalForward:
1.42 provos 548: arg = strdelim(&s);
1.40 ho 549: if (!arg || *arg == '\0')
1.17 markus 550: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 551: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 552: fatal("%.200s line %d: Badly formatted port number.",
553: filename, linenum);
1.38 provos 554: fwd_port = atoi(arg);
1.42 provos 555: arg = strdelim(&s);
1.40 ho 556: if (!arg || *arg == '\0')
1.17 markus 557: fatal("%.200s line %d: Missing second argument.",
558: filename, linenum);
1.38 provos 559: if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17 markus 560: fatal("%.200s line %d: Badly formatted host:port.",
561: filename, linenum);
562: if (*activep)
563: add_local_forward(options, fwd_port, buf, fwd_host_port);
564: break;
565:
566: case oHost:
567: *activep = 0;
1.42 provos 568: while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38 provos 569: if (match_pattern(host, arg)) {
570: debug("Applying options for %.100s", arg);
1.17 markus 571: *activep = 1;
572: break;
573: }
1.42 provos 574: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 575: return 0;
576:
577: case oEscapeChar:
578: intptr = &options->escape_char;
1.42 provos 579: arg = strdelim(&s);
1.40 ho 580: if (!arg || *arg == '\0')
1.17 markus 581: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 582: if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 583: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
584: value = (u_char) arg[1] & 31;
1.38 provos 585: else if (strlen(arg) == 1)
1.51 markus 586: value = (u_char) arg[0];
1.38 provos 587: else if (strcmp(arg, "none") == 0)
1.17 markus 588: value = -2;
589: else {
590: fatal("%.200s line %d: Bad escape character.",
591: filename, linenum);
592: /* NOTREACHED */
593: value = 0; /* Avoid compiler warning. */
594: }
595: if (*activep && *intptr == -1)
596: *intptr = value;
597: break;
598:
599: default:
600: fatal("process_config_line: Unimplemented opcode %d", opcode);
601: }
602:
603: /* Check that there is no garbage at end of line. */
1.57 djm 604: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 605: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
606: filename, linenum, arg);
607: }
1.17 markus 608: return 0;
1.1 deraadt 609: }
610:
611:
1.19 markus 612: /*
613: * Reads the config file and modifies the options accordingly. Options
614: * should already be initialized before this call. This never returns if
615: * there is an error. If the file does not exist, this returns immediately.
616: */
1.1 deraadt 617:
1.26 markus 618: void
1.17 markus 619: read_config_file(const char *filename, const char *host, Options *options)
1.1 deraadt 620: {
1.17 markus 621: FILE *f;
622: char line[1024];
623: int active, linenum;
624: int bad_options = 0;
625:
626: /* Open the file. */
627: f = fopen(filename, "r");
628: if (!f)
629: return;
630:
631: debug("Reading configuration data %.200s", filename);
632:
1.19 markus 633: /*
634: * Mark that we are now processing the options. This flag is turned
635: * on/off by Host specifications.
636: */
1.17 markus 637: active = 1;
638: linenum = 0;
639: while (fgets(line, sizeof(line), f)) {
640: /* Update line number counter. */
641: linenum++;
642: if (process_config_line(options, host, line, filename, linenum, &active) != 0)
643: bad_options++;
644: }
645: fclose(f);
646: if (bad_options > 0)
647: fatal("%s: terminating, %d bad configuration options\n",
648: filename, bad_options);
1.1 deraadt 649: }
650:
1.19 markus 651: /*
652: * Initializes options to special values that indicate that they have not yet
653: * been set. Read_config_file will only set options with this value. Options
654: * are processed in the following order: command line, user config file,
655: * system config file. Last, fill_default_options is called.
656: */
1.1 deraadt 657:
1.26 markus 658: void
1.17 markus 659: initialize_options(Options * options)
1.1 deraadt 660: {
1.17 markus 661: memset(options, 'X', sizeof(*options));
662: options->forward_agent = -1;
663: options->forward_x11 = -1;
1.34 markus 664: options->xauth_location = NULL;
1.17 markus 665: options->gateway_ports = -1;
666: options->use_privileged_port = -1;
667: options->rhosts_authentication = -1;
668: options->rsa_authentication = -1;
1.50 markus 669: options->pubkey_authentication = -1;
1.17 markus 670: options->skey_authentication = -1;
1.1 deraadt 671: #ifdef KRB4
1.17 markus 672: options->kerberos_authentication = -1;
1.1 deraadt 673: #endif
1.5 dugsong 674: #ifdef AFS
1.17 markus 675: options->kerberos_tgt_passing = -1;
676: options->afs_token_passing = -1;
1.1 deraadt 677: #endif
1.17 markus 678: options->password_authentication = -1;
1.48 markus 679: options->kbd_interactive_authentication = -1;
680: options->kbd_interactive_devices = NULL;
1.17 markus 681: options->rhosts_rsa_authentication = -1;
682: options->fallback_to_rsh = -1;
683: options->use_rsh = -1;
684: options->batch_mode = -1;
685: options->check_host_ip = -1;
686: options->strict_host_key_checking = -1;
687: options->compression = -1;
688: options->keepalives = -1;
689: options->compression_level = -1;
690: options->port = -1;
691: options->connection_attempts = -1;
692: options->number_of_password_prompts = -1;
693: options->cipher = -1;
1.25 markus 694: options->ciphers = NULL;
695: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 696: options->num_identity_files = 0;
697: options->hostname = NULL;
1.52 markus 698: options->host_key_alias = NULL;
1.17 markus 699: options->proxy_command = NULL;
700: options->user = NULL;
701: options->escape_char = -1;
702: options->system_hostfile = NULL;
703: options->user_hostfile = NULL;
1.27 markus 704: options->system_hostfile2 = NULL;
705: options->user_hostfile2 = NULL;
1.17 markus 706: options->num_local_forwards = 0;
707: options->num_remote_forwards = 0;
708: options->log_level = (LogLevel) - 1;
1.1 deraadt 709: }
710:
1.19 markus 711: /*
712: * Called after processing other sources of option data, this fills those
713: * options for which no value has been specified with their default values.
714: */
1.1 deraadt 715:
1.26 markus 716: void
1.17 markus 717: fill_default_options(Options * options)
1.1 deraadt 718: {
1.17 markus 719: if (options->forward_agent == -1)
1.33 markus 720: options->forward_agent = 0;
1.17 markus 721: if (options->forward_x11 == -1)
1.23 markus 722: options->forward_x11 = 0;
1.34 markus 723: #ifdef XAUTH_PATH
724: if (options->xauth_location == NULL)
1.35 markus 725: options->xauth_location = XAUTH_PATH;
1.34 markus 726: #endif /* XAUTH_PATH */
1.17 markus 727: if (options->gateway_ports == -1)
728: options->gateway_ports = 0;
729: if (options->use_privileged_port == -1)
730: options->use_privileged_port = 1;
731: if (options->rhosts_authentication == -1)
732: options->rhosts_authentication = 1;
733: if (options->rsa_authentication == -1)
734: options->rsa_authentication = 1;
1.50 markus 735: if (options->pubkey_authentication == -1)
736: options->pubkey_authentication = 1;
1.17 markus 737: if (options->skey_authentication == -1)
738: options->skey_authentication = 0;
1.1 deraadt 739: #ifdef KRB4
1.17 markus 740: if (options->kerberos_authentication == -1)
1.45 provos 741: options->kerberos_authentication = 1;
1.5 dugsong 742: #endif /* KRB4 */
743: #ifdef AFS
1.17 markus 744: if (options->kerberos_tgt_passing == -1)
745: options->kerberos_tgt_passing = 1;
746: if (options->afs_token_passing == -1)
747: options->afs_token_passing = 1;
1.5 dugsong 748: #endif /* AFS */
1.17 markus 749: if (options->password_authentication == -1)
750: options->password_authentication = 1;
1.48 markus 751: if (options->kbd_interactive_authentication == -1)
752: options->kbd_interactive_authentication = 0;
1.17 markus 753: if (options->rhosts_rsa_authentication == -1)
754: options->rhosts_rsa_authentication = 1;
755: if (options->fallback_to_rsh == -1)
1.41 deraadt 756: options->fallback_to_rsh = 0;
1.17 markus 757: if (options->use_rsh == -1)
758: options->use_rsh = 0;
759: if (options->batch_mode == -1)
760: options->batch_mode = 0;
761: if (options->check_host_ip == -1)
762: options->check_host_ip = 1;
763: if (options->strict_host_key_checking == -1)
764: options->strict_host_key_checking = 2; /* 2 is default */
765: if (options->compression == -1)
766: options->compression = 0;
767: if (options->keepalives == -1)
768: options->keepalives = 1;
769: if (options->compression_level == -1)
770: options->compression_level = 6;
771: if (options->port == -1)
772: options->port = 0; /* Filled in ssh_connect. */
773: if (options->connection_attempts == -1)
774: options->connection_attempts = 4;
775: if (options->number_of_password_prompts == -1)
776: options->number_of_password_prompts = 3;
777: /* Selected in ssh_login(). */
778: if (options->cipher == -1)
779: options->cipher = SSH_CIPHER_NOT_SET;
1.31 markus 780: /* options->ciphers, default set in myproposals.h */
1.25 markus 781: if (options->protocol == SSH_PROTO_UNKNOWN)
1.28 markus 782: options->protocol = SSH_PROTO_1|SSH_PROTO_2|SSH_PROTO_1_PREFERRED;
1.17 markus 783: if (options->num_identity_files == 0) {
1.50 markus 784: if (options->protocol & SSH_PROTO_1) {
785: options->identity_files[options->num_identity_files] =
1.55 markus 786: xmalloc(2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1);
1.50 markus 787: sprintf(options->identity_files[options->num_identity_files++],
1.55 markus 788: "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50 markus 789: }
790: if (options->protocol & SSH_PROTO_2) {
791: options->identity_files[options->num_identity_files] =
1.55 markus 792: xmalloc(2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1);
1.50 markus 793: sprintf(options->identity_files[options->num_identity_files++],
1.55 markus 794: "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50 markus 795: }
1.27 markus 796: }
1.17 markus 797: if (options->escape_char == -1)
798: options->escape_char = '~';
799: if (options->system_hostfile == NULL)
1.55 markus 800: options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17 markus 801: if (options->user_hostfile == NULL)
1.55 markus 802: options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27 markus 803: if (options->system_hostfile2 == NULL)
1.55 markus 804: options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27 markus 805: if (options->user_hostfile2 == NULL)
1.55 markus 806: options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.17 markus 807: if (options->log_level == (LogLevel) - 1)
1.54 markus 808: options->log_level = SYSLOG_LEVEL_INFO;
1.17 markus 809: /* options->proxy_command should not be set by default */
810: /* options->user will be set in the main program if appropriate */
811: /* options->hostname will be set in the main program if appropriate */
1.52 markus 812: /* options->host_key_alias should not be set by default */
1.1 deraadt 813: }