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