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