Annotation of src/usr.bin/ssh/servconf.c, Revision 1.14
1.1 deraadt 1: /*
2:
3: servconf.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: Mon Aug 21 15:48:58 1995 ylo
11:
12: */
13:
14: #include "includes.h"
1.14 ! markus 15: RCSID("$Id: servconf.c,v 1.13 1999/10/11 21:48:29 markus Exp $");
1.1 deraadt 16:
17: #include "ssh.h"
18: #include "servconf.h"
19: #include "xmalloc.h"
20:
21: /* Initializes the server options to their default values. */
22:
23: void initialize_server_options(ServerOptions *options)
24: {
25: memset(options, 0, sizeof(*options));
26: options->port = -1;
1.3 deraadt 27: options->listen_addr.s_addr = htonl(INADDR_ANY);
1.1 deraadt 28: options->host_key_file = NULL;
29: options->server_key_bits = -1;
30: options->login_grace_time = -1;
31: options->key_regeneration_time = -1;
32: options->permit_root_login = -1;
33: options->ignore_rhosts = -1;
34: options->quiet_mode = -1;
35: options->fascist_logging = -1;
36: options->print_motd = -1;
1.11 markus 37: options->check_mail = -1;
1.1 deraadt 38: options->x11_forwarding = -1;
1.2 deraadt 39: options->x11_display_offset = -1;
1.1 deraadt 40: options->strict_modes = -1;
41: options->keepalives = -1;
42: options->log_facility = (SyslogFacility)-1;
43: options->rhosts_authentication = -1;
44: options->rhosts_rsa_authentication = -1;
45: options->rsa_authentication = -1;
46: #ifdef KRB4
47: options->kerberos_authentication = -1;
48: options->kerberos_or_local_passwd = -1;
49: options->kerberos_ticket_cleanup = -1;
50: #endif
1.4 dugsong 51: #ifdef AFS
1.1 deraadt 52: options->kerberos_tgt_passing = -1;
53: options->afs_token_passing = -1;
54: #endif
55: options->password_authentication = -1;
1.10 markus 56: #ifdef SKEY
57: options->skey_authentication = -1;
58: #endif
1.1 deraadt 59: options->permit_empty_passwd = -1;
1.12 markus 60: options->use_login = -1;
1.14 ! markus 61: options->silent_deny = -1;
1.1 deraadt 62: options->num_allow_hosts = 0;
63: options->num_deny_hosts = 0;
1.13 markus 64: options->num_allow_users = 0;
65: options->num_deny_users = 0;
66: options->num_allow_groups = 0;
67: options->num_deny_groups = 0;
1.1 deraadt 68: }
69:
70: void fill_default_server_options(ServerOptions *options)
71: {
72: if (options->port == -1)
73: {
74: struct servent *sp;
75:
76: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
77: if (sp)
78: options->port = ntohs(sp->s_port);
79: else
80: options->port = SSH_DEFAULT_PORT;
81: endservent();
82: }
83: if (options->host_key_file == NULL)
84: options->host_key_file = HOST_KEY_FILE;
85: if (options->server_key_bits == -1)
86: options->server_key_bits = 768;
87: if (options->login_grace_time == -1)
88: options->login_grace_time = 600;
89: if (options->key_regeneration_time == -1)
90: options->key_regeneration_time = 3600;
91: if (options->permit_root_login == -1)
92: options->permit_root_login = 1;
93: if (options->ignore_rhosts == -1)
94: options->ignore_rhosts = 0;
95: if (options->quiet_mode == -1)
96: options->quiet_mode = 0;
1.11 markus 97: if (options->check_mail == -1)
98: options->check_mail = 0;
1.1 deraadt 99: if (options->fascist_logging == -1)
100: options->fascist_logging = 1;
101: if (options->print_motd == -1)
102: options->print_motd = 1;
103: if (options->x11_forwarding == -1)
104: options->x11_forwarding = 1;
1.2 deraadt 105: if (options->x11_display_offset == -1)
106: options->x11_display_offset = 1;
1.1 deraadt 107: if (options->strict_modes == -1)
108: options->strict_modes = 1;
109: if (options->keepalives == -1)
110: options->keepalives = 1;
111: if (options->log_facility == (SyslogFacility)(-1))
112: options->log_facility = SYSLOG_FACILITY_DAEMON;
113: if (options->rhosts_authentication == -1)
114: options->rhosts_authentication = 0;
115: if (options->rhosts_rsa_authentication == -1)
116: options->rhosts_rsa_authentication = 1;
117: if (options->rsa_authentication == -1)
118: options->rsa_authentication = 1;
119: #ifdef KRB4
120: if (options->kerberos_authentication == -1)
1.5 dugsong 121: options->kerberos_authentication = (access(KEYFILE, R_OK) == 0);
1.1 deraadt 122: if (options->kerberos_or_local_passwd == -1)
123: options->kerberos_or_local_passwd = 0;
124: if (options->kerberos_ticket_cleanup == -1)
125: options->kerberos_ticket_cleanup = 1;
1.4 dugsong 126: #endif /* KRB4 */
127: #ifdef AFS
1.1 deraadt 128: if (options->kerberos_tgt_passing == -1)
129: options->kerberos_tgt_passing = 0;
130: if (options->afs_token_passing == -1)
1.5 dugsong 131: options->afs_token_passing = k_hasafs();
1.4 dugsong 132: #endif /* AFS */
1.1 deraadt 133: if (options->password_authentication == -1)
134: options->password_authentication = 1;
1.10 markus 135: #ifdef SKEY
136: if (options->skey_authentication == -1)
137: options->skey_authentication = 1;
138: #endif
1.1 deraadt 139: if (options->permit_empty_passwd == -1)
1.10 markus 140: options->permit_empty_passwd = 1;
1.12 markus 141: if (options->use_login == -1)
142: options->use_login = 0;
1.14 ! markus 143: if (options->silent_deny == -1)
! 144: options->silent_deny = 0;
1.1 deraadt 145: }
146:
147: #define WHITESPACE " \t\r\n"
148:
149: /* Keyword tokens. */
150: typedef enum
151: {
152: sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
153: sPermitRootLogin, sQuietMode, sFascistLogging, sLogFacility,
154: sRhostsAuthentication, sRhostsRSAAuthentication, sRSAAuthentication,
155: #ifdef KRB4
1.4 dugsong 156: sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.1 deraadt 157: #endif
158: #ifdef AFS
1.4 dugsong 159: sKerberosTgtPassing, sAFSTokenPassing,
1.1 deraadt 160: #endif
1.10 markus 161: #ifdef SKEY
162: sSkeyAuthentication,
163: #endif
1.1 deraadt 164: sPasswordAuthentication, sAllowHosts, sDenyHosts, sListenAddress,
1.2 deraadt 165: sPrintMotd, sIgnoreRhosts, sX11Forwarding, sX11DisplayOffset,
1.12 markus 166: sStrictModes, sEmptyPasswd, sRandomSeedFile, sKeepAlives, sCheckMail,
1.14 ! markus 167: sUseLogin, sSilentDeny, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups
1.13 markus 168:
1.1 deraadt 169: } ServerOpCodes;
170:
171: /* Textual representation of the tokens. */
172: static struct
173: {
174: const char *name;
175: ServerOpCodes opcode;
176: } keywords[] =
177: {
178: { "port", sPort },
179: { "hostkey", sHostKeyFile },
180: { "serverkeybits", sServerKeyBits },
181: { "logingracetime", sLoginGraceTime },
182: { "keyregenerationinterval", sKeyRegenerationTime },
183: { "permitrootlogin", sPermitRootLogin },
184: { "quietmode", sQuietMode },
185: { "fascistlogging", sFascistLogging },
186: { "syslogfacility", sLogFacility },
187: { "rhostsauthentication", sRhostsAuthentication },
188: { "rhostsrsaauthentication", sRhostsRSAAuthentication },
189: { "rsaauthentication", sRSAAuthentication },
190: #ifdef KRB4
191: { "kerberosauthentication", sKerberosAuthentication },
192: { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
193: { "kerberosticketcleanup", sKerberosTicketCleanup },
194: #endif
1.4 dugsong 195: #ifdef AFS
1.1 deraadt 196: { "kerberostgtpassing", sKerberosTgtPassing },
197: { "afstokenpassing", sAFSTokenPassing },
198: #endif
199: { "passwordauthentication", sPasswordAuthentication },
1.10 markus 200: #ifdef SKEY
201: { "skeyauthentication", sSkeyAuthentication },
202: #endif
1.1 deraadt 203: { "allowhosts", sAllowHosts },
1.11 markus 204: { "checkmail", sCheckMail },
1.1 deraadt 205: { "denyhosts", sDenyHosts },
206: { "listenaddress", sListenAddress },
207: { "printmotd", sPrintMotd },
208: { "ignorerhosts", sIgnoreRhosts },
209: { "x11forwarding", sX11Forwarding },
1.2 deraadt 210: { "x11displayoffset", sX11DisplayOffset },
1.1 deraadt 211: { "strictmodes", sStrictModes },
212: { "permitemptypasswords", sEmptyPasswd },
1.12 markus 213: { "uselogin", sUseLogin },
1.14 ! markus 214: { "silentdeny", sSilentDeny },
1.1 deraadt 215: { "randomseed", sRandomSeedFile },
216: { "keepalive", sKeepAlives },
1.13 markus 217: { "allowusers", sAllowUsers },
218: { "denyusers", sDenyUsers },
219: { "allowgroups", sAllowGroups },
220: { "denygroups", sDenyGroups },
1.1 deraadt 221: { NULL, 0 }
222: };
223:
224: static struct
225: {
226: const char *name;
227: SyslogFacility facility;
228: } log_facilities[] =
229: {
230: { "DAEMON", SYSLOG_FACILITY_DAEMON },
231: { "USER", SYSLOG_FACILITY_USER },
232: { "AUTH", SYSLOG_FACILITY_AUTH },
233: { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
234: { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
235: { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
236: { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
237: { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
238: { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
239: { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
240: { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
241: { NULL, 0 }
242: };
243:
244: /* Returns the number of the token pointed to by cp of length len.
245: Never returns if the token is not known. */
246:
247: static ServerOpCodes parse_token(const char *cp, const char *filename,
248: int linenum)
249: {
250: unsigned int i;
251:
252: for (i = 0; keywords[i].name; i++)
253: if (strcmp(cp, keywords[i].name) == 0)
254: return keywords[i].opcode;
255:
256: fprintf(stderr, "%s line %d: Bad configuration option: %s\n",
257: filename, linenum, cp);
258: exit(1);
259: }
260:
261: /* Reads the server configuration file. */
262:
263: void read_server_config(ServerOptions *options, const char *filename)
264: {
265: FILE *f;
266: char line[1024];
267: char *cp, **charptr;
268: int linenum, *intptr, i, value;
269: ServerOpCodes opcode;
270:
271: f = fopen(filename, "r");
272: if (!f)
273: {
274: perror(filename);
275: return;
276: }
277:
278: linenum = 0;
279: while (fgets(line, sizeof(line), f))
280: {
281: linenum++;
282: cp = line + strspn(line, WHITESPACE);
283: if (!*cp || *cp == '#')
284: continue;
285: cp = strtok(cp, WHITESPACE);
286: {
287: char *t = cp;
288: for (; *t != 0; t++)
289: if ('A' <= *t && *t <= 'Z')
290: *t = *t - 'A' + 'a'; /* tolower */
291:
292: }
293: opcode = parse_token(cp, filename, linenum);
294: switch (opcode)
295: {
296: case sPort:
297: intptr = &options->port;
298: parse_int:
299: cp = strtok(NULL, WHITESPACE);
300: if (!cp)
301: {
302: fprintf(stderr, "%s line %d: missing integer value.\n",
303: filename, linenum);
304: exit(1);
305: }
306: value = atoi(cp);
307: if (*intptr == -1)
308: *intptr = value;
309: break;
310:
311: case sServerKeyBits:
312: intptr = &options->server_key_bits;
313: goto parse_int;
314:
315: case sLoginGraceTime:
316: intptr = &options->login_grace_time;
317: goto parse_int;
318:
319: case sKeyRegenerationTime:
320: intptr = &options->key_regeneration_time;
321: goto parse_int;
322:
323: case sListenAddress:
324: cp = strtok(NULL, WHITESPACE);
325: if (!cp)
326: {
327: fprintf(stderr, "%s line %d: missing inet addr.\n",
328: filename, linenum);
329: exit(1);
330: }
331: options->listen_addr.s_addr = inet_addr(cp);
332: break;
333:
334: case sHostKeyFile:
335: charptr = &options->host_key_file;
336: cp = strtok(NULL, WHITESPACE);
337: if (!cp)
338: {
339: fprintf(stderr, "%s line %d: missing file name.\n",
340: filename, linenum);
341: exit(1);
342: }
343: if (*charptr == NULL)
344: *charptr = tilde_expand_filename(cp, getuid());
345: break;
346:
347: case sRandomSeedFile:
1.8 deraadt 348: fprintf(stderr, "%s line %d: \"randomseed\" option is obsolete.\n",
1.7 provos 349: filename, linenum);
350: cp = strtok(NULL, WHITESPACE);
351: break;
1.1 deraadt 352:
353: case sPermitRootLogin:
354: intptr = &options->permit_root_login;
355: parse_flag:
356: cp = strtok(NULL, WHITESPACE);
357: if (!cp)
358: {
359: fprintf(stderr, "%s line %d: missing yes/no argument.\n",
360: filename, linenum);
361: exit(1);
362: }
363: if (strcmp(cp, "yes") == 0)
364: value = 1;
365: else
366: if (strcmp(cp, "no") == 0)
367: value = 0;
368: else
369: {
370: fprintf(stderr, "%s line %d: Bad yes/no argument: %s\n",
371: filename, linenum, cp);
372: exit(1);
373: }
374: if (*intptr == -1)
375: *intptr = value;
376: break;
377:
378: case sIgnoreRhosts:
379: intptr = &options->ignore_rhosts;
380: goto parse_flag;
381:
382: case sQuietMode:
383: intptr = &options->quiet_mode;
384: goto parse_flag;
385:
386: case sFascistLogging:
387: intptr = &options->fascist_logging;
388: goto parse_flag;
389:
390: case sRhostsAuthentication:
391: intptr = &options->rhosts_authentication;
392: goto parse_flag;
393:
394: case sRhostsRSAAuthentication:
395: intptr = &options->rhosts_rsa_authentication;
396: goto parse_flag;
397:
398: case sRSAAuthentication:
399: intptr = &options->rsa_authentication;
400: goto parse_flag;
401:
402: #ifdef KRB4
403: case sKerberosAuthentication:
404: intptr = &options->kerberos_authentication;
405: goto parse_flag;
406:
407: case sKerberosOrLocalPasswd:
408: intptr = &options->kerberos_or_local_passwd;
409: goto parse_flag;
410:
411: case sKerberosTicketCleanup:
412: intptr = &options->kerberos_ticket_cleanup;
413: goto parse_flag;
414: #endif
415:
1.4 dugsong 416: #ifdef AFS
1.1 deraadt 417: case sKerberosTgtPassing:
418: intptr = &options->kerberos_tgt_passing;
419: goto parse_flag;
420:
421: case sAFSTokenPassing:
422: intptr = &options->afs_token_passing;
423: goto parse_flag;
424: #endif
425:
426: case sPasswordAuthentication:
427: intptr = &options->password_authentication;
428: goto parse_flag;
1.11 markus 429:
430: case sCheckMail:
431: intptr = &options->check_mail;
432: goto parse_flag;
1.10 markus 433:
434: #ifdef SKEY
435: case sSkeyAuthentication:
436: intptr = &options->skey_authentication;
437: goto parse_flag;
438: #endif
1.1 deraadt 439:
440: case sPrintMotd:
441: intptr = &options->print_motd;
442: goto parse_flag;
443:
444: case sX11Forwarding:
445: intptr = &options->x11_forwarding;
446: goto parse_flag;
1.2 deraadt 447:
448: case sX11DisplayOffset:
449: intptr = &options->x11_display_offset;
450: goto parse_int;
1.1 deraadt 451:
452: case sStrictModes:
453: intptr = &options->strict_modes;
454: goto parse_flag;
455:
456: case sKeepAlives:
457: intptr = &options->keepalives;
458: goto parse_flag;
459:
460: case sEmptyPasswd:
461: intptr = &options->permit_empty_passwd;
462: goto parse_flag;
1.12 markus 463:
464: case sUseLogin:
465: intptr = &options->use_login;
1.14 ! markus 466: goto parse_flag;
! 467:
! 468: case sSilentDeny:
! 469: intptr = &options->silent_deny;
1.12 markus 470: goto parse_flag;
1.1 deraadt 471:
472: case sLogFacility:
473: cp = strtok(NULL, WHITESPACE);
474: if (!cp)
475: {
476: fprintf(stderr, "%s line %d: missing facility name.\n",
477: filename, linenum);
478: exit(1);
479: }
480: for (i = 0; log_facilities[i].name; i++)
481: if (strcmp(log_facilities[i].name, cp) == 0)
482: break;
483: if (!log_facilities[i].name)
484: {
485: fprintf(stderr, "%s line %d: unsupported log facility %s\n",
486: filename, linenum, cp);
487: exit(1);
488: }
489: if (options->log_facility == (SyslogFacility)(-1))
490: options->log_facility = log_facilities[i].facility;
491: break;
492:
493: case sAllowHosts:
494: while ((cp = strtok(NULL, WHITESPACE)))
495: {
496: if (options->num_allow_hosts >= MAX_ALLOW_HOSTS)
497: {
498: fprintf(stderr, "%s line %d: too many allow hosts.\n",
499: filename, linenum);
500: exit(1);
501: }
502: options->allow_hosts[options->num_allow_hosts++] = xstrdup(cp);
503: }
504: break;
505:
506: case sDenyHosts:
507: while ((cp = strtok(NULL, WHITESPACE)))
508: {
509: if (options->num_deny_hosts >= MAX_DENY_HOSTS)
510: {
511: fprintf(stderr, "%s line %d: too many deny hosts.\n",
512: filename, linenum);
513: exit(1);
514: }
515: options->deny_hosts[options->num_deny_hosts++] = xstrdup(cp);
1.13 markus 516: }
517: break;
518:
519: case sAllowUsers:
520: while ((cp = strtok(NULL, WHITESPACE)))
521: {
522: if (options->num_allow_users >= MAX_ALLOW_USERS)
523: {
524: fprintf(stderr, "%s line %d: too many allow users.\n",
525: filename, linenum);
526: exit(1);
527: }
528: options->allow_users[options->num_allow_users++] = xstrdup(cp);
529: }
530: break;
531:
532: case sDenyUsers:
533: while ((cp = strtok(NULL, WHITESPACE)))
534: {
535: if (options->num_deny_users >= MAX_DENY_USERS)
536: {
537: fprintf(stderr, "%s line %d: too many deny users.\n",
538: filename, linenum);
539: exit(1);
540: }
541: options->deny_users[options->num_deny_users++] = xstrdup(cp);
542: }
543: break;
544:
545: case sAllowGroups:
546: while ((cp = strtok(NULL, WHITESPACE)))
547: {
548: if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
549: {
550: fprintf(stderr, "%s line %d: too many allow groups.\n",
551: filename, linenum);
552: exit(1);
553: }
554: options->allow_groups[options->num_allow_groups++] = xstrdup(cp);
555: }
556: break;
557:
558: case sDenyGroups:
559: while ((cp = strtok(NULL, WHITESPACE)))
560: {
561: if (options->num_deny_groups >= MAX_DENY_GROUPS)
562: {
563: fprintf(stderr, "%s line %d: too many deny groups.\n",
564: filename, linenum);
565: exit(1);
566: }
567: options->deny_groups[options->num_deny_groups++] = xstrdup(cp);
1.1 deraadt 568: }
569: break;
570:
571: default:
572: fprintf(stderr, "%s line %d: Missing handler for opcode %s (%d)\n",
573: filename, linenum, cp, opcode);
574: exit(1);
575: }
576: if (strtok(NULL, WHITESPACE) != NULL)
577: {
578: fprintf(stderr, "%s line %d: garbage at end of line.\n",
579: filename, linenum);
580: exit(1);
581: }
582: }
583: fclose(f);
584: }