Annotation of src/usr.bin/ssh/readconf.c, Revision 1.52
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.52 ! markus 15: RCSID("$OpenBSD: readconf.c,v 1.51 2000/12/19 23:17:57 markus Exp $");
1.1 deraadt 16:
17: #include "ssh.h"
18: #include "readconf.h"
1.24 markus 19: #include "match.h"
1.1 deraadt 20: #include "xmalloc.h"
1.25 markus 21: #include "compat.h"
1.1 deraadt 22:
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
1.50 markus 71: ForwardX11 no
1.1 deraadt 72: 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:
1.17 markus 88: typedef enum {
89: oBadOption,
90: oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
91: oPasswordAuthentication, oRSAAuthentication, oFallBackToRsh, oUseRsh,
1.34 markus 92: oSkeyAuthentication, oXAuthLocation,
1.1 deraadt 93: #ifdef KRB4
1.17 markus 94: oKerberosAuthentication,
1.1 deraadt 95: #endif /* KRB4 */
96: #ifdef AFS
1.17 markus 97: oKerberosTgtPassing, oAFSTokenPassing,
1.1 deraadt 98: #endif
1.17 markus 99: 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,
1.50 markus 104: oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol,
105: oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.52 ! markus 106: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias
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 },
1.48 markus 122: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
123: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17 markus 124: { "rsaauthentication", oRSAAuthentication },
1.50 markus 125: { "pubkeyauthentication", oPubkeyAuthentication },
126: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
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.50 markus 138: { "identityfile2", oIdentityFile }, /* alias */
1.17 markus 139: { "hostname", oHostName },
1.52 ! markus 140: { "hostkeyalias", oHostKeyAlias },
1.17 markus 141: { "proxycommand", oProxyCommand },
142: { "port", oPort },
143: { "cipher", oCipher },
1.25 markus 144: { "ciphers", oCiphers },
145: { "protocol", oProtocol },
1.17 markus 146: { "remoteforward", oRemoteForward },
147: { "localforward", oLocalForward },
148: { "user", oUser },
149: { "host", oHost },
150: { "escapechar", oEscapeChar },
151: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
152: { "globalknownhostsfile", oGlobalKnownHostsFile },
153: { "userknownhostsfile", oUserKnownHostsFile },
1.27 markus 154: { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
155: { "userknownhostsfile2", oUserKnownHostsFile2 },
1.17 markus 156: { "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 }
1.13 markus 167: };
168:
1.19 markus 169: /*
170: * Adds a local TCP/IP port forward to options. Never returns if there is an
171: * error.
172: */
1.1 deraadt 173:
1.26 markus 174: void
1.22 markus 175: add_local_forward(Options *options, u_short port, const char *host,
176: u_short host_port)
1.1 deraadt 177: {
1.17 markus 178: Forward *fwd;
179: extern uid_t original_real_uid;
180: if (port < IPPORT_RESERVED && original_real_uid != 0)
181: fatal("Privileged ports can only be forwarded by root.\n");
182: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
183: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
184: fwd = &options->local_forwards[options->num_local_forwards++];
185: fwd->port = port;
186: fwd->host = xstrdup(host);
187: fwd->host_port = host_port;
1.1 deraadt 188: }
189:
1.19 markus 190: /*
191: * Adds a remote TCP/IP port forward to options. Never returns if there is
192: * an error.
193: */
1.1 deraadt 194:
1.26 markus 195: void
1.22 markus 196: add_remote_forward(Options *options, u_short port, const char *host,
197: u_short host_port)
1.1 deraadt 198: {
1.17 markus 199: Forward *fwd;
200: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
201: fatal("Too many remote forwards (max %d).",
202: SSH_MAX_FORWARDS_PER_DIRECTION);
203: fwd = &options->remote_forwards[options->num_remote_forwards++];
204: fwd->port = port;
205: fwd->host = xstrdup(host);
206: fwd->host_port = host_port;
1.1 deraadt 207: }
208:
1.19 markus 209: /*
210: * Returns the number of the token pointed to by cp of length len. Never
211: * returns if the token is not known.
212: */
1.1 deraadt 213:
1.26 markus 214: static OpCodes
1.17 markus 215: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 216: {
1.51 markus 217: u_int i;
1.1 deraadt 218:
1.17 markus 219: for (i = 0; keywords[i].name; i++)
1.20 markus 220: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 221: return keywords[i].opcode;
222:
223: fprintf(stderr, "%s: line %d: Bad configuration option: %s\n",
224: filename, linenum, cp);
225: return oBadOption;
1.1 deraadt 226: }
227:
1.19 markus 228: /*
229: * Processes a single option line as used in the configuration files. This
230: * only sets those values that have not already been set.
231: */
1.1 deraadt 232:
1.14 markus 233: int
234: process_config_line(Options *options, const char *host,
1.17 markus 235: char *line, const char *filename, int linenum,
236: int *activep)
1.1 deraadt 237: {
1.38 provos 238: char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 239: int opcode, *intptr, value;
240: u_short fwd_port, fwd_host_port;
1.1 deraadt 241:
1.42 provos 242: s = line;
243: /* Get the keyword. (Each line is supposed to begin with a keyword). */
244: keyword = strdelim(&s);
245: /* Ignore leading whitespace. */
246: if (*keyword == '\0')
1.43 markus 247: keyword = strdelim(&s);
1.42 provos 248: if (!*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 249: return 0;
250:
1.38 provos 251: opcode = parse_token(keyword, filename, linenum);
1.17 markus 252:
253: switch (opcode) {
254: case oBadOption:
1.19 markus 255: /* don't panic, but count bad options */
256: return -1;
1.17 markus 257: /* NOTREACHED */
258: case oForwardAgent:
259: intptr = &options->forward_agent;
260: parse_flag:
1.42 provos 261: arg = strdelim(&s);
1.40 ho 262: if (!arg || *arg == '\0')
1.17 markus 263: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
264: value = 0; /* To avoid compiler warning... */
1.38 provos 265: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 266: value = 1;
1.38 provos 267: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 268: value = 0;
269: else
270: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
271: if (*activep && *intptr == -1)
272: *intptr = value;
273: break;
274:
275: case oForwardX11:
276: intptr = &options->forward_x11;
277: goto parse_flag;
278:
279: case oGatewayPorts:
280: intptr = &options->gateway_ports;
281: goto parse_flag;
282:
283: case oUsePrivilegedPort:
284: intptr = &options->use_privileged_port;
285: goto parse_flag;
286:
287: case oRhostsAuthentication:
288: intptr = &options->rhosts_authentication;
289: goto parse_flag;
290:
291: case oPasswordAuthentication:
292: intptr = &options->password_authentication;
293: goto parse_flag;
294:
1.48 markus 295: case oKbdInteractiveAuthentication:
296: intptr = &options->kbd_interactive_authentication;
297: goto parse_flag;
298:
299: case oKbdInteractiveDevices:
300: charptr = &options->kbd_interactive_devices;
301: goto parse_string;
302:
1.50 markus 303: case oPubkeyAuthentication:
304: intptr = &options->pubkey_authentication;
1.30 markus 305: goto parse_flag;
306:
1.17 markus 307: case oRSAAuthentication:
308: intptr = &options->rsa_authentication;
309: goto parse_flag;
310:
311: case oRhostsRSAAuthentication:
312: intptr = &options->rhosts_rsa_authentication;
313: goto parse_flag;
314:
315: case oTISAuthentication:
316: /* fallthrough, there is no difference on the client side */
317: case oSkeyAuthentication:
318: intptr = &options->skey_authentication;
319: goto parse_flag;
1.16 markus 320:
1.1 deraadt 321: #ifdef KRB4
1.17 markus 322: case oKerberosAuthentication:
323: intptr = &options->kerberos_authentication;
324: goto parse_flag;
1.1 deraadt 325: #endif /* KRB4 */
326:
1.5 dugsong 327: #ifdef AFS
1.17 markus 328: case oKerberosTgtPassing:
329: intptr = &options->kerberos_tgt_passing;
330: goto parse_flag;
331:
332: case oAFSTokenPassing:
333: intptr = &options->afs_token_passing;
334: goto parse_flag;
1.1 deraadt 335: #endif
1.17 markus 336:
337: case oFallBackToRsh:
338: intptr = &options->fallback_to_rsh;
339: goto parse_flag;
340:
341: case oUseRsh:
342: intptr = &options->use_rsh;
343: goto parse_flag;
344:
345: case oBatchMode:
346: intptr = &options->batch_mode;
347: goto parse_flag;
348:
349: case oCheckHostIP:
350: intptr = &options->check_host_ip;
351: goto parse_flag;
352:
353: case oStrictHostKeyChecking:
354: intptr = &options->strict_host_key_checking;
1.42 provos 355: arg = strdelim(&s);
1.40 ho 356: if (!arg || *arg == '\0')
1.17 markus 357: fatal("%.200s line %d: Missing yes/no argument.",
358: filename, linenum);
359: value = 0; /* To avoid compiler warning... */
1.38 provos 360: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 361: value = 1;
1.38 provos 362: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 363: value = 0;
1.38 provos 364: else if (strcmp(arg, "ask") == 0)
1.17 markus 365: value = 2;
366: else
367: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
368: if (*activep && *intptr == -1)
369: *intptr = value;
370: break;
371:
372: case oCompression:
373: intptr = &options->compression;
374: goto parse_flag;
375:
376: case oKeepAlives:
377: intptr = &options->keepalives;
378: goto parse_flag;
379:
380: case oNumberOfPasswordPrompts:
381: intptr = &options->number_of_password_prompts;
382: goto parse_int;
383:
384: case oCompressionLevel:
385: intptr = &options->compression_level;
386: goto parse_int;
387:
388: case oIdentityFile:
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.50 markus 393: intptr = &options->num_identity_files;
1.27 markus 394: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 395: fatal("%.200s line %d: Too many identity files specified (max %d).",
396: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 397: charptr = &options->identity_files[*intptr];
1.38 provos 398: *charptr = xstrdup(arg);
1.27 markus 399: *intptr = *intptr + 1;
1.17 markus 400: }
401: break;
402:
1.34 markus 403: case oXAuthLocation:
404: charptr=&options->xauth_location;
405: goto parse_string;
406:
1.17 markus 407: case oUser:
408: charptr = &options->user;
409: parse_string:
1.42 provos 410: arg = strdelim(&s);
1.40 ho 411: if (!arg || *arg == '\0')
1.17 markus 412: fatal("%.200s line %d: Missing argument.", filename, linenum);
413: if (*activep && *charptr == NULL)
1.38 provos 414: *charptr = xstrdup(arg);
1.17 markus 415: break;
416:
417: case oGlobalKnownHostsFile:
418: charptr = &options->system_hostfile;
419: goto parse_string;
420:
421: case oUserKnownHostsFile:
422: charptr = &options->user_hostfile;
423: goto parse_string;
424:
1.27 markus 425: case oGlobalKnownHostsFile2:
426: charptr = &options->system_hostfile2;
427: goto parse_string;
428:
429: case oUserKnownHostsFile2:
430: charptr = &options->user_hostfile2;
431: goto parse_string;
432:
1.17 markus 433: case oHostName:
434: charptr = &options->hostname;
435: goto parse_string;
436:
1.52 ! markus 437: case oHostKeyAlias:
! 438: charptr = &options->host_key_alias;
! 439: goto parse_string;
! 440:
1.17 markus 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 &&
1.51 markus 579: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
580: value = (u_char) arg[1] & 31;
1.38 provos 581: else if (strlen(arg) == 1)
1.51 markus 582: value = (u_char) arg[0];
1.38 provos 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.50 markus 666: options->pubkey_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;
694: options->hostname = NULL;
1.52 ! markus 695: options->host_key_alias = NULL;
1.17 markus 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.50 markus 732: if (options->pubkey_authentication == -1)
733: options->pubkey_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) {
1.50 markus 781: if (options->protocol & SSH_PROTO_1) {
782: options->identity_files[options->num_identity_files] =
783: xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
784: sprintf(options->identity_files[options->num_identity_files++],
785: "~/%.100s", SSH_CLIENT_IDENTITY);
786: }
787: if (options->protocol & SSH_PROTO_2) {
788: options->identity_files[options->num_identity_files] =
789: xmalloc(2 + strlen(SSH_CLIENT_ID_DSA) + 1);
790: sprintf(options->identity_files[options->num_identity_files++],
791: "~/%.100s", SSH_CLIENT_ID_DSA);
792: }
1.27 markus 793: }
1.17 markus 794: if (options->escape_char == -1)
795: options->escape_char = '~';
796: if (options->system_hostfile == NULL)
797: options->system_hostfile = SSH_SYSTEM_HOSTFILE;
798: if (options->user_hostfile == NULL)
799: options->user_hostfile = SSH_USER_HOSTFILE;
1.27 markus 800: if (options->system_hostfile2 == NULL)
801: options->system_hostfile2 = SSH_SYSTEM_HOSTFILE2;
802: if (options->user_hostfile2 == NULL)
803: options->user_hostfile2 = SSH_USER_HOSTFILE2;
1.17 markus 804: if (options->log_level == (LogLevel) - 1)
805: options->log_level = SYSLOG_LEVEL_INFO;
806: /* options->proxy_command should not be set by default */
807: /* options->user will be set in the main program if appropriate */
808: /* options->hostname will be set in the main program if appropriate */
1.52 ! markus 809: /* options->host_key_alias should not be set by default */
1.1 deraadt 810: }