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