1/*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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.
34 */
35
36#include "kdc_locl.h"
37#include <getarg.h>
38#include <parse_bytes.h>
39
40static void usage(int ret) __attribute__((noreturn));
41
42
43struct dbinfo {
44    char *realm;
45    char *dbname;
46    char *mkey_file;
47    struct dbinfo *next;
48};
49
50static char *config_file;	/* location of kdc config file */
51
52static int require_preauth = -1; /* 1 == require preauth for all principals */
53static char *max_request_str;	/* `max_request' as a string */
54
55static int disable_des = -1;
56
57static int builtin_hdb_flag;
58static int help_flag;
59static int version_flag;
60
61static struct getarg_strings addresses_str;	/* addresses to listen on */
62
63char *runas_string;
64char *chroot_string;
65
66int listen_on_ipc = 1;
67int listen_on_network = 1;
68
69
70static struct getargs args[] = {
71    {
72	"config-file",	'c',	arg_string,	&config_file,
73	"location of config file",	"file"
74    },
75    {
76	"require-preauth",	'p',	arg_negative_flag, &require_preauth,
77	"don't require pa-data in as-reqs", NULL
78    },
79    {
80	"max-request",	0,	arg_string, &max_request_str,
81	"max size for a kdc-request", "size"
82    },
83    { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support" },
84    { "listen-on-ipc", 0, arg_flag, &listen_on_ipc, "listen on local IPC requests" },
85    { "listen-on-network", 0, arg_flag, &listen_on_network, "listen on network requests" },
86
87    {	"ports",	'P', 	arg_string, &port_str,
88	"ports to listen to", "portspec"
89    },
90#ifdef __APPLE__
91    {	"sandbox",	0, 	arg_negative_flag, &sandbox_flag,
92	"use sandbox or not"
93    },
94#endif /* __APPLE__ */
95#ifdef SUPPORT_DETACH
96#if DETACH_IS_DEFAULT
97    {
98	"detach",       'D',      arg_negative_flag, &detach_from_console,
99	"don't detach from console", NULL
100    },
101#else
102    {
103	"detach",       0 ,      arg_flag, &detach_from_console,
104	"detach from console", NULL
105    },
106#endif
107#endif
108    {	"addresses",	0,	arg_strings, &addresses_str,
109	"addresses to listen on", "list of addresses" },
110    {	"disable-des",	0,	arg_flag, &disable_des,
111	"disable DES", NULL },
112    {	"builtin-hdb",	0,	arg_flag,   &builtin_hdb_flag,
113	"list builtin hdb backends", NULL},
114    {   "runas-user",	0,	arg_string, &runas_string,
115	"run as this user when connected to network", NULL
116    },
117    {   "chroot",	0,	arg_string, &chroot_string,
118	"chroot directory to run in", NULL
119    },
120    {	"help",		'h',	arg_flag,   &help_flag, NULL, NULL },
121    {	"version",	'v',	arg_flag,   &version_flag, NULL, NULL }
122};
123
124static int num_args = sizeof(args) / sizeof(args[0]);
125
126static void
127usage(int ret)
128{
129    arg_printusage (args, num_args, NULL, "");
130    exit (ret);
131}
132
133static void
134add_one_address (krb5_context context, const char *str, int first)
135{
136    krb5_error_code ret;
137    krb5_addresses tmp;
138
139    ret = krb5_parse_address (context, str, &tmp);
140    if (ret)
141	krb5_err (context, 1, ret, "parse_address `%s'", str);
142    if (first)
143	krb5_copy_addresses(context, &tmp, &explicit_addresses);
144    else
145	krb5_append_addresses(context, &explicit_addresses, &tmp);
146    krb5_free_addresses (context, &tmp);
147}
148
149krb5_kdc_configuration *
150configure(krb5_context context, int argc, char **argv)
151{
152    krb5_kdc_configuration *config;
153    krb5_error_code ret;
154    int optidx = 0;
155    const char *p;
156
157    while(getarg(args, num_args, argc, argv, &optidx))
158	warnx("error at argument `%s'", argv[optidx]);
159
160    if(help_flag)
161	usage (0);
162
163    if (version_flag) {
164	print_version(NULL);
165	exit(0);
166    }
167
168    if (builtin_hdb_flag) {
169	char *list;
170	ret = hdb_list_builtin(context, &list);
171	if (ret)
172	    krb5_err(context, 1, ret, "listing builtin hdb backends");
173	printf("builtin hdb backends: %s\n", list);
174	free(list);
175	exit(0);
176    }
177
178    argc -= optidx;
179
180    if (argc != 0)
181	usage(1);
182
183    {
184	char **files;
185
186	if (config_file == NULL) {
187	    asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
188	    if (config_file == NULL)
189		errx(1, "out of memory");
190	}
191
192	ret = krb5_prepend_config_files_default(config_file, &files);
193	if (ret)
194	    krb5_err(context, 1, ret, "getting configuration files");
195
196	ret = krb5_set_config_files(context, files);
197	krb5_free_config_files(files);
198	if(ret)
199	    krb5_err(context, 1, ret, "reading configuration files");
200    }
201
202    ret = krb5_kdc_get_config(context, &config);
203    if (ret)
204	krb5_err(context, 1, ret, "krb5_kdc_default_config");
205
206    kdc_openlog(context, "kdc", config);
207
208    ret = krb5_kdc_set_dbinfo(context, config);
209    if (ret)
210	krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
211
212    if(max_request_str)
213	max_request_tcp = max_request_udp = parse_bytes(max_request_str, NULL);
214
215    if(max_request_tcp == 0){
216	p = krb5_config_get_string (context,
217				    NULL,
218				    "kdc",
219				    "max-request",
220				    NULL);
221	if(p)
222	    max_request_tcp = max_request_udp = parse_bytes(p, NULL);
223    }
224
225    if(require_preauth != -1)
226	config->require_preauth = require_preauth;
227
228    if(port_str == NULL){
229	p = krb5_config_get_string(context, NULL, "kdc", "ports", NULL);
230	if (p != NULL)
231	    port_str = strdup(p);
232    }
233
234    explicit_addresses.len = 0;
235
236    if (addresses_str.num_strings) {
237	int i;
238
239	for (i = 0; i < addresses_str.num_strings; ++i)
240	    add_one_address (context, addresses_str.strings[i], i == 0);
241	free_getarg_strings (&addresses_str);
242    } else {
243	char **foo = krb5_config_get_strings (context, NULL,
244					      "kdc", "addresses", NULL);
245
246	if (foo != NULL) {
247	    add_one_address (context, *foo++, TRUE);
248	    while (*foo)
249		add_one_address (context, *foo++, FALSE);
250	}
251    }
252
253    if(enable_http == -1)
254	enable_http = krb5_config_get_bool(context, NULL, "kdc",
255					   "enable-http", NULL);
256
257    if(request_log == NULL)
258	request_log = krb5_config_get_string(context, NULL,
259					     "kdc",
260					     "kdc-request-log",
261					     NULL);
262
263    if (krb5_config_get_string(context, NULL, "kdc",
264			       "enforce-transited-policy", NULL))
265	krb5_errx(context, 1, "enforce-transited-policy deprecated, "
266		  "use [kdc]transited-policy instead");
267
268#ifdef SUPPORT_DETACH
269    if(detach_from_console == -1)
270	detach_from_console = krb5_config_get_bool_default(context, NULL,
271							   DETACH_IS_DEFAULT,
272							   "kdc",
273							   "detach", NULL);
274#endif /* SUPPORT_DETACH */
275
276    if(max_request_tcp == 0)
277	max_request_tcp = 64 * 1024;
278    if(max_request_udp == 0)
279	max_request_udp = 64 * 1024;
280
281    if (port_str == NULL)
282	port_str = "+";
283
284#ifdef __APPLE__
285    if (config->lkdc_realm == NULL) {
286	unsigned int n;
287
288	for (n = 0; n < config->num_db; n++) {
289	    char **realms = NULL, **r;
290
291	    if (config->db[n]->hdb_get_realms == NULL)
292		continue;
293
294	    ret = (config->db[n]->hdb_get_realms)(context, config->db[n], &realms);
295	    if (ret == 0 && realms) {
296		for (r = realms; *r; r++) {
297		    if (krb5_realm_is_lkdc(*r)) {
298			config->lkdc_realm = strdup(*r);
299			break;
300		    }
301		}
302		krb5_free_host_realm(context, realms);
303	    }
304	    if (config->lkdc_realm)
305		break;
306	}
307    }
308#endif
309
310
311    if(disable_des == -1)
312	disable_des = krb5_config_get_bool_default(context, NULL,
313						   FALSE,
314						   "kdc",
315						   "disable-des", NULL);
316    if(disable_des) {
317	krb5_enctype_disable(context, ETYPE_DES_CBC_CRC);
318	krb5_enctype_disable(context, ETYPE_DES_CBC_MD4);
319	krb5_enctype_disable(context, ETYPE_DES_CBC_MD5);
320	krb5_enctype_disable(context, ETYPE_DES_CBC_NONE);
321	krb5_enctype_disable(context, ETYPE_DES_CFB64_NONE);
322	krb5_enctype_disable(context, ETYPE_DES_PCBC_NONE);
323    }
324
325    krb5_kdc_windc_init(context);
326
327    krb5_kdc_pkinit_config(context, config);
328
329    return config;
330}
331