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