This commit was manufactured by cvs2svn to create tag
[heimdal.git] / kdc / config.c
blobc772e29e8afbe028e3d7e94e524d69219da6c0e9
1 /*
2 * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
35 #include <getarg.h>
36 #include <parse_bytes.h>
38 RCSID("$Id$");
40 static const char *config_file; /* location of kdc config file */
42 int require_preauth = -1; /* 1 == require preauth for all principals */
44 size_t max_request; /* maximal size of a request */
46 static char *max_request_str; /* `max_request' as a string */
48 time_t kdc_warn_pwexpire; /* time before expiration to print a warning */
50 struct dbinfo *databases;
51 HDB **db;
52 int num_db;
54 const char *port_str;
56 #ifdef HAVE_DAEMON
57 int detach_from_console = -1;
58 #define DETACH_IS_DEFAULT FALSE
59 #endif
61 int enable_http = -1;
62 krb5_boolean encode_as_rep_as_tgs_rep; /* bug compatibility */
64 krb5_boolean check_ticket_addresses;
65 krb5_boolean allow_null_ticket_addresses;
66 krb5_boolean allow_anonymous;
68 static struct getarg_strings addresses_str; /* addresses to listen on */
69 krb5_addresses explicit_addresses;
71 #ifdef KRB4
72 char *v4_realm;
73 int enable_v4 = -1;
74 int enable_524 = -1;
75 int enable_v4_cross_realm = -1;
76 int enable_kaserver = -1;
77 #endif
79 static int help_flag;
80 static int version_flag;
82 static struct getargs args[] = {
84 "config-file", 'c', arg_string, &config_file,
85 "location of config file", "file"
88 "require-preauth", 'p', arg_negative_flag, &require_preauth,
89 "don't require pa-data in as-reqs"
92 "max-request", 0, arg_string, &max_request,
93 "max size for a kdc-request", "size"
95 #if 0
97 "database", 'd', arg_string, &databases,
98 "location of database", "database"
100 #endif
101 { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support" },
102 #ifdef KRB4
103 { "kerberos4", 0, arg_negative_flag, &enable_v4,
104 "don't respond to kerberos 4 requests"
106 { "524", 0, arg_negative_flag, &enable_524,
107 "don't respond to 524 requests"
109 { "kerberos4-cross-realm", 0, arg_flag,
110 &enable_v4_cross_realm,
111 "respond to kerberos 4 requests from foreign realms"
114 "v4-realm", 'r', arg_string, &v4_realm,
115 "realm to serve v4-requests for"
118 "kaserver", 'K', arg_flag, &enable_kaserver,
119 "enable kaserver support"
121 #endif
122 { "ports", 'P', arg_string, &port_str,
123 "ports to listen to", "portspec"
125 #ifdef HAVE_DAEMON
126 #if DETACH_IS_DEFAULT
128 "detach", 'D', arg_negative_flag, &detach_from_console,
129 "don't detach from console"
131 #else
133 "detach", 0 , arg_flag, &detach_from_console,
134 "detach from console"
136 #endif
137 #endif
138 { "addresses", 0, arg_strings, &addresses_str,
139 "addresses to listen on", "list of addresses" },
140 { "help", 'h', arg_flag, &help_flag },
141 { "version", 'v', arg_flag, &version_flag }
144 static int num_args = sizeof(args) / sizeof(args[0]);
146 static void
147 usage(int ret)
149 arg_printusage (args, num_args, NULL, "");
150 exit (ret);
153 static void
154 get_dbinfo(void)
156 const krb5_config_binding *top_binding = NULL;
157 const krb5_config_binding *db_binding;
158 const krb5_config_binding *default_binding = NULL;
159 struct dbinfo *di, **dt;
160 const char *default_dbname = HDB_DEFAULT_DB;
161 const char *default_mkey = HDB_DB_DIR "/m-key";
162 const char *p;
164 databases = NULL;
165 dt = &databases;
166 while((db_binding = (const krb5_config_binding *)
167 krb5_config_get_next(context, NULL, &top_binding,
168 krb5_config_list,
169 "kdc",
170 "database",
171 NULL))) {
172 p = krb5_config_get_string(context, db_binding, "realm", NULL);
173 if(p == NULL) {
174 if(default_binding) {
175 krb5_warnx(context, "WARNING: more than one realm-less "
176 "database specification");
177 krb5_warnx(context, "WARNING: using the first encountered");
178 } else
179 default_binding = db_binding;
180 continue;
182 di = calloc(1, sizeof(*di));
183 di->realm = strdup(p);
184 p = krb5_config_get_string(context, db_binding, "dbname", NULL);
185 if(p)
186 di->dbname = strdup(p);
187 p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
188 if(p)
189 di->mkey_file = strdup(p);
190 *dt = di;
191 dt = &di->next;
193 if(default_binding) {
194 di = calloc(1, sizeof(*di));
195 p = krb5_config_get_string(context, default_binding, "dbname", NULL);
196 if(p) {
197 di->dbname = strdup(p);
198 default_dbname = p;
200 p = krb5_config_get_string(context, default_binding, "mkey_file", NULL);
201 if(p) {
202 di->mkey_file = strdup(p);
203 default_mkey = p;
205 *dt = di;
206 dt = &di->next;
207 } else if(databases == NULL) {
208 /* if there are none specified, use some default */
209 di = calloc(1, sizeof(*di));
210 di->dbname = strdup(default_dbname);
211 di->mkey_file = strdup(default_mkey);
212 *dt = di;
213 dt = &di->next;
215 for(di = databases; di; di = di->next) {
216 if(di->dbname == NULL)
217 di->dbname = strdup(default_dbname);
218 if(di->mkey_file == NULL) {
219 p = strrchr(di->dbname, '.');
220 if(p == NULL || strchr(p, '/') != NULL)
221 /* final pathname component does not contain a . */
222 asprintf(&di->mkey_file, "%s.mkey", di->dbname);
223 else
224 /* the filename is something.else, replace .else with
225 .mkey */
226 asprintf(&di->mkey_file, "%.*s.mkey",
227 (int)(p - di->dbname), di->dbname);
232 static void
233 add_one_address (const char *str, int first)
235 krb5_error_code ret;
236 krb5_addresses tmp;
238 ret = krb5_parse_address (context, str, &tmp);
239 if (ret)
240 krb5_err (context, 1, ret, "parse_address `%s'", str);
241 if (first)
242 krb5_copy_addresses(context, &tmp, &explicit_addresses);
243 else
244 krb5_append_addresses(context, &explicit_addresses, &tmp);
245 krb5_free_addresses (context, &tmp);
248 void
249 configure(int argc, char **argv)
251 int optind = 0;
252 int e;
253 const char *p;
255 while((e = getarg(args, num_args, argc, argv, &optind)))
256 warnx("error at argument `%s'", argv[optind]);
258 if(help_flag)
259 usage (0);
261 if (version_flag) {
262 print_version(NULL);
263 exit(0);
266 argc -= optind;
267 argv += optind;
269 if (argc != 0)
270 usage(1);
273 krb5_error_code ret;
274 char **files;
275 char *tmp;
276 if(config_file == NULL)
277 config_file = _PATH_KDC_CONF;
278 asprintf(&tmp, "%s:%s", config_file, krb5_config_file);
279 if(tmp == NULL)
280 krb5_errx(context, 1, "out of memory");
282 krb5_config_file = tmp;
284 ret = krb5_get_default_config_files(&files);
285 if(ret)
286 krb5_err(context, 1, ret, "reading configuration files");
287 ret = krb5_set_config_files(context, files);
288 krb5_free_config_files(files);
289 if(ret)
290 krb5_err(context, 1, ret, "reading configuration files");
293 get_dbinfo();
295 if(max_request_str){
296 max_request = parse_bytes(max_request_str, NULL);
299 if(max_request == 0){
300 p = krb5_config_get_string (context,
301 NULL,
302 "kdc",
303 "max-request",
304 NULL);
305 if(p)
306 max_request = parse_bytes(p, NULL);
309 if(require_preauth == -1)
310 require_preauth = krb5_config_get_bool(context, NULL, "kdc",
311 "require-preauth", NULL);
313 if(port_str == NULL){
314 p = krb5_config_get_string(context, NULL, "kdc", "ports", NULL);
315 if (p != NULL)
316 port_str = strdup(p);
319 explicit_addresses.len = 0;
321 if (addresses_str.num_strings) {
322 int i;
324 for (i = 0; i < addresses_str.num_strings; ++i)
325 add_one_address (addresses_str.strings[i], i == 0);
326 free_getarg_strings (&addresses_str);
327 } else {
328 char **foo = krb5_config_get_strings (context, NULL,
329 "kdc", "addresses", NULL);
331 if (foo != NULL) {
332 add_one_address (*foo++, TRUE);
333 while (*foo)
334 add_one_address (*foo++, FALSE);
338 #ifdef KRB4
339 if(enable_v4 == -1)
340 enable_v4 = krb5_config_get_bool_default(context, NULL, TRUE, "kdc",
341 "enable-kerberos4", NULL);
342 if(enable_v4_cross_realm == -1)
343 enable_v4_cross_realm =
344 krb5_config_get_bool_default(context, NULL,
345 FALSE, "kdc",
346 "enable-kerberos4-cross-realm",
347 NULL);
348 if(enable_524 == -1)
349 enable_524 = krb5_config_get_bool_default(context, NULL, enable_v4,
350 "kdc", "enable-524", NULL);
351 #else
352 #define enable_v4 0
353 #endif
355 if(enable_http == -1)
356 enable_http = krb5_config_get_bool(context, NULL, "kdc",
357 "enable-http", NULL);
358 check_ticket_addresses =
359 krb5_config_get_bool_default(context, NULL, TRUE, "kdc",
360 "check-ticket-addresses", NULL);
361 allow_null_ticket_addresses =
362 krb5_config_get_bool_default(context, NULL, TRUE, "kdc",
363 "allow-null-ticket-addresses", NULL);
365 allow_anonymous =
366 krb5_config_get_bool(context, NULL, "kdc",
367 "allow-anonymous", NULL);
368 #ifdef KRB4
369 if(v4_realm == NULL){
370 p = krb5_config_get_string (context, NULL,
371 "kdc",
372 "v4-realm",
373 NULL);
374 if(p != NULL) {
375 v4_realm = strdup(p);
376 if (v4_realm == NULL)
377 krb5_errx(context, 1, "out of memory");
380 if (enable_kaserver == -1)
381 enable_kaserver = krb5_config_get_bool_default(context, NULL, FALSE,
382 "kdc",
383 "enable-kaserver",
384 NULL);
385 #endif
387 encode_as_rep_as_tgs_rep = krb5_config_get_bool(context, NULL, "kdc",
388 "encode_as_rep_as_tgs_rep",
389 NULL);
391 kdc_warn_pwexpire = krb5_config_get_time (context, NULL,
392 "kdc",
393 "kdc_warn_pwexpire",
394 NULL);
396 #ifdef HAVE_DAEMON
397 if(detach_from_console == -1)
398 detach_from_console = krb5_config_get_bool_default(context, NULL,
399 DETACH_IS_DEFAULT,
400 "kdc",
401 "detach", NULL);
402 #endif
403 kdc_openlog();
404 if(max_request == 0)
405 max_request = 64 * 1024;
406 if(require_preauth == -1)
407 require_preauth = 1;
408 if (port_str == NULL)
409 port_str = "+";
410 #ifdef KRB4
411 if(v4_realm == NULL){
412 v4_realm = malloc(40); /* REALM_SZ */
413 if (v4_realm == NULL)
414 krb5_errx(context, 1, "out of memory");
415 krb_get_lrealm(v4_realm, 1);
417 #endif