fix FreeBSD PTHREAD_LIBADD
[heimdal.git] / kdc / config.c
blob267bde750ad491d247572a030c9e85b56698adee
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "kdc_locl.h"
37 #include <getarg.h>
38 #include <parse_bytes.h>
40 struct dbinfo {
41 char *realm;
42 char *dbname;
43 char *mkey_file;
44 struct dbinfo *next;
47 static char *config_file; /* location of kdc config file */
49 static int require_preauth = -1; /* 1 == require preauth for all principals */
50 static char *max_request_str; /* `max_request' as a string */
52 static int disable_des = -1;
53 static int enable_v4 = -1;
54 static int enable_kaserver = -1;
55 static int enable_524 = -1;
56 static int enable_v4_cross_realm = -1;
58 static int builtin_hdb_flag;
59 static int help_flag;
60 static int version_flag;
62 static struct getarg_strings addresses_str; /* addresses to listen on */
64 static char *v4_realm;
66 char *runas_string;
67 char *chroot_string;
70 static struct getargs args[] = {
72 "config-file", 'c', arg_string, &config_file,
73 "location of config file", "file"
76 "require-preauth", 'p', arg_negative_flag, &require_preauth,
77 "don't require pa-data in as-reqs"
80 "max-request", 0, arg_string, &max_request_str,
81 "max size for a kdc-request", "size"
83 { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support" },
84 { "524", 0, arg_negative_flag, &enable_524,
85 "don't respond to 524 requests"
88 "kaserver", 'K', arg_flag, &enable_kaserver,
89 "enable kaserver support"
91 { "kerberos4", 0, arg_flag, &enable_v4,
92 "respond to kerberos 4 requests"
95 "v4-realm", 'r', arg_string, &v4_realm,
96 "realm to serve v4-requests for"
98 { "kerberos4-cross-realm", 0, arg_flag,
99 &enable_v4_cross_realm,
100 "respond to kerberos 4 requests from foreign realms"
102 { "ports", 'P', arg_string, &port_str,
103 "ports to listen to", "portspec"
105 #ifdef SUPPORT_DETACH
106 #if DETACH_IS_DEFAULT
108 "detach", 'D', arg_negative_flag, &detach_from_console,
109 "don't detach from console"
111 #else
113 "detach", 0 , arg_flag, &detach_from_console,
114 "detach from console"
116 #endif
117 #endif
118 { "addresses", 0, arg_strings, &addresses_str,
119 "addresses to listen on", "list of addresses" },
120 { "disable-des", 0, arg_flag, &disable_des,
121 "disable DES" },
122 { "builtin-hdb", 0, arg_flag, &builtin_hdb_flag,
123 "list builtin hdb backends"},
124 { "runas-user", 0, arg_string, &runas_string,
125 "run as this user when connected to network"
127 { "chroot", 0, arg_string, &chroot_string,
128 "chroot directory to run in"
130 { "help", 'h', arg_flag, &help_flag },
131 { "version", 'v', arg_flag, &version_flag }
134 static int num_args = sizeof(args) / sizeof(args[0]);
136 static void
137 usage(int ret)
139 arg_printusage (args, num_args, NULL, "");
140 exit (ret);
143 static void
144 add_one_address (krb5_context context, const char *str, int first)
146 krb5_error_code ret;
147 krb5_addresses tmp;
149 ret = krb5_parse_address (context, str, &tmp);
150 if (ret)
151 krb5_err (context, 1, ret, "parse_address `%s'", str);
152 if (first)
153 krb5_copy_addresses(context, &tmp, &explicit_addresses);
154 else
155 krb5_append_addresses(context, &explicit_addresses, &tmp);
156 krb5_free_addresses (context, &tmp);
159 krb5_kdc_configuration *
160 configure(krb5_context context, int argc, char **argv)
162 krb5_kdc_configuration *config;
163 krb5_error_code ret;
164 int optidx = 0;
165 const char *p;
167 while(getarg(args, num_args, argc, argv, &optidx))
168 warnx("error at argument `%s'", argv[optidx]);
170 if(help_flag)
171 usage (0);
173 if (version_flag) {
174 print_version(NULL);
175 exit(0);
178 if (builtin_hdb_flag) {
179 char *list;
180 ret = hdb_list_builtin(context, &list);
181 if (ret)
182 krb5_err(context, 1, ret, "listing builtin hdb backends");
183 printf("builtin hdb backends: %s\n", list);
184 free(list);
185 exit(0);
188 argc -= optidx;
189 argv += optidx;
191 if (argc != 0)
192 usage(1);
195 char **files;
197 if (config_file == NULL) {
198 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
199 if (config_file == NULL)
200 errx(1, "out of memory");
203 ret = krb5_prepend_config_files_default(config_file, &files);
204 if (ret)
205 krb5_err(context, 1, ret, "getting configuration files");
207 ret = krb5_set_config_files(context, files);
208 krb5_free_config_files(files);
209 if(ret)
210 krb5_err(context, 1, ret, "reading configuration files");
213 ret = krb5_kdc_get_config(context, &config);
214 if (ret)
215 krb5_err(context, 1, ret, "krb5_kdc_default_config");
217 kdc_openlog(context, "kdc", config);
219 ret = krb5_kdc_set_dbinfo(context, config);
220 if (ret)
221 krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
223 if(max_request_str)
224 max_request_tcp = max_request_udp = parse_bytes(max_request_str, NULL);
226 if(max_request_tcp == 0){
227 p = krb5_config_get_string (context,
228 NULL,
229 "kdc",
230 "max-request",
231 NULL);
232 if(p)
233 max_request_tcp = max_request_udp = parse_bytes(p, NULL);
236 if(require_preauth != -1)
237 config->require_preauth = require_preauth;
239 if(port_str == NULL){
240 p = krb5_config_get_string(context, NULL, "kdc", "ports", NULL);
241 if (p != NULL)
242 port_str = strdup(p);
245 explicit_addresses.len = 0;
247 if (addresses_str.num_strings) {
248 int i;
250 for (i = 0; i < addresses_str.num_strings; ++i)
251 add_one_address (context, addresses_str.strings[i], i == 0);
252 free_getarg_strings (&addresses_str);
253 } else {
254 char **foo = krb5_config_get_strings (context, NULL,
255 "kdc", "addresses", NULL);
257 if (foo != NULL) {
258 add_one_address (context, *foo++, TRUE);
259 while (*foo)
260 add_one_address (context, *foo++, FALSE);
264 if(enable_v4 != -1)
265 config->enable_v4 = enable_v4;
267 if(enable_v4_cross_realm != -1)
268 config->enable_v4_cross_realm = enable_v4_cross_realm;
270 if(enable_524 != -1)
271 config->enable_524 = enable_524;
273 if(enable_http == -1)
274 enable_http = krb5_config_get_bool(context, NULL, "kdc",
275 "enable-http", NULL);
277 if(request_log == NULL)
278 request_log = krb5_config_get_string(context, NULL,
279 "kdc",
280 "kdc-request-log",
281 NULL);
283 if (krb5_config_get_string(context, NULL, "kdc",
284 "enforce-transited-policy", NULL))
285 krb5_errx(context, 1, "enforce-transited-policy deprecated, "
286 "use [kdc]transited-policy instead");
288 if (enable_kaserver != -1)
289 config->enable_kaserver = enable_kaserver;
291 #ifdef SUPPORT_DETACH
292 if(detach_from_console == -1)
293 detach_from_console = krb5_config_get_bool_default(context, NULL,
294 DETACH_IS_DEFAULT,
295 "kdc",
296 "detach", NULL);
297 #endif /* SUPPORT_DETACH */
299 if(max_request_tcp == 0)
300 max_request_tcp = 64 * 1024;
301 if(max_request_udp == 0)
302 max_request_udp = 64 * 1024;
304 if (port_str == NULL)
305 port_str = "+";
307 if (v4_realm)
308 config->v4_realm = v4_realm;
310 if(config->v4_realm == NULL && (config->enable_kaserver || config->enable_v4))
311 krb5_errx(context, 1, "Kerberos 4 enabled but no realm configured");
313 if(disable_des == -1)
314 disable_des = krb5_config_get_bool_default(context, NULL,
315 FALSE,
316 "kdc",
317 "disable-des", NULL);
318 if(disable_des) {
319 krb5_enctype_disable(context, ETYPE_DES_CBC_CRC);
320 krb5_enctype_disable(context, ETYPE_DES_CBC_MD4);
321 krb5_enctype_disable(context, ETYPE_DES_CBC_MD5);
322 krb5_enctype_disable(context, ETYPE_DES_CBC_NONE);
323 krb5_enctype_disable(context, ETYPE_DES_CFB64_NONE);
324 krb5_enctype_disable(context, ETYPE_DES_PCBC_NONE);
326 kdc_log(context, config,
327 0, "DES was disabled, turned off Kerberos V4, 524 "
328 "and kaserver");
329 config->enable_v4 = 0;
330 config->enable_524 = 0;
331 config->enable_kaserver = 0;
334 krb5_kdc_windc_init(context);
336 #ifdef PKINIT
337 #ifdef __APPLE__
338 config->enable_pkinit = 1;
340 if (config->pkinit_kdc_identity == NULL) {
341 if (config->pkinit_kdc_friendly_name == NULL)
342 config->pkinit_kdc_friendly_name =
343 strdup("O=System Identity,CN=com.apple.kerberos.kdc");
344 config->pkinit_kdc_identity = strdup("KEYCHAIN:");
346 if (config->pkinit_kdc_anchors == NULL)
347 config->pkinit_kdc_anchors = strdup("KEYCHAIN:");
349 #endif /* __APPLE__ */
351 if (config->enable_pkinit) {
352 if (config->pkinit_kdc_identity == NULL)
353 krb5_errx(context, 1, "pkinit enabled but no identity");
355 if (config->pkinit_kdc_anchors == NULL)
356 krb5_errx(context, 1, "pkinit enabled but no X509 anchors");
358 krb5_kdc_pk_initialize(context, config,
359 config->pkinit_kdc_identity,
360 config->pkinit_kdc_anchors,
361 config->pkinit_kdc_cert_pool,
362 config->pkinit_kdc_revoke);
366 #endif /* PKINIT */
368 return config;