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