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
40struct dbinfo {
41    char *realm;
42    char *dbname;
43    char *mkey_file;
44    struct dbinfo *next;
45};
46
47static char *config_file;	/* location of kdc config file */
48
49static int require_preauth = -1; /* 1 == require preauth for all principals */
50static char *max_request_str;	/* `max_request' as a string */
51
52static int disable_des = -1;
53
54static int builtin_hdb_flag;
55static int help_flag;
56static int version_flag;
57
58static struct getarg_strings addresses_str;	/* addresses to listen on */
59
60char *runas_string;
61char *chroot_string;
62
63
64static struct getargs args[] = {
65    {
66	"config-file",	'c',	arg_string,	&config_file,
67	"location of config file",	"file"
68    },
69    {
70	"require-preauth",	'p',	arg_negative_flag, &require_preauth,
71	"don't require pa-data in as-reqs", NULL
72    },
73    {
74	"max-request",	0,	arg_string, &max_request_str,
75	"max size for a kdc-request", "size"
76    },
77    { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support",
78   	 NULL },
79    {	"ports",	'P', 	arg_string, rk_UNCONST(&port_str),
80	"ports to listen to", "portspec"
81    },
82#ifdef SUPPORT_DETACH
83#if DETACH_IS_DEFAULT
84    {
85	"detach",       'D',      arg_negative_flag, &detach_from_console,
86	"don't detach from console", NULL
87    },
88#else
89    {
90	"detach",       0 ,      arg_flag, &detach_from_console,
91	"detach from console", NULL
92    },
93#endif
94#endif
95    {	"addresses",	0,	arg_strings, &addresses_str,
96	"addresses to listen on", "list of addresses" },
97    {	"disable-des",	0,	arg_flag, &disable_des,
98	"disable DES", NULL },
99    {	"builtin-hdb",	0,	arg_flag,   &builtin_hdb_flag,
100	"list builtin hdb backends", NULL},
101    {   "runas-user",	0,	arg_string, &runas_string,
102	"run as this user when connected to network", NULL
103    },
104    {   "chroot",	0,	arg_string, &chroot_string,
105	"chroot directory to run in", NULL
106    },
107    {	"help",		'h',	arg_flag,   &help_flag, NULL, NULL },
108    {	"version",	'v',	arg_flag,   &version_flag, NULL, NULL }
109};
110
111static int num_args = sizeof(args) / sizeof(args[0]);
112
113static void
114usage(int ret)
115{
116    arg_printusage (args, num_args, NULL, "");
117    exit (ret);
118}
119
120static void
121add_one_address (krb5_context context, const char *str, int first)
122{
123    krb5_error_code ret;
124    krb5_addresses tmp;
125
126    ret = krb5_parse_address (context, str, &tmp);
127    if (ret)
128	krb5_err (context, 1, ret, "parse_address `%s'", str);
129    if (first)
130	krb5_copy_addresses(context, &tmp, &explicit_addresses);
131    else
132	krb5_append_addresses(context, &explicit_addresses, &tmp);
133    krb5_free_addresses (context, &tmp);
134}
135
136krb5_kdc_configuration *
137configure(krb5_context context, int argc, char **argv)
138{
139    krb5_kdc_configuration *config;
140    krb5_error_code ret;
141    int optidx = 0;
142    const char *p;
143
144    while(getarg(args, num_args, argc, argv, &optidx))
145	warnx("error at argument `%s'", argv[optidx]);
146
147    if(help_flag)
148	usage (0);
149
150    if (version_flag) {
151	print_version(NULL);
152	exit(0);
153    }
154
155    if (builtin_hdb_flag) {
156	char *list;
157	ret = hdb_list_builtin(context, &list);
158	if (ret)
159	    krb5_err(context, 1, ret, "listing builtin hdb backends");
160	printf("builtin hdb backends: %s\n", list);
161	free(list);
162	exit(0);
163    }
164
165    argc -= optidx;
166    argv += optidx;
167
168    if (argc != 0)
169	usage(1);
170
171    {
172	char **files;
173
174	if (config_file == NULL) {
175	    asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
176	    if (config_file == NULL)
177		errx(1, "out of memory");
178	}
179
180	ret = krb5_prepend_config_files_default(config_file, &files);
181	if (ret)
182	    krb5_err(context, 1, ret, "getting configuration files");
183
184	ret = krb5_set_config_files(context, files);
185	krb5_free_config_files(files);
186	if(ret)
187	    krb5_err(context, 1, ret, "reading configuration files");
188    }
189
190    ret = krb5_kdc_get_config(context, &config);
191    if (ret)
192	krb5_err(context, 1, ret, "krb5_kdc_default_config");
193
194    kdc_openlog(context, "kdc", config);
195
196    ret = krb5_kdc_set_dbinfo(context, config);
197    if (ret)
198	krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
199
200    if(max_request_str)
201	max_request_tcp = max_request_udp = parse_bytes(max_request_str, NULL);
202
203    if(max_request_tcp == 0){
204	p = krb5_config_get_string (context,
205				    NULL,
206				    "kdc",
207				    "max-request",
208				    NULL);
209	if(p)
210	    max_request_tcp = max_request_udp = parse_bytes(p, NULL);
211    }
212
213    if(require_preauth != -1)
214	config->require_preauth = require_preauth;
215
216    if(port_str == NULL){
217	p = krb5_config_get_string(context, NULL, "kdc", "ports", NULL);
218	if (p != NULL)
219	    port_str = strdup(p);
220    }
221
222    explicit_addresses.len = 0;
223
224    if (addresses_str.num_strings) {
225	int i;
226
227	for (i = 0; i < addresses_str.num_strings; ++i)
228	    add_one_address (context, addresses_str.strings[i], i == 0);
229	free_getarg_strings (&addresses_str);
230    } else {
231	char **foo = krb5_config_get_strings (context, NULL,
232					      "kdc", "addresses", NULL);
233
234	if (foo != NULL) {
235	    add_one_address (context, *foo++, TRUE);
236	    while (*foo)
237		add_one_address (context, *foo++, FALSE);
238	}
239    }
240
241    if(enable_http == -1)
242	enable_http = krb5_config_get_bool(context, NULL, "kdc",
243					   "enable-http", NULL);
244
245    if(request_log == NULL)
246	request_log = krb5_config_get_string(context, NULL,
247					     "kdc",
248					     "kdc-request-log",
249					     NULL);
250
251    if (krb5_config_get_string(context, NULL, "kdc",
252			       "enforce-transited-policy", NULL))
253	krb5_errx(context, 1, "enforce-transited-policy deprecated, "
254		  "use [kdc]transited-policy instead");
255
256#ifdef SUPPORT_DETACH
257    if(detach_from_console == -1)
258	detach_from_console = krb5_config_get_bool_default(context, NULL,
259							   DETACH_IS_DEFAULT,
260							   "kdc",
261							   "detach", NULL);
262#endif /* SUPPORT_DETACH */
263
264    if(max_request_tcp == 0)
265	max_request_tcp = 64 * 1024;
266    if(max_request_udp == 0)
267	max_request_udp = 64 * 1024;
268
269    if (port_str == NULL)
270	port_str = "+";
271
272    if(disable_des == -1)
273	disable_des = krb5_config_get_bool_default(context, NULL,
274						   FALSE,
275						   "kdc",
276						   "disable-des", NULL);
277    if(disable_des) {
278	krb5_enctype_disable(context, ETYPE_DES_CBC_CRC);
279	krb5_enctype_disable(context, ETYPE_DES_CBC_MD4);
280	krb5_enctype_disable(context, ETYPE_DES_CBC_MD5);
281	krb5_enctype_disable(context, ETYPE_DES_CBC_NONE);
282	krb5_enctype_disable(context, ETYPE_DES_CFB64_NONE);
283	krb5_enctype_disable(context, ETYPE_DES_PCBC_NONE);
284    }
285
286    krb5_kdc_windc_init(context);
287
288    krb5_kdc_pkinit_config(context, config);
289
290    return config;
291}
292