Annotation of src/usr.bin/ssh/servconf.c, Revision 1.9
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.9 ! deraadt 15: RCSID("$Id: servconf.c,v 1.8 1999/10/01 22:23:48 deraadt 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;
37: options->x11_forwarding = -1;
1.2 deraadt 38: options->x11_display_offset = -1;
1.1 deraadt 39: options->strict_modes = -1;
40: options->keepalives = -1;
41: options->log_facility = (SyslogFacility)-1;
42: options->rhosts_authentication = -1;
43: options->rhosts_rsa_authentication = -1;
44: options->rsa_authentication = -1;
45: #ifdef KRB4
46: options->kerberos_authentication = -1;
47: options->kerberos_or_local_passwd = -1;
48: options->kerberos_ticket_cleanup = -1;
49: #endif
1.4 dugsong 50: #ifdef AFS
1.1 deraadt 51: options->kerberos_tgt_passing = -1;
52: options->afs_token_passing = -1;
53: #endif
54: options->password_authentication = -1;
55: options->permit_empty_passwd = -1;
56: options->num_allow_hosts = 0;
57: options->num_deny_hosts = 0;
58: }
59:
60: void fill_default_server_options(ServerOptions *options)
61: {
62: if (options->port == -1)
63: {
64: struct servent *sp;
65:
66: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
67: if (sp)
68: options->port = ntohs(sp->s_port);
69: else
70: options->port = SSH_DEFAULT_PORT;
71: endservent();
72: }
73: if (options->host_key_file == NULL)
74: options->host_key_file = HOST_KEY_FILE;
75: if (options->server_key_bits == -1)
76: options->server_key_bits = 768;
77: if (options->login_grace_time == -1)
78: options->login_grace_time = 600;
79: if (options->key_regeneration_time == -1)
80: options->key_regeneration_time = 3600;
81: if (options->permit_root_login == -1)
82: options->permit_root_login = 1;
83: if (options->ignore_rhosts == -1)
84: options->ignore_rhosts = 0;
85: if (options->quiet_mode == -1)
86: options->quiet_mode = 0;
87: if (options->fascist_logging == -1)
88: options->fascist_logging = 1;
89: if (options->print_motd == -1)
90: options->print_motd = 1;
91: if (options->x11_forwarding == -1)
92: options->x11_forwarding = 1;
1.2 deraadt 93: if (options->x11_display_offset == -1)
94: options->x11_display_offset = 1;
1.1 deraadt 95: if (options->strict_modes == -1)
96: options->strict_modes = 1;
97: if (options->keepalives == -1)
98: options->keepalives = 1;
99: if (options->log_facility == (SyslogFacility)(-1))
100: options->log_facility = SYSLOG_FACILITY_DAEMON;
101: if (options->rhosts_authentication == -1)
102: options->rhosts_authentication = 0;
103: if (options->rhosts_rsa_authentication == -1)
104: options->rhosts_rsa_authentication = 1;
105: if (options->rsa_authentication == -1)
106: options->rsa_authentication = 1;
107: #ifdef KRB4
108: if (options->kerberos_authentication == -1)
1.5 dugsong 109: options->kerberos_authentication = (access(KEYFILE, R_OK) == 0);
1.1 deraadt 110: if (options->kerberos_or_local_passwd == -1)
111: options->kerberos_or_local_passwd = 0;
112: if (options->kerberos_ticket_cleanup == -1)
113: options->kerberos_ticket_cleanup = 1;
1.4 dugsong 114: #endif /* KRB4 */
115: #ifdef AFS
1.1 deraadt 116: if (options->kerberos_tgt_passing == -1)
117: options->kerberos_tgt_passing = 0;
118: if (options->afs_token_passing == -1)
1.5 dugsong 119: options->afs_token_passing = k_hasafs();
1.4 dugsong 120: #endif /* AFS */
1.1 deraadt 121: if (options->password_authentication == -1)
122: options->password_authentication = 1;
123: if (options->permit_empty_passwd == -1)
124: options->permit_empty_passwd = 1;
125: }
126:
127: #define WHITESPACE " \t\r\n"
128:
129: /* Keyword tokens. */
130: typedef enum
131: {
132: sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
133: sPermitRootLogin, sQuietMode, sFascistLogging, sLogFacility,
134: sRhostsAuthentication, sRhostsRSAAuthentication, sRSAAuthentication,
135: #ifdef KRB4
1.4 dugsong 136: sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.1 deraadt 137: #endif
138: #ifdef AFS
1.4 dugsong 139: sKerberosTgtPassing, sAFSTokenPassing,
1.1 deraadt 140: #endif
141: sPasswordAuthentication, sAllowHosts, sDenyHosts, sListenAddress,
1.2 deraadt 142: sPrintMotd, sIgnoreRhosts, sX11Forwarding, sX11DisplayOffset,
1.1 deraadt 143: sStrictModes, sEmptyPasswd, sRandomSeedFile, sKeepAlives
144: } ServerOpCodes;
145:
146: /* Textual representation of the tokens. */
147: static struct
148: {
149: const char *name;
150: ServerOpCodes opcode;
151: } keywords[] =
152: {
153: { "port", sPort },
154: { "hostkey", sHostKeyFile },
155: { "serverkeybits", sServerKeyBits },
156: { "logingracetime", sLoginGraceTime },
157: { "keyregenerationinterval", sKeyRegenerationTime },
158: { "permitrootlogin", sPermitRootLogin },
159: { "quietmode", sQuietMode },
160: { "fascistlogging", sFascistLogging },
161: { "syslogfacility", sLogFacility },
162: { "rhostsauthentication", sRhostsAuthentication },
163: { "rhostsrsaauthentication", sRhostsRSAAuthentication },
164: { "rsaauthentication", sRSAAuthentication },
165: #ifdef KRB4
166: { "kerberosauthentication", sKerberosAuthentication },
167: { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
168: { "kerberosticketcleanup", sKerberosTicketCleanup },
169: #endif
1.4 dugsong 170: #ifdef AFS
1.1 deraadt 171: { "kerberostgtpassing", sKerberosTgtPassing },
172: { "afstokenpassing", sAFSTokenPassing },
173: #endif
174: { "passwordauthentication", sPasswordAuthentication },
175: { "allowhosts", sAllowHosts },
176: { "denyhosts", sDenyHosts },
177: { "listenaddress", sListenAddress },
178: { "printmotd", sPrintMotd },
179: { "ignorerhosts", sIgnoreRhosts },
180: { "x11forwarding", sX11Forwarding },
1.2 deraadt 181: { "x11displayoffset", sX11DisplayOffset },
1.1 deraadt 182: { "strictmodes", sStrictModes },
183: { "permitemptypasswords", sEmptyPasswd },
184: { "randomseed", sRandomSeedFile },
185: { "keepalive", sKeepAlives },
186: { NULL, 0 }
187: };
188:
189: static struct
190: {
191: const char *name;
192: SyslogFacility facility;
193: } log_facilities[] =
194: {
195: { "DAEMON", SYSLOG_FACILITY_DAEMON },
196: { "USER", SYSLOG_FACILITY_USER },
197: { "AUTH", SYSLOG_FACILITY_AUTH },
198: { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
199: { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
200: { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
201: { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
202: { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
203: { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
204: { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
205: { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
206: { NULL, 0 }
207: };
208:
209: /* Returns the number of the token pointed to by cp of length len.
210: Never returns if the token is not known. */
211:
212: static ServerOpCodes parse_token(const char *cp, const char *filename,
213: int linenum)
214: {
215: unsigned int i;
216:
217: for (i = 0; keywords[i].name; i++)
218: if (strcmp(cp, keywords[i].name) == 0)
219: return keywords[i].opcode;
220:
221: fprintf(stderr, "%s line %d: Bad configuration option: %s\n",
222: filename, linenum, cp);
223: exit(1);
224: }
225:
226: /* Reads the server configuration file. */
227:
228: void read_server_config(ServerOptions *options, const char *filename)
229: {
230: FILE *f;
231: char line[1024];
232: char *cp, **charptr;
233: int linenum, *intptr, i, value;
234: ServerOpCodes opcode;
235:
236: f = fopen(filename, "r");
237: if (!f)
238: {
239: perror(filename);
240: return;
241: }
242:
243: linenum = 0;
244: while (fgets(line, sizeof(line), f))
245: {
246: linenum++;
247: cp = line + strspn(line, WHITESPACE);
248: if (!*cp || *cp == '#')
249: continue;
250: cp = strtok(cp, WHITESPACE);
251: {
252: char *t = cp;
253: for (; *t != 0; t++)
254: if ('A' <= *t && *t <= 'Z')
255: *t = *t - 'A' + 'a'; /* tolower */
256:
257: }
258: opcode = parse_token(cp, filename, linenum);
259: switch (opcode)
260: {
261: case sPort:
262: intptr = &options->port;
263: parse_int:
264: cp = strtok(NULL, WHITESPACE);
265: if (!cp)
266: {
267: fprintf(stderr, "%s line %d: missing integer value.\n",
268: filename, linenum);
269: exit(1);
270: }
271: value = atoi(cp);
272: if (*intptr == -1)
273: *intptr = value;
274: break;
275:
276: case sServerKeyBits:
277: intptr = &options->server_key_bits;
278: goto parse_int;
279:
280: case sLoginGraceTime:
281: intptr = &options->login_grace_time;
282: goto parse_int;
283:
284: case sKeyRegenerationTime:
285: intptr = &options->key_regeneration_time;
286: goto parse_int;
287:
288: case sListenAddress:
289: cp = strtok(NULL, WHITESPACE);
290: if (!cp)
291: {
292: fprintf(stderr, "%s line %d: missing inet addr.\n",
293: filename, linenum);
294: exit(1);
295: }
296: options->listen_addr.s_addr = inet_addr(cp);
297: break;
298:
299: case sHostKeyFile:
300: charptr = &options->host_key_file;
301: cp = strtok(NULL, WHITESPACE);
302: if (!cp)
303: {
304: fprintf(stderr, "%s line %d: missing file name.\n",
305: filename, linenum);
306: exit(1);
307: }
308: if (*charptr == NULL)
309: *charptr = tilde_expand_filename(cp, getuid());
310: break;
311:
312: case sRandomSeedFile:
1.8 deraadt 313: fprintf(stderr, "%s line %d: \"randomseed\" option is obsolete.\n",
1.7 provos 314: filename, linenum);
315: cp = strtok(NULL, WHITESPACE);
316: break;
1.1 deraadt 317:
318: case sPermitRootLogin:
319: intptr = &options->permit_root_login;
320: parse_flag:
321: cp = strtok(NULL, WHITESPACE);
322: if (!cp)
323: {
324: fprintf(stderr, "%s line %d: missing yes/no argument.\n",
325: filename, linenum);
326: exit(1);
327: }
328: if (strcmp(cp, "yes") == 0)
329: value = 1;
330: else
331: if (strcmp(cp, "no") == 0)
332: value = 0;
333: else
334: {
335: fprintf(stderr, "%s line %d: Bad yes/no argument: %s\n",
336: filename, linenum, cp);
337: exit(1);
338: }
339: if (*intptr == -1)
340: *intptr = value;
341: break;
342:
343: case sIgnoreRhosts:
344: intptr = &options->ignore_rhosts;
345: goto parse_flag;
346:
347: case sQuietMode:
348: intptr = &options->quiet_mode;
349: goto parse_flag;
350:
351: case sFascistLogging:
352: intptr = &options->fascist_logging;
353: goto parse_flag;
354:
355: case sRhostsAuthentication:
356: intptr = &options->rhosts_authentication;
357: goto parse_flag;
358:
359: case sRhostsRSAAuthentication:
360: intptr = &options->rhosts_rsa_authentication;
361: goto parse_flag;
362:
363: case sRSAAuthentication:
364: intptr = &options->rsa_authentication;
365: goto parse_flag;
366:
367: #ifdef KRB4
368: case sKerberosAuthentication:
369: intptr = &options->kerberos_authentication;
370: goto parse_flag;
371:
372: case sKerberosOrLocalPasswd:
373: intptr = &options->kerberos_or_local_passwd;
374: goto parse_flag;
375:
376: case sKerberosTicketCleanup:
377: intptr = &options->kerberos_ticket_cleanup;
378: goto parse_flag;
379: #endif
380:
1.4 dugsong 381: #ifdef AFS
1.1 deraadt 382: case sKerberosTgtPassing:
383: intptr = &options->kerberos_tgt_passing;
384: goto parse_flag;
385:
386: case sAFSTokenPassing:
387: intptr = &options->afs_token_passing;
388: goto parse_flag;
389: #endif
390:
391: case sPasswordAuthentication:
392: intptr = &options->password_authentication;
393: goto parse_flag;
394:
395: case sPrintMotd:
396: intptr = &options->print_motd;
397: goto parse_flag;
398:
399: case sX11Forwarding:
400: intptr = &options->x11_forwarding;
401: goto parse_flag;
1.2 deraadt 402:
403: case sX11DisplayOffset:
404: intptr = &options->x11_display_offset;
405: goto parse_int;
1.1 deraadt 406:
407: case sStrictModes:
408: intptr = &options->strict_modes;
409: goto parse_flag;
410:
411: case sKeepAlives:
412: intptr = &options->keepalives;
413: goto parse_flag;
414:
415: case sEmptyPasswd:
416: intptr = &options->permit_empty_passwd;
417: goto parse_flag;
418:
419: case sLogFacility:
420: cp = strtok(NULL, WHITESPACE);
421: if (!cp)
422: {
423: fprintf(stderr, "%s line %d: missing facility name.\n",
424: filename, linenum);
425: exit(1);
426: }
427: for (i = 0; log_facilities[i].name; i++)
428: if (strcmp(log_facilities[i].name, cp) == 0)
429: break;
430: if (!log_facilities[i].name)
431: {
432: fprintf(stderr, "%s line %d: unsupported log facility %s\n",
433: filename, linenum, cp);
434: exit(1);
435: }
436: if (options->log_facility == (SyslogFacility)(-1))
437: options->log_facility = log_facilities[i].facility;
438: break;
439:
440: case sAllowHosts:
441: while ((cp = strtok(NULL, WHITESPACE)))
442: {
443: if (options->num_allow_hosts >= MAX_ALLOW_HOSTS)
444: {
445: fprintf(stderr, "%s line %d: too many allow hosts.\n",
446: filename, linenum);
447: exit(1);
448: }
449: options->allow_hosts[options->num_allow_hosts++] = xstrdup(cp);
450: }
451: break;
452:
453: case sDenyHosts:
454: while ((cp = strtok(NULL, WHITESPACE)))
455: {
456: if (options->num_deny_hosts >= MAX_DENY_HOSTS)
457: {
458: fprintf(stderr, "%s line %d: too many deny hosts.\n",
459: filename, linenum);
460: exit(1);
461: }
462: options->deny_hosts[options->num_deny_hosts++] = xstrdup(cp);
463: }
464: break;
465:
466: default:
467: fprintf(stderr, "%s line %d: Missing handler for opcode %s (%d)\n",
468: filename, linenum, cp, opcode);
469: exit(1);
470: }
471: if (strtok(NULL, WHITESPACE) != NULL)
472: {
473: fprintf(stderr, "%s line %d: garbage at end of line.\n",
474: filename, linenum);
475: exit(1);
476: }
477: }
478: fclose(f);
479: }