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