Annotation of src/usr.bin/ssh/readconf.c, Revision 1.3
1.1 deraadt 1: /*
2:
3: readconf.c
4:
5: Author: Tatu Ylonen <ylo@cs.hut.fi>
6:
7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: All rights reserved
9:
10: Created: Sat Apr 22 00:03:10 1995 ylo
11:
12: Functions for reading the configuration files.
13:
14: */
15:
16: #include "includes.h"
1.3 ! deraadt 17: RCSID("$Id: readconf.c,v 1.2 1999/09/28 04:45:36 provos Exp $");
1.1 deraadt 18:
19: #include "ssh.h"
20: #include "cipher.h"
21: #include "readconf.h"
22: #include "xmalloc.h"
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:
89: typedef enum
90: {
1.3 ! deraadt 91: oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
1.1 deraadt 92: oPasswordAuthentication, oRSAAuthentication, oFallBackToRsh, oUseRsh,
93: #ifdef KRB4
94: oKerberosAuthentication,
95: #endif /* KRB4 */
96: #ifdef KERBEROS_TGT_PASSING
97: oKerberosTgtPassing,
98: #endif
99: #ifdef AFS
100: oAFSTokenPassing,
101: #endif
102: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
103: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
104: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
105: oBatchMode, oStrictHostKeyChecking, oCompression, oCompressionLevel,
106: oKeepAlives, oTISAuthentication
107: } OpCodes;
108:
109: /* Textual representations of the tokens. */
110:
111: static struct
112: {
113: const char *name;
114: OpCodes opcode;
115: } keywords[] =
116: {
117: { "forwardagent", oForwardAgent },
118: { "forwardx11", oForwardX11 },
1.3 ! deraadt 119: { "gatewayports", oGatewayPorts },
1.1 deraadt 120: { "rhostsauthentication", oRhostsAuthentication },
121: { "passwordauthentication", oPasswordAuthentication },
122: { "rsaauthentication", oRSAAuthentication },
123: #ifdef KRB4
124: { "kerberosauthentication", oKerberosAuthentication },
125: #endif /* KRB4 */
126: #ifdef KERBEROS_TGT_PASSING
127: { "kerberostgtpassing", oKerberosTgtPassing },
128: #endif
129: #ifdef AFS
130: { "afstokenpassing", oAFSTokenPassing },
131: #endif
132: { "fallbacktorsh", oFallBackToRsh },
133: { "usersh", oUseRsh },
134: { "identityfile", oIdentityFile },
135: { "hostname", oHostName },
136: { "proxycommand", oProxyCommand },
137: { "port", oPort },
138: { "cipher", oCipher },
139: { "remoteforward", oRemoteForward },
140: { "localforward", oLocalForward },
141: { "user", oUser },
142: { "host", oHost },
143: { "escapechar", oEscapeChar },
144: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
145: { "globalknownhostsfile", oGlobalKnownHostsFile },
146: { "userknownhostsfile", oUserKnownHostsFile },
147: { "connectionattempts", oConnectionAttempts },
148: { "batchmode", oBatchMode },
149: { "stricthostkeychecking", oStrictHostKeyChecking },
150: { "compression", oCompression },
151: { "compressionlevel", oCompressionLevel },
152: { "keepalive", oKeepAlives },
153: { "tisauthentication", oTISAuthentication },
154: { NULL, 0 }
155: };
156:
157: /* Characters considered whitespace in strtok calls. */
158: #define WHITESPACE " \t\r\n"
159:
160:
161: /* Adds a local TCP/IP port forward to options. Never returns if there
162: is an error. */
163:
164: void add_local_forward(Options *options, int port, const char *host,
165: int host_port)
166: {
167: Forward *fwd;
168: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
169: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
170: fwd = &options->local_forwards[options->num_local_forwards++];
171: fwd->port = port;
172: fwd->host = xstrdup(host);
173: fwd->host_port = host_port;
174: }
175:
176: /* Adds a remote TCP/IP port forward to options. Never returns if there
177: is an error. */
178:
179: void add_remote_forward(Options *options, int port, const char *host,
180: int host_port)
181: {
182: Forward *fwd;
183: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
184: fatal("Too many remote forwards (max %d).",
185: SSH_MAX_FORWARDS_PER_DIRECTION);
186: fwd = &options->remote_forwards[options->num_remote_forwards++];
187: fwd->port = port;
188: fwd->host = xstrdup(host);
189: fwd->host_port = host_port;
190: }
191:
192: /* Returns the number of the token pointed to by cp of length len.
193: Never returns if the token is not known. */
194:
195: static OpCodes parse_token(const char *cp, const char *filename, int linenum)
196: {
197: unsigned int i;
198:
199: for (i = 0; keywords[i].name; i++)
200: if (strcmp(cp, keywords[i].name) == 0)
201: return keywords[i].opcode;
202:
203: fatal("%.200s line %d: Bad configuration option.",
204: filename, linenum);
205: /*NOTREACHED*/
206: return 0;
207: }
208:
209: /* Processes a single option line as used in the configuration files.
210: This only sets those values that have not already been set. */
211:
212: void process_config_line(Options *options, const char *host,
213: char *line, const char *filename, int linenum,
214: int *activep)
215: {
216: char buf[256], *cp, *string, **charptr;
217: int opcode, *intptr, value, fwd_port, fwd_host_port;
218:
219: /* Skip leading whitespace. */
220: cp = line + strspn(line, WHITESPACE);
221: if (!*cp || *cp == '\n' || *cp == '#')
222: return;
223:
224: /* Get the keyword. (Each line is supposed to begin with a keyword). */
225: cp = strtok(cp, WHITESPACE);
226: {
227: char *t = cp;
228: for (; *t != 0; t++)
229: if ('A' <= *t && *t <= 'Z')
230: *t = *t - 'A' + 'a'; /* tolower */
231:
232: }
233: opcode = parse_token(cp, filename, linenum);
234:
235: switch (opcode)
236: {
237:
238: case oForwardAgent:
239: intptr = &options->forward_agent;
240: parse_flag:
241: cp = strtok(NULL, WHITESPACE);
242: if (!cp)
243: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
244: value = 0; /* To avoid compiler warning... */
245: if (strcmp(cp, "yes") == 0 || strcmp(cp, "true") == 0)
246: value = 1;
247: else if (strcmp(cp, "no") == 0 || strcmp(cp, "false") == 0)
248: value = 0;
249: else
250: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
251: if (*activep && *intptr == -1)
252: *intptr = value;
253: break;
254:
255: case oForwardX11:
256: intptr = &options->forward_x11;
257: goto parse_flag;
1.3 ! deraadt 258:
! 259: case oGatewayPorts:
! 260: intptr = &options->gateway_ports;
! 261: goto parse_flag;
1.1 deraadt 262:
263: case oRhostsAuthentication:
264: intptr = &options->rhosts_authentication;
265: goto parse_flag;
266:
267: case oPasswordAuthentication:
268: intptr = &options->password_authentication;
269: goto parse_flag;
270:
271: case oRSAAuthentication:
272: intptr = &options->rsa_authentication;
273: goto parse_flag;
274:
275: case oRhostsRSAAuthentication:
276: intptr = &options->rhosts_rsa_authentication;
277: goto parse_flag;
278:
279: #ifdef KRB4
280: case oKerberosAuthentication:
281: intptr = &options->kerberos_authentication;
282: goto parse_flag;
283: #endif /* KRB4 */
284:
285: #ifdef KERBEROS_TGT_PASSING
286: case oKerberosTgtPassing:
287: intptr = &options->kerberos_tgt_passing;
288: goto parse_flag;
289: #endif
290:
291: #ifdef AFS
292: case oAFSTokenPassing:
293: intptr = &options->afs_token_passing;
294: goto parse_flag;
295: #endif
296:
297: case oFallBackToRsh:
298: intptr = &options->fallback_to_rsh;
299: goto parse_flag;
300:
301: case oUseRsh:
302: intptr = &options->use_rsh;
303: goto parse_flag;
304:
305: case oBatchMode:
306: intptr = &options->batch_mode;
307: goto parse_flag;
308:
309: case oStrictHostKeyChecking:
310: intptr = &options->strict_host_key_checking;
311: cp = strtok(NULL, WHITESPACE);
312: if (!cp)
313: fatal("%.200s line %d: Missing yes/no argument.",
314: filename, linenum);
315: value = 0; /* To avoid compiler warning... */
316: if (strcmp(cp, "yes") == 0 || strcmp(cp, "true") == 0)
317: value = 1;
318: else if (strcmp(cp, "no") == 0 || strcmp(cp, "false") == 0)
319: value = 0;
320: else if (strcmp(cp, "ask") == 0)
321: value = 2;
322: else
323: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
324: if (*activep && *intptr == -1)
325: *intptr = value;
326: break;
327:
328: #ifdef WITH_ZLIB
329: case oCompression:
330: intptr = &options->compression;
331: goto parse_flag;
332: #endif /* WITH_ZLIB */
333:
334: case oKeepAlives:
335: intptr = &options->keepalives;
336: goto parse_flag;
337:
338: case oTISAuthentication:
339: cp = strtok(NULL, WHITESPACE);
340: if (cp != 0 && (strcmp(cp, "yes") == 0 || strcmp(cp, "true") == 0))
341: fprintf(stderr,
342: "%.99s line %d: Warning, TIS is not supported.\n",
343: filename,
344: linenum);
345: break;
346:
347: #ifdef WITH_ZLIB
348: case oCompressionLevel:
349: intptr = &options->compression_level;
350: goto parse_int;
351: #endif /* WITH_ZLIB */
352:
353: case oIdentityFile:
354: cp = strtok(NULL, WHITESPACE);
355: if (!cp)
356: fatal("%.200s line %d: Missing argument.", filename, linenum);
357: if (*activep)
358: {
359: if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
360: fatal("%.200s line %d: Too many identity files specified (max %d).",
361: filename, linenum, SSH_MAX_IDENTITY_FILES);
362: options->identity_files[options->num_identity_files++] = xstrdup(cp);
363: }
364: break;
365:
366: case oUser:
367: charptr = &options->user;
368: parse_string:
369: cp = strtok(NULL, WHITESPACE);
370: if (!cp)
371: fatal("%.200s line %d: Missing argument.", filename, linenum);
372: if (*activep && *charptr == NULL)
373: *charptr = xstrdup(cp);
374: break;
375:
376: case oGlobalKnownHostsFile:
377: charptr = &options->system_hostfile;
378: goto parse_string;
379:
380: case oUserKnownHostsFile:
381: charptr = &options->user_hostfile;
382: goto parse_string;
383:
384: case oHostName:
385: charptr = &options->hostname;
386: goto parse_string;
387:
388: case oProxyCommand:
389: charptr = &options->proxy_command;
390: string = xstrdup("");
391: while ((cp = strtok(NULL, WHITESPACE)) != NULL)
392: {
393: string = xrealloc(string, strlen(string) + strlen(cp) + 2);
394: strcat(string, " ");
395: strcat(string, cp);
396: }
397: if (*activep && *charptr == NULL)
398: *charptr = string;
399: else
400: xfree(string);
401: return;
402:
403: case oPort:
404: intptr = &options->port;
405: parse_int:
406: cp = strtok(NULL, WHITESPACE);
407: if (!cp)
408: fatal("%.200s line %d: Missing argument.", filename, linenum);
409: if (cp[0] < '0' || cp[0] > '9')
410: fatal("%.200s line %d: Bad number.", filename, linenum);
411: #if 0
412: value = atoi(cp);
413: #else
414: {
415: char *ptr;
416: value = strtol(cp, &ptr, 0); /* Octal, decimal, or hex format? */
417: if (cp == ptr)
418: fatal("%.200s line %d: Bad number.", filename, linenum);
419: }
420: #endif
421: if (*activep && *intptr == -1)
422: *intptr = value;
423: break;
424:
425: case oConnectionAttempts:
426: intptr = &options->connection_attempts;
427: goto parse_int;
428:
429: case oCipher:
430: intptr = &options->cipher;
431: cp = strtok(NULL, WHITESPACE);
432: value = cipher_number(cp);
433: if (value == -1)
434: fatal("%.200s line %d: Bad cipher.", filename, linenum);
435: if (*activep && *intptr == -1)
436: *intptr = value;
437: break;
438:
439: case oRemoteForward:
440: cp = strtok(NULL, WHITESPACE);
441: if (!cp)
442: fatal("%.200s line %d: Missing argument.", filename, linenum);
443: if (cp[0] < '0' || cp[0] > '9')
444: fatal("%.200s line %d: Badly formatted port number.",
445: filename, linenum);
446: fwd_port = atoi(cp);
447: cp = strtok(NULL, WHITESPACE);
448: if (!cp)
449: fatal("%.200s line %d: Missing second argument.",
450: filename, linenum);
451: if (sscanf(cp, "%255[^:]:%d", buf, &fwd_host_port) != 2)
452: fatal("%.200s line %d: Badly formatted host:port.",
453: filename, linenum);
454: if (*activep)
455: add_remote_forward(options, fwd_port, buf, fwd_host_port);
456: break;
457:
458: case oLocalForward:
459: cp = strtok(NULL, WHITESPACE);
460: if (!cp)
461: fatal("%.200s line %d: Missing argument.", filename, linenum);
462: if (cp[0] < '0' || cp[0] > '9')
463: fatal("%.200s line %d: Badly formatted port number.",
464: filename, linenum);
465: fwd_port = atoi(cp);
466: cp = strtok(NULL, WHITESPACE);
467: if (!cp)
468: fatal("%.200s line %d: Missing second argument.",
469: filename, linenum);
470: if (sscanf(cp, "%255[^:]:%d", buf, &fwd_host_port) != 2)
471: fatal("%.200s line %d: Badly formatted host:port.",
472: filename, linenum);
473: if (*activep)
474: add_local_forward(options, fwd_port, buf, fwd_host_port);
475: break;
476:
477: case oHost:
478: *activep = 0;
479: while ((cp = strtok(NULL, WHITESPACE)) != NULL)
480: if (match_pattern(host, cp))
481: {
482: debug("Applying options for %.100s", cp);
483: *activep = 1;
484: break;
485: }
486: /* Avoid garbage check below, as strtok already returned NULL. */
487: return;
488:
489: case oEscapeChar:
490: intptr = &options->escape_char;
491: cp = strtok(NULL, WHITESPACE);
492: if (!cp)
493: fatal("%.200s line %d: Missing argument.", filename, linenum);
494: if (cp[0] == '^' && cp[2] == 0 &&
495: (unsigned char)cp[1] >= 64 && (unsigned char)cp[1] < 128)
496: value = (unsigned char)cp[1] & 31;
497: else
498: if (strlen(cp) == 1)
499: value = (unsigned char)cp[0];
500: else
501: if (strcmp(cp, "none") == 0)
502: value = -2;
503: else
504: {
505: fatal("%.200s line %d: Bad escape character.",
506: filename, linenum);
507: /*NOTREACHED*/
508: value = 0; /* Avoid compiler warning. */
509: }
510: if (*activep && *intptr == -1)
511: *intptr = value;
512: break;
513:
514: default:
515: fatal("parse_config_file: Unimplemented opcode %d", opcode);
516: }
517:
518: /* Check that there is no garbage at end of line. */
519: if (strtok(NULL, WHITESPACE) != NULL)
520: fatal("%.200s line %d: garbage at end of line.",
521: filename, linenum);
522: }
523:
524:
525: /* Reads the config file and modifies the options accordingly. Options should
526: already be initialized before this call. This never returns if there
527: is an error. If the file does not exist, this returns immediately. */
528:
529: void read_config_file(const char *filename, const char *host, Options *options)
530: {
531: FILE *f;
532: char line[1024];
533: int active, linenum;
534:
535: /* Open the file. */
536: f = fopen(filename, "r");
537: if (!f)
538: return;
539:
540: debug("Reading configuration data %.200s", filename);
541:
542: /* Mark that we are now processing the options. This flag is turned on/off
543: by Host specifications. */
544: active = 1;
545: linenum = 0;
546: while (fgets(line, sizeof(line), f))
547: {
548: /* Update line number counter. */
549: linenum++;
550:
551: process_config_line(options, host, line, filename, linenum, &active);
552: }
553: fclose(f);
554: }
555:
556: /* Initializes options to special values that indicate that they have not
557: yet been set. Read_config_file will only set options with this value.
558: Options are processed in the following order: command line, user config
559: file, system config file. Last, fill_default_options is called. */
560:
561: void initialize_options(Options *options)
562: {
563: memset(options, 'X', sizeof(*options));
564: options->forward_agent = -1;
565: options->forward_x11 = -1;
1.3 ! deraadt 566: options->gateway_ports = -1;
1.1 deraadt 567: options->rhosts_authentication = -1;
568: options->rsa_authentication = -1;
569: #ifdef KRB4
570: options->kerberos_authentication = -1;
571: #endif
572: #ifdef KERBEROS_TGT_PASSING
573: options->kerberos_tgt_passing = -1;
574: #endif
575: #ifdef AFS
576: options->afs_token_passing = -1;
577: #endif
578: options->password_authentication = -1;
579: options->rhosts_rsa_authentication = -1;
580: options->fallback_to_rsh = -1;
581: options->use_rsh = -1;
582: options->batch_mode = -1;
583: options->strict_host_key_checking = -1;
584: #ifdef WITH_ZLIB
585: options->compression = -1;
586: #endif /* WITH_ZLIB */
587: options->keepalives = -1;
588: #ifdef WITH_ZLIB
589: options->compression_level = -1;
590: #endif /* WITH_ZLIB */
591: options->port = -1;
592: options->connection_attempts = -1;
593: options->cipher = -1;
594: options->num_identity_files = 0;
595: options->hostname = NULL;
596: options->proxy_command = NULL;
597: options->user = NULL;
598: options->escape_char = -1;
599: options->system_hostfile = NULL;
600: options->user_hostfile = NULL;
601: options->num_local_forwards = 0;
602: options->num_remote_forwards = 0;
603: }
604:
605: /* Called after processing other sources of option data, this fills those
606: options for which no value has been specified with their default values. */
607:
608: void fill_default_options(Options *options)
609: {
610: if (options->forward_agent == -1)
611: options->forward_agent = 1;
612: if (options->forward_x11 == -1)
613: options->forward_x11 = 1;
1.3 ! deraadt 614: if (options->gateway_ports == -1)
! 615: options->gateway_ports = 0;
1.1 deraadt 616: if (options->rhosts_authentication == -1)
617: options->rhosts_authentication = 1;
618: if (options->rsa_authentication == -1)
619: options->rsa_authentication = 1;
620: #ifdef KRB4
621: if (options->kerberos_authentication == -1)
622: options->kerberos_authentication = 1;
623: #endif
624: #ifdef KERBEROS_TGT_PASSING
625: if (options->kerberos_tgt_passing == -1)
626: options->kerberos_tgt_passing = 1;
627: #endif
628: #ifdef AFS
629: if (options->afs_token_passing == -1)
630: options->afs_token_passing = 1;
631: #endif
632: if (options->password_authentication == -1)
633: options->password_authentication = 1;
634: if (options->rhosts_rsa_authentication == -1)
635: options->rhosts_rsa_authentication = 1;
636: if (options->fallback_to_rsh == -1)
637: options->fallback_to_rsh = 1;
638: if (options->use_rsh == -1)
639: options->use_rsh = 0;
640: if (options->batch_mode == -1)
641: options->batch_mode = 0;
642: if (options->strict_host_key_checking == -1)
643: options->strict_host_key_checking = 2; /* 2 is default */
644: #ifdef WITH_ZLIB
645: if (options->compression == -1)
646: options->compression = 0;
647: #endif /* WITH_ZLIB */
648: if (options->keepalives == -1)
649: options->keepalives = 1;
650: #ifdef WITH_ZLIB
651: if (options->compression_level == -1)
652: options->compression_level = 6;
653: #endif /* WITH_ZLIB */
654: if (options->port == -1)
655: options->port = 0; /* Filled in ssh_connect. */
656: if (options->connection_attempts == -1)
657: options->connection_attempts = 4;
658: if (options->cipher == -1)
659: options->cipher = SSH_CIPHER_NOT_SET; /* Selected in ssh_login(). */
660: if (options->num_identity_files == 0)
661: {
662: options->identity_files[0] =
663: xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
664: sprintf(options->identity_files[0], "~/%.100s", SSH_CLIENT_IDENTITY);
665: options->num_identity_files = 1;
666: }
667: if (options->escape_char == -1)
668: options->escape_char = '~';
669: if (options->system_hostfile == NULL)
670: options->system_hostfile = SSH_SYSTEM_HOSTFILE;
671: if (options->user_hostfile == NULL)
672: options->user_hostfile = SSH_USER_HOSTFILE;
673: /* options->proxy_command should not be set by default */
674: /* options->user will be set in the main program if appropriate */
675: /* options->hostname will be set in the main program if appropriate */
676: }
677: