Annotation of src/usr.bin/ssh/readconf.c, Revision 1.7
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.7 ! deraadt 17: RCSID("$Id: readconf.c,v 1.6 1999/09/30 05:03:05 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: {
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 AFS
1.5 dugsong 97: oKerberosTgtPassing, oAFSTokenPassing,
1.1 deraadt 98: #endif
99: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
100: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
101: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
102: oBatchMode, oStrictHostKeyChecking, oCompression, oCompressionLevel,
103: oKeepAlives, oTISAuthentication
104: } OpCodes;
105:
106: /* Textual representations of the tokens. */
107:
108: static struct
109: {
110: const char *name;
111: OpCodes opcode;
112: } keywords[] =
113: {
114: { "forwardagent", oForwardAgent },
115: { "forwardx11", oForwardX11 },
1.3 deraadt 116: { "gatewayports", oGatewayPorts },
1.1 deraadt 117: { "rhostsauthentication", oRhostsAuthentication },
118: { "passwordauthentication", oPasswordAuthentication },
119: { "rsaauthentication", oRSAAuthentication },
120: #ifdef KRB4
121: { "kerberosauthentication", oKerberosAuthentication },
122: #endif /* KRB4 */
1.5 dugsong 123: #ifdef AFS
1.1 deraadt 124: { "kerberostgtpassing", oKerberosTgtPassing },
125: { "afstokenpassing", oAFSTokenPassing },
126: #endif
127: { "fallbacktorsh", oFallBackToRsh },
128: { "usersh", oUseRsh },
129: { "identityfile", oIdentityFile },
130: { "hostname", oHostName },
131: { "proxycommand", oProxyCommand },
132: { "port", oPort },
133: { "cipher", oCipher },
134: { "remoteforward", oRemoteForward },
135: { "localforward", oLocalForward },
136: { "user", oUser },
137: { "host", oHost },
138: { "escapechar", oEscapeChar },
139: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
140: { "globalknownhostsfile", oGlobalKnownHostsFile },
141: { "userknownhostsfile", oUserKnownHostsFile },
142: { "connectionattempts", oConnectionAttempts },
143: { "batchmode", oBatchMode },
144: { "stricthostkeychecking", oStrictHostKeyChecking },
145: { "compression", oCompression },
146: { "compressionlevel", oCompressionLevel },
147: { "keepalive", oKeepAlives },
148: { "tisauthentication", oTISAuthentication },
149: { NULL, 0 }
150: };
151:
152: /* Characters considered whitespace in strtok calls. */
153: #define WHITESPACE " \t\r\n"
154:
155:
156: /* Adds a local TCP/IP port forward to options. Never returns if there
157: is an error. */
158:
159: void add_local_forward(Options *options, int port, const char *host,
160: int host_port)
161: {
162: Forward *fwd;
1.4 deraadt 163: extern uid_t original_real_uid;
164: if ((port & 0xffff) != port)
165: fatal("Requested forwarding of nonexistent port %d.", port);
1.7 ! deraadt 166: if (port < IPPORT_RESERVED && original_real_uid != 0)
1.4 deraadt 167: fatal("Privileged ports can only be forwarded by root.\n");
1.1 deraadt 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:
1.5 dugsong 285: #ifdef AFS
1.1 deraadt 286: case oKerberosTgtPassing:
287: intptr = &options->kerberos_tgt_passing;
288: goto parse_flag;
289:
290: case oAFSTokenPassing:
291: intptr = &options->afs_token_passing;
292: goto parse_flag;
293: #endif
294:
295: case oFallBackToRsh:
296: intptr = &options->fallback_to_rsh;
297: goto parse_flag;
298:
299: case oUseRsh:
300: intptr = &options->use_rsh;
301: goto parse_flag;
302:
303: case oBatchMode:
304: intptr = &options->batch_mode;
305: goto parse_flag;
306:
307: case oStrictHostKeyChecking:
308: intptr = &options->strict_host_key_checking;
309: cp = strtok(NULL, WHITESPACE);
310: if (!cp)
311: fatal("%.200s line %d: Missing yes/no argument.",
312: filename, linenum);
313: value = 0; /* To avoid compiler warning... */
314: if (strcmp(cp, "yes") == 0 || strcmp(cp, "true") == 0)
315: value = 1;
316: else if (strcmp(cp, "no") == 0 || strcmp(cp, "false") == 0)
317: value = 0;
318: else if (strcmp(cp, "ask") == 0)
319: value = 2;
320: else
321: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
322: if (*activep && *intptr == -1)
323: *intptr = value;
324: break;
325:
326: case oCompression:
327: intptr = &options->compression;
328: goto parse_flag;
329:
330: case oKeepAlives:
331: intptr = &options->keepalives;
332: goto parse_flag;
333:
334: case oTISAuthentication:
335: cp = strtok(NULL, WHITESPACE);
336: if (cp != 0 && (strcmp(cp, "yes") == 0 || strcmp(cp, "true") == 0))
337: fprintf(stderr,
338: "%.99s line %d: Warning, TIS is not supported.\n",
339: filename,
340: linenum);
341: break;
342:
343: case oCompressionLevel:
344: intptr = &options->compression_level;
345: goto parse_int;
346:
347: case oIdentityFile:
348: cp = strtok(NULL, WHITESPACE);
349: if (!cp)
350: fatal("%.200s line %d: Missing argument.", filename, linenum);
351: if (*activep)
352: {
353: if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
354: fatal("%.200s line %d: Too many identity files specified (max %d).",
355: filename, linenum, SSH_MAX_IDENTITY_FILES);
356: options->identity_files[options->num_identity_files++] = xstrdup(cp);
357: }
358: break;
359:
360: case oUser:
361: charptr = &options->user;
362: parse_string:
363: cp = strtok(NULL, WHITESPACE);
364: if (!cp)
365: fatal("%.200s line %d: Missing argument.", filename, linenum);
366: if (*activep && *charptr == NULL)
367: *charptr = xstrdup(cp);
368: break;
369:
370: case oGlobalKnownHostsFile:
371: charptr = &options->system_hostfile;
372: goto parse_string;
373:
374: case oUserKnownHostsFile:
375: charptr = &options->user_hostfile;
376: goto parse_string;
377:
378: case oHostName:
379: charptr = &options->hostname;
380: goto parse_string;
381:
382: case oProxyCommand:
383: charptr = &options->proxy_command;
384: string = xstrdup("");
385: while ((cp = strtok(NULL, WHITESPACE)) != NULL)
386: {
387: string = xrealloc(string, strlen(string) + strlen(cp) + 2);
388: strcat(string, " ");
389: strcat(string, cp);
390: }
391: if (*activep && *charptr == NULL)
392: *charptr = string;
393: else
394: xfree(string);
395: return;
396:
397: case oPort:
398: intptr = &options->port;
399: parse_int:
400: cp = strtok(NULL, WHITESPACE);
401: if (!cp)
402: fatal("%.200s line %d: Missing argument.", filename, linenum);
403: if (cp[0] < '0' || cp[0] > '9')
404: fatal("%.200s line %d: Bad number.", filename, linenum);
405: #if 0
406: value = atoi(cp);
407: #else
408: {
409: char *ptr;
410: value = strtol(cp, &ptr, 0); /* Octal, decimal, or hex format? */
411: if (cp == ptr)
412: fatal("%.200s line %d: Bad number.", filename, linenum);
413: }
414: #endif
415: if (*activep && *intptr == -1)
416: *intptr = value;
417: break;
418:
419: case oConnectionAttempts:
420: intptr = &options->connection_attempts;
421: goto parse_int;
422:
423: case oCipher:
424: intptr = &options->cipher;
425: cp = strtok(NULL, WHITESPACE);
426: value = cipher_number(cp);
427: if (value == -1)
428: fatal("%.200s line %d: Bad cipher.", filename, linenum);
429: if (*activep && *intptr == -1)
430: *intptr = value;
431: break;
432:
433: case oRemoteForward:
434: cp = strtok(NULL, WHITESPACE);
435: if (!cp)
436: fatal("%.200s line %d: Missing argument.", filename, linenum);
437: if (cp[0] < '0' || cp[0] > '9')
438: fatal("%.200s line %d: Badly formatted port number.",
439: filename, linenum);
440: fwd_port = atoi(cp);
441: cp = strtok(NULL, WHITESPACE);
442: if (!cp)
443: fatal("%.200s line %d: Missing second argument.",
444: filename, linenum);
445: if (sscanf(cp, "%255[^:]:%d", buf, &fwd_host_port) != 2)
446: fatal("%.200s line %d: Badly formatted host:port.",
447: filename, linenum);
448: if (*activep)
449: add_remote_forward(options, fwd_port, buf, fwd_host_port);
450: break;
451:
452: case oLocalForward:
453: cp = strtok(NULL, WHITESPACE);
454: if (!cp)
455: fatal("%.200s line %d: Missing argument.", filename, linenum);
456: if (cp[0] < '0' || cp[0] > '9')
457: fatal("%.200s line %d: Badly formatted port number.",
458: filename, linenum);
459: fwd_port = atoi(cp);
460: cp = strtok(NULL, WHITESPACE);
461: if (!cp)
462: fatal("%.200s line %d: Missing second argument.",
463: filename, linenum);
464: if (sscanf(cp, "%255[^:]:%d", buf, &fwd_host_port) != 2)
465: fatal("%.200s line %d: Badly formatted host:port.",
466: filename, linenum);
467: if (*activep)
468: add_local_forward(options, fwd_port, buf, fwd_host_port);
469: break;
470:
471: case oHost:
472: *activep = 0;
473: while ((cp = strtok(NULL, WHITESPACE)) != NULL)
474: if (match_pattern(host, cp))
475: {
476: debug("Applying options for %.100s", cp);
477: *activep = 1;
478: break;
479: }
480: /* Avoid garbage check below, as strtok already returned NULL. */
481: return;
482:
483: case oEscapeChar:
484: intptr = &options->escape_char;
485: cp = strtok(NULL, WHITESPACE);
486: if (!cp)
487: fatal("%.200s line %d: Missing argument.", filename, linenum);
488: if (cp[0] == '^' && cp[2] == 0 &&
489: (unsigned char)cp[1] >= 64 && (unsigned char)cp[1] < 128)
490: value = (unsigned char)cp[1] & 31;
491: else
492: if (strlen(cp) == 1)
493: value = (unsigned char)cp[0];
494: else
495: if (strcmp(cp, "none") == 0)
496: value = -2;
497: else
498: {
499: fatal("%.200s line %d: Bad escape character.",
500: filename, linenum);
501: /*NOTREACHED*/
502: value = 0; /* Avoid compiler warning. */
503: }
504: if (*activep && *intptr == -1)
505: *intptr = value;
506: break;
507:
508: default:
509: fatal("parse_config_file: Unimplemented opcode %d", opcode);
510: }
511:
512: /* Check that there is no garbage at end of line. */
513: if (strtok(NULL, WHITESPACE) != NULL)
514: fatal("%.200s line %d: garbage at end of line.",
515: filename, linenum);
516: }
517:
518:
519: /* Reads the config file and modifies the options accordingly. Options should
520: already be initialized before this call. This never returns if there
521: is an error. If the file does not exist, this returns immediately. */
522:
523: void read_config_file(const char *filename, const char *host, Options *options)
524: {
525: FILE *f;
526: char line[1024];
527: int active, linenum;
528:
529: /* Open the file. */
530: f = fopen(filename, "r");
531: if (!f)
532: return;
533:
534: debug("Reading configuration data %.200s", filename);
535:
536: /* Mark that we are now processing the options. This flag is turned on/off
537: by Host specifications. */
538: active = 1;
539: linenum = 0;
540: while (fgets(line, sizeof(line), f))
541: {
542: /* Update line number counter. */
543: linenum++;
544:
545: process_config_line(options, host, line, filename, linenum, &active);
546: }
547: fclose(f);
548: }
549:
550: /* Initializes options to special values that indicate that they have not
551: yet been set. Read_config_file will only set options with this value.
552: Options are processed in the following order: command line, user config
553: file, system config file. Last, fill_default_options is called. */
554:
555: void initialize_options(Options *options)
556: {
557: memset(options, 'X', sizeof(*options));
558: options->forward_agent = -1;
559: options->forward_x11 = -1;
1.3 deraadt 560: options->gateway_ports = -1;
1.1 deraadt 561: options->rhosts_authentication = -1;
562: options->rsa_authentication = -1;
563: #ifdef KRB4
564: options->kerberos_authentication = -1;
565: #endif
1.5 dugsong 566: #ifdef AFS
1.1 deraadt 567: options->kerberos_tgt_passing = -1;
568: options->afs_token_passing = -1;
569: #endif
570: options->password_authentication = -1;
571: options->rhosts_rsa_authentication = -1;
572: options->fallback_to_rsh = -1;
573: options->use_rsh = -1;
574: options->batch_mode = -1;
575: options->strict_host_key_checking = -1;
576: options->compression = -1;
577: options->keepalives = -1;
578: options->compression_level = -1;
579: options->port = -1;
580: options->connection_attempts = -1;
581: options->cipher = -1;
582: options->num_identity_files = 0;
583: options->hostname = NULL;
584: options->proxy_command = NULL;
585: options->user = NULL;
586: options->escape_char = -1;
587: options->system_hostfile = NULL;
588: options->user_hostfile = NULL;
589: options->num_local_forwards = 0;
590: options->num_remote_forwards = 0;
591: }
592:
593: /* Called after processing other sources of option data, this fills those
594: options for which no value has been specified with their default values. */
595:
596: void fill_default_options(Options *options)
597: {
598: if (options->forward_agent == -1)
599: options->forward_agent = 1;
600: if (options->forward_x11 == -1)
601: options->forward_x11 = 1;
1.3 deraadt 602: if (options->gateway_ports == -1)
603: options->gateway_ports = 0;
1.1 deraadt 604: if (options->rhosts_authentication == -1)
605: options->rhosts_authentication = 1;
606: if (options->rsa_authentication == -1)
607: options->rsa_authentication = 1;
608: #ifdef KRB4
609: if (options->kerberos_authentication == -1)
610: options->kerberos_authentication = 1;
1.5 dugsong 611: #endif /* KRB4 */
612: #ifdef AFS
1.1 deraadt 613: if (options->kerberos_tgt_passing == -1)
614: options->kerberos_tgt_passing = 1;
615: if (options->afs_token_passing == -1)
616: options->afs_token_passing = 1;
1.5 dugsong 617: #endif /* AFS */
1.1 deraadt 618: if (options->password_authentication == -1)
619: options->password_authentication = 1;
620: if (options->rhosts_rsa_authentication == -1)
621: options->rhosts_rsa_authentication = 1;
622: if (options->fallback_to_rsh == -1)
623: options->fallback_to_rsh = 1;
624: if (options->use_rsh == -1)
625: options->use_rsh = 0;
626: if (options->batch_mode == -1)
627: options->batch_mode = 0;
628: if (options->strict_host_key_checking == -1)
629: options->strict_host_key_checking = 2; /* 2 is default */
630: if (options->compression == -1)
631: options->compression = 0;
632: if (options->keepalives == -1)
633: options->keepalives = 1;
634: if (options->compression_level == -1)
635: options->compression_level = 6;
636: if (options->port == -1)
637: options->port = 0; /* Filled in ssh_connect. */
638: if (options->connection_attempts == -1)
639: options->connection_attempts = 4;
640: if (options->cipher == -1)
641: options->cipher = SSH_CIPHER_NOT_SET; /* Selected in ssh_login(). */
642: if (options->num_identity_files == 0)
643: {
644: options->identity_files[0] =
645: xmalloc(2 + strlen(SSH_CLIENT_IDENTITY) + 1);
646: sprintf(options->identity_files[0], "~/%.100s", SSH_CLIENT_IDENTITY);
647: options->num_identity_files = 1;
648: }
649: if (options->escape_char == -1)
650: options->escape_char = '~';
651: if (options->system_hostfile == NULL)
652: options->system_hostfile = SSH_SYSTEM_HOSTFILE;
653: if (options->user_hostfile == NULL)
654: options->user_hostfile = SSH_USER_HOSTFILE;
655: /* options->proxy_command should not be set by default */
656: /* options->user will be set in the main program if appropriate */
657: /* options->hostname will be set in the main program if appropriate */
658: }
659: