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